Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class app_db
- {
- protected $_db;
- protected $_connect;
- /**
- * Адаптер СУБД
- * @throws app_exception
- * @return app_db_interface
- */
- protected function adaptor()
- {
- if (!isset($this->_db))
- {
- $class = "app_db_".APP_SQL_SERVER;
- if (!class_exists($class))
- {
- throw new app_exception("SQL server engine not found: ".APP_SQL_SERVER);
- }
- $this->_db = new $class;
- }
- return $this->_db;
- }
- /**
- * Подключение к СУБД
- * @see app_db_interface::connect()
- */
- public function connect()
- {
- return $this->adaptor()->connect();
- }
- /**
- * Выполняем запрос к СУБД
- * @see app_db_interface::query()
- */
- public function query($string)
- {
- return $this->adaptor()->query($string);
- }
- /**
- * Возвращаем количество строк при выполнении MySQL
- * @see app_db_interface::rows()
- */
- public function rows()
- {
- return $this->adaptor()->rows();
- }
- /**
- * Возвращает следующий элемент результата
- * @see app_db_interface::fetch()
- */
- public function fetch()
- {
- return $this->adaptor()->fetch();
- }
- /**
- * Возвращает ассоциативный массив для запросов типа join
- * @see app_db_interface::fetch_qualified_array
- */
- public function fetch_qualified_array()
- {
- return $this->adaptor()->fetch_qualified_array();
- }
- /**
- * Возвращает экранированную строку
- * @param string $string
- * @return string
- */
- public function escape($string)
- {
- return $this->adaptor()->escape($string);
- }
- /**
- * Возвращает количество строк всего (SQL_CALC_FOUND_ROWS)
- * @return number
- */
- public function found_rows()
- {
- return $this->adaptor()->found_rows();
- }
- /**
- * Возвращает id вставленной записи
- * @return number
- */
- public function inserted_id()
- {
- return $this->adaptor()->inserted_id();
- }
- /**
- * Метод Метод возвращает профиль выполнения sql
- * @return app_db_interface
- */
- public function profiling()
- {
- return $this->adaptor()->profiling();
- }
- }
- <?php
- class app_db_mysqli implements app_db_interface
- {
- /**
- * MySQLi object
- * @var mysqli
- */
- protected static $_mysqli;
- /**
- * MySQLi Result object
- * @var mysqli_result
- */
- protected $_result;
- /**
- * (non-PHPdoc)
- * @see app_db_interface::connect()
- */
- public function connect()
- {
- if (!isset(self::$_mysqli))
- {
- mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);
- try {
- self::$_mysqli = new mysqli(
- APP_MYSQLI_HOST,
- APP_MYSQLI_USER,
- APP_MYSQLI_PASS,
- APP_MYSQLI_DB
- );
- self::$_mysqli->set_charset(APP_DEFAULT_SQL_CHARSET);
- if (defined("APP_DEVELOPMENT_VERSION") && APP_DEVELOPMENT_VERSION)
- {
- self::$_mysqli->query("set profiling = 1;");
- self::$_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->connect();
- }
- return self::$_mysqli;
- }
- /**
- * (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;
- }
- }
- <?php
- interface app_db_interface
- {
- /**
- * Соединение с СУБД
- * @return app_db_interface
- */
- public function connect();
- /**
- * Выполнить запрос к СУБД
- * @return app_db_interface
- */
- public function query($query);
- /**
- * Возвращает количество строк
- * @return app_db_interface
- */
- public function rows();
- /**
- * Возвращает следующую строку
- * @return array
- */
- public function fetch();
- /**
- * Возвращает строку в виде ассоциативная массива по таблицам участвующим в join
- * @return array
- */
- public function fetch_qualified_array();
- /**
- * Возвращает экранированную строку
- * @return string
- */
- public function escape($string);
- /**
- * Возвращает количество найденых строк в запросах с LIMIT
- * @return number
- */
- public function found_rows();
- /**
- * Возвращает вставленный id
- * @return number
- */
- public function inserted_id();
- /**
- * Метод вызывает данные профилинга для SQL
- * @return stdClass[]
- */
- public function profiling();
- }
Advertisement
Add Comment
Please, Sign In to add comment