2 /** @package verysimple::DB::DataDriver */
3 require_once("IDataDriver.php");
4 require_once("verysimple/DB/ISqlFunction.php");
5 require_once("verysimple/DB/DatabaseException.php");
6 require_once("verysimple/DB/DatabaseConfig.php");
9 * An implementation of IDataDriver that communicates with
11 * This is one of the native drivers
12 * supported by Phreeze
14 * @package verysimple::DB::DataDriver
15 * @author VerySimple Inc. <noreply@verysimple.com>
16 * @copyright 1997-2010 VerySimple Inc.
17 * @license http://www.gnu.org/licenses/lgpl.html LGPL
20 class DataDriverMySQL_PDO
implements IDataDriver
22 /** @var characters that will be escaped */
23 static $BAD_CHARS = array (
33 /** @var characters that will be used to replace bad chars */
34 static $GOOD_CHARS = array (
47 function GetServerType()
51 function Ping($connection)
53 return mysql_ping($connection);
59 function Open($connectionstring, $database, $username, $password, $charset = '', $bootstrap = '')
61 if (! class_exists("PDO")) {
62 throw new DatabaseException('PDO extension is not enabled on this server.', DatabaseException
::$CONNECTION_ERROR);
68 // if the port is provided in the connection string then strip it out and provide it as a separate param
69 $hostAndPort = explode(":", $connectionstring);
70 $host = $hostAndPort [0];
71 $port = count($hostAndPort) > 1 ?
$hostAndPort [1] : null;
73 $dsn = 'mysql:dbname=' . $this->Escape($database) . ';host=' . $this->Escape($host);
76 $dsn .= ";port=" . $this->Escape($port);
80 $dsn .= ";charset=" . $this->Escape($charset);
83 $connection = new PDO($dsn, $username, $password);
84 } catch (Exception
$e) {
85 throw new DatabaseException("Error connecting to database: " . $e->getMessage(), DatabaseException
::$CONNECTION_ERROR);
89 $statements = explode(';', $bootstrap);
90 foreach ($statements as $sql) {
92 $this->Execute($connection, $sql);
93 } catch (Exception
$ex) {
94 throw new DatabaseException("Connection Bootstrap Error: " . $ex->getMessage(), DatabaseException
::$ERROR_IN_QUERY);
105 function Close($connection)
107 $connection = null; // ignore warnings
113 function Query($connection, $sql)
115 if (! $stmt = $connection->query($sql)) {
116 throw new DatabaseException($this->GetErrorDescription($connection), DatabaseException
::$ERROR_IN_QUERY);
125 function Execute($connection, $sql)
127 $stmt = $connection->prepare($sql);
130 throw new DatabaseException($this->GetErrorDescription($connection), DatabaseException
::$ERROR_IN_QUERY);
133 if (! $numRows = $stmt->execute()) {
134 throw new DatabaseException($this->GetErrorDescription($stmt), DatabaseException
::$ERROR_IN_QUERY);
141 * Given a PDO object, return the last error
143 * @param PDO:errorInfo $errorInfo
145 private function GetErrorDescription($obj)
147 $errorInfo = $obj->errorInfo();
148 return $errorInfo [2];
154 public function GetQuotedSql($val)
157 return DatabaseConfig
::$CONVERT_NULL_TO_EMPTYSTRING ?
"''" : 'NULL';
160 if ($val instanceof ISqlFunction
) {
161 return $val->GetQuotedSql($this);
164 return "'" . $this->Escape($val) . "'";
170 function Fetch($connection, $rs)
172 return $rs->fetch(PDO
::FETCH_ASSOC
);
178 function GetLastInsertId($connection)
180 return $connection->lastInsertId();
186 function GetLastError($connection)
188 return $this->GetErrorDescription($connection);
194 function Release($connection, $rs)
201 * this method currently uses replacement and not mysql_real_escape_string
202 * so that a database connection is not necessary in order to escape.
203 * this way cached queries can be used without connecting to the DB server
205 function Escape($val)
207 return str_replace(self
::$BAD_CHARS, self
::$GOOD_CHARS, $val);
208 // return mysql_real_escape_string($val);
214 function GetTableNames($connection, $dbname, $ommitEmptyTables = false)
216 $sql = "SHOW TABLE STATUS FROM `" . $this->Escape($dbname) . "`";
217 $rs = $this->Query($connection, $sql);
221 while ($row = $this->Fetch($connection, $rs)) {
222 if ($ommitEmptyTables == false ||
$rs ['Data_free'] > 0) {
223 $tables [] = $row ['Name'];
233 function Optimize($connection, $table)
236 $rs = $this->Query($connection, "optimize table `" . $this->Escape($table) . "`");
238 while ($row = $this->Fetch($connection, $rs)) {
239 $tbl = $row ['Table'];
240 if (! isset($results [$tbl])) {
241 $results [$tbl] = "";
244 $result .= trim($results [$tbl] . " " . $row ['Msg_type'] . "=\"" . $row ['Msg_text'] . "\"");
253 function StartTransaction($connection)
255 $connection->beginTransaction();
261 function CommitTransaction($connection)
263 $connection->commit();
269 function RollbackTransaction($connection)
271 $connection->rollBack();