<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\db;
use PDO;
use think\Collection;
use think\Container;
use think\Db;
use think\db\exception\BindParamException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Loader;
use think\Model;
use think\model\Collection as ModelCollection;
use think\model\Relation;
use think\model\relation\OneToOne;
use think\Paginator;
class Query
{
/**
* 当前数据库连接对象
* @var Connection
*/
protected $connection;
/**
* 当前模型对象
* @var Model
*/
protected $model;
/**
* 当前数据表名称(不含前缀)
* @var string
*/
protected $name = '';
/**
* 当前数据表主键
* @var string|array
*/
protected $pk;
/**
* 当前数据表前缀
* @var string
*/
protected $prefix = '';
/**
* 当前查询参数
* @var array
*/
protected $options = [];
/**
* 当前参数绑定
* @var array
*/
protected $bind = [];
/**
* 事件回调
* @var array
*/
private static $event = [];
/**
* 扩展查询方法
* @var array
*/
private static $extend = [];
/**
* 读取主库的表
* @var array
*/
protected static $readMaster = [];
/**
* 日期查询表达式
* @var array
*/
protected $timeRule = [
'today' => ['today', 'tomorrow -1second'],
'yesterday' => ['yesterday', 'today -1second'],
'week' => ['this week 00:00:00', 'next week 00:00:00 -1second'],
'last week' => ['last week 00:00:00', 'this week 00:00:00 -1second'],
'month' => ['first Day of this month 00:00:00', 'first Day of next month 00:00:00 -1second'],
'last month' => ['first Day of last month 00:00:00', 'first Day of this month 00:00:00 -1second'],
'year' => ['this year 1/1', 'next year 1/1 -1second'],
'last year' => ['last year 1/1', 'this year 1/1 -1second'],
];
/**
* 日期查询快捷定义
* @var array
*/
protected $timeExp = ['d' => 'today', 'w' => 'week', 'm' => 'month', 'y' => 'year'];
/**
* 架构函数
* @access public
*/
public function __construct(Connection $connection = null)
{
if (is_null($connection)) {
$this->connection = Db::connect();
} else {
$this->connection = $connection;
}
$this->prefix = $this->connection->getConfig('prefix');
}
/**
* 创建一个新的查询对象
* @access public
* @return Query
*/
public function newQuery()
{
$query = new static($this->connection);
if ($this->model) {
$query->model($this->model);
}
if (isset($this->options['table'])) {
$query->table($this->options['table']);
} else {
$query->name($this->name);
}
if (isset($this->options['json'])) {
$query->json($this->options['json'], $this->options['json_assoc']);
}
if (isset($this->options['field_type'])) {
$query->setJsonFieldType($this->options['field_type']);
}
return $query;
}
/**
* 利用__call方法实现一些特殊的Model方法
* @access public
* @param string $method 方法名称
* @param array $args 调用参数
* @return mixed
* @throws DbException
* @throws Exception
*/
public function __call($method, $args)
{
if (isset(self::$extend[strtolower($method)])) {
// 调用扩展查询方法
array_unshift($args, $this);
return Container::getInstance()
->invoke(self::$extend[strtolower($method)], $args);
} elseif (strtolower(substr($method, 0, 5)) == 'getby') {
// 根据某个字段获取记录
$field = Loader::parseName(substr($method, 5));
return $this->where($field, '=', $args[0])->find();
} elseif (strtolower(substr($method, 0, 10)) == 'getfieldby') {
// 根据某个字段获取记录的某个值
$name = Loader::parseName(substr($method, 10));
return $this->where($name, '=', $args[0])->value($args[1]);
} elseif (strtolower(substr($method, 0, 7)) == 'whereor') {
$name = Loader::parseName(substr($method, 7));
array_unshift($args, $name);
return call_user_func_array([$this, 'whereOr'], $args);
} elseif (strtolower(substr($method, 0, 5)) == 'where') {
$name = Loader::parseName(substr($method, 5));
array_unshift($args, $name);
return call_user_func_array([$this, 'where'], $args);
} elseif ($this->model && method_exists($this->model, 'scope' . $method)) {
// 动态调用命名范围
$method = 'scope' . $method;
array_unshift($args, $this);
call_user_func_array([$this->model, $method], $args);
return $this;
} else {
throw new Exception('method not exist:' . ($this->model ? get_class($this->model) : static::class) . '->' . $method);
}
}
/**
* 扩展查询方法
* @access public
* @param string|array $method 查询方法名
* @param callable $callback
* @return void
*/
public static function extend($method, $callback = null)
{
if (is_array($method)) {
foreach ($method as $key => $val) {
self::$extend[strtolower($key)] = $val;
}
} else {
self::$extend[strtolower($method)] = $callback;
}
}
/**
* 设置当前的数据库Connection对象
* @access public
* @param Connection $connection
* @return $this
*/
public function setConnection(Connection $connection)
{
$this->connection = $connection;
$this->prefix = $this->connection->getConfig('prefix');
return $this;
}
/**
* 获取当前的数据库Connection对象
* @access public
* @return Connection
*/
public function getConnection()
{
return $this->connection;
}
/**
* 指定模型
* @access public
* @param Model $model 模型对象实例
* @return $this
*/
public function model(Model $model)
{
$this->model = $model;
return $this;
}
/**
* 获取当前的模型对象
* @access public
* @return Model|null
*/
public function getModel()
{
return $this->model ? $this->model->setQuery($this) : null;
}
/**
* 设置从主库读取数据
* @access public
* @param bool $all 是否所有表有效
* @return $this
*/
public function readMaster($all = false)
{
$table = $all ? '*' : $this->getTable();
static::$readMaster[$table] = true;
return $this;
}
/**
* 指定当前数据表名(不含前缀)
* @access public
* @param string $name
* @return $this
*/
public function name($name)
{
$this->name = $name;
return $this;
}
/**
行动之上
- 粉丝: 2275
- 资源: 931
最新资源
- 基于前端vue3+element-plus,后端springboot+mysql的智慧云党建系统,BS架构全部资料+高分项目+详细文档.zip
- gripper-anhe
- 基于情感分析的智慧养老系统详细文档+全部资料+高分项目.zip
- 基于停车场系统后台管理,新能源电动车充电系统,智慧社区物业人脸门禁后台管理全部资料+高分项目+详细文档.zip
- 基于微家政-智慧社区家政服务系统全部资料+高分项目+详细文档.zip
- 基于认知计算的智慧就业服务系统全部资料+高分项目+详细文档.zip
- 基于至文掌上社区系统微信小程序端,街道居委在线服务小程序、智慧社区小程序系统全部资料+高分项目+详细文档.zip
- 基于云C智慧药店系统全部资料+高分项目+详细文档.zip
- 基于智慧办公室就是以办公室为平台,兼具办公室环境、办公、设备信息化、设 备智能化、考勤自动化、办公智能化,集系统、结构、服务、管理、监督于一体, 具有高效、安全
- 基于智慧仓库管理系统全部资料+高分项目+详细文档.zip
- 基于智慧城市交通策略优化与调控系统 前端项目全部资料+高分项目+详细文档.zip
- 基于智慧城市大屏可视化系统全部资料+高分项目+详细文档.zip
- 基于智慧城市空气质量预测与分析系统全部资料+高分项目+详细文档.zip
- 基于智慧档案管理系统全部资料+高分项目+详细文档.zip
- 基于智慧点餐系统正式成立全部资料+高分项目+详细文档.zip
- 基于智慧工匠,智能排产系统框架全部资料+高分项目+详细文档.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈