mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-03 05:52:48 +08:00
改进Query类和Builder类
This commit is contained in:
@@ -37,22 +37,33 @@ abstract class Builder
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param Connection $connection 数据库连接对象实例
|
||||
* @param Connection $connection 数据库连接对象实例
|
||||
* @param Query $query 数据库查询对象实例
|
||||
*/
|
||||
public function __construct(Connection $connection)
|
||||
public function __construct(Connection $connection, Query $query)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->query = $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前的Query对象实例
|
||||
* @access protected
|
||||
* @param Query $query 当前查询对象实例
|
||||
* 获取当前的连接对象实例
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function setQuery(Query $query)
|
||||
public function getConnection()
|
||||
{
|
||||
$this->query = $query;
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的Query对象实例
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function getQuery()
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -330,9 +341,7 @@ abstract class Builder
|
||||
} elseif (in_array($exp, ['NOT IN', 'IN'])) {
|
||||
// IN 查询
|
||||
if ($value instanceof \Closure) {
|
||||
$query = $this->query;
|
||||
$whereStr .= $key . ' ' . $exp . ' ' . $this->parseClosure($value);
|
||||
$this->query = $query;
|
||||
} else {
|
||||
$value = is_array($value) ? $value : explode(',', $value);
|
||||
if (array_key_exists($field, $binds)) {
|
||||
@@ -378,9 +387,7 @@ abstract class Builder
|
||||
} elseif (in_array($exp, ['NOT EXISTS', 'EXISTS'])) {
|
||||
// EXISTS 查询
|
||||
if ($value instanceof \Closure) {
|
||||
$query = $this->query;
|
||||
$whereStr .= $exp . ' ' . $this->parseClosure($value);
|
||||
$this->query = $query;
|
||||
} else {
|
||||
$whereStr .= $exp . ' (' . $value . ')';
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ class Query
|
||||
{
|
||||
// 数据库Connection对象实例
|
||||
protected $connection;
|
||||
// 数据库驱动类型
|
||||
// 数据库Builder对象实例
|
||||
protected $builder;
|
||||
// 当前模型类名称
|
||||
protected $model;
|
||||
@@ -63,9 +63,10 @@ class Query
|
||||
public function __construct(Connection $connection = null, $model = '')
|
||||
{
|
||||
$this->connection = $connection ?: Db::connect([], true);
|
||||
$this->builder = $this->connection->getConfig('builder') ?: $this->connection->getConfig('type');
|
||||
$this->prefix = $this->connection->getConfig('prefix');
|
||||
$this->model = $model;
|
||||
// 设置当前连接的Builder对象
|
||||
$this->setBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,19 +114,42 @@ class Query
|
||||
public function connect($config)
|
||||
{
|
||||
$this->connection = Db::connect($config);
|
||||
$this->setBuilder();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前的数据库Builder对象
|
||||
* @access protected
|
||||
* @return void
|
||||
*/
|
||||
protected function setBuilder()
|
||||
{
|
||||
$builder = $this->connection->getConfig('builder') ?: $this->connection->getConfig('type');
|
||||
$class = false !== strpos($builder, '\\') ? $builder : '\\think\\db\\builder\\' . ucfirst($builder);
|
||||
$this->builder = new $class($this->connection, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的模型对象名
|
||||
* @access public
|
||||
* @return Connection
|
||||
* @return string
|
||||
*/
|
||||
public function getModel()
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的builder实例对象
|
||||
* @access public
|
||||
* @return Builder
|
||||
*/
|
||||
public function getBuilder()
|
||||
{
|
||||
return $this->builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定默认的数据表名(不含前缀)
|
||||
* @access public
|
||||
@@ -362,24 +386,6 @@ class Query
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的builder实例对象
|
||||
* @access protected
|
||||
* @return Builder
|
||||
*/
|
||||
protected function builder()
|
||||
{
|
||||
static $builder = [];
|
||||
$driver = $this->builder;
|
||||
if (!isset($builder[$driver])) {
|
||||
$class = false !== strpos($driver, '\\') ? $driver : '\\think\\db\\builder\\' . ucfirst($driver);
|
||||
$builder[$driver] = new $class($this->connection);
|
||||
}
|
||||
// 设置当前查询对象
|
||||
$builder[$driver]->setQuery($this);
|
||||
return $builder[$driver];
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到某个字段的值
|
||||
* @access public
|
||||
@@ -1758,7 +1764,7 @@ class Query
|
||||
// 分析查询表达式
|
||||
$options = $this->parseExpress();
|
||||
// 生成SQL语句
|
||||
$sql = $this->builder()->insert($data, $options, $replace);
|
||||
$sql = $this->builder->insert($data, $options, $replace);
|
||||
// 获取参数绑定
|
||||
$bind = $this->getBind();
|
||||
if ($options['fetch_sql']) {
|
||||
@@ -1802,7 +1808,7 @@ class Query
|
||||
return false;
|
||||
}
|
||||
// 生成SQL语句
|
||||
$sql = $this->builder()->insertAll($dataSet, $options);
|
||||
$sql = $this->builder->insertAll($dataSet, $options);
|
||||
// 获取参数绑定
|
||||
$bind = $this->getBind();
|
||||
if ($options['fetch_sql']) {
|
||||
@@ -1828,7 +1834,7 @@ class Query
|
||||
$options = $this->parseExpress();
|
||||
// 生成SQL语句
|
||||
$table = $this->parseSqlTable($table);
|
||||
$sql = $this->builder()->selectInsert($fields, $table, $options);
|
||||
$sql = $this->builder->selectInsert($fields, $table, $options);
|
||||
// 获取参数绑定
|
||||
$bind = $this->getBind();
|
||||
if ($options['fetch_sql']) {
|
||||
@@ -1886,7 +1892,7 @@ class Query
|
||||
$key = 'think:' . $options['table'] . '|' . $options['where']['AND'][$pk];
|
||||
}
|
||||
// 生成UPDATE SQL语句
|
||||
$sql = $this->builder()->update($data, $options);
|
||||
$sql = $this->builder->update($data, $options);
|
||||
// 获取参数绑定
|
||||
$bind = $this->getBind();
|
||||
if ($options['fetch_sql']) {
|
||||
@@ -1941,7 +1947,7 @@ class Query
|
||||
}
|
||||
if (!$resultSet) {
|
||||
// 生成查询SQL
|
||||
$sql = $this->builder()->select($options);
|
||||
$sql = $this->builder->select($options);
|
||||
// 获取参数绑定
|
||||
$bind = $this->getBind();
|
||||
if ($options['fetch_sql']) {
|
||||
@@ -2035,7 +2041,7 @@ class Query
|
||||
}
|
||||
if (!$result) {
|
||||
// 生成查询SQL
|
||||
$sql = $this->builder()->select($options);
|
||||
$sql = $this->builder->select($options);
|
||||
// 获取参数绑定
|
||||
$bind = $this->getBind();
|
||||
if ($options['fetch_sql']) {
|
||||
@@ -2231,7 +2237,7 @@ class Query
|
||||
throw new Exception('delete without condition');
|
||||
}
|
||||
// 生成删除SQL语句
|
||||
$sql = $this->builder()->delete($options);
|
||||
$sql = $this->builder->delete($options);
|
||||
// 获取参数绑定
|
||||
$bind = $this->getBind();
|
||||
if ($options['fetch_sql']) {
|
||||
|
||||
Reference in New Issue
Block a user