<?php
namespace think\db;
use PDO;
use think\App;
use think\Cache;
use think\Collection;
use think\Config;
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\Relation;
use think\model\relation\OneToOne;
use think\Paginator;
class Query
{
// 数据库Connection对象实例
protected $connection;
// 数据库Builder对象实例
protected $builder;
// 当前模型类名称
protected $model;
// 当前数据表名称(含前缀)
protected $table = '';
// 当前数据表名称(不含前缀)
protected $name = '';
// 当前数据表主键
protected $pk;
// 当前数据表前缀
protected $prefix = '';
// 查询参数
protected $options = [];
// 参数绑定
protected $bind = [];
// 数据表信息
protected static $info = [];
// 回调事件
private static $event = [];
/**
* 构造函数
* @access public
* @param Connection $connection 数据库对象实例
* @param Model $model 模型对象
*/
public function __construct(Connection $connection = null, $model = null)
{
$this->connection = $connection ?: Db::connect([], true);
$this->prefix = $this->connection->getConfig('prefix');
$this->model = $model;
// 设置当前连接的Builder对象
$this->setBuilder();
}
/**
* 利用__call方法实现一些特殊的Model方法
* @access public
* @param string $method 方法名称
* @param array $args 调用参数
* @return mixed
* @throws DbException
* @throws Exception
*/
public function __call($method, $args)
{
if (strtolower(substr($method, 0, 5)) == 'getby') {
// 根据某个字段获取记录
$field = Loader::parseName(substr($method, 5));
$where[$field] = $args[0];
return $this->where($where)->find();
} elseif (strtolower(substr($method, 0, 10)) == 'getfieldby') {
// 根据某个字段获取记录的某个值
$name = Loader::parseName(substr($method, 10));
$where[$name] = $args[0];
return $this->where($where)->value($args[1]);
} else {
throw new Exception('method not exist:' . __CLASS__ . '->' . $method);
}
}
/**
* 获取当前的数据库Connection对象
* @access public
* @return Connection
*/
public function getConnection()
{
return $this->connection;
}
/**
* 切换当前的数据库连接
* @access public
* @param mixed $config
* @return $this
*/
public function connect($config)
{
$this->connection = Db::connect($config);
$this->setBuilder();
return $this;
}
/**
* 设置当前的数据库Builder对象
* @access protected
* @return void
*/
protected function setBuilder()
{
$class = $this->connection->getBuilder();
$this->builder = new $class($this->connection, $this);
}
/**
* 获取当前的模型对象实例
* @access public
* @return Model|null
*/
public function getModel()
{
return $this->model;
}
/**
* 获取当前的builder实例对象
* @access public
* @return Builder
*/
public function getBuilder()
{
return $this->builder;
}
/**
* 指定默认的数据表名(不含前缀)
* @access public
* @param string $name
* @return $this
*/
public function name($name)
{
$this->name = $name;
return $this;
}
/**
* 指定默认数据表名(含前缀)
* @access public
* @param string $table 表名
* @return $this
*/
public function setTable($table)
{
$this->table = $table;
return $this;
}
/**
* 得到当前或者指定名称的数据表
* @access public
* @param string $name
* @return string
*/
public function getTable($name = '')
{
if ($name || empty($this->table)) {
$name = $name ?: $this->name;
$tableName = $this->prefix;
if ($name) {
$tableName .= Loader::parseName($name);
}
} else {
$tableName = $this->table;
}
return $tableName;
}
/**
* 将SQL语句中的__TABLE_NAME__字符串替换成带前缀的表名(小写)
* @access public
* @param string $sql sql语句
* @return string
*/
public function parseSqlTable($sql)
{
if (false !== strpos($sql, '__')) {
$prefix = $this->prefix;
$sql = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {
return $prefix . strtolower($match[1]);
}, $sql);
}
return $sql;
}
/**
* 执行查询 返回数据集
* @access public
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param boolean $master 是否在主服务器读操作
* @param bool|string $class 指定返回的数据集对象
* @return mixed
* @throws BindParamException
* @throws PDOException
*/
public function query($sql, $bind = [], $master = false, $class = false)
{
return $this->connection->query($sql, $bind, $master, $class);
}
/**
* 执行语句
* @access public
* @param string $sql sql指令
* @param array $bind 参数绑定
* @return int
* @throws BindParamException
* @throws PDOException
*/
public function execute($sql, $bind = [])
{
return $this->connection->execute($sql, $bind);
}
/**
* 获取最近插入的ID
* @access public
* @param string $sequence 自增序列名
* @return string
*/
public function getLastInsID($sequence = null)
{
return $this->connection->getLastInsID($sequence);
}
/**
* 获取最近一次查询的sql语句
* @access public
* @return string
*/
public function getLastSql()
{
return $this->connection->getLastSql();
}
/**
* 执行数据库事务
* @access public
* @param callable $callback 数据操作方法回调
* @return mixed
*/
public function transaction($callback)
{
return $this->connection->transaction($callback);
}
/**
* 启动事务
* @access public
* @return void
*/
public function startTrans()
{
$this->connection->startTrans();
}
/**
* 用于非自动提交状态下面的查询提交
* @access public
* @return void
* @throws PDOException
*/
public function commit()
{
$this->connection->commit();
}
/**
* 事务回滚
* @access public
* @return void
* @throws PDOException
*/
public function rollback()
{
$this->connection->rollback();
}
/**
* 批处理执行SQL语句
* 批处理的指令都认为是execute操作
* @access public
* @param array $sql SQL批处理指令
* @return boolean
*/
public function batchQuery($sql = [])
{
return $this->connection->batchQuery($sql);
}
/**
* 获取数据库的配置参数
* @access public
* @param string $name 参数名称
* @return boolean
*/
public function getConfig($name = '')
{
return $this->connection->getConfig($name);
}
/**
* 得到分表的的数据表名
* @access public
* @param arr
没有合适的资源?快使用搜索试试~ 我知道了~
易优CMS 企业管理模板
共1264个文件
php:383个
png:200个
htm:160个
1星 需积分: 21 10 下载量 161 浏览量
2018-10-10
09:21:32
上传
评论
收藏 11.81MB ZIP 举报
温馨提示
做网站,用易优! 超过 10 万家优秀企业选择 简单易用 / 模板多 / 开源 / 易优化 / 全免费
资源推荐
资源详情
资源评论
收起资源包目录
易优CMS 企业管理模板 (1264个子文件)
test.bmp 0B
basic.css 272KB
bootstrap.css 138KB
common.css 135KB
bootstrap.min.css 115KB
main.css 76KB
main.css 76KB
ueditor.css 44KB
index.css 39KB
font-awesome-ie7.min.css 37KB
font-awesome-ie7.min.css 37KB
ueditor.min.css 34KB
font-awesome.css 26KB
font-awesome.css 26KB
font-awesome.min.css 23KB
font-awesome.min.css 23KB
font-awesome.min.css 22KB
font-awesome.min.css 22KB
video-js.css 21KB
jquery-ui.min.css 20KB
jquery-ui.min.css 20KB
image.css 19KB
style.css 16KB
video.css 15KB
attachment.css 15KB
layer.css 14KB
video-js.min.css 11KB
jquery.qtip.min.css 11KB
shCoreBlue.css 9KB
shCoreDefault2.css 9KB
codemirror.css 8KB
shCoreDefault.css 7KB
install.css 7KB
web-icons.min.css 6KB
laydate.css 6KB
layer.css 5KB
login.css 5KB
scrawl.css 4KB
dialog.css 4KB
laydate.css 3KB
laydate.css 3KB
laydate.css 3KB
page.css 3KB
page.css 3KB
codemirror.css 3KB
charts.css 3KB
perfect-scrollbar.min.css 3KB
perfect-scrollbar.min.css 2KB
perfect-scrollbar.min.css 2KB
background.css 2KB
emotion.css 2KB
dialogbase.css 2KB
music.css 2KB
style.css 1KB
edittable.css 1KB
template.css 1KB
iconfont.css 667B
webuploader.css 515B
webuploader.css 515B
help.css 395B
eyou.css 354B
iframe.css 42B
pinyin.dat 53KB
Thumbs.db 24KB
fontawesome-webfont.eot 69KB
fontawesome-webfont.eot 59KB
fontawesome-webfont.eot 55KB
fontawesome-webfont.eot 55KB
web-icons.eot 35KB
glyphicons-halflings-regular.eot 20KB
iconfont.eot 5KB
lg.eot 4KB
vjs.eot 3KB
UEditorSnapscreen.exe 508KB
hiddeninput.exe 9KB
test.gif 233KB
wface.gif 49KB
jxface2.gif 40KB
yface.gif 28KB
bface.gif 27KB
icons.gif 20KB
file-icons.gif 20KB
file-icons.gif 20KB
tface.gif 19KB
fface.gif 18KB
loading.gif 11KB
cface.gif 8KB
loading-0.gif 6KB
go_2.gif 5KB
ajaxLoader.gif 5KB
go_3.gif 5KB
go_1.gif 4KB
go_4.gif 4KB
ajax-loader.gif 4KB
icons-all.gif 4KB
icon_xlsg.gif 3KB
channel_bg.gif 2KB
loading.gif 2KB
loading-2.gif 2KB
videologo.gif 2KB
共 1264 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
- qq_401070422019-06-22和官网下载的一样
vaststrong
- 粉丝: 9
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功