Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / verysimple / DB / DataDriver / MySQL_PDO.php
blob79c9ae865a6c8252bdf96c90e0db729a5c990888
1 <?php
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");
8 /**
9 * An implementation of IDataDriver that communicates with
10 * a MySQL server.
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
18 * @version 1.0
20 class DataDriverMySQL_PDO implements IDataDriver
22 /** @var characters that will be escaped */
23 static $BAD_CHARS = array (
24 "\\",
25 "\0",
26 "\n",
27 "\r",
28 "\x1a",
29 "'",
30 '"'
33 /** @var characters that will be used to replace bad chars */
34 static $GOOD_CHARS = array (
35 "\\\\",
36 "\\0",
37 "\\n",
38 "\\r",
39 "\Z",
40 "\'",
41 '\"'
44 /**
45 * @inheritdocs
47 function GetServerType()
49 return "MySQL";
51 function Ping($connection)
53 return mysql_ping($connection);
56 /**
57 * @inheritdocs
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);
65 $connection = null;
67 try {
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);
75 if ($port) {
76 $dsn .= ";port=" . $this->Escape($port);
79 if ($charset) {
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);
88 if ($bootstrap) {
89 $statements = explode(';', $bootstrap);
90 foreach ($statements as $sql) {
91 try {
92 $this->Execute($connection, $sql);
93 } catch (Exception $ex) {
94 throw new DatabaseException("Connection Bootstrap Error: " . $ex->getMessage(), DatabaseException::$ERROR_IN_QUERY);
99 return $connection;
103 * @inheritdocs
105 function Close($connection)
107 $connection = null; // ignore warnings
111 * @inheritdocs
113 function Query($connection, $sql)
115 if (! $stmt = $connection->query($sql)) {
116 throw new DatabaseException($this->GetErrorDescription($connection), DatabaseException::$ERROR_IN_QUERY);
119 return $stmt;
123 * @inheritdocs
125 function Execute($connection, $sql)
127 $stmt = $connection->prepare($sql);
129 if (! $stmt) {
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);
137 return $numRows;
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];
152 * @inheritdocs
154 public function GetQuotedSql($val)
156 if ($val === null) {
157 return DatabaseConfig::$CONVERT_NULL_TO_EMPTYSTRING ? "''" : 'NULL';
160 if ($val instanceof ISqlFunction) {
161 return $val->GetQuotedSql($this);
164 return "'" . $this->Escape($val) . "'";
168 * @inheritdocs
170 function Fetch($connection, $rs)
172 return $rs->fetch(PDO::FETCH_ASSOC);
176 * @inheritdocs
178 function GetLastInsertId($connection)
180 return $connection->lastInsertId();
184 * @inheritdocs
186 function GetLastError($connection)
188 return $this->GetErrorDescription($connection);
192 * @inheritdocs
194 function Release($connection, $rs)
196 $rs = null;
200 * @inheritdocs
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);
212 * @inheritdocs
214 function GetTableNames($connection, $dbname, $ommitEmptyTables = false)
216 $sql = "SHOW TABLE STATUS FROM `" . $this->Escape($dbname) . "`";
217 $rs = $this->Query($connection, $sql);
219 $tables = array ();
221 while ($row = $this->Fetch($connection, $rs)) {
222 if ($ommitEmptyTables == false || $rs ['Data_free'] > 0) {
223 $tables [] = $row ['Name'];
227 return $tables;
231 * @inheritdocs
233 function Optimize($connection, $table)
235 $result = "";
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'] . "\"");
247 return $result;
251 * @inheritdocs
253 function StartTransaction($connection)
255 $connection->beginTransaction();
259 * @inheritdocs
261 function CommitTransaction($connection)
263 $connection->commit();
267 * @inheritdocs
269 function RollbackTransaction($connection)
271 $connection->rollBack();