예제 #1
0
 /**
  *
  * @param string $sql 'select order_id,order_sn from order where user_id=#user_id# '
  * @param array $params 只能为一唯数组,非日期分表必须包括分表的列名 array('user_id'=>100)
  */
 private function decorate($sql, $params = array())
 {
     $sql = strtolower($sql);
     $params = array_change_key_case($params, CASE_LOWER);
     $logicTable = $this->getDbParse()->getLogicTable();
     $db = null;
     $db_sql_arr = array();
     if ($logicTable) {
         $logic_col_value = $this->get_logic_column_value($params);
         $db_sql_arr['db_name'] = $this->get_db_name($params);
         $is_data_table = $this->getDbParse()->getIsdateTable();
         if ($is_data_table) {
             $db_sql_arr['sql'] = $this->getNewSql($sql, $logic_col_value);
         } else {
             if ($this->db_hash_type === 'mod_hash' || $this->db_hash_type === 'consistent_hash') {
                 $mod_value = $this->getDbParse()->getTableMod($logic_col_value);
                 $cache_key = md5($sql . '_' . $db_sql_arr['db_name'] . $mod_value);
                 $cache_sql = cls_shmop::read($cache_key);
                 // TODO 此处生产环境中应该放入 memcache 或 redis 中,否则可能造成php本地cache占用过多内存,shmop没有LRU和缓存失效时间设置
                 if ($cache_sql) {
                     $db_sql_arr['sql'] = $cache_sql;
                 } else {
                     $newSql = $this->getNewSql($sql, $logic_col_value);
                     cls_shmop::write($cache_key, $newSql);
                     $db_sql_arr['sql'] = $newSql;
                 }
             } elseif ($this->db_hash_type == 'virtual_hash') {
                 $db_sql_arr['sql'] = $this->getNewSql($sql, $logic_col_value);
             }
         }
     } else {
         $db_sql_arr['db_name'] = $this->config_array['db'];
         $db_sql_arr['sql'] = $sql;
     }
     $db_sql_arr['params'] = $params;
     $this->setDBConn($db_sql_arr['db_name']);
     return $db_sql_arr;
 }