内核更新
This commit is contained in:
@@ -24,9 +24,6 @@ abstract class Builder
|
||||
// 查询对象实例
|
||||
protected $query;
|
||||
|
||||
// 查询参数
|
||||
protected $options = [];
|
||||
|
||||
// 数据库表达式
|
||||
protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'exp' => 'EXP', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN', 'exists' => 'EXISTS', 'notexists' => 'NOT EXISTS', 'not exists' => 'NOT EXISTS', 'null' => 'NULL', 'notnull' => 'NOT NULL', 'not null' => 'NOT NULL', '> time' => '> TIME', '< time' => '< TIME', '>= time' => '>= TIME', '<= time' => '<= TIME', 'between time' => 'BETWEEN TIME', 'not between time' => 'NOT BETWEEN TIME', 'notbetween time' => 'NOT BETWEEN TIME'];
|
||||
|
||||
@@ -606,7 +603,7 @@ abstract class Builder
|
||||
{
|
||||
// 获取合法的字段
|
||||
if ('*' == $options['field']) {
|
||||
$fields = $this->query->getFieldsType($options);
|
||||
$fields = array_keys($this->query->getFieldsType($options));
|
||||
} else {
|
||||
$fields = $options['field'];
|
||||
}
|
||||
@@ -687,7 +684,7 @@ abstract class Builder
|
||||
$this->parseWhere($options['where'], $options),
|
||||
$this->parseOrder($options['order']),
|
||||
$this->parseLimit($options['limit']),
|
||||
$this->parseLimit($options['lock']),
|
||||
$this->parseLock($options['lock']),
|
||||
$this->parseComment($options['comment']),
|
||||
], $this->updateSql);
|
||||
|
||||
@@ -711,7 +708,7 @@ abstract class Builder
|
||||
$this->parseWhere($options['where'], $options),
|
||||
$this->parseOrder($options['order']),
|
||||
$this->parseLimit($options['limit']),
|
||||
$this->parseLimit($options['lock']),
|
||||
$this->parseLock($options['lock']),
|
||||
$this->parseComment($options['comment']),
|
||||
], $this->deleteSql);
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ namespace think\db;
|
||||
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use think\App;
|
||||
use think\Collection;
|
||||
use think\Db;
|
||||
use think\db\exception\BindParamException;
|
||||
@@ -255,6 +254,8 @@ abstract class Connection
|
||||
if (!isset($this->links[$linkNum])) {
|
||||
if (!$config) {
|
||||
$config = $this->config;
|
||||
} else {
|
||||
$config = array_merge($this->config, $config);
|
||||
}
|
||||
// 连接参数
|
||||
if (isset($config['params']) && is_array($config['params'])) {
|
||||
@@ -272,15 +273,20 @@ abstract class Connection
|
||||
if (empty($config['dsn'])) {
|
||||
$config['dsn'] = $this->parseDsn($config);
|
||||
}
|
||||
if ($config['debug']) {
|
||||
$startTime = microtime(true);
|
||||
}
|
||||
$this->links[$linkNum] = new PDO($config['dsn'], $config['username'], $config['password'], $params);
|
||||
// 记录数据库连接信息
|
||||
App::$debug && Log::record('[ DB ] CONNECT: ' . $config['dsn'], 'info');
|
||||
if ($config['debug']) {
|
||||
// 记录数据库连接信息
|
||||
Log::record('[ DB ] CONNECT:[ UseTime:' . number_format(microtime(true) - $startTime, 6) . 's ] ' . $config['dsn'], 'sql');
|
||||
}
|
||||
} catch (\PDOException $e) {
|
||||
if ($autoConnection) {
|
||||
Log::record($e->getMessage(), 'error');
|
||||
return $this->connect($autoConnection, $linkNum);
|
||||
} else {
|
||||
throw new Exception($e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -359,13 +365,11 @@ abstract class Connection
|
||||
* @access public
|
||||
* @param string $sql sql指令
|
||||
* @param array $bind 参数绑定
|
||||
* @param boolean $getLastInsID 是否获取自增ID
|
||||
* @param string $sequence 自增序列名
|
||||
* @return int
|
||||
* @throws BindParamException
|
||||
* @throws PDOException
|
||||
*/
|
||||
public function execute($sql, $bind = [], $getLastInsID = false, $sequence = null)
|
||||
public function execute($sql, $bind = [])
|
||||
{
|
||||
$this->initConnect(true);
|
||||
if (!$this->linkID) {
|
||||
@@ -393,12 +397,6 @@ abstract class Connection
|
||||
$this->debug(false);
|
||||
|
||||
$this->numRows = $this->PDOStatement->rowCount();
|
||||
if (preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $sql)) {
|
||||
$this->lastInsID = $this->linkID->lastInsertId($sequence);
|
||||
if ($getLastInsID) {
|
||||
return $this->lastInsID;
|
||||
}
|
||||
}
|
||||
return $this->numRows;
|
||||
} catch (\PDOException $e) {
|
||||
throw new PDOException($e, $this->config, $this->queryStr);
|
||||
@@ -696,10 +694,14 @@ abstract class Connection
|
||||
/**
|
||||
* 获取最近插入的ID
|
||||
* @access public
|
||||
* @param string $sequence 自增序列名
|
||||
* @return string
|
||||
*/
|
||||
public function getLastInsID()
|
||||
public function getLastInsID($sequence = null)
|
||||
{
|
||||
if (is_null($this->lastInsID)) {
|
||||
$this->lastInsID = $this->linkID->lastInsertId($sequence);
|
||||
}
|
||||
return $this->lastInsID;
|
||||
}
|
||||
|
||||
@@ -738,9 +740,10 @@ abstract class Connection
|
||||
* 数据库调试 记录当前SQL及分析性能
|
||||
* @access protected
|
||||
* @param boolean $start 调试开始标记 true 开始 false 结束
|
||||
* @param string $sql 执行的SQL语句 留空自动获取
|
||||
* @return void
|
||||
*/
|
||||
protected function debug($start)
|
||||
protected function debug($start, $sql = '')
|
||||
{
|
||||
if (!empty($this->config['debug'])) {
|
||||
// 开启数据库调试模式
|
||||
@@ -750,14 +753,15 @@ abstract class Connection
|
||||
// 记录操作结束时间
|
||||
Debug::remark('queryEndTime', 'time');
|
||||
$runtime = Debug::getRangeTime('queryStartTime', 'queryEndTime');
|
||||
$log = $this->queryStr . ' [ RunTime:' . $runtime . 's ]';
|
||||
$sql = $sql ?: $this->queryStr;
|
||||
$log = $sql . ' [ RunTime:' . $runtime . 's ]';
|
||||
$result = [];
|
||||
// SQL性能分析
|
||||
if ($this->config['sql_explain'] && 0 === stripos(trim($this->queryStr), 'select')) {
|
||||
$result = $this->getExplain($this->queryStr);
|
||||
if ($this->config['sql_explain'] && 0 === stripos(trim($sql), 'select')) {
|
||||
$result = $this->getExplain($sql);
|
||||
}
|
||||
// SQL监听
|
||||
$this->trigger($this->queryStr, $runtime, $result);
|
||||
$this->trigger($sql, $runtime, $result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -791,7 +795,7 @@ abstract class Connection
|
||||
}
|
||||
} else {
|
||||
// 未注册监听则记录到日志中
|
||||
Log::record('[ SQL ] ' . $this->queryStr . ' [ RunTime:' . $runtime . 's ]', 'sql');
|
||||
Log::record('[ SQL ] ' . $sql . ' [ RunTime:' . $runtime . 's ]', 'sql');
|
||||
if (!empty($explain)) {
|
||||
Log::record('[ EXPLAIN : ' . var_export($explain, true) . ' ]', 'sql');
|
||||
}
|
||||
|
||||
@@ -194,25 +194,24 @@ class Query
|
||||
* @access public
|
||||
* @param string $sql sql指令
|
||||
* @param array $bind 参数绑定
|
||||
* @param boolean $getLastInsID 是否获取自增ID
|
||||
* @param boolean $sequence 自增序列名
|
||||
* @return int
|
||||
* @throws BindParamException
|
||||
* @throws PDOException
|
||||
*/
|
||||
public function execute($sql, $bind = [], $getLastInsID = false, $sequence = null)
|
||||
public function execute($sql, $bind = [])
|
||||
{
|
||||
return $this->connection->execute($sql, $bind, $getLastInsID, $sequence);
|
||||
return $this->connection->execute($sql, $bind);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最近插入的ID
|
||||
* @access public
|
||||
* @param string $sequence 自增序列名
|
||||
* @return string
|
||||
*/
|
||||
public function getLastInsID()
|
||||
public function getLastInsID($sequence = null)
|
||||
{
|
||||
return $this->connection->getLastInsID();
|
||||
return $this->connection->getLastInsID($sequence);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -728,11 +727,11 @@ class Query
|
||||
}
|
||||
if (true === $field) {
|
||||
// 获取全部字段
|
||||
$fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName, 'fields');
|
||||
$fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields');
|
||||
$field = $fields ?: ['*'];
|
||||
} elseif ($except) {
|
||||
// 字段排除
|
||||
$fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName, 'fields');
|
||||
$fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields');
|
||||
$field = $fields ? array_diff($fields, $field) : $field;
|
||||
}
|
||||
if ($tableName) {
|
||||
@@ -749,7 +748,7 @@ class Query
|
||||
if (isset($this->options['field'])) {
|
||||
$field = array_merge($this->options['field'], $field);
|
||||
}
|
||||
$this->options['field'] = $field;
|
||||
$this->options['field'] = array_unique($field);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -1415,8 +1414,10 @@ class Query
|
||||
{
|
||||
$types = $this->getFieldsType($options);
|
||||
$bind = [];
|
||||
foreach ($types as $key => $type) {
|
||||
$bind[$key] = $this->getFieldBindType($type);
|
||||
if ($types) {
|
||||
foreach ($types as $key => $type) {
|
||||
$bind[$key] = $this->getFieldBindType($type);
|
||||
}
|
||||
}
|
||||
return $bind;
|
||||
}
|
||||
@@ -1665,7 +1666,7 @@ class Query
|
||||
* @access public
|
||||
* @param mixed $data 数据
|
||||
* @param boolean $replace 是否replace
|
||||
* @param boolean $getLastInsID 是否获取自增ID
|
||||
* @param boolean $getLastInsID 返回自增主键
|
||||
* @param string $sequence 自增序列名
|
||||
* @return integer|string
|
||||
*/
|
||||
@@ -1681,9 +1682,14 @@ class Query
|
||||
// 获取实际执行的SQL语句
|
||||
return $this->connection->getRealSql($sql, $bind);
|
||||
}
|
||||
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
|
||||
|
||||
// 执行操作
|
||||
return $this->execute($sql, $bind, $getLastInsID, $sequence);
|
||||
$result = $this->execute($sql, $bind);
|
||||
if ($getLastInsID) {
|
||||
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
|
||||
return $this->getLastInsID($sequence);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2031,7 +2037,7 @@ class Query
|
||||
public function chunk($count, $callback, $column = null)
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$column = $column ?: ($options['pk'] ?: $this->getPk());
|
||||
$column = $column ?: $this->getPk();
|
||||
$bind = $this->bind;
|
||||
$resultSet = $this->limit($count)->order($column, 'asc')->select();
|
||||
|
||||
|
||||
@@ -54,8 +54,12 @@ class Mysql extends Connection
|
||||
if (strpos($tableName, '.')) {
|
||||
$tableName = str_replace('.', '`.`', $tableName);
|
||||
}
|
||||
$sql = 'SHOW COLUMNS FROM `' . $tableName . '`';
|
||||
$pdo = $this->linkID->query($sql);
|
||||
$sql = 'SHOW COLUMNS FROM `' . $tableName . '`';
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试结束
|
||||
$this->debug(false, $sql);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
if ($result) {
|
||||
@@ -82,8 +86,12 @@ class Mysql extends Connection
|
||||
*/
|
||||
public function getTables($dbName = '')
|
||||
{
|
||||
$sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
|
||||
$pdo = $this->linkID->query($sql);
|
||||
$sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试结束
|
||||
$this->debug(false, $sql);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
foreach ($result as $key => $val) {
|
||||
@@ -110,8 +118,9 @@ class Mysql extends Connection
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function supportSavepoint(){
|
||||
|
||||
protected function supportSavepoint()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,9 +46,13 @@ class Pgsql extends Connection
|
||||
$this->initConnect(true);
|
||||
list($tableName) = explode(' ', $tableName);
|
||||
$sql = 'select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\'' . $tableName . '\');';
|
||||
$pdo = $this->linkID->query($sql);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试结束
|
||||
$this->debug(false, $sql);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
if ($result) {
|
||||
foreach ($result as $key => $val) {
|
||||
$val = array_change_key_case($val);
|
||||
@@ -73,8 +77,12 @@ class Pgsql extends Connection
|
||||
*/
|
||||
public function getTables($dbName = '')
|
||||
{
|
||||
$sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'";
|
||||
$pdo = $this->linkID->query($sql);
|
||||
$sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'";
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试结束
|
||||
$this->debug(false, $sql);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
foreach ($result as $key => $val) {
|
||||
@@ -94,7 +102,8 @@ class Pgsql extends Connection
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function supportSavepoint(){
|
||||
protected function supportSavepoint()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,9 +43,13 @@ class Sqlite extends Connection
|
||||
$this->initConnect(true);
|
||||
list($tableName) = explode(' ', $tableName);
|
||||
$sql = 'PRAGMA table_info( ' . $tableName . ' )';
|
||||
$pdo = $this->linkID->query($sql);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试结束
|
||||
$this->debug(false, $sql);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
if ($result) {
|
||||
foreach ($result as $key => $val) {
|
||||
$val = array_change_key_case($val);
|
||||
@@ -73,7 +77,11 @@ class Sqlite extends Connection
|
||||
$sql = "SELECT name FROM sqlite_master WHERE type='table' "
|
||||
. "UNION ALL SELECT name FROM sqlite_temp_master "
|
||||
. "WHERE type='table' ORDER BY name";
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试结束
|
||||
$this->debug(false, $sql);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
foreach ($result as $key => $val) {
|
||||
@@ -93,7 +101,8 @@ class Sqlite extends Connection
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function supportSavepoint(){
|
||||
protected function supportSavepoint()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,11 @@ class Sqlsrv extends Connection
|
||||
AND t.table_schema = c.table_schema
|
||||
AND t.table_name = c.table_name
|
||||
WHERE t.table_name = '$tableName'";
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试结束
|
||||
$this->debug(false, $sql);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
if ($result) {
|
||||
@@ -74,8 +78,12 @@ class Sqlsrv extends Connection
|
||||
];
|
||||
}
|
||||
}
|
||||
$sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'";
|
||||
$pdo = $this->linkID->query($sql);
|
||||
$sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'";
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试结束
|
||||
$this->debug(false, $sql);
|
||||
$result = $pdo->fetch(PDO::FETCH_ASSOC);
|
||||
if ($result) {
|
||||
$info[$result['column_name']]['primary'] = true;
|
||||
@@ -95,7 +103,11 @@ class Sqlsrv extends Connection
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE TABLE_TYPE = 'BASE TABLE'
|
||||
";
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试结束
|
||||
$this->debug(false, $sql);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
foreach ($result as $key => $val) {
|
||||
|
||||
Reference in New Issue
Block a user