查询
$use_connection_type 默认不传为自动判断,可传true/false,若传字符串(只支持a-z0-9的字符串),则可以切换到另外一个连接,比如传other,则可以连接到$this->_connection_other_id所对应的ID的连接
Database_Driver_MySQL_Result Driver_Database_Driver_MySQL::query( string $sql [, string $as_object = null , boolean $use_connection_type = null ] )
参数列表
参数 类型 描述 默认值 $sql
string
查询语句 $as_object
string
是否返回对象 null $use_connection_type
boolean
是否使用主数据库,不设置则自动判断 null
Database_Driver_MySQL_Result
public function query($sql, $as_object=null, $use_connection_type=null)
{
$sql = trim($sql);
if (preg_match('#^([a-z]+)(:? |\n|\r)#i',$sql,$m))
{
$type = strtoupper($m[1]);
}
$typeArr = array
(
'SELECT',
'SHOW', //显示表
'EXPLAIN', //分析
'DESCRIBE', //显示结结构
'INSERT',
'REPLACE',
'UPDATE',
'DELETE',
);
if (!in_array($type, $typeArr))
{
$type = 'MASTER';
}
$slaverType = array('SELECT', 'SHOW', 'EXPLAIN');
if ($type!='MASTER' && in_array($type, $slaverType))
{
if (true===$use_connection_type)
{
$use_connection_type = 'master';
}
else if (is_string($use_connection_type))
{
if (!preg_match('#^[a-z0-9_]+$#i',$use_connection_type))$use_connection_type = 'master';
}
else
{
$use_connection_type = 'slaver';
}
}
else
{
$use_connection_type = 'master';
}
# 设置连接类型
$this->_set_connection_type($use_connection_type);
# 连接数据库
$connection = $this->connection();
# 记录调试
if(IS_DEBUG)
{
Core::debug()->info($sql,'MySQL');
static $is_sql_debug = null;
if (null === $is_sql_debug) $is_sql_debug = (bool)Core::debug()->profiler('sql')->is_open();
if ($is_sql_debug)
{
$host = $this->_get_hostname_by_connection_hash($this->connection_id());
$benchmark = Core::debug()->profiler('sql')->start('Database', 'mysql://' . ($host['username']?$host['username'].'@':'') . $host['hostname'] . ($host['port'] && $host['port'] != '3306' ? ':' . $host['port'] : ''));
}
}
static $is_no_cache = null;
if (null === $is_no_cache) $is_no_cache = (bool)Core::debug()->profiler('nocached')->is_open();
//显示无缓存数据
if ($is_no_cache && strtoupper(substr($sql, 0, 6)) == 'SELECT')
{
$sql = 'SELECT SQL_NO_CACHE' . substr($sql, 6);
}
// Execute the query
if (($result = mysql_query($sql, $connection)) === false)
{
if (isset($benchmark))
{
// This benchmark is worthless
$benchmark->delete();
}
if (IS_DEBUG)
{
$err = 'Error:' . mysql_error($connection) . '. SQL:' . $sql;
}
else
{
$err = mysql_error($connection);
}
throw new Exception($err, mysql_errno($connection));
}
if (isset($benchmark))
{
# 在线查看SQL情况
if ($is_sql_debug)
{
$data = array();
$data[0]['db'] = $host['hostname'] . '/' . $this->config['connection']['database'] . '/';
$data[0]['select_type'] = '';
$data[0]['table'] = '';
$data[0]['key'] = '';
$data[0]['key_len'] = '';
$data[0]['Extra'] = '';
$data[0]['query'] = '';
$data[0]['type'] = '';
$data[0]['id'] = '';
$data[0]['row'] = count($result);
$data[0]['ref'] = '';
$data[0]['all rows'] = '';
$data[0]['possible_keys'] = '';
if (strtoupper(substr($sql,0,6))=='SELECT')
{
$re = mysql_query('EXPLAIN ' . $sql, $connection);
$i = 0;
while (true == ($row = mysql_fetch_array($re , MYSQL_NUM)))
{
$data[$i]['select_type'] = (string)$row[1];
$data[$i]['table'] = (string)$row[2];
$data[$i]['key'] = (string)$row[5];
$data[$i]['key_len'] = (string)$row[6];
$data[$i]['Extra'] = (string)$row[9];
if ($i==0) $data[$i]['query'] = '';
$data[$i]['type'] = (string)$row[3];
$data[$i]['id'] = (string)$row[0];
$data[$i]['ref'] = (string)$row[7];
$data[$i]['all rows'] = (string)$row[8];
$data[$i]['possible_keys'] = (string)$row[4];
$i++;
}
}
$data[0]['query'] = $sql;
}
else
{
$data = null;
}
Core::debug()->profiler('sql')->stop($data);
}
// Set the last query
$this->last_query = $sql;
if ($type === 'INSERT' || $type === 'REPLACE')
{
// Return a list of insert id and rows created
return array
(
mysql_insert_id($connection),
mysql_affected_rows($connection)
);
}
elseif ($type === 'UPDATE' || $type === 'DELETE')
{
// Return the number of rows affected
return mysql_affected_rows($connection);
}
else
{
// Return an iterator of results
return new Database_Driver_MySQL_Result($result, $sql, $as_object, $this->config);
}
}