* @author Louis Lapointe * @link http://pmo.developpez.com/ * @since PhpMyObject v0.1 * @version $Revision: $ * @copyright Copyright (C) 2007-2008 Nicolas Boiteux * @copyright Copyright (C) 2008 Louis Lapointe * @license GPLv3 {@link http://www.gnu.org/licenses/gpl} * @filesource * */ /** * This class implements a PDO driver. * * @package PhpMyObject * @subpackage PMO_Core */ class PMO_Dbms_Pdo extends PMO_MyDbms { public function __construct(PDO $pdo = NULL) { if(isset($pdo)) $this->setDB($pdo); } public function __destruct() { $this->disconnect(); } public function connect(array $authdb){ $dsn = $authdb['pdodriver'] . ':host=' . $authdb['host'] . ';dbname=' . $authdb['base']; $this->setDB(new PDO($dsn, $authdb['user'], $authdb['pass'])); $this->getDB()->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } public function disconnect() { $this->setDB(NULL); } public function query($query){ PMO_MyLog::factory()->log($query); if ($this->getDb() instanceof PDO) { $this->result = $this->getDB()->prepare($query); if(!$this->result->execute()){ $errorinfo = $this->result->errorInfo(); throw new Exception("Error: SQL ".$errorinfo[0]." ".$errorinfo[2]); } else { return TRUE; } } else { throw new Exception("No connection available!"); } } public function fetchArray() { return $this->result->fetch(PDO::FETCH_ASSOC); } public function getTableDesc($table) { $sql = sprintf('DESC %s ;', addslashes($table)); $this->query($sql); while($dbresult = $this->fetchArray()){ $tmparray[] = array("Field"=>$dbresult['Field'], "Type" => $this->translateType($dbresult['Type']), "Null" => $dbresult['Null'], "Key"=>$dbresult['Key'], "Default"=>$dbresult['Default'], "Extra"=>$dbresult['Extra'], "Perm"=>"rw"); } return $tmparray; } public function getLastId() { return $this->getDB()->lastInsertId(); } /** * begin a transaction with Dbms * only with pdo driver */ public function beginTransaction(){ return $this->getDb()->beginTransaction(); } /** * commit the transaction with Dbms * only with pdo driver */ public function commit(){ return $this->getDb()->commit(); } /** * rollback the transaction with Dbms * only with pdo driver */ public function rollback(){ return $this->getDb()->rollback(); } } ?>