Guest User

Untitled

a guest
Jun 25th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.09 KB | None | 0 0
  1. <?php
  2. class app_db
  3. {
  4.     protected $_db;
  5.     protected $_connect;
  6.    
  7.     /**
  8.      * Адаптер СУБД
  9.      * @throws app_exception
  10.      * @return app_db_interface
  11.      */
  12.     protected function adaptor()
  13.     {
  14.         if (!isset($this->_db))
  15.         {
  16.             $class  = "app_db_".APP_SQL_SERVER;
  17.            
  18.             if (!class_exists($class))
  19.             {
  20.                 throw new app_exception("SQL server engine not found: ".APP_SQL_SERVER);
  21.             }
  22.            
  23.             $this->_db = new $class;
  24.         }
  25.  
  26.         return $this->_db;
  27.     }
  28.    
  29.     /**
  30.      * Подключение к СУБД
  31.      * @see app_db_interface::connect()
  32.      */
  33.     public function connect()
  34.     {
  35.         return $this->adaptor()->connect();
  36.     }
  37.    
  38.     /**
  39.      * Выполняем запрос к СУБД
  40.      * @see app_db_interface::query()
  41.      */
  42.     public function query($string)
  43.     {
  44.         return $this->adaptor()->query($string);
  45.     }
  46.    
  47.     /**
  48.      * Возвращаем количество строк при выполнении MySQL
  49.      * @see app_db_interface::rows()
  50.      */
  51.     public function rows()
  52.     {
  53.         return $this->adaptor()->rows();
  54.     }
  55.    
  56.     /**
  57.      * Возвращает следующий элемент результата
  58.      * @see app_db_interface::fetch()
  59.      */
  60.     public function fetch()
  61.     {
  62.         return $this->adaptor()->fetch();
  63.     }
  64.    
  65.     /**
  66.      * Возвращает ассоциативный массив для запросов типа join
  67.      * @see app_db_interface::fetch_qualified_array
  68.      */
  69.     public function fetch_qualified_array()
  70.     {
  71.         return $this->adaptor()->fetch_qualified_array();
  72.     }
  73.    
  74.     /**
  75.      * Возвращает экранированную строку
  76.      * @param string $string
  77.      * @return string
  78.      */
  79.     public function escape($string)
  80.     {
  81.         return $this->adaptor()->escape($string);
  82.     }
  83.    
  84.     /**
  85.      * Возвращает количество строк всего (SQL_CALC_FOUND_ROWS)
  86.      * @return number
  87.      */
  88.     public function found_rows()
  89.     {
  90.         return $this->adaptor()->found_rows();
  91.     }
  92.    
  93.     /**
  94.      * Возвращает id вставленной записи
  95.      * @return number
  96.      */
  97.     public function inserted_id()
  98.     {
  99.         return $this->adaptor()->inserted_id();
  100.     }
  101.    
  102.     /**
  103.      * Метод Метод возвращает профиль выполнения sql
  104.      * @return app_db_interface
  105.      */
  106.     public function profiling()
  107.     {
  108.         return $this->adaptor()->profiling();
  109.     }
  110. }
  111.  
  112. <?php
  113. class app_db_mysqli implements app_db_interface
  114. {
  115.     /**
  116.      * MySQLi object
  117.      * @var mysqli
  118.      */
  119.     protected static $_mysqli;
  120.    
  121.     /**
  122.      * MySQLi Result object
  123.      * @var mysqli_result
  124.      */
  125.     protected $_result;
  126.    
  127.     /**
  128.      * (non-PHPdoc)
  129.      * @see app_db_interface::connect()
  130.      */
  131.     public function connect()
  132.     {
  133.         if (!isset(self::$_mysqli))
  134.         {
  135.             mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);
  136.            
  137.             try {
  138.                 self::$_mysqli = new mysqli(
  139.                     APP_MYSQLI_HOST,
  140.                     APP_MYSQLI_USER,
  141.                     APP_MYSQLI_PASS,
  142.                     APP_MYSQLI_DB
  143.                 );
  144.                
  145.                 self::$_mysqli->set_charset(APP_DEFAULT_SQL_CHARSET);
  146.                
  147.                 if (defined("APP_DEVELOPMENT_VERSION") && APP_DEVELOPMENT_VERSION)
  148.                 {
  149.                    
  150.                     self::$_mysqli->query("set profiling = 1;");
  151.                     self::$_mysqli->query("set @@profiling_history_size = 100;");
  152.                 }
  153.             }
  154.             catch(mysqli_sql_exception $e)
  155.             {
  156.                 throw new app_exception($e->getMessage(), 503);
  157.             }
  158.            
  159.            
  160.         }
  161.         return $this;
  162.     }
  163.    
  164.     /**
  165.      * (non-PHPdoc)
  166.      * @see app_db_interface::query()
  167.      */
  168.     public function query($query)
  169.     {
  170.         try {
  171.            
  172.             if ($this->_result instanceof mysqli_result)
  173.             {
  174.                 $this->_result->free_result();
  175.             }
  176.            
  177.             $this->_result = $this->_mysqli()->query($query);
  178.         }
  179.         catch (mysqli_sql_exception $e)
  180.         {
  181.             throw new app_exception("MySQL Query: {$query} <br>\n" . $e->getMessage(), 503);
  182.         }
  183.        
  184.         return $this;
  185.     }
  186.    
  187.     /**
  188.      * (non-PHPdoc)
  189.      * @see app_db_interface::rows()
  190.      */
  191.     public function rows()
  192.     {
  193.         if (!$this->_result instanceof mysqli_result)
  194.         {
  195.             return null;
  196.         }
  197.        
  198.         return $this->_result->num_rows;
  199.     }
  200.    
  201.     /**
  202.      * (non-PHPdoc)
  203.      * @see app_db_interface::fetch()
  204.      */
  205.     public function fetch()
  206.     {
  207.         if (!$this->_result instanceof mysqli_result)
  208.         {
  209.             return null;
  210.         }
  211.        
  212.         return $this->_result->fetch_assoc();
  213.     }
  214.    
  215.     /**
  216.      * (non-PHPdoc)
  217.      * @see app_db_interface::fetch_qualified_array()
  218.      */
  219.     function fetch_qualified_array()
  220.     {
  221.         if (!$this->_result instanceof mysqli_result)
  222.         {
  223.             return null;
  224.         }
  225.    
  226.         $return = array();
  227.    
  228.         if ($row = $this->_result->fetch_row())
  229.         {
  230.                
  231.             $fields = $this->_result->fetch_fields();
  232.                
  233.             foreach ($row as $_key => $_value)
  234.             {
  235.                 $return[ $fields[$_key]->table ][ $fields[$_key]->name ] = $_value;
  236.             }
  237.                
  238.             return $return;
  239.         }
  240.    
  241.         return false;
  242.     }
  243.    
  244.     /**
  245.      * (non-PHPdoc)
  246.      * @see app_db_interface::escape()
  247.      */
  248.     public function escape($string)
  249.     {
  250.         return $this->_mysqli()->real_escape_string($string);
  251.     }
  252.    
  253.     /**
  254.      * (non-PHPdoc)
  255.      * @see app_db_interface::found_rows()
  256.      */
  257.     public function found_rows()
  258.     {
  259.         if (!$this->_result instanceof mysqli_result)
  260.         {
  261.             return null;
  262.         }
  263.         /* @var $result mysqli_result */
  264.         $result = $this->_mysqli()->query("select found_rows() as `rows`");
  265.        
  266.         $rows = $result->fetch_row();
  267.        
  268.         $result->free_result();
  269.        
  270.         return $rows[0];
  271.     }
  272.    
  273.     /**
  274.      * (non-PHPdoc)
  275.      * @see app_db_interface::inserted_id()
  276.      */
  277.     public function inserted_id()
  278.     {
  279.         return $this->_mysqli()->insert_id;
  280.     }
  281.    
  282.     /**
  283.      * Возвращает объект MySQLi
  284.      * @return mysqli
  285.      */
  286.     protected function _mysqli()
  287.     {
  288.         if (!isset(self::$_mysqli))
  289.         {
  290.             $this->connect();
  291.         }
  292.        
  293.         return self::$_mysqli;
  294.     }
  295.    
  296.     /**
  297.      * (non-PHPdoc)
  298.      * @see app_db_interface::profiling()
  299.      */
  300.     public function profiling()
  301.     {
  302.         $return = array();
  303.        
  304.         $result = $this->_mysqli()->query("show profiles;");
  305.         while($row = $result->fetch_object())
  306.         {
  307.             $return[] = $row;
  308.         }
  309.        
  310.         return $return;
  311.     }
  312. }
  313.  
  314. <?php
  315. interface app_db_interface
  316. {
  317.     /**
  318.      * Соединение с СУБД
  319.      * @return app_db_interface
  320.      */
  321.     public function connect();
  322.    
  323.     /**
  324.      * Выполнить запрос к СУБД
  325.      * @return app_db_interface
  326.      */
  327.     public function query($query);
  328.    
  329.     /**
  330.      * Возвращает количество строк
  331.      * @return app_db_interface
  332.      */
  333.     public function rows();
  334.    
  335.     /**
  336.      * Возвращает следующую строку
  337.      * @return array
  338.      */
  339.     public function fetch();
  340.    
  341.     /**
  342.      * Возвращает строку в виде ассоциативная массива по таблицам участвующим в join
  343.      * @return array
  344.      */
  345.     public function fetch_qualified_array();
  346.    
  347.     /**
  348.      * Возвращает экранированную строку
  349.      * @return string
  350.      */
  351.     public function escape($string);
  352.    
  353.     /**
  354.      * Возвращает количество найденых строк в запросах с LIMIT
  355.      * @return number
  356.      */
  357.     public function found_rows();
  358.    
  359.     /**
  360.      * Возвращает вставленный id
  361.      * @return number
  362.      */
  363.     public function inserted_id();
  364.    
  365.     /**
  366.      * Метод вызывает данные профилинга для SQL
  367.      * @return stdClass[]
  368.      */
  369.     public function profiling();
  370.    
  371. }
Advertisement
Add Comment
Please, Sign In to add comment