| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?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 DataDriverMySQLi 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 "MySQLi";
- }
-
- function Ping($connection)
- {
- return mysqli_ping($connection);
- }
-
- /**
- * @inheritdocs
- */
- function Open($connectionstring,$database,$username,$password,$charset='',$bootstrap='')
- {
- if (!function_exists("mysqli_connect")) throw new DatabaseException('mysqli extension is not enabled on this server.',DatabaseException::$CONNECTION_ERROR);
-
- // 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;
-
- $connection = mysqli_connect($host, $username, $password, $database, $port);
-
- if ( mysqli_connect_errno() )
- {
- throw new DatabaseException("Error connecting to database: " . mysqli_connect_error(),DatabaseException::$CONNECTION_ERROR);
- }
-
- if ($charset)
- {
- mysqli_set_charset($connection,$charset);
-
- if ( mysqli_connect_errno() )
- {
- throw new DatabaseException("Unable to set charset: " . mysqli_connect_error(),DatabaseException::$CONNECTION_ERROR);
- }
- }
-
- if ($bootstrap)
- {
- $statements = explode(';',$bootstrap);
- foreach ($statements as $sql)
- {
- try
- {
- $this->Execute($connection, $sql);
- }
- catch (Exception $ex)
- {
- throw new DatabaseException("problem with bootstrap sql: " . $ex->getMessage(),DatabaseException::$ERROR_IN_QUERY);
- }
- }
- }
-
- return $connection;
- }
-
- /**
- * @inheritdocs
- */
- function Close($connection)
- {
- @mysqli_close($connection); // ignore warnings
- }
-
- /**
- * @inheritdocs
- */
- function Query($connection,$sql)
- {
- if ( !$rs = @mysqli_query($connection,$sql) )
- {
- throw new DatabaseException(mysqli_error($connection),DatabaseException::$ERROR_IN_QUERY);
- }
-
- return $rs;
- }
- /**
- * @inheritdocs
- */
- function Execute($connection,$sql)
- {
- if ( !$result = @mysqli_query($connection,$sql) )
- {
- throw new DatabaseException(mysqli_error($connection),DatabaseException::$ERROR_IN_QUERY);
- }
-
- return mysqli_affected_rows($connection);
- }
-
- /**
- * @inheritdocs
- */
- function Fetch($connection,$rs)
- {
- return mysqli_fetch_assoc($rs);
- }
- /**
- * @inheritdocs
- */
- function GetLastInsertId($connection)
- {
- return (mysqli_insert_id($connection));
- }
- /**
- * @inheritdocs
- */
- function GetLastError($connection)
- {
- return mysqli_error($connection);
- }
-
- /**
- * @inheritdocs
- */
- function Release($connection,$rs)
- {
- mysqli_free_result($rs);
- }
-
- /**
- * @inheritdocs
- * this method currently uses replacement and not mysqli_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 mysqli_real_escape_string($val);
- }
- /**
- * @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 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)
- {
- $this->Execute($connection, "SET AUTOCOMMIT=0");
- $this->Execute($connection, "START TRANSACTION");
- }
-
- /**
- * @inheritdocs
- */
- function CommitTransaction($connection)
- {
- $this->Execute($connection, "COMMIT");
- $this->Execute($connection, "SET AUTOCOMMIT=1");
- }
-
- /**
- * @inheritdocs
- */
- function RollbackTransaction($connection)
- {
- $this->Execute($connection, "ROLLBACK");
- $this->Execute($connection, "SET AUTOCOMMIT=1");
- }
-
- }
- ?>
|