Advertisement
Guest User

Untitled

a guest
Jun 26th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.29 KB | None | 0 0
  1. <?php
  2. class app_db_mysqli implements app_db_interface
  3. {
  4.     /**
  5.      * MySQLi object
  6.      * @var mysqli
  7.      */
  8.     protected static $_mysqli = array();
  9.    
  10.     /**
  11.      * MySQLi Result object
  12.      * @var mysqli_result
  13.      */
  14.     protected $_result;
  15.    
  16.     /**
  17.      * @var string
  18.      */
  19.     protected $_use_mirror = "default";
  20.    
  21.    
  22.     /**
  23.      *
  24.      * @var array
  25.      */
  26.     protected $servers = array(
  27.             "default" => [ "localhost", "root", "pass", "db" ],
  28.             "russia-mirror" => [ "russia", "root", "pass", "db" ],
  29.             "europe-mirror" => [ "europe", "root", "pass", "db" ],
  30.     );
  31.    
  32.     /**
  33.      *
  34.      * @param unknown $string
  35.      * @throws app_exception
  36.      */
  37.     public function useMirror($string)
  38.     {
  39.         if (!isset($this->servers[$string]))
  40.         {
  41.             throw new app_exception("Mirror not exists", 503);
  42.            
  43.         }
  44.        
  45.         $this->_use_mirror = $string;
  46.        
  47.         return $this;
  48.     }
  49.    
  50.     /**
  51.      * (non-PHPdoc)
  52.      * @see app_db_interface::connect()
  53.      */
  54.     public function connect()
  55.     {
  56.         if (!isset(self::$_mysqli[$this->_use_mirror]))
  57.         {
  58.            
  59.             mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);
  60.            
  61.             try {
  62.                 self::$_mysqli[$this->_use_mirror] = new mysqli(
  63.                     $this->servers[$this->_use_mirror][0],
  64.                     $this->servers[$this->_use_mirror][1],
  65.                     $this->servers[$this->_use_mirror][2],
  66.                     $this->servers[$this->_use_mirror][3]
  67.                 );
  68.                
  69.                 $this->_mysqli()->set_charset(APP_DEFAULT_SQL_CHARSET);
  70.                
  71.                 if (defined("APP_DEVELOPMENT_VERSION") && APP_DEVELOPMENT_VERSION)
  72.                 {
  73.                    
  74.                     $this->_mysqli()->query("set profiling = 1;");
  75.                     $this->_mysqli()->query("set @@profiling_history_size = 100;");
  76.                 }
  77.             }
  78.             catch(mysqli_sql_exception $e)
  79.             {
  80.                 throw new app_exception($e->getMessage(), 503);
  81.             }
  82.            
  83.            
  84.         }
  85.         return $this;
  86.     }
  87.    
  88.     /**
  89.      * (non-PHPdoc)
  90.      * @see app_db_interface::query()
  91.      */
  92.     public function query($query)
  93.     {
  94.         try {
  95.            
  96.             if ($this->_result instanceof mysqli_result)
  97.             {
  98.                 $this->_result->free_result();
  99.             }
  100.            
  101.             $this->_result = $this->_mysqli()->query($query);
  102.         }
  103.         catch (mysqli_sql_exception $e)
  104.         {
  105.             throw new app_exception("MySQL Query: {$query} <br>\n" . $e->getMessage(), 503);
  106.         }
  107.        
  108.         return $this;
  109.     }
  110.    
  111.     /**
  112.      * (non-PHPdoc)
  113.      * @see app_db_interface::rows()
  114.      */
  115.     public function rows()
  116.     {
  117.         if (!$this->_result instanceof mysqli_result)
  118.         {
  119.             return null;
  120.         }
  121.        
  122.         return $this->_result->num_rows;
  123.     }
  124.    
  125.     /**
  126.      * (non-PHPdoc)
  127.      * @see app_db_interface::fetch()
  128.      */
  129.     public function fetch()
  130.     {
  131.         if (!$this->_result instanceof mysqli_result)
  132.         {
  133.             return null;
  134.         }
  135.        
  136.         return $this->_result->fetch_assoc();
  137.     }
  138.    
  139.     /**
  140.      * (non-PHPdoc)
  141.      * @see app_db_interface::fetch_qualified_array()
  142.      */
  143.     function fetch_qualified_array()
  144.     {
  145.         if (!$this->_result instanceof mysqli_result)
  146.         {
  147.             return null;
  148.         }
  149.    
  150.         $return = array();
  151.    
  152.         if ($row = $this->_result->fetch_row())
  153.         {
  154.                
  155.             $fields = $this->_result->fetch_fields();
  156.                
  157.             foreach ($row as $_key => $_value)
  158.             {
  159.                 $return[ $fields[$_key]->table ][ $fields[$_key]->name ] = $_value;
  160.             }
  161.                
  162.             return $return;
  163.         }
  164.    
  165.         return false;
  166.     }
  167.    
  168.     /**
  169.      * (non-PHPdoc)
  170.      * @see app_db_interface::escape()
  171.      */
  172.     public function escape($string)
  173.     {
  174.         return $this->_mysqli()->real_escape_string($string);
  175.     }
  176.    
  177.     /**
  178.      * (non-PHPdoc)
  179.      * @see app_db_interface::found_rows()
  180.      */
  181.     public function found_rows()
  182.     {
  183.         if (!$this->_result instanceof mysqli_result)
  184.         {
  185.             return null;
  186.         }
  187.         /* @var $result mysqli_result */
  188.         $result = $this->_mysqli()->query("select found_rows() as `rows`");
  189.        
  190.         $rows = $result->fetch_row();
  191.        
  192.         $result->free_result();
  193.        
  194.         return $rows[0];
  195.     }
  196.    
  197.     /**
  198.      * (non-PHPdoc)
  199.      * @see app_db_interface::inserted_id()
  200.      */
  201.     public function inserted_id()
  202.     {
  203.         return $this->_mysqli()->insert_id;
  204.     }
  205.    
  206.     /**
  207.      * Возвращает объект MySQLi
  208.      * @return mysqli
  209.      */
  210.     protected function _mysqli()
  211.     {
  212.         if (!isset(self::$_mysqli[$this->_use_mirror]))
  213.         {
  214.             $this->connect();
  215.         }
  216.        
  217.         return self::$_mysqli[$this->_use_mirror];
  218.     }
  219.    
  220.     /**
  221.      * (non-PHPdoc)
  222.      * @see app_db_interface::profiling()
  223.      */
  224.     public function profiling()
  225.     {
  226.         $return = array();
  227.        
  228.         $result = $this->_mysqli()->query("show profiles;");
  229.         while($row = $result->fetch_object())
  230.         {
  231.             $return[] = $row;
  232.         }
  233.        
  234.         return $return;
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement