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 DataDriverMySQLi
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 mysqli_ping($connection);
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);
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);
85 $statements = explode(';', $bootstrap);
86 foreach ($statements as $sql) {
88 $this->Execute($connection, $sql);
89 } catch (Exception
$ex) {
90 throw new DatabaseException("problem with bootstrap sql: " . $ex->getMessage(), DatabaseException
::$ERROR_IN_QUERY);
101 function Close($connection)
103 @mysqli_close
($connection); // ignore warnings
109 function Query($connection, $sql)
111 if (! $rs = @mysqli_query
($connection, $sql)) {
112 throw new DatabaseException(mysqli_error($connection), DatabaseException
::$ERROR_IN_QUERY);
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);
133 function Fetch($connection, $rs)
135 return mysqli_fetch_assoc($rs);
141 function GetLastInsertId($connection)
143 return (mysqli_insert_id($connection));
149 function GetLastError($connection)
151 return mysqli_error($connection);
157 function Release($connection, $rs)
159 mysqli_free_result($rs);
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);
177 public function GetQuotedSql($val)
180 return DatabaseConfig
::$CONVERT_NULL_TO_EMPTYSTRING ?
"''" : 'NULL';
183 if ($val instanceof ISqlFunction
) {
184 return $val->GetQuotedSql($this);
187 return "'" . $this->Escape($val) . "'";
193 function GetTableNames($connection, $dbname, $ommitEmptyTables = false)
195 $sql = "SHOW TABLE STATUS FROM `" . $this->Escape($dbname) . "`";
196 $rs = $this->Query($connection, $sql);
200 while ($row = $this->Fetch($connection, $rs)) {
201 if ($ommitEmptyTables == false ||
$rs ['Data_free'] > 0) {
202 $tables [] = $row ['Name'];
212 function Optimize($connection, $table)
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'] . "\"");
232 function StartTransaction($connection)
234 $this->Execute($connection, "SET AUTOCOMMIT=0");
235 $this->Execute($connection, "START TRANSACTION");
241 function CommitTransaction($connection)
243 $this->Execute($connection, "COMMIT");
244 $this->Execute($connection, "SET AUTOCOMMIT=1");
250 function RollbackTransaction($connection)
252 $this->Execute($connection, "ROLLBACK");
253 $this->Execute($connection, "SET AUTOCOMMIT=1");