mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
数据库重构
This commit is contained in:
@@ -41,7 +41,7 @@ class Db
|
||||
if (empty($options['type'])) {
|
||||
throw new Exception('db type error');
|
||||
}
|
||||
$class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\driver\\') . ucwords($options['type']);
|
||||
$class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\connector\\') . ucwords($options['type']);
|
||||
self::$instances[$md5] = new $class($options);
|
||||
// 记录初始化信息
|
||||
APP_DEBUG && Log::record('[ DB ] INIT ' . $options['type'] . ':' . var_export($options, true), 'info');
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
namespace think;
|
||||
|
||||
use think\Cache;
|
||||
use think\Db;
|
||||
use think\db\Query;
|
||||
use think\Loader;
|
||||
|
||||
abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
@@ -700,6 +702,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
$class = self::db();
|
||||
if ($name instanceof \Closure) {
|
||||
call_user_func_array($name, [ & $class, $params]);
|
||||
} elseif ($name instanceof Query) {
|
||||
return $name;
|
||||
} else {
|
||||
$names = explode(',', $name);
|
||||
foreach ($names as $scope) {
|
||||
|
||||
620
library/think/db/Builder.php
Normal file
620
library/think/db/Builder.php
Normal file
@@ -0,0 +1,620 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 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\Db;
|
||||
use think\Exception;
|
||||
|
||||
abstract class Builder
|
||||
{
|
||||
// db对象实例
|
||||
protected $connection;
|
||||
protected $query;
|
||||
|
||||
// 查询参数
|
||||
protected $options = [];
|
||||
|
||||
// 数据库表达式
|
||||
protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'exp' => 'EXP', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN', 'exists' => 'EXISTS', 'notexists' => 'NOT EXISTS', 'not exists' => 'NOT EXISTS', 'null' => 'NULL', 'notnull' => 'NOT NULL', 'not null' => 'NOT NULL'];
|
||||
// 查询表达式
|
||||
protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%LOCK%%COMMENT%';
|
||||
protected $insertSql = '%INSERT% INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%';
|
||||
protected $insertAllSql = 'INSERT INTO %TABLE% (%FIELD%) %DATA% %COMMENT%';
|
||||
protected $updateSql = 'UPDATE %TABLE% SET %SET% %JOIN% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%';
|
||||
protected $deleteSql = 'DELETE FROM %TABLE% %USING% %JOIN% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%';
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param object $db 数据库对象实例
|
||||
*/
|
||||
public function __construct($connection, $query)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->query = $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将SQL语句中的__TABLE_NAME__字符串替换成带前缀的表名(小写)
|
||||
* @access protected
|
||||
* @param string $sql sql语句
|
||||
* @return string
|
||||
*/
|
||||
protected function parseSqlTable($sql)
|
||||
{
|
||||
return $this->connection->parseSqlTable($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据分析
|
||||
* @access protected
|
||||
* @param array $data 数据
|
||||
* @param array $bind 参数绑定类型
|
||||
* @param string $type insert update
|
||||
* @return array
|
||||
*/
|
||||
protected function parseData($data, $options)
|
||||
{
|
||||
if (empty($data)) {
|
||||
return [];
|
||||
}
|
||||
// 获取绑定信息
|
||||
$bind = $this->connection->getTableInfo($options['table'], 'bind');
|
||||
$fields = array_keys($bind);
|
||||
$result = [];
|
||||
foreach ($data as $key => $val) {
|
||||
if (!in_array($key, $fields, true)) {
|
||||
if ($this->connection->getAttribute('fields_strict')) {
|
||||
throw new Exception(' fields not exists :[' . $key . ']');
|
||||
}
|
||||
} else {
|
||||
$item = $this->parseKey($key);
|
||||
if (isset($val[0]) && 'exp' == $val[0]) {
|
||||
$result[$item] = $val[1];
|
||||
} elseif (is_null($val)) {
|
||||
$result[$item] = 'NULL';
|
||||
} elseif (is_scalar($val)) {
|
||||
// 过滤非标量数据
|
||||
$this->query->bind($key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
|
||||
$result[$item] = ':' . $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段名分析
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key)
|
||||
{
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* value分析
|
||||
* @access protected
|
||||
* @param mixed $value
|
||||
* @return string
|
||||
*/
|
||||
protected function parseValue($value)
|
||||
{
|
||||
if (is_string($value)) {
|
||||
$value = strpos($value, ':') === 0 && $this->query->isBind(substr($value, 1)) ? $value : $this->connection->quote($value);
|
||||
} elseif (is_array($value) && is_string($value[0]) && strtolower($value[0]) == 'exp') {
|
||||
$value = $value[1];
|
||||
} elseif (is_array($value)) {
|
||||
$value = array_map([$this, 'parseValue'], $value);
|
||||
} elseif (is_bool($value)) {
|
||||
$value = $value ? '1' : '0';
|
||||
} elseif (is_null($value)) {
|
||||
$value = 'null';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* field分析
|
||||
* @access protected
|
||||
* @param mixed $fields
|
||||
* @return string
|
||||
*/
|
||||
protected function parseField($fields)
|
||||
{
|
||||
if ('*' == $fields || empty($fields)) {
|
||||
$fieldsStr = '*';
|
||||
} elseif (is_array($fields)) {
|
||||
// 支持 'field1'=>'field2' 这样的字段别名定义
|
||||
$array = [];
|
||||
foreach ($fields as $key => $field) {
|
||||
if (!is_numeric($key)) {
|
||||
$array[] = $this->parseKey($key) . ' AS ' . $this->parseKey($field);
|
||||
} else {
|
||||
$array[] = $this->parseKey($field);
|
||||
}
|
||||
}
|
||||
$fieldsStr = implode(',', $array);
|
||||
}
|
||||
return $fieldsStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* table分析
|
||||
* @access protected
|
||||
* @param mixed $table
|
||||
* @return string
|
||||
*/
|
||||
protected function parseTable($tables)
|
||||
{
|
||||
if (is_array($tables)) {
|
||||
// 支持别名定义
|
||||
foreach ($tables as $table => $alias) {
|
||||
$array[] = !is_numeric($table) ?
|
||||
$this->parseKey($table) . ' ' . $this->parseKey($alias) :
|
||||
$this->parseKey($alias);
|
||||
}
|
||||
$tables = $array;
|
||||
} elseif (is_string($tables)) {
|
||||
$tables = $this->parseSqlTable($tables);
|
||||
$tables = array_map([$this, 'parseKey'], explode(',', $tables));
|
||||
}
|
||||
return implode(',', $tables);
|
||||
}
|
||||
|
||||
/**
|
||||
* where分析
|
||||
* @access protected
|
||||
* @param mixed $where
|
||||
* @return string
|
||||
*/
|
||||
protected function parseWhere($where, $table)
|
||||
{
|
||||
$whereStr = $this->buildWhere($where, $table);
|
||||
return empty($whereStr) ? '' : ' WHERE ' . $whereStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成查询条件SQL
|
||||
* @access public
|
||||
* @param mixed $where
|
||||
* @return string
|
||||
*/
|
||||
public function buildWhere($where, $table)
|
||||
{
|
||||
if (empty($where)) {
|
||||
$where = [];
|
||||
}
|
||||
|
||||
if ($where instanceof Query) {
|
||||
return $this->buildWhere($where->getOptions('where'), $table);
|
||||
}
|
||||
|
||||
$whereStr = '';
|
||||
// 获取字段信息
|
||||
$fields = $this->connection->getTableInfo($table, 'fields');
|
||||
$binds = $this->connection->getTableInfo($table, 'bind');
|
||||
foreach ($where as $key => $val) {
|
||||
$str = [];
|
||||
foreach ($val as $field => $value) {
|
||||
if (in_array($field, $fields, true) && is_scalar($value) && !$this->query->isBind($field)) {
|
||||
$this->query->bind($field, $value, isset($binds[$field]) ? $binds[$field] : PDO::PARAM_STR);
|
||||
$value = ':' . $field;
|
||||
}
|
||||
if ($value instanceof \Closure) {
|
||||
// 使用闭包查询
|
||||
$class = clone $this->query;
|
||||
$class->options([]);
|
||||
call_user_func_array($value, [ & $class]);
|
||||
$str[] = ' ' . $key . ' ( ' . $this->buildWhere($class->getOptions('where'), $table) . ' )';
|
||||
} else {
|
||||
if (strpos($field, '|')) {
|
||||
// 不同字段使用相同查询条件(OR)
|
||||
$array = explode('|', $field);
|
||||
$item = [];
|
||||
foreach ($array as $k) {
|
||||
$item[] = $this->parseWhereItem($k, $value);
|
||||
}
|
||||
$str[] = ' ' . $key . ' ( ' . implode(' OR ', $item) . ' )';
|
||||
} elseif (strpos($field, '&')) {
|
||||
// 不同字段使用相同查询条件(AND)
|
||||
$array = explode('&', $field);
|
||||
$item = [];
|
||||
foreach ($array as $k) {
|
||||
$item[] = $this->parseWhereItem($k, $value);
|
||||
}
|
||||
$str[] = ' ' . $key . ' ( ' . implode(' AND ', $item) . ' )';
|
||||
} else {
|
||||
// 对字段使用表达式查询
|
||||
$field = is_string($field) ? $field : '';
|
||||
$str[] = ' ' . $key . ' ' . $this->parseWhereItem($field, $value, $key);
|
||||
}
|
||||
}
|
||||
}
|
||||
$whereStr .= empty($whereStr) ? substr(implode(' ', $str), strlen($key) + 1) : implode(' ', $str);
|
||||
}
|
||||
return $whereStr;
|
||||
}
|
||||
|
||||
// where子单元分析
|
||||
protected function parseWhereItem($key, $val, $rule = '')
|
||||
{
|
||||
if ($key) {
|
||||
// 字段分析
|
||||
$key = $this->parseKey($key);
|
||||
}
|
||||
|
||||
// 查询规则和条件
|
||||
if (!is_array($val)) {
|
||||
$val = ['=', $val];
|
||||
}
|
||||
list($exp, $value) = $val;
|
||||
|
||||
// 对一个字段使用多个查询条件
|
||||
if (is_array($exp)) {
|
||||
foreach ($val as $item) {
|
||||
$str[] = $this->parseWhereItem($key, $item);
|
||||
}
|
||||
return '( ' . implode(' ' . $rule . ' ', $str) . ' )';
|
||||
}
|
||||
|
||||
// 检测操作符
|
||||
if (!in_array($exp, $this->exp)) {
|
||||
$exp = strtolower($exp);
|
||||
if (isset($this->exp[$exp])) {
|
||||
$exp = $this->exp[$exp];
|
||||
} else {
|
||||
throw new Exception('where express error:' . $exp);
|
||||
}
|
||||
}
|
||||
|
||||
$whereStr = '';
|
||||
if (in_array($exp, ['=', '<>', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE'])) {
|
||||
// 比较运算 及 模糊匹配
|
||||
$whereStr .= $key . ' ' . $exp . ' ' . $this->parseValue($value);
|
||||
} elseif ('EXP' == $exp) {
|
||||
// 表达式查询
|
||||
$whereStr .= $key . ' ' . $value;
|
||||
} elseif (in_array($exp, ['NOT NULL', 'NULL'])) {
|
||||
// NULL 查询
|
||||
$whereStr .= $key . ' IS ' . $exp;
|
||||
} elseif (in_array($exp, ['NOT IN', 'IN'])) {
|
||||
// IN 查询
|
||||
if ($value instanceof \Closure) {
|
||||
$whereStr .= $key . ' ' . $exp . ' ' . $this->parseClosure($value);
|
||||
} else {
|
||||
$value = is_string($value) ? explode(',', $value) : $value;
|
||||
$zone = implode(',', $this->parseValue($value));
|
||||
$whereStr .= $key . ' ' . $exp . ' (' . $zone . ')';
|
||||
}
|
||||
} elseif (in_array($exp, ['NOT BETWEEN', 'BETWEEN'])) {
|
||||
// BETWEEN 查询
|
||||
$data = is_string($value) ? explode(',', $value) : $value;
|
||||
$whereStr .= $key . ' ' . $exp . ' ' . $this->parseValue($data[0]) . ' AND ' . $this->parseValue($data[1]);
|
||||
} elseif (in_array($exp, ['NOT EXISTS', 'EXISTS'])) {
|
||||
// EXISTS 查询
|
||||
$whereStr .= $exp . ' ' . $this->parseClosure($value);
|
||||
}
|
||||
return $whereStr;
|
||||
}
|
||||
|
||||
// 执行闭包子查询
|
||||
protected function parseClosure($call, $show = true)
|
||||
{
|
||||
$class = clone $this->query;
|
||||
$class->options([]);
|
||||
call_user_func_array($call, [ & $class]);
|
||||
return $class->buildSql($show);
|
||||
}
|
||||
|
||||
/**
|
||||
* limit分析
|
||||
* @access protected
|
||||
* @param mixed $lmit
|
||||
* @return string
|
||||
*/
|
||||
protected function parseLimit($limit)
|
||||
{
|
||||
return (!empty($limit) && false === strpos($limit, '(')) ? ' LIMIT ' . $limit . ' ' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* join分析
|
||||
* @access protected
|
||||
* @param mixed $join
|
||||
* @return string
|
||||
*/
|
||||
protected function parseJoin($join)
|
||||
{
|
||||
$joinStr = '';
|
||||
if (!empty($join)) {
|
||||
$joinStr = ' ' . implode(' ', $join) . ' ';
|
||||
}
|
||||
return $joinStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* order分析
|
||||
* @access protected
|
||||
* @param mixed $order
|
||||
* @return string
|
||||
*/
|
||||
protected function parseOrder($order)
|
||||
{
|
||||
if (is_array($order)) {
|
||||
$array = [];
|
||||
foreach ($order as $key => $val) {
|
||||
if (is_numeric($key)) {
|
||||
if (false === strpos($val, '(')) {
|
||||
$array[] = $this->parseKey($val);
|
||||
} elseif ('[rand]' == $val) {
|
||||
$array[] = $this->parseRand();
|
||||
}
|
||||
} else {
|
||||
$sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : '';
|
||||
$array[] = $this->parseKey($key) . ' ' . $sort;
|
||||
}
|
||||
}
|
||||
$order = implode(',', $array);
|
||||
}
|
||||
return !empty($order) ? ' ORDER BY ' . $order : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* group分析
|
||||
* @access protected
|
||||
* @param mixed $group
|
||||
* @return string
|
||||
*/
|
||||
protected function parseGroup($group)
|
||||
{
|
||||
return !empty($group) ? ' GROUP BY ' . $group : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* having分析
|
||||
* @access protected
|
||||
* @param string $having
|
||||
* @return string
|
||||
*/
|
||||
protected function parseHaving($having)
|
||||
{
|
||||
return !empty($having) ? ' HAVING ' . $having : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* comment分析
|
||||
* @access protected
|
||||
* @param string $comment
|
||||
* @return string
|
||||
*/
|
||||
protected function parseComment($comment)
|
||||
{
|
||||
return !empty($comment) ? ' /* ' . $comment . ' */' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* distinct分析
|
||||
* @access protected
|
||||
* @param mixed $distinct
|
||||
* @return string
|
||||
*/
|
||||
protected function parseDistinct($distinct)
|
||||
{
|
||||
return !empty($distinct) ? ' DISTINCT ' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* union分析
|
||||
* @access protected
|
||||
* @param mixed $union
|
||||
* @return string
|
||||
*/
|
||||
protected function parseUnion($union)
|
||||
{
|
||||
if (empty($union)) {
|
||||
return '';
|
||||
}
|
||||
$type = $union['type'];
|
||||
unset($union['type']);
|
||||
foreach ($union as $u) {
|
||||
if ($u instanceof \Closure) {
|
||||
$sql[] = $type . ' ' . $this->parseClosure($u, false);
|
||||
} elseif (is_string($u)) {
|
||||
$sql[] = $type . ' ' . $this->parseSqlTable($u);
|
||||
}
|
||||
}
|
||||
return implode(' ', $sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* index分析,可在操作链中指定需要强制使用的索引
|
||||
* @access protected
|
||||
* @param mixed $index
|
||||
* @return string
|
||||
*/
|
||||
protected function parseForce($index)
|
||||
{
|
||||
if (empty($index)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (is_array($index)) {
|
||||
$index = join(",", $index);
|
||||
}
|
||||
|
||||
return sprintf(" FORCE INDEX ( %s ) ", $index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置锁机制
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseLock($lock = false)
|
||||
{
|
||||
return $lock ? ' FOR UPDATE ' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成查询SQL
|
||||
* @access public
|
||||
* @param array $options 表达式
|
||||
* @return string
|
||||
*/
|
||||
public function select($options = [])
|
||||
{
|
||||
if (isset($options['page'])) {
|
||||
// 根据页数计算limit
|
||||
list($page, $listRows) = $options['page'];
|
||||
$page = $page > 0 ? $page : 1;
|
||||
$listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20);
|
||||
$offset = $listRows * ($page - 1);
|
||||
$options['limit'] = $offset . ',' . $listRows;
|
||||
}
|
||||
$sql = $this->parseSql($this->selectSql, $options);
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换SQL语句中表达式
|
||||
* @access public
|
||||
* @param array $options 表达式
|
||||
* @return string
|
||||
*/
|
||||
public function parseSql($sql, $options = [])
|
||||
{
|
||||
$sql = str_replace(
|
||||
['%TABLE%', '%DISTINCT%', '%FIELD%', '%JOIN%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%', '%UNION%', '%LOCK%', '%COMMENT%', '%FORCE%'],
|
||||
[
|
||||
$this->parseTable($options['table']),
|
||||
$this->parseDistinct($options['distinct']),
|
||||
$this->parseField($options['field']),
|
||||
$this->parseJoin($options['join']),
|
||||
$this->parseWhere($options['where'], $options['table']),
|
||||
$this->parseGroup($options['group']),
|
||||
$this->parseHaving($options['having']),
|
||||
$this->parseOrder($options['order']),
|
||||
$this->parseLimit($options['limit']),
|
||||
$this->parseUnion($options['union']),
|
||||
$this->parseLock($options['lock']),
|
||||
$this->parseComment($options['comment']),
|
||||
$this->parseForce($options['force']),
|
||||
], $sql);
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function insert(array $data, $options = [], $replace = false)
|
||||
{
|
||||
// 分析并处理数据
|
||||
$data = $this->parseData($data, $options);
|
||||
if (empty($data)) {
|
||||
return 0;
|
||||
}
|
||||
$fields = array_keys($data);
|
||||
$values = array_values($data);
|
||||
|
||||
$sql = str_replace(
|
||||
['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
|
||||
[
|
||||
$replace ? 'REPLACE' : 'INSERT',
|
||||
$this->parseTable($options['table']),
|
||||
implode(' , ', $fields),
|
||||
implode(' , ', $values),
|
||||
$this->parseComment($options['comment']),
|
||||
], $this->insertSql);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function insertAll($dataSet, $options)
|
||||
{
|
||||
$fields = array_map([$this, 'parseKey'], array_keys($dataSet[0]));
|
||||
foreach ($dataSet as $data) {
|
||||
//$data = $this->parseData($data, $bind);
|
||||
$value = array_values($data);
|
||||
$values[] = 'SELECT ' . implode(',', $value);
|
||||
}
|
||||
|
||||
$sql = str_replace(
|
||||
['%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
|
||||
[
|
||||
$this->parseTable($options['table']),
|
||||
implode(' , ', $fields),
|
||||
implode(' UNION ALL ', $values),
|
||||
$this->parseComment($options['comment']),
|
||||
], $this->insertAllSql);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function selectInsert($fields, $table, $options)
|
||||
{
|
||||
if (is_string($fields)) {
|
||||
$fields = explode(',', $fields);
|
||||
}
|
||||
|
||||
$fields = array_map([$this, 'parseKey'], $fields);
|
||||
$sql = 'INSERT INTO ' . $this->parseTable($table) . ' (' . implode(',', $fields) . ') ';
|
||||
$sql .= $this->buildSelectSql($options);
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function update($data, $options)
|
||||
{
|
||||
|
||||
$table = $this->parseTable($options['table']);
|
||||
$data = $this->parseData($data, $options);
|
||||
if (empty($data)) {
|
||||
return '';
|
||||
}
|
||||
foreach ($data as $key => $val) {
|
||||
$set[] = $key . '=' . $val;
|
||||
}
|
||||
|
||||
$sql = str_replace(
|
||||
['%TABLE%', '%SET%', '%JOIN%', '%WHERE%', '%ORDER%', '%LIMIT%', '%LOCK%', '%COMMENT%'],
|
||||
[
|
||||
$this->parseTable($options['table']),
|
||||
implode(',', $set),
|
||||
$this->parseJoin($options['join']),
|
||||
$this->parseWhere($options['where'], $options['table']),
|
||||
$this->parseOrder($options['order']),
|
||||
$this->parseLimit($options['limit']),
|
||||
$this->parseLimit($options['lock']),
|
||||
$this->parseComment($options['comment']),
|
||||
], $this->updateSql);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function delete($options)
|
||||
{
|
||||
$sql = str_replace(
|
||||
['%TABLE%', '%USING%', '%JOIN%', '%WHERE%', '%ORDER%', '%LIMIT%', '%LOCK%', '%COMMENT%'],
|
||||
[
|
||||
$this->parseTable($options['table']),
|
||||
!empty($options['using']) ? ' USING ' . $this->parseTable($options['using']) . ' ' : '',
|
||||
$this->parseJoin($options['join']),
|
||||
$this->parseWhere($options['where'], $options['table']),
|
||||
$this->parseOrder($options['order']),
|
||||
$this->parseLimit($options['limit']),
|
||||
$this->parseLimit($options['lock']),
|
||||
$this->parseComment($options['comment']),
|
||||
], $this->deleteSql);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
1018
library/think/db/Connection.php
Normal file
1018
library/think/db/Connection.php
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
1016
library/think/db/Query.php
Normal file
1016
library/think/db/Query.php
Normal file
File diff suppressed because it is too large
Load Diff
52
library/think/db/builder/Mysql.php
Normal file
52
library/think/db/builder/Mysql.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\builder;
|
||||
|
||||
use think\db\Builder;
|
||||
|
||||
/**
|
||||
* mysql数据库驱动
|
||||
*/
|
||||
class Mysql extends Builder
|
||||
{
|
||||
|
||||
/**
|
||||
* 字段和表名处理
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key)
|
||||
{
|
||||
$key = trim($key);
|
||||
if (strpos($key, '$.') && false === strpos($key, '(')) {
|
||||
// JSON字段支持
|
||||
list($field, $name) = explode($key, '$.');
|
||||
$key = 'jsn_extract(' . $field . ', \'$.\'.' . $name . ')';
|
||||
}
|
||||
if (!preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {
|
||||
$key = '`' . $key . '`';
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'rand()';
|
||||
}
|
||||
|
||||
}
|
||||
87
library/think/db/builder/Oracle.php
Normal file
87
library/think/db/builder/Oracle.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\builder;
|
||||
|
||||
use think\Db;
|
||||
use think\db\Builder;
|
||||
|
||||
/**
|
||||
* Oracle数据库驱动
|
||||
*/
|
||||
class Oracle extends Builder
|
||||
{
|
||||
|
||||
protected $selectSql = 'SELECT * FROM (SELECT thinkphp.*, rownum AS numrow FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%) thinkphp ) %LIMIT%%COMMENT%';
|
||||
|
||||
/**
|
||||
* limit
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function parseLimit($limit)
|
||||
{
|
||||
$limitStr = '';
|
||||
if (!empty($limit)) {
|
||||
$limit = explode(',', $limit);
|
||||
if (count($limit) > 1) {
|
||||
$limitStr = "(numrow>" . $limit[0] . ") AND (numrow<=" . ($limit[0] + $limit[1]) . ")";
|
||||
} else {
|
||||
$limitStr = "(numrow>0 AND numrow<=" . $limit[0] . ")";
|
||||
}
|
||||
|
||||
}
|
||||
return $limitStr ? ' WHERE ' . $limitStr : '';
|
||||
}
|
||||
/**
|
||||
* 设置锁机制
|
||||
* @access protected
|
||||
* @param bool|false $lock
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function parseLock($lock = false)
|
||||
{
|
||||
if (!$lock) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return ' FOR UPDATE NOWAIT ';
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段和表名处理
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key)
|
||||
{
|
||||
$key = trim($key);
|
||||
if (strpos($key, '$.') && false === strpos($key, '(')) {
|
||||
// JSON字段支持
|
||||
list($field, $name) = explode($key, '$.');
|
||||
$key = $field . '."' . $name . '"';
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'DBMS_RANDOM.value';
|
||||
}
|
||||
|
||||
}
|
||||
69
library/think/db/builder/Pgsql.php
Normal file
69
library/think/db/builder/Pgsql.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\builder;
|
||||
|
||||
use think\db\Builder;
|
||||
|
||||
/**
|
||||
* Pgsql数据库驱动
|
||||
*/
|
||||
class Pgsql extends Builder
|
||||
{
|
||||
|
||||
/**
|
||||
* limit分析
|
||||
* @access protected
|
||||
* @param mixed $limit
|
||||
* @return string
|
||||
*/
|
||||
public function parseLimit($limit)
|
||||
{
|
||||
$limitStr = '';
|
||||
if (!empty($limit)) {
|
||||
$limit = explode(',', $limit);
|
||||
if (count($limit) > 1) {
|
||||
$limitStr .= ' LIMIT ' . $limit[1] . ' OFFSET ' . $limit[0] . ' ';
|
||||
} else {
|
||||
$limitStr .= ' LIMIT ' . $limit[0] . ' ';
|
||||
}
|
||||
}
|
||||
return $limitStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段和表名处理
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key)
|
||||
{
|
||||
$key = trim($key);
|
||||
if (strpos($key, '$.') && false === strpos($key, '(')) {
|
||||
// JSON字段支持
|
||||
list($field, $name) = explode($key, '$.');
|
||||
$key = $field . '->>\'' . $name . '\'';
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'RANDOM()';
|
||||
}
|
||||
|
||||
}
|
||||
51
library/think/db/builder/Sqlite.php
Normal file
51
library/think/db/builder/Sqlite.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\builder;
|
||||
|
||||
use think\db\Builder;
|
||||
|
||||
/**
|
||||
* Sqlite数据库驱动
|
||||
*/
|
||||
class Sqlite extends Builder
|
||||
{
|
||||
|
||||
/**
|
||||
* limit
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function parseLimit($limit)
|
||||
{
|
||||
$limitStr = '';
|
||||
if (!empty($limit)) {
|
||||
$limit = explode(',', $limit);
|
||||
if (count($limit) > 1) {
|
||||
$limitStr .= ' LIMIT ' . $limit[1] . ' OFFSET ' . $limit[0] . ' ';
|
||||
} else {
|
||||
$limitStr .= ' LIMIT ' . $limit[0] . ' ';
|
||||
}
|
||||
}
|
||||
return $limitStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'RANDOM()';
|
||||
}
|
||||
|
||||
}
|
||||
80
library/think/db/builder/Sqlsrv.php
Normal file
80
library/think/db/builder/Sqlsrv.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\builder;
|
||||
|
||||
use think\db\Builder;
|
||||
|
||||
/**
|
||||
* Sqlsrv数据库驱动
|
||||
*/
|
||||
class Sqlsrv extends Builder
|
||||
{
|
||||
protected $selectSql = 'SELECT T1.* FROM (SELECT thinkphp.*, ROW_NUMBER() OVER (%ORDER%) AS ROW_NUMBER FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%) AS thinkphp) AS T1 %LIMIT%%COMMENT%';
|
||||
|
||||
/**
|
||||
* order分析
|
||||
* @access protected
|
||||
* @param mixed $order
|
||||
* @return string
|
||||
*/
|
||||
protected function parseOrder($order)
|
||||
{
|
||||
return !empty($order) ? ' ORDER BY ' . $order[0] : ' ORDER BY rand()';
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'rand()';
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段名分析
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key)
|
||||
{
|
||||
$key = trim($key);
|
||||
if (!is_numeric($key) && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) {
|
||||
$key = '[' . $key . ']';
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* limit
|
||||
* @access public
|
||||
* @param mixed $limit
|
||||
* @return string
|
||||
*/
|
||||
public function parseLimit($limit)
|
||||
{
|
||||
if (empty($limit)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$limit = explode(',', $limit);
|
||||
if (count($limit) > 1) {
|
||||
$limitStr = '(T1.ROW_NUMBER BETWEEN ' . $limit[0] . ' + 1 AND ' . $limit[0] . ' + ' . $limit[1] . ')';
|
||||
} else {
|
||||
$limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND ' . $limit[0] . ")";
|
||||
}
|
||||
return 'WHERE ' . $limitStr;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,15 +9,15 @@
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\driver;
|
||||
namespace think\db\connector;
|
||||
|
||||
use think\db\Driver;
|
||||
use think\db\Connection;
|
||||
use think\Log;
|
||||
|
||||
/**
|
||||
* mysql数据库驱动
|
||||
*/
|
||||
class Mysql extends Driver
|
||||
class Mysql extends Connection
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -89,36 +89,6 @@ class Mysql extends Driver
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段和表名处理
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key)
|
||||
{
|
||||
$key = trim($key);
|
||||
if (strpos($key, '$.') && false === strpos($key, '(')) {
|
||||
// JSON字段支持
|
||||
list($field, $name) = explode($key, '$.');
|
||||
$key = 'jsn_extract(' . $field . ', \'$.\'.' . $name . ')';
|
||||
}
|
||||
if (!preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {
|
||||
$key = '`' . $key . '`';
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'rand()';
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL性能分析
|
||||
* @access protected
|
||||
@@ -9,20 +9,18 @@
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\driver;
|
||||
namespace think\db\connector;
|
||||
|
||||
use think\Config;
|
||||
use think\Db;
|
||||
use think\db\Driver;
|
||||
use think\db\Connection;
|
||||
|
||||
/**
|
||||
* Oracle数据库驱动
|
||||
*/
|
||||
class Oracle extends Driver
|
||||
class Oracle extends Connection
|
||||
{
|
||||
|
||||
private $table = '';
|
||||
protected $selectSql = 'SELECT * FROM (SELECT thinkphp.*, rownum AS numrow FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%) thinkphp ) %LIMIT%%COMMENT%';
|
||||
private $table = '';
|
||||
|
||||
/**
|
||||
* 解析pdo连接的dsn信息
|
||||
@@ -141,68 +139,6 @@ class Oracle extends Driver
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* limit
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function parseLimit($limit)
|
||||
{
|
||||
$limitStr = '';
|
||||
if (!empty($limit)) {
|
||||
$limit = explode(',', $limit);
|
||||
if (count($limit) > 1) {
|
||||
$limitStr = "(numrow>" . $limit[0] . ") AND (numrow<=" . ($limit[0] + $limit[1]) . ")";
|
||||
} else {
|
||||
$limitStr = "(numrow>0 AND numrow<=" . $limit[0] . ")";
|
||||
}
|
||||
|
||||
}
|
||||
return $limitStr ? ' WHERE ' . $limitStr : '';
|
||||
}
|
||||
/**
|
||||
* 设置锁机制
|
||||
* @access protected
|
||||
* @param bool|false $lock
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function parseLock($lock = false)
|
||||
{
|
||||
if (!$lock) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return ' FOR UPDATE NOWAIT ';
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段和表名处理
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key)
|
||||
{
|
||||
$key = trim($key);
|
||||
if (strpos($key, '$.') && false === strpos($key, '(')) {
|
||||
// JSON字段支持
|
||||
list($field, $name) = explode($key, '$.');
|
||||
$key = $field . '."' . $name . '"';
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'DBMS_RANDOM.value';
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL性能分析
|
||||
* @access protected
|
||||
@@ -9,14 +9,14 @@
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\driver;
|
||||
namespace think\db\connector;
|
||||
|
||||
use think\db\Driver;
|
||||
use think\db\Connection;
|
||||
|
||||
/**
|
||||
* Pgsql数据库驱动
|
||||
*/
|
||||
class Pgsql extends Driver
|
||||
class Pgsql extends Connection
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -74,53 +74,6 @@ class Pgsql extends Driver
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* limit分析
|
||||
* @access protected
|
||||
* @param mixed $limit
|
||||
* @return string
|
||||
*/
|
||||
public function parseLimit($limit)
|
||||
{
|
||||
$limitStr = '';
|
||||
if (!empty($limit)) {
|
||||
$limit = explode(',', $limit);
|
||||
if (count($limit) > 1) {
|
||||
$limitStr .= ' LIMIT ' . $limit[1] . ' OFFSET ' . $limit[0] . ' ';
|
||||
} else {
|
||||
$limitStr .= ' LIMIT ' . $limit[0] . ' ';
|
||||
}
|
||||
}
|
||||
return $limitStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段和表名处理
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key)
|
||||
{
|
||||
$key = trim($key);
|
||||
if (strpos($key, '$.') && false === strpos($key, '(')) {
|
||||
// JSON字段支持
|
||||
list($field, $name) = explode($key, '$.');
|
||||
$key = $field . '->>\'' . $name . '\'';
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'RANDOM()';
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL性能分析
|
||||
* @access protected
|
||||
@@ -9,14 +9,14 @@
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\driver;
|
||||
namespace think\db\connector;
|
||||
|
||||
use think\db\Driver;
|
||||
use think\db\Connection;
|
||||
|
||||
/**
|
||||
* Sqlite数据库驱动
|
||||
*/
|
||||
class Sqlite extends Driver
|
||||
class Sqlite extends Connection
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -73,35 +73,6 @@ class Sqlite extends Driver
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* limit
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function parseLimit($limit)
|
||||
{
|
||||
$limitStr = '';
|
||||
if (!empty($limit)) {
|
||||
$limit = explode(',', $limit);
|
||||
if (count($limit) > 1) {
|
||||
$limitStr .= ' LIMIT ' . $limit[1] . ' OFFSET ' . $limit[0] . ' ';
|
||||
} else {
|
||||
$limitStr .= ' LIMIT ' . $limit[0] . ' ';
|
||||
}
|
||||
}
|
||||
return $limitStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'RANDOM()';
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL性能分析
|
||||
* @access protected
|
||||
@@ -9,15 +9,15 @@
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\driver;
|
||||
namespace think\db\connector;
|
||||
|
||||
use PDO;
|
||||
use think\db\Driver;
|
||||
use think\db\Connection;
|
||||
|
||||
/**
|
||||
* Sqlsrv数据库驱动
|
||||
*/
|
||||
class Sqlsrv extends Driver
|
||||
class Sqlsrv extends Connection
|
||||
{
|
||||
protected $selectSql = 'SELECT T1.* FROM (SELECT thinkphp.*, ROW_NUMBER() OVER (%ORDER%) AS ROW_NUMBER FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%) AS thinkphp) AS T1 %LIMIT%%COMMENT%';
|
||||
// PDO连接参数
|
||||
@@ -92,101 +92,6 @@ class Sqlsrv extends Driver
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* order分析
|
||||
* @access protected
|
||||
* @param mixed $order
|
||||
* @return string
|
||||
*/
|
||||
protected function parseOrder($order)
|
||||
{
|
||||
return !empty($order) ? ' ORDER BY ' . $order[0] : ' ORDER BY rand()';
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'rand()';
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段名分析
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key)
|
||||
{
|
||||
$key = trim($key);
|
||||
if (!is_numeric($key) && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) {
|
||||
$key = '[' . $key . ']';
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* limit
|
||||
* @access public
|
||||
* @param mixed $limit
|
||||
* @return string
|
||||
*/
|
||||
public function parseLimit($limit)
|
||||
{
|
||||
if (empty($limit)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$limit = explode(',', $limit);
|
||||
if (count($limit) > 1) {
|
||||
$limitStr = '(T1.ROW_NUMBER BETWEEN ' . $limit[0] . ' + 1 AND ' . $limit[0] . ' + ' . $limit[1] . ')';
|
||||
} else {
|
||||
$limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND ' . $limit[0] . ")";
|
||||
}
|
||||
return 'WHERE ' . $limitStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新记录
|
||||
* @access public
|
||||
* @param mixed $data 数据
|
||||
* @param array $options 表达式
|
||||
* @return false | integer
|
||||
*/
|
||||
public function update($data, $options)
|
||||
{
|
||||
$this->model = $options['model'];
|
||||
$this->bind = array_merge($this->bind, !empty($options['bind']) ? $options['bind'] : []);
|
||||
$sql = 'UPDATE '
|
||||
. $this->parseTable($options['table'])
|
||||
. $this->parseSet($data)
|
||||
. $this->parseWhere(!empty($options['where']) ? $options['where'] : '')
|
||||
. $this->parseLock(isset($options['lock']) ? $options['lock'] : false)
|
||||
. $this->parseComment(!empty($options['comment']) ? $options['comment'] : '');
|
||||
return $this->execute($sql, $this->getBindParams(true), !empty($options['fetch_sql']) ? true : false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除记录
|
||||
* @access public
|
||||
* @param array $options 表达式
|
||||
* @return false | integer
|
||||
*/
|
||||
public function delete($options = [])
|
||||
{
|
||||
$this->model = $options['model'];
|
||||
$this->bind = array_merge($this->bind, !empty($options['bind']) ? $options['bind'] : []);
|
||||
$sql = 'DELETE FROM '
|
||||
. $this->parseTable($options['table'])
|
||||
. $this->parseWhere(!empty($options['where']) ? $options['where'] : '')
|
||||
. $this->parseLock(isset($options['lock']) ? $options['lock'] : false)
|
||||
. $this->parseComment(!empty($options['comment']) ? $options['comment'] : '');
|
||||
return $this->execute($sql, $this->getBindParams(true), !empty($options['fetch_sql']) ? true : false);
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL性能分析
|
||||
* @access protected
|
||||
@@ -1,21 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\model;
|
||||
|
||||
\think\Loader::import('model/Adv', TRAIT_PATH, EXT);
|
||||
\think\Loader::import('model/Transaction', TRAIT_PATH, EXT);
|
||||
|
||||
class Adv extends \think\Model
|
||||
{
|
||||
use \traits\model\Adv;
|
||||
use \traits\model\Transaction;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\model;
|
||||
|
||||
\think\Loader::import('model/Relation', TRAIT_PATH, EXT);
|
||||
|
||||
class Relation extends \think\Model
|
||||
{
|
||||
use \traits\model\Relation;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\model;
|
||||
|
||||
\think\Loader::import('model/View', TRAIT_PATH, EXT);
|
||||
|
||||
class View extends \think\Model
|
||||
{
|
||||
use \traits\model\View;
|
||||
}
|
||||
Reference in New Issue
Block a user