<?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 <[email protected]>
// +----------------------------------------------------------------------
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 = [];
// 读取主库
private static $readMaster = [];
/**
* 构造函数
* @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();
$this->prefix = $this->connection->getConfig('prefix');
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;
}
/**
* 设置后续从主库读取数据
* @access public
* @param bool $allTable
* @return void
*/
public function readMaster($allTable = false)
{
if ($allTable) {
$table = '*';
} else {
$table = isset($this->options['table']) ? $this->options['table'] : $this->getTable();
}
static::$readMaster[$table] = true;
return $this;
}
/**
* 获取当前的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, $this);
}
/**
* 获取最近插入的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();
}
/**
* 执行�
没有合适的资源?快使用搜索试试~ 我知道了~
wechat-norm-demo.zip
共2000个文件
php:783个
png:332个
gif:311个
需积分: 10 1 下载量 115 浏览量
2021-12-01
12:33:08
上传
评论
收藏 850.26MB ZIP 举报
温馨提示
wechat小程序与layuiadmin实现多规格商品源码
资源详情
资源评论
资源推荐
收起资源包目录
wechat-norm-demo.zip (2000个子文件)
006aa3fd85fea504bc62d7ec153d3d1ba9be95 494B
00b50a444b09f963441d0ab0e64c582e485745 239B
010ca94001374157561bc41e035d4a50a84301 27KB
019f8ff85d7520cd36fa6f67c6311996228217 1KB
02a42fdb6dcbe143d205fc7d3657e52cddc081 967B
02add20b581be471b8d17f887b8e8337070546 194B
03828ebee99e12a85566163ee6bef1253a0d9f 126B
04f2afb90ef1a93323dfb07b31db77763187f1 2KB
063b0fd9a834e9c98333c33662d30362d125e3 81B
07b456f7d7e1865e4f701846c6902a7c31e51c 134B
086f7f893f031c23837ef5d32543da52eaa84f 48KB
0897e40b26a3cc6529cb4ab5c2de8cd35a15c5 7KB
09085a03e249b2af928275fa55bf356eec2f4e 10KB
091fd16460730f6f19aa2450fcc356538008dd 52KB
09559f2a4b2c0baf254ce4a41f89f4a6dc43e3 3KB
09d336a30070add12af16be11397ac97436234 1KB
09e16a89ccfdccdd75cb7d67cfa5dcef7c10d5 1KB
0b23955a604cd8af1b145e743baf8c89546d5f 135B
0b81ba4dd535bbfe38ddd6bc5b35ed85d22a0f 2KB
0c128e433cd6ed4cde7aa50c2a43603f48870d 133B
0d2d6d6deebc6de7ba90537d74ef686f48d105 132B
0d3dd84026c33b31712f94d1c189db2b3e7295 2KB
0e2b9af46c46a72bb935137f139294f16ab7eb 3KB
0e7d339fca9d0d12b9e66a1fbb6a68f4c6f4b4 1KB
0e864264353800e362f1e7b160d9be9980a7b3 368B
0ec65f1090ed879101d6a739bd41c14b4c2745 1KB
0fd946200a217cdb288a560af11d7919de4335 504B
0ff0b1a7e08400d1aaf0edf7214d4bab199304 40KB
105c988898cc03bf21c4ef26df1f7fb6f34824 430B
10d5838b37d3bd01c3572de24b81def813b143 638B
113eead2a7258acf539d1a6ee140df28f001b6 3KB
11afb60358b9c30728a87f9089bd3cde7b7f82 394B
12dd5da196b15a6fa31115da13cb24730c14ef 8KB
139e1d1ebbb9123de502b7b8bc0127dc55334f 3KB
167457403fccff8738b9f694d97b98129ff418 609B
192139bd7de52859177792835d315eae04e2de 252B
1b56411e8f8e4341251e15bb874cd94c4ec573 1KB
1b7868f3841c70b63c70a807f43e42cb685a06 2KB
1cfdb5440686b0a0c152586b64faa2f8584ec4 47KB
1e3b0f5093b7098d8307b2731fa5c47669b857 512B
1e3e99303f66c29773c16f32d9564e2da9535c 78B
1e6432859d7878da5582be4316da81c869358b 132B
1e8aab1e210fbdf7cb54247a38b05f086c3072 2KB
1ed1140af275d8c5c8d511bf8016af997fadd5 487B
21abaa6d811cf0562ff82ccb24fa76bb9508df 610B
23bfaac75af2b450692e93890435fde4225efb 5KB
24606c787dcfa5dd266db85f63f3ea7c282a35 126B
24eaf00f5a6a11679498adeb6460aa0a1b8ed8 2KB
255004ceea385e7d193d36f9791885db5de933 485B
255784886c07e450ca3430a1873c3f42056f29 386B
26d7d734f5a92178a96f81a7b409e9eca9bcc7 2KB
2710453f6b8f6fc9b6c34192c52dfe5a538293 595B
272a690b6740b94956037d021a9f0aa7136b64 253B
27663ae037c9be9646c26703aad9c0aa90c57b 2KB
277736bef85b87f9008ca21d6cc156fefa2181 5KB
27923476b400c23aa05e5b5fb80d100a6db174 135B
284cf39d84e7e70e1633199579330ac996e598 1KB
28e3da103099151b00c5e700302d8d9c69f6a7 323B
29101a71efc84d76fcdc0e0acab11fa4b832fd 2KB
293c3ab0750b5a1f654fcd1851c5565dfd1199 2KB
29be87c890a6633eda84dacba996a29183c09e 2KB
2c09e4f5cb6aa2734e850059564c027385daef 166B
2dbc2059512f65a12c4a50beee3736171ab588 276B
2e3b9d6741ed282a9185ad6361e6447e005c72 403B
2edb9eb89c172ab5979f1c87f95518bcea82d3 2KB
2eff23e345569189e0dd16516733f20d7b4442 4KB
3014e273d6761725441a64318005e63a613ca0 550B
3057ab7eceb885238c9308a9591b118213703a 3KB
30ab7bb1ec2780d64870cce456d169c55f8e7e 1KB
310a3f4175ecf4a80226ff6328b27fd23e7e07 49KB
31185f91fb5fe96e3ad7db25717d4cb326063c 166B
3148e4e09619b4757f698e2d1ba4eec76988b5 317B
326bd2fd865df3a6d418f636e10f1b8d10a50f 471B
348684ced9be6e3be0579d689fad9465ab15c9 55KB
352dde46f7f530c7f4e9cc71a49a8db2e5d525 2KB
3538a844512524db87e2f428421fca7fb3f97a 1KB
353cadc85e447155e636b50ab392a99b36f664 1KB
356b19a53ac1d741d9e1dbd01acfb8ae21faf6 126B
35af0699ccec004cbe685ef938cd2d63ea7037 43B
373f8e062739ef29559cd6e3f005735dcb812d 134B
375e4790f2a5124be66c4541002524d18eafc0 575B
37b63ef5a3804b869600521712c2e67d33fd0f 272B
37e556ac1ae011ac9846411a41b2b736b28cf1 47KB
38f3bb21d443dfb06a19b02fb4067412c1f385 9KB
3b813a8004234b55e4d046c2951159cbbfbaf9 192B
3c3dfaf8ec8a47b8eb32192f0ef70dc15ad5c8 48B
3e743ce5f5611321cb04f9e6266024db0070e2 3KB
41d40f0a2c14d085eb603c1bc5dd86aa4ba240 249B
42920fb10c565f007c8bf928b646d822170e5d 77KB
431f0da2c5a35906584411fc051db015ab99c3 113B
43aab916deb92f066b04523bfc3b778dc52b28 166B
43ee4100ca42580141388762a814af6ef52d82 586B
440cc308fa5e3e92d55d01e2aa0ff7ef6ca76c 335B
4539998426e7cbc4114eb8809b680566a70129 3KB
454abbb2407a3001423785201c32bbac28faa1 568B
455f8c42ac24e754e368f7e8d72289623c9571 628B
45fb316793708e66edcc27ec8489d4aa378179 168B
45ff2af252ee7a3abbc468442ef7bd98ab16f9 6KB
4621c1023dae709a6aca3ef0aa47b09eef44b0 664B
468901e4a9412da4f5fc0c4edeaa73b3acf570 2KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
余祥伟
- 粉丝: 21
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0