Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class app_db_mysqli implements app_db_interface
- {
- /**
- * MySQLi object
- * @var mysqli
- */
- protected static $_mysqli = array();
- /**
- * MySQLi Result object
- * @var mysqli_result
- */
- protected $_result;
- /**
- * @var string
- */
- protected $_use_mirror = "default";
- /**
- *
- * @var array
- */
- protected $servers = array(
- "default" => [ "localhost", "root", "pass", "db" ],
- "russia-mirror" => [ "russia", "root", "pass", "db" ],
- "europe-mirror" => [ "europe", "root", "pass", "db" ],
- );
- /**
- *
- * @param unknown $string
- * @throws app_exception
- */
- public function useMirror($string)
- {
- if (!isset($this->servers[$string]))
- {
- throw new app_exception("Mirror not exists", 503);
- }
- $this->_use_mirror = $string;
- return $this;
- }
- /**
- * (non-PHPdoc)
- * @see app_db_interface::connect()
- */
- public function connect()
- {
- if (!isset(self::$_mysqli[$this->_use_mirror]))
- {
- mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);
- try {
- self::$_mysqli[$this->_use_mirror] = new mysqli(
- $this->servers[$this->_use_mirror][0],
- $this->servers[$this->_use_mirror][1],
- $this->servers[$this->_use_mirror][2],
- $this->servers[$this->_use_mirror][3]
- );
- $this->_mysqli()->set_charset(APP_DEFAULT_SQL_CHARSET);
- if (defined("APP_DEVELOPMENT_VERSION") && APP_DEVELOPMENT_VERSION)
- {
- $this->_mysqli()->query("set profiling = 1;");
- $this->_mysqli()->query("set @@profiling_history_size = 100;");
- }
- }
- catch(mysqli_sql_exception $e)
- {
- throw new app_exception($e->getMessage(), 503);
- }
- }
- return $this;
- }
- /**
- * (non-PHPdoc)
- * @see app_db_interface::query()
- */
- public function query($query)
- {
- try {
- if ($this->_result instanceof mysqli_result)
- {
- $this->_result->free_result();
- }
- $this->_result = $this->_mysqli()->query($query);
- }
- catch (mysqli_sql_exception $e)
- {
- throw new app_exception("MySQL Query: {$query} <br>\n" . $e->getMessage(), 503);
- }
- return $this;
- }
- /**
- * (non-PHPdoc)
- * @see app_db_interface::rows()
- */
- public function rows()
- {
- if (!$this->_result instanceof mysqli_result)
- {
- return null;
- }
- return $this->_result->num_rows;
- }
- /**
- * (non-PHPdoc)
- * @see app_db_interface::fetch()
- */
- public function fetch()
- {
- if (!$this->_result instanceof mysqli_result)
- {
- return null;
- }
- return $this->_result->fetch_assoc();
- }
- /**
- * (non-PHPdoc)
- * @see app_db_interface::fetch_qualified_array()
- */
- function fetch_qualified_array()
- {
- if (!$this->_result instanceof mysqli_result)
- {
- return null;
- }
- $return = array();
- if ($row = $this->_result->fetch_row())
- {
- $fields = $this->_result->fetch_fields();
- foreach ($row as $_key => $_value)
- {
- $return[ $fields[$_key]->table ][ $fields[$_key]->name ] = $_value;
- }
- return $return;
- }
- return false;
- }
- /**
- * (non-PHPdoc)
- * @see app_db_interface::escape()
- */
- public function escape($string)
- {
- return $this->_mysqli()->real_escape_string($string);
- }
- /**
- * (non-PHPdoc)
- * @see app_db_interface::found_rows()
- */
- public function found_rows()
- {
- if (!$this->_result instanceof mysqli_result)
- {
- return null;
- }
- /* @var $result mysqli_result */
- $result = $this->_mysqli()->query("select found_rows() as `rows`");
- $rows = $result->fetch_row();
- $result->free_result();
- return $rows[0];
- }
- /**
- * (non-PHPdoc)
- * @see app_db_interface::inserted_id()
- */
- public function inserted_id()
- {
- return $this->_mysqli()->insert_id;
- }
- /**
- * Возвращает объект MySQLi
- * @return mysqli
- */
- protected function _mysqli()
- {
- if (!isset(self::$_mysqli[$this->_use_mirror]))
- {
- $this->connect();
- }
- return self::$_mysqli[$this->_use_mirror];
- }
- /**
- * (non-PHPdoc)
- * @see app_db_interface::profiling()
- */
- public function profiling()
- {
- $return = array();
- $result = $this->_mysqli()->query("show profiles;");
- while($row = $result->fetch_object())
- {
- $return[] = $row;
- }
- return $return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement