Example #1
0
 /**
  * Simple Query
  * This is a simplified version of the query() function.  Internally
  * we only use it when running transaction commands since they do
  * not require all the features of the main query() function.
  *
  * @access	public
  * @param	string	the sql query
  * @return	mixed
  */
 function simple_query($sql)
 {
     $proxy_setting = load_db_proxy_setting($this->group_name, $this->is_write_type($sql), $this->db_force_master);
     if (is_array($proxy_setting) && !empty($proxy_setting)) {
         $proxy_setting_key = key($proxy_setting);
         $this->group_name = $proxy_setting_key;
         foreach ($proxy_setting[$proxy_setting_key] as $key => $val) {
             $this->{$key} = $val;
         }
         $proxy_conn_id = 'conn_' . $proxy_setting_key;
         $CI =& get_instance();
         if (isset($CI->{$proxy_conn_id}) && is_resource($CI->{$proxy_conn_id})) {
             $this->conn_id = $CI->{$proxy_conn_id};
             //$this->reconnect();
         } else {
             //$this->_close($this->conn_id);
             $this->conn_id = false;
             $this->initialize();
             $CI->{$proxy_conn_id} = $this->conn_id;
         }
         $this->reset_force_master();
     }
     if (!$this->conn_id) {
         $this->initialize();
     }
     if (defined('SQL_LOG_WRITE') && SQL_LOG_WRITE === true) {
         $CI =& get_instance();
         $CI->load->library('log');
         $CI->log->write("ip={$CI->input->ip_address()}; host={$this->hostname}; dbname={$this->database}; group={$this->group_name};\r\n" . preg_replace("/\\s+/", " ", $sql), 'sqllog/log');
     }
     return $this->_execute($sql);
 }
 /**
  * Simple Query
  * This is a simplified version of the query() function. Internally
  * we only use it when running transaction commands since they do
  * not require all the features of the main query() function.
  *
  * @param	string	the sql query
  * @return	mixed
  */
 public function simple_query($sql)
 {
     //load_db_proxy_setting方法这里写在helper中,也可以直接写在该类中,写在helper中则需要在自动加载中加载该helper
     //该方法的作用是根据当前链接group name 和sql读写类型,以及是否强制使用主库判断使用哪个链接。使用主库 OR 重库?
     //主重库的负载均衡,单点故障都可以在这里考虑。也就是根据3个参数返回一个可用的配置数组。
     $proxy_setting = load_db_proxy_setting($this->group_name, $this->is_write_type($sql), $this->db_force_master);
     if (is_array($proxy_setting) && !empty($proxy_setting)) {
         $proxy_setting_key = key($proxy_setting);
         $this->group_name = $proxy_setting_key;
         //将当前配置重新赋值给类的属性,如果database.php配置的是DSN字符串,则需要在load_db_proxy_setting中做处理
         foreach ($proxy_setting[$proxy_setting_key] as $key => $val) {
             $this->{$key} = $val;
         }
         //定义链接ID为conn_前缀
         $proxy_conn_id = 'conn_' . $proxy_setting_key;
         $CI =& get_instance();
         //赋值给CI超级对象或者直接从CI超级对象中读取
         if (isset($CI->{$proxy_conn_id}) && is_resource($CI->{$proxy_conn_id})) {
             $this->conn_id = $CI->{$proxy_conn_id};
         } else {
             $this->conn_id = false;
             $this->initialize();
             $CI->{$proxy_conn_id} = $this->conn_id;
         }
         //强制只一次有效,下次查询失效,防止一直强制主库
         $this->reset_force_master();
     }
     if (!$this->conn_id) {
         $this->initialize();
     }
     return $this->_execute($sql);
 }