| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- /** @package verysimple::DB::DataDriver */
- require_once("IDataDriver.php");
- require_once("verysimple/DB/ISqlFunction.php");
- require_once("verysimple/DB/DatabaseException.php");
- require_once("verysimple/DB/DatabaseConfig.php");
- /**
- * An implementation of IDataDriver that communicates with
- * a MySQL server. This is one of the native drivers
- * supported by Phreeze
- *
- * @package verysimple::DB::DataDriver
- * @author VerySimple Inc. <[email protected]>
- * @copyright 1997-2010 VerySimple Inc.
- * @license http://www.gnu.org/licenses/lgpl.html LGPL
- * @version 1.0
- */
- class DataDriverMySQL_PDO implements IDataDriver
- {
- /** @var characters that will be escaped */
- static $BAD_CHARS = array("\\","\0","\n","\r","\x1a","'",'"');
-
- /** @var characters that will be used to replace bad chars */
- static $GOOD_CHARS = array("\\\\","\\0","\\n","\\r","\Z","\'",'\"');
-
- /**
- * @inheritdocs
- */
- function GetServerType()
- {
- return "MySQL";
- }
-
- function Ping($connection)
- {
- return mysql_ping($connection);
- }
-
- /**
- * @inheritdocs
- */
- function Open($connectionstring,$database,$username,$password,$charset='',$bootstrap='')
- {
- if (!class_exists("PDO")) throw new DatabaseException('PDO extension is not enabled on this server.',DatabaseException::$CONNECTION_ERROR);
-
- $connection = null;
-
- try
- {
- // if the port is provided in the connection string then strip it out and provide it as a separate param
- $hostAndPort = explode(":",$connectionstring);
- $host = $hostAndPort[0];
- $port = count($hostAndPort) > 1 ? $hostAndPort[1] : null;
-
- $dsn = 'mysql:dbname='. $this->Escape($database) .';host=' . $this->Escape($host);
-
- if ($port) {
- $dsn .= ";port=" . $this->Escape($port);
- }
-
- if ($charset) {
- $dsn .= ";charset=" . $this->Escape($charset);
- }
-
- $connection = new PDO($dsn, $username, $password);
- }
- catch (Exception $e)
- {
- throw new DatabaseException("Error connecting to database: " . $e->getMessage(),DatabaseException::$CONNECTION_ERROR);
- }
-
- if ($bootstrap)
- {
- $statements = explode(';',$bootstrap);
- foreach ($statements as $sql)
- {
- try
- {
- $this->Execute($connection, $sql);
- }
- catch (Exception $ex)
- {
- throw new DatabaseException("Connection Bootstrap Error: " . $ex->getMessage(),DatabaseException::$ERROR_IN_QUERY);
- }
- }
- }
-
- return $connection;
- }
-
- /**
- * @inheritdocs
- */
- function Close($connection)
- {
- $connection = null; // ignore warnings
- }
-
- /**
- * @inheritdocs
- */
- function Query($connection,$sql)
- {
-
- if (!$stmt = $connection->query($sql)) {
- throw new DatabaseException($this->GetErrorDescription($connection),DatabaseException::$ERROR_IN_QUERY);
- }
-
- return $stmt;
- }
-
- /**
- * @inheritdocs
- */
- function Execute($connection,$sql)
- {
- $stmt = $connection->prepare($sql);
-
- if (!$stmt) {
- throw new DatabaseException($this->GetErrorDescription($connection),DatabaseException::$ERROR_IN_QUERY);
- }
-
- if (!$numRows = $stmt->execute())
- {
- throw new DatabaseException($this->GetErrorDescription($sth),DatabaseException::$ERROR_IN_QUERY);
- }
-
- return $numRows;
- }
- /**
- * Given a PDO object, return the last error
- * @param PDO:errorInfo $errorInfo
- */
- private function GetErrorDescription($obj)
- {
- $errorInfo = $obj->errorInfo();
- return $errorInfo[2];
- }
-
- /**
- * @inheritdocs
- */
- public function GetQuotedSql($val)
- {
- if ($val === null) return DatabaseConfig::$CONVERT_NULL_TO_EMPTYSTRING ? "''" : 'NULL';
-
- if ($val instanceof ISqlFunction) return $val->GetQuotedSql($this);
-
- return "'" . $this->Escape($val) . "'";
- }
-
- /**
- * @inheritdocs
- */
- function Fetch($connection,$rs)
- {
- return $rs->fetch(PDO::FETCH_ASSOC);
- }
- /**
- * @inheritdocs
- */
- function GetLastInsertId($connection)
- {
- return $connection->lastInsertId();
- }
- /**
- * @inheritdocs
- */
- function GetLastError($connection)
- {
- return $this->GetErrorDescription($connection);
- }
-
- /**
- * @inheritdocs
- */
- function Release($connection,$rs)
- {
- $rs = null;
- }
-
- /**
- * @inheritdocs
- * this method currently uses replacement and not mysql_real_escape_string
- * so that a database connection is not necessary in order to escape.
- * this way cached queries can be used without connecting to the DB server
- */
- function Escape($val)
- {
- return str_replace(self::$BAD_CHARS, self::$GOOD_CHARS, $val);
- // return mysql_real_escape_string($val);
- }
-
- /**
- * @inheritdocs
- */
- function GetTableNames($connection, $dbname, $ommitEmptyTables = false)
- {
- $sql = "SHOW TABLE STATUS FROM `" . $this->Escape($dbname) . "`";
- $rs = $this->Query($connection,$sql);
-
- $tables = array();
-
- while ( $row = $this->Fetch($connection,$rs) )
- {
- if ( $ommitEmptyTables == false || $rs['Data_free'] > 0 )
- {
- $tables[] = $row['Name'];
- }
- }
-
- return $tables;
- }
-
- /**
- * @inheritdocs
- */
- function Optimize($connection,$table)
- {
- $result = "";
- $rs = $this->Query($connection,"optimize table `". $this->Escape($table)."`");
- while ( $row = $this->Fetch($connection,$rs) )
- {
- $tbl = $row['Table'];
- if (!isset($results[$tbl])) $results[$tbl] = "";
- $result .= trim($results[$tbl] . " " . $row['Msg_type'] . "=\"" . $row['Msg_text'] . "\"");
- }
-
- return $result;
- }
-
- /**
- * @inheritdocs
- */
- function StartTransaction($connection)
- {
- $connection->beginTransaction();
- }
-
- /**
- * @inheritdocs
- */
- function CommitTransaction($connection)
- {
- $connection->commit();
- }
-
- /**
- * @inheritdocs
- */
- function RollbackTransaction($connection)
- {
- $connection->rollBack();
- }
- }
- ?>
|