<?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\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]);
} 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:' . __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();
}
/**
没有合适的资源?快使用搜索试试~ 我知道了~
Bty分销系统v1.0开源版源码.rar
共1100个文件
js:321个
png:215个
php:205个
需积分: 0 0 下载量 132 浏览量
2023-05-10
14:50:55
上传
评论
收藏 9.3MB RAR 举报
温馨提示
Bty分销系统开源版,宝塔分销系统开源版,该系统基于宝塔开放API底层控制器,进行主机控制,目前可以完成大部分主机操作功能如:新增主机、修改主机配置、删除主机、查找主机、以及宝塔面板能开放使用的主机操作功能,由于宝塔面板的单一用户性,所以开发这款可以由个人操作的IDC分销系统。 项目特色: 独立的用户管理、后台配置 使用宝塔API接入服务器可实现正常的网站操作,实现网站开通、域名绑定、ssl证书、防盗链、一键部署、网站防篡改、网站监控报表、防火墙等功能 配合宝塔强大的生态系统,安装更多插件后可使用到许多有趣的功能 内置易支付,简单方便完成会员充值操作 软件架构 code thinkphp5flatlab-bootstrap3 环境需求 code Centos7.2PHP5.3-7.3Mysql5.5Redis4.0.9Nginx1.14.0Pure-Ftpd 1.0.47phpMyAdmin 4.4 安装教程 1.上传并解压源码到网站根目录(暂不支持二级目录) 2.修改application/database.php中数据库信息 3.上传并导入数据库文件dkewl.sql 4.
资源推荐
资源详情
资源评论
收起资源包目录
Bty分销系统v1.0开源版源码.rar (1100个子文件)
bootstrap.min.css 111KB
style.css 107KB
bootstrap.min.css 107KB
editor_ie7.css 37KB
editor_iequirks.css 36KB
editor_ie8.css 36KB
editor_ie.css 35KB
editor_gecko.css 35KB
editor.css 34KB
style.css 33KB
jquery-ui-1.10.1.custom.min.css 26KB
settings.css 23KB
font-awesome.css 21KB
font-awesome.css 21KB
style2.css 19KB
dialog_ie7.css 17KB
style.css 17KB
dialog_ie8.css 17KB
dialog_iequirks.css 16KB
dialog_ie.css 16KB
dialog.css 16KB
soon.css 14KB
datetimepicker.css 12KB
style-responsive.css 12KB
bootstrap-fullcalendar.css 11KB
demo_table.css 11KB
dropzone.css 10KB
gallery.css 8KB
default.css 8KB
bootstrap-reset.css 7KB
toastr.css 7KB
jquery.bxslider.css 6KB
sample.css 5KB
bootstrap-reset.css 5KB
xcharts.css 5KB
daterangepicker-bs3.css 5KB
tree-style.css 4KB
datepicker.css 4KB
daterangepicker.css 4KB
jquery.nestable.css 4KB
jquery.fancybox.css 4KB
jquery.fancybox.css 4KB
DT_bootstrap.css 4KB
flexslider.css 4KB
slidebars.css 4KB
table-responsive.css 3KB
style-responsive.css 3KB
bootstrap-wysihtml5.css 2KB
colorpicker.css 2KB
multi-select.css 2KB
bootstrap-fileupload.css 2KB
jquery.Jcrop.min.css 2KB
jquery.gritter.css 2KB
timepicker.css 2KB
outputxhtml.css 2KB
rs-style.css 2KB
contents.css 2KB
login.css 2KB
demo_page.css 2KB
tasks.css 2KB
superfish.css 2KB
jquery.fileupload-ui.css 1KB
owl.carousel.css 1KB
toolbar.css 1KB
wsc.css 1KB
jquery.fileupload-ui.css 1KB
image-crop.css 890B
piao_msg.css 719B
jquery.fileupload.css 705B
morris.css 605B
superfish-vertical.css 583B
jquery.fileupload-noscript.css 431B
yamm.css 417B
jquery.fileupload-ui-noscript.css 371B
theme.css 199B
invoice-print.css 179B
jquery.easy-pie-chart.css 141B
fontawesome-webfont.eot 37KB
fontawesome-webfont.eot 37KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.eot 20KB
hiddeninput.exe 9KB
outputforflash.fla 84KB
logo.gif 27KB
logo.gif 27KB
logo.gif 27KB
logo.gif 27KB
bx_loader.gif 8KB
loading.gif 4KB
loading.gif 4KB
fancybox_loading.gif 4KB
fancybox_loading.gif 4KB
progressbar.gif 3KB
progressbar.gif 3KB
loader.gif 2KB
input-spinner.gif 2KB
animated-overlay.gif 2KB
loading.gif 381B
Jcrop.gif 329B
foo.gif 322B
共 1100 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
资源评论
小正太浩二
- 粉丝: 183
- 资源: 5909
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功