Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / verysimple / DB / DataDriver / MySQLi.php
blob1b36910a72010329cfc8fb04f64638dffa368140
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 DataDriverMySQLi 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 "MySQLi";
51 function Ping($connection)
53 return mysqli_ping($connection);
56 /**
57 * @inheritdocs
59 function Open($connectionstring, $database, $username, $password, $charset = '', $bootstrap = '')
61 if (! function_exists("mysqli_connect")) {
62 throw new DatabaseException('mysqli extension is not enabled on this server.', DatabaseException::$CONNECTION_ERROR);
65 // if the port is provided in the connection string then strip it out and provide it as a separate param
66 $hostAndPort = explode(":", $connectionstring);
67 $host = $hostAndPort [0];
68 $port = count($hostAndPort) > 1 ? $hostAndPort [1] : null;
70 $connection = mysqli_connect($host, $username, $password, $database, $port);
72 if (mysqli_connect_errno()) {
73 throw new DatabaseException("Error connecting to database: " . mysqli_connect_error(), DatabaseException::$CONNECTION_ERROR);
76 if ($charset) {
77 mysqli_set_charset($connection, $charset);
79 if (mysqli_connect_errno()) {
80 throw new DatabaseException("Unable to set charset: " . mysqli_connect_error(), DatabaseException::$CONNECTION_ERROR);
84 if ($bootstrap) {
85 $statements = explode(';', $bootstrap);
86 foreach ($statements as $sql) {
87 try {
88 $this->Execute($connection, $sql);
89 } catch (Exception $ex) {
90 throw new DatabaseException("problem with bootstrap sql: " . $ex->getMessage(), DatabaseException::$ERROR_IN_QUERY);
95 return $connection;
98 /**
99 * @inheritdocs
101 function Close($connection)
103 @mysqli_close($connection); // ignore warnings
107 * @inheritdocs
109 function Query($connection, $sql)
111 if (! $rs = @mysqli_query($connection, $sql)) {
112 throw new DatabaseException(mysqli_error($connection), DatabaseException::$ERROR_IN_QUERY);
115 return $rs;
119 * @inheritdocs
121 function Execute($connection, $sql)
123 if (! $result = @mysqli_query($connection, $sql)) {
124 throw new DatabaseException(mysqli_error($connection), DatabaseException::$ERROR_IN_QUERY);
127 return mysqli_affected_rows($connection);
131 * @inheritdocs
133 function Fetch($connection, $rs)
135 return mysqli_fetch_assoc($rs);
139 * @inheritdocs
141 function GetLastInsertId($connection)
143 return (mysqli_insert_id($connection));
147 * @inheritdocs
149 function GetLastError($connection)
151 return mysqli_error($connection);
155 * @inheritdocs
157 function Release($connection, $rs)
159 mysqli_free_result($rs);
163 * @inheritdocs
164 * this method currently uses replacement and not mysqli_real_escape_string
165 * so that a database connection is not necessary in order to escape.
166 * this way cached queries can be used without connecting to the DB server
168 function Escape($val)
170 return str_replace(self::$BAD_CHARS, self::$GOOD_CHARS, $val);
171 // return mysqli_real_escape_string($val);
175 * @inheritdocs
177 public function GetQuotedSql($val)
179 if ($val === null) {
180 return DatabaseConfig::$CONVERT_NULL_TO_EMPTYSTRING ? "''" : 'NULL';
183 if ($val instanceof ISqlFunction) {
184 return $val->GetQuotedSql($this);
187 return "'" . $this->Escape($val) . "'";
191 * @inheritdocs
193 function GetTableNames($connection, $dbname, $ommitEmptyTables = false)
195 $sql = "SHOW TABLE STATUS FROM `" . $this->Escape($dbname) . "`";
196 $rs = $this->Query($connection, $sql);
198 $tables = array ();
200 while ($row = $this->Fetch($connection, $rs)) {
201 if ($ommitEmptyTables == false || $rs ['Data_free'] > 0) {
202 $tables [] = $row ['Name'];
206 return $tables;
210 * @inheritdocs
212 function Optimize($connection, $table)
214 $result = "";
215 $rs = $this->Query($connection, "optimize table `" . $this->Escape($table) . "`");
217 while ($row = $this->Fetch($connection, $rs)) {
218 $tbl = $row ['Table'];
219 if (! isset($results [$tbl])) {
220 $results [$tbl] = "";
223 $result .= trim($results [$tbl] . " " . $row ['Msg_type'] . "=\"" . $row ['Msg_text'] . "\"");
226 return $result;
230 * @inheritdocs
232 function StartTransaction($connection)
234 $this->Execute($connection, "SET AUTOCOMMIT=0");
235 $this->Execute($connection, "START TRANSACTION");
239 * @inheritdocs
241 function CommitTransaction($connection)
243 $this->Execute($connection, "COMMIT");
244 $this->Execute($connection, "SET AUTOCOMMIT=1");
248 * @inheritdocs
250 function RollbackTransaction($connection)
252 $this->Execute($connection, "ROLLBACK");
253 $this->Execute($connection, "SET AUTOCOMMIT=1");