You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
864
thinkphp/library/think/db/Builder.php
Executable file
864
thinkphp/library/think/db/Builder.php
Executable file
@ -0,0 +1,864 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db;
|
||||
|
||||
use BadMethodCallException;
|
||||
use PDO;
|
||||
use think\Exception;
|
||||
|
||||
abstract class Builder
|
||||
{
|
||||
// connection对象实例
|
||||
protected $connection;
|
||||
// 查询对象实例
|
||||
protected $query;
|
||||
|
||||
// 数据库表达式
|
||||
protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'not like' => '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', '> time' => '> TIME', '< time' => '< TIME', '>= time' => '>= TIME', '<= time' => '<= TIME', 'between time' => 'BETWEEN TIME', 'not between time' => 'NOT BETWEEN TIME', 'notbetween time' => 'NOT BETWEEN TIME'];
|
||||
|
||||
// SQL表达式
|
||||
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 Connection $connection 数据库连接对象实例
|
||||
* @param Query $query 数据库查询对象实例
|
||||
*/
|
||||
public function __construct(Connection $connection, Query $query)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->query = $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的连接对象实例
|
||||
* @access public
|
||||
* @return Connection
|
||||
*/
|
||||
public function getConnection()
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的Query对象实例
|
||||
* @access public
|
||||
* @return Query
|
||||
*/
|
||||
public function getQuery()
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将SQL语句中的__TABLE_NAME__字符串替换成带前缀的表名(小写)
|
||||
* @access protected
|
||||
* @param string $sql sql语句
|
||||
* @return string
|
||||
*/
|
||||
protected function parseSqlTable($sql)
|
||||
{
|
||||
return $this->query->parseSqlTable($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据分析
|
||||
* @access protected
|
||||
* @param array $data 数据
|
||||
* @param array $options 查询参数
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function parseData($data, $options)
|
||||
{
|
||||
if (empty($data)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 获取绑定信息
|
||||
$bind = $this->query->getFieldsBind($options['table']);
|
||||
if ('*' == $options['field']) {
|
||||
$fields = array_keys($bind);
|
||||
} else {
|
||||
$fields = $options['field'];
|
||||
}
|
||||
|
||||
$result = [];
|
||||
foreach ($data as $key => $val) {
|
||||
$item = $this->parseKey($key, $options);
|
||||
if (is_object($val) && method_exists($val, '__toString')) {
|
||||
// 对象数据写入
|
||||
$val = $val->__toString();
|
||||
}
|
||||
if (false === strpos($key, '.') && !in_array($key, $fields, true)) {
|
||||
if ($options['strict']) {
|
||||
throw new Exception('fields not exists:[' . $key . ']');
|
||||
}
|
||||
} elseif (is_null($val)) {
|
||||
$result[$item] = 'NULL';
|
||||
} elseif (isset($val[0]) && 'exp' == $val[0]) {
|
||||
$result[$item] = $val[1];
|
||||
} elseif (is_scalar($val)) {
|
||||
// 过滤非标量数据
|
||||
if (0 === strpos($val, ':') && $this->query->isBind(substr($val, 1))) {
|
||||
$result[$item] = $val;
|
||||
} else {
|
||||
$key = str_replace('.', '_', $key);
|
||||
$this->query->bind('data__' . $key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
|
||||
$result[$item] = ':data__' . $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段名分析
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key, $options = [])
|
||||
{
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* value分析
|
||||
* @access protected
|
||||
* @param mixed $value
|
||||
* @param string $field
|
||||
* @return string|array
|
||||
*/
|
||||
protected function parseValue($value, $field = '')
|
||||
{
|
||||
if (is_string($value)) {
|
||||
$value = strpos($value, ':') === 0 && $this->query->isBind(substr($value, 1)) ? $value : $this->connection->quote($value);
|
||||
} 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
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function parseField($fields, $options = [])
|
||||
{
|
||||
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, $options) . ' AS ' . $this->parseKey($field, $options);
|
||||
} else {
|
||||
$array[] = $this->parseKey($field, $options);
|
||||
}
|
||||
}
|
||||
$fieldsStr = implode(',', $array);
|
||||
}
|
||||
return $fieldsStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* table分析
|
||||
* @access protected
|
||||
* @param mixed $tables
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function parseTable($tables, $options = [])
|
||||
{
|
||||
$item = [];
|
||||
foreach ((array) $tables as $key => $table) {
|
||||
if (!is_numeric($key)) {
|
||||
if (strpos($key, '@think')) {
|
||||
$key = strstr($key, '@think', true);
|
||||
}
|
||||
$key = $this->parseSqlTable($key);
|
||||
$item[] = $this->parseKey($key) . ' ' . (isset($options['alias'][$table]) ? $this->parseKey($options['alias'][$table]) : $this->parseKey($table));
|
||||
} else {
|
||||
$table = $this->parseSqlTable($table);
|
||||
if (isset($options['alias'][$table])) {
|
||||
$item[] = $this->parseKey($table) . ' ' . $this->parseKey($options['alias'][$table]);
|
||||
} else {
|
||||
$item[] = $this->parseKey($table);
|
||||
}
|
||||
}
|
||||
}
|
||||
return implode(',', $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* where分析
|
||||
* @access protected
|
||||
* @param mixed $where 查询条件
|
||||
* @param array $options 查询参数
|
||||
* @return string
|
||||
*/
|
||||
protected function parseWhere($where, $options)
|
||||
{
|
||||
$whereStr = $this->buildWhere($where, $options);
|
||||
if (!empty($options['soft_delete'])) {
|
||||
// 附加软删除条件
|
||||
list($field, $condition) = $options['soft_delete'];
|
||||
|
||||
$binds = $this->query->getFieldsBind($options['table']);
|
||||
$whereStr = $whereStr ? '( ' . $whereStr . ' ) AND ' : '';
|
||||
$whereStr = $whereStr . $this->parseWhereItem($field, $condition, '', $options, $binds);
|
||||
}
|
||||
return empty($whereStr) ? '' : ' WHERE ' . $whereStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成查询条件SQL
|
||||
* @access public
|
||||
* @param mixed $where
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
public function buildWhere($where, $options)
|
||||
{
|
||||
if (empty($where)) {
|
||||
$where = [];
|
||||
}
|
||||
|
||||
if ($where instanceof Query) {
|
||||
return $this->buildWhere($where->getOptions('where'), $options);
|
||||
}
|
||||
|
||||
$whereStr = '';
|
||||
$binds = $this->query->getFieldsBind($options['table']);
|
||||
foreach ($where as $key => $val) {
|
||||
$str = [];
|
||||
foreach ($val as $field => $value) {
|
||||
if ($value instanceof \Closure) {
|
||||
// 使用闭包查询
|
||||
$query = new Query($this->connection);
|
||||
call_user_func_array($value, [ & $query]);
|
||||
$whereClause = $this->buildWhere($query->getOptions('where'), $options);
|
||||
if (!empty($whereClause)) {
|
||||
$str[] = ' ' . $key . ' ( ' . $whereClause . ' )';
|
||||
}
|
||||
} elseif (strpos($field, '|')) {
|
||||
// 不同字段使用相同查询条件(OR)
|
||||
$array = explode('|', $field);
|
||||
$item = [];
|
||||
foreach ($array as $k) {
|
||||
$item[] = $this->parseWhereItem($k, $value, '', $options, $binds);
|
||||
}
|
||||
$str[] = ' ' . $key . ' ( ' . implode(' OR ', $item) . ' )';
|
||||
} elseif (strpos($field, '&')) {
|
||||
// 不同字段使用相同查询条件(AND)
|
||||
$array = explode('&', $field);
|
||||
$item = [];
|
||||
foreach ($array as $k) {
|
||||
$item[] = $this->parseWhereItem($k, $value, '', $options, $binds);
|
||||
}
|
||||
$str[] = ' ' . $key . ' ( ' . implode(' AND ', $item) . ' )';
|
||||
} else {
|
||||
// 对字段使用表达式查询
|
||||
$field = is_string($field) ? $field : '';
|
||||
$str[] = ' ' . $key . ' ' . $this->parseWhereItem($field, $value, $key, $options, $binds);
|
||||
}
|
||||
}
|
||||
|
||||
$whereStr .= empty($whereStr) ? substr(implode(' ', $str), strlen($key) + 1) : implode(' ', $str);
|
||||
}
|
||||
|
||||
return $whereStr;
|
||||
}
|
||||
|
||||
// where子单元分析
|
||||
protected function parseWhereItem($field, $val, $rule = '', $options = [], $binds = [], $bindName = null)
|
||||
{
|
||||
// 字段分析
|
||||
$key = $field ? $this->parseKey($field, $options) : '';
|
||||
|
||||
// 查询规则和条件
|
||||
if (!is_array($val)) {
|
||||
$val = is_null($val) ? ['null', ''] : ['=', $val];
|
||||
}
|
||||
list($exp, $value) = $val;
|
||||
|
||||
// 对一个字段使用多个查询条件
|
||||
if (is_array($exp)) {
|
||||
$item = array_pop($val);
|
||||
// 传入 or 或者 and
|
||||
if (is_string($item) && in_array($item, ['AND', 'and', 'OR', 'or'])) {
|
||||
$rule = $item;
|
||||
} else {
|
||||
array_push($val, $item);
|
||||
}
|
||||
foreach ($val as $k => $item) {
|
||||
$bindName = 'where_' . str_replace('.', '_', $field) . '_' . $k;
|
||||
$str[] = $this->parseWhereItem($field, $item, $rule, $options, $binds, $bindName);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
$bindName = $bindName ?: 'where_' . str_replace(['.', '-'], '_', $field);
|
||||
if (preg_match('/\W/', $bindName)) {
|
||||
// 处理带非单词字符的字段名
|
||||
$bindName = md5($bindName);
|
||||
}
|
||||
|
||||
if (is_object($value) && method_exists($value, '__toString')) {
|
||||
// 对象数据写入
|
||||
$value = $value->__toString();
|
||||
}
|
||||
|
||||
$bindType = isset($binds[$field]) ? $binds[$field] : PDO::PARAM_STR;
|
||||
if (is_scalar($value) && array_key_exists($field, $binds) && !in_array($exp, ['EXP', 'NOT NULL', 'NULL', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN']) && strpos($exp, 'TIME') === false) {
|
||||
if (strpos($value, ':') !== 0 || !$this->query->isBind(substr($value, 1))) {
|
||||
if ($this->query->isBind($bindName)) {
|
||||
$bindName .= '_' . str_replace('.', '_', uniqid('', true));
|
||||
}
|
||||
$this->query->bind($bindName, $value, $bindType);
|
||||
$value = ':' . $bindName;
|
||||
}
|
||||
}
|
||||
|
||||
$whereStr = '';
|
||||
if (in_array($exp, ['=', '<>', '>', '>=', '<', '<='])) {
|
||||
// 比较运算
|
||||
if ($value instanceof \Closure) {
|
||||
$whereStr .= $key . ' ' . $exp . ' ' . $this->parseClosure($value);
|
||||
} else {
|
||||
$whereStr .= $key . ' ' . $exp . ' ' . $this->parseValue($value, $field);
|
||||
}
|
||||
} elseif ('LIKE' == $exp || 'NOT LIKE' == $exp) {
|
||||
// 模糊匹配
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $item) {
|
||||
$array[] = $key . ' ' . $exp . ' ' . $this->parseValue($item, $field);
|
||||
}
|
||||
$logic = isset($val[2]) ? $val[2] : 'AND';
|
||||
$whereStr .= '(' . implode($array, ' ' . strtoupper($logic) . ' ') . ')';
|
||||
} else {
|
||||
$whereStr .= $key . ' ' . $exp . ' ' . $this->parseValue($value, $field);
|
||||
}
|
||||
} 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 = array_unique(is_array($value) ? $value : explode(',', $value));
|
||||
if (array_key_exists($field, $binds)) {
|
||||
$bind = [];
|
||||
$array = [];
|
||||
$i = 0;
|
||||
foreach ($value as $v) {
|
||||
$i++;
|
||||
if ($this->query->isBind($bindName . '_in_' . $i)) {
|
||||
$bindKey = $bindName . '_in_' . uniqid() . '_' . $i;
|
||||
} else {
|
||||
$bindKey = $bindName . '_in_' . $i;
|
||||
}
|
||||
$bind[$bindKey] = [$v, $bindType];
|
||||
$array[] = ':' . $bindKey;
|
||||
}
|
||||
$this->query->bind($bind);
|
||||
$zone = implode(',', $array);
|
||||
} else {
|
||||
$zone = implode(',', $this->parseValue($value, $field));
|
||||
}
|
||||
$whereStr .= $key . ' ' . $exp . ' (' . (empty($zone) ? "''" : $zone) . ')';
|
||||
}
|
||||
} elseif (in_array($exp, ['NOT BETWEEN', 'BETWEEN'])) {
|
||||
// BETWEEN 查询
|
||||
$data = is_array($value) ? $value : explode(',', $value);
|
||||
if (array_key_exists($field, $binds)) {
|
||||
if ($this->query->isBind($bindName . '_between_1')) {
|
||||
$bindKey1 = $bindName . '_between_1' . uniqid();
|
||||
$bindKey2 = $bindName . '_between_2' . uniqid();
|
||||
} else {
|
||||
$bindKey1 = $bindName . '_between_1';
|
||||
$bindKey2 = $bindName . '_between_2';
|
||||
}
|
||||
$bind = [
|
||||
$bindKey1 => [$data[0], $bindType],
|
||||
$bindKey2 => [$data[1], $bindType],
|
||||
];
|
||||
$this->query->bind($bind);
|
||||
$between = ':' . $bindKey1 . ' AND :' . $bindKey2;
|
||||
} else {
|
||||
$between = $this->parseValue($data[0], $field) . ' AND ' . $this->parseValue($data[1], $field);
|
||||
}
|
||||
$whereStr .= $key . ' ' . $exp . ' ' . $between;
|
||||
} elseif (in_array($exp, ['NOT EXISTS', 'EXISTS'])) {
|
||||
// EXISTS 查询
|
||||
if ($value instanceof \Closure) {
|
||||
$whereStr .= $exp . ' ' . $this->parseClosure($value);
|
||||
} else {
|
||||
$whereStr .= $exp . ' (' . $value . ')';
|
||||
}
|
||||
} elseif (in_array($exp, ['< TIME', '> TIME', '<= TIME', '>= TIME'])) {
|
||||
$whereStr .= $key . ' ' . substr($exp, 0, 2) . ' ' . $this->parseDateTime($value, $field, $options, $bindName, $bindType);
|
||||
} elseif (in_array($exp, ['BETWEEN TIME', 'NOT BETWEEN TIME'])) {
|
||||
if (is_string($value)) {
|
||||
$value = explode(',', $value);
|
||||
}
|
||||
|
||||
$whereStr .= $key . ' ' . substr($exp, 0, -4) . $this->parseDateTime($value[0], $field, $options, $bindName . '_between_1', $bindType) . ' AND ' . $this->parseDateTime($value[1], $field, $options, $bindName . '_between_2', $bindType);
|
||||
}
|
||||
return $whereStr;
|
||||
}
|
||||
|
||||
// 执行闭包子查询
|
||||
protected function parseClosure($call, $show = true)
|
||||
{
|
||||
$query = new Query($this->connection);
|
||||
call_user_func_array($call, [ & $query]);
|
||||
return $query->buildSql($show);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期时间条件解析
|
||||
* @access protected
|
||||
* @param string $value
|
||||
* @param string $key
|
||||
* @param array $options
|
||||
* @param string $bindName
|
||||
* @param integer $bindType
|
||||
* @return string
|
||||
*/
|
||||
protected function parseDateTime($value, $key, $options = [], $bindName = null, $bindType = null)
|
||||
{
|
||||
// 获取时间字段类型
|
||||
if (strpos($key, '.')) {
|
||||
list($table, $key) = explode('.', $key);
|
||||
if (isset($options['alias']) && $pos = array_search($table, $options['alias'])) {
|
||||
$table = $pos;
|
||||
}
|
||||
} else {
|
||||
$table = $options['table'];
|
||||
}
|
||||
$type = $this->query->getTableInfo($table, 'type');
|
||||
if (isset($type[$key])) {
|
||||
$info = $type[$key];
|
||||
}
|
||||
if (isset($info)) {
|
||||
if (is_string($value)) {
|
||||
$value = strtotime($value) ?: $value;
|
||||
}
|
||||
|
||||
if (preg_match('/(datetime|timestamp)/is', $info)) {
|
||||
// 日期及时间戳类型
|
||||
$value = date('Y-m-d H:i:s', $value);
|
||||
} elseif (preg_match('/(date)/is', $info)) {
|
||||
// 日期及时间戳类型
|
||||
$value = date('Y-m-d', $value);
|
||||
}
|
||||
}
|
||||
$bindName = $bindName ?: $key;
|
||||
$this->query->bind($bindName, $value, $bindType);
|
||||
return ':' . $bindName;
|
||||
}
|
||||
|
||||
/**
|
||||
* limit分析
|
||||
* @access protected
|
||||
* @param mixed $limit
|
||||
* @return string
|
||||
*/
|
||||
protected function parseLimit($limit)
|
||||
{
|
||||
return (!empty($limit) && false === strpos($limit, '(')) ? ' LIMIT ' . $limit . ' ' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* join分析
|
||||
* @access protected
|
||||
* @param array $join
|
||||
* @param array $options 查询条件
|
||||
* @return string
|
||||
*/
|
||||
protected function parseJoin($join, $options = [])
|
||||
{
|
||||
$joinStr = '';
|
||||
if (!empty($join)) {
|
||||
foreach ($join as $item) {
|
||||
list($table, $type, $on) = $item;
|
||||
$condition = [];
|
||||
foreach ((array) $on as $val) {
|
||||
if (strpos($val, '=')) {
|
||||
list($val1, $val2) = explode('=', $val, 2);
|
||||
$condition[] = $this->parseKey($val1, $options) . '=' . $this->parseKey($val2, $options);
|
||||
} else {
|
||||
$condition[] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
$table = $this->parseTable($table, $options);
|
||||
$joinStr .= ' ' . $type . ' JOIN ' . $table . ' ON ' . implode(' AND ', $condition);
|
||||
}
|
||||
}
|
||||
return $joinStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* order分析
|
||||
* @access protected
|
||||
* @param mixed $order
|
||||
* @param array $options 查询条件
|
||||
* @return string
|
||||
*/
|
||||
protected function parseOrder($order, $options = [])
|
||||
{
|
||||
if (is_array($order)) {
|
||||
$array = [];
|
||||
foreach ($order as $key => $val) {
|
||||
if (is_numeric($key)) {
|
||||
if ('[rand]' == $val) {
|
||||
if (method_exists($this, 'parseRand')) {
|
||||
$array[] = $this->parseRand();
|
||||
} else {
|
||||
throw new BadMethodCallException('method not exists:' . get_class($this) . '-> parseRand');
|
||||
}
|
||||
} elseif (false === strpos($val, '(')) {
|
||||
$array[] = $this->parseKey($val, $options);
|
||||
} else {
|
||||
$array[] = $val;
|
||||
}
|
||||
} else {
|
||||
$sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : '';
|
||||
$array[] = $this->parseKey($key, $options) . ' ' . $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 ' . $this->parseKey($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
|
||||
* @param bool|string $lock
|
||||
* @return string
|
||||
*/
|
||||
protected function parseLock($lock = false)
|
||||
{
|
||||
if (is_bool($lock)) {
|
||||
return $lock ? ' FOR UPDATE ' : '';
|
||||
} elseif (is_string($lock)) {
|
||||
return ' ' . trim($lock) . ' ';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成查询SQL
|
||||
* @access public
|
||||
* @param array $options 表达式
|
||||
* @return string
|
||||
*/
|
||||
public function select($options = [])
|
||||
{
|
||||
$sql = str_replace(
|
||||
['%TABLE%', '%DISTINCT%', '%FIELD%', '%JOIN%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%', '%UNION%', '%LOCK%', '%COMMENT%', '%FORCE%'],
|
||||
[
|
||||
$this->parseTable($options['table'], $options),
|
||||
$this->parseDistinct($options['distinct']),
|
||||
$this->parseField($options['field'], $options),
|
||||
$this->parseJoin($options['join'], $options),
|
||||
$this->parseWhere($options['where'], $options),
|
||||
$this->parseGroup($options['group']),
|
||||
$this->parseHaving($options['having']),
|
||||
$this->parseOrder($options['order'], $options),
|
||||
$this->parseLimit($options['limit']),
|
||||
$this->parseUnion($options['union']),
|
||||
$this->parseLock($options['lock']),
|
||||
$this->parseComment($options['comment']),
|
||||
$this->parseForce($options['force']),
|
||||
], $this->selectSql);
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成insert SQL
|
||||
* @access public
|
||||
* @param array $data 数据
|
||||
* @param array $options 表达式
|
||||
* @param bool $replace 是否replace
|
||||
* @return string
|
||||
*/
|
||||
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'], $options),
|
||||
implode(' , ', $fields),
|
||||
implode(' , ', $values),
|
||||
$this->parseComment($options['comment']),
|
||||
], $this->insertSql);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成insertall SQL
|
||||
* @access public
|
||||
* @param array $dataSet 数据集
|
||||
* @param array $options 表达式
|
||||
* @param bool $replace 是否replace
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function insertAll($dataSet, $options = [], $replace = false)
|
||||
{
|
||||
// 获取合法的字段
|
||||
if ('*' == $options['field']) {
|
||||
$fields = array_keys($this->query->getFieldsType($options['table']));
|
||||
} else {
|
||||
$fields = $options['field'];
|
||||
}
|
||||
|
||||
foreach ($dataSet as &$data) {
|
||||
foreach ($data as $key => $val) {
|
||||
if (!in_array($key, $fields, true)) {
|
||||
if ($options['strict']) {
|
||||
throw new Exception('fields not exists:[' . $key . ']');
|
||||
}
|
||||
unset($data[$key]);
|
||||
} elseif (is_null($val)) {
|
||||
$data[$key] = 'NULL';
|
||||
} elseif (is_scalar($val)) {
|
||||
$data[$key] = $this->parseValue($val, $key);
|
||||
} elseif (is_object($val) && method_exists($val, '__toString')) {
|
||||
// 对象数据写入
|
||||
$data[$key] = $val->__toString();
|
||||
} else {
|
||||
// 过滤掉非标量数据
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
$value = array_values($data);
|
||||
$values[] = 'SELECT ' . implode(',', $value);
|
||||
}
|
||||
$fields = array_map([$this, 'parseKey'], array_keys(reset($dataSet)));
|
||||
$sql = str_replace(
|
||||
['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
|
||||
[
|
||||
$replace ? 'REPLACE' : 'INSERT',
|
||||
$this->parseTable($options['table'], $options),
|
||||
implode(' , ', $fields),
|
||||
implode(' UNION ALL ', $values),
|
||||
$this->parseComment($options['comment']),
|
||||
], $this->insertAllSql);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成select insert SQL
|
||||
* @access public
|
||||
* @param array $fields 数据
|
||||
* @param string $table 数据表
|
||||
* @param array $options 表达式
|
||||
* @return string
|
||||
*/
|
||||
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, $options) . ' (' . implode(',', $fields) . ') ' . $this->select($options);
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成update SQL
|
||||
* @access public
|
||||
* @param array $data 数据
|
||||
* @param array $options 表达式
|
||||
* @return string
|
||||
*/
|
||||
public function update($data, $options)
|
||||
{
|
||||
$table = $this->parseTable($options['table'], $options);
|
||||
$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'], $options),
|
||||
implode(',', $set),
|
||||
$this->parseJoin($options['join'], $options),
|
||||
$this->parseWhere($options['where'], $options),
|
||||
$this->parseOrder($options['order'], $options),
|
||||
$this->parseLimit($options['limit']),
|
||||
$this->parseLock($options['lock']),
|
||||
$this->parseComment($options['comment']),
|
||||
], $this->updateSql);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成delete SQL
|
||||
* @access public
|
||||
* @param array $options 表达式
|
||||
* @return string
|
||||
*/
|
||||
public function delete($options)
|
||||
{
|
||||
$sql = str_replace(
|
||||
['%TABLE%', '%USING%', '%JOIN%', '%WHERE%', '%ORDER%', '%LIMIT%', '%LOCK%', '%COMMENT%'],
|
||||
[
|
||||
$this->parseTable($options['table'], $options),
|
||||
!empty($options['using']) ? ' USING ' . $this->parseTable($options['using'], $options) . ' ' : '',
|
||||
$this->parseJoin($options['join'], $options),
|
||||
$this->parseWhere($options['where'], $options),
|
||||
$this->parseOrder($options['order'], $options),
|
||||
$this->parseLimit($options['limit']),
|
||||
$this->parseLock($options['lock']),
|
||||
$this->parseComment($options['comment']),
|
||||
], $this->deleteSql);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
1038
thinkphp/library/think/db/Connection.php
Executable file
1038
thinkphp/library/think/db/Connection.php
Executable file
File diff suppressed because it is too large
Load Diff
2898
thinkphp/library/think/db/Query.php
Executable file
2898
thinkphp/library/think/db/Query.php
Executable file
File diff suppressed because it is too large
Load Diff
68
thinkphp/library/think/db/builder/Mysql.php
Executable file
68
thinkphp/library/think/db/builder/Mysql.php
Executable file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 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
|
||||
{
|
||||
protected $updateSql = 'UPDATE %TABLE% %JOIN% SET %SET% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%';
|
||||
|
||||
/**
|
||||
* 字段和表名处理
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key, $options = [])
|
||||
{
|
||||
$key = trim($key);
|
||||
if (strpos($key, '$.') && false === strpos($key, '(')) {
|
||||
// JSON字段支持
|
||||
list($field, $name) = explode('$.', $key);
|
||||
$key = 'json_extract(' . $field . ', \'$.' . $name . '\')';
|
||||
} elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) {
|
||||
list($table, $key) = explode('.', $key, 2);
|
||||
if ('__TABLE__' == $table) {
|
||||
$table = $this->query->getTable();
|
||||
}
|
||||
if (isset($options['alias'][$table])) {
|
||||
$table = $options['alias'][$table];
|
||||
}
|
||||
}
|
||||
if (!preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {
|
||||
$key = '`' . $key . '`';
|
||||
}
|
||||
if (isset($table)) {
|
||||
if (strpos($table, '.')) {
|
||||
$table = str_replace('.', '`.`', $table);
|
||||
}
|
||||
$key = '`' . $table . '`.' . $key;
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'rand()';
|
||||
}
|
||||
|
||||
}
|
83
thinkphp/library/think/db/builder/Pgsql.php
Executable file
83
thinkphp/library/think/db/builder/Pgsql.php
Executable file
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 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
|
||||
{
|
||||
protected $insertSql = 'INSERT INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%';
|
||||
protected $insertAllSql = 'INSERT INTO %TABLE% (%FIELD%) %DATA% %COMMENT%';
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key, $options = [])
|
||||
{
|
||||
$key = trim($key);
|
||||
if (strpos($key, '$.') && false === strpos($key, '(')) {
|
||||
// JSON字段支持
|
||||
list($field, $name) = explode('$.', $key);
|
||||
$key = $field . '->>\'' . $name . '\'';
|
||||
} elseif (strpos($key, '.')) {
|
||||
list($table, $key) = explode('.', $key, 2);
|
||||
if ('__TABLE__' == $table) {
|
||||
$table = $this->query->getTable();
|
||||
}
|
||||
if (isset($options['alias'][$table])) {
|
||||
$table = $options['alias'][$table];
|
||||
}
|
||||
}
|
||||
if (isset($table)) {
|
||||
$key = $table . '.' . $key;
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'RANDOM()';
|
||||
}
|
||||
|
||||
}
|
76
thinkphp/library/think/db/builder/Sqlite.php
Executable file
76
thinkphp/library/think/db/builder/Sqlite.php
Executable file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 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
|
||||
* @param string $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
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'RANDOM()';
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段和表名处理
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key, $options = [])
|
||||
{
|
||||
$key = trim($key);
|
||||
if (strpos($key, '.')) {
|
||||
list($table, $key) = explode('.', $key, 2);
|
||||
if ('__TABLE__' == $table) {
|
||||
$table = $this->query->getTable();
|
||||
}
|
||||
if (isset($options['alias'][$table])) {
|
||||
$table = $options['alias'][$table];
|
||||
}
|
||||
}
|
||||
if (isset($table)) {
|
||||
$key = $table . '.' . $key;
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
}
|
123
thinkphp/library/think/db/builder/Sqlsrv.php
Executable file
123
thinkphp/library/think/db/builder/Sqlsrv.php
Executable file
@ -0,0 +1,123 @@
|
||||
<?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%';
|
||||
protected $selectInsertSql = 'SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%';
|
||||
protected $updateSql = 'UPDATE %TABLE% SET %SET% FROM %TABLE% %JOIN% %WHERE% %LIMIT% %LOCK%%COMMENT%';
|
||||
protected $deleteSql = 'DELETE FROM %TABLE% %USING% FROM %TABLE% %JOIN% %WHERE% %LIMIT% %LOCK%%COMMENT%';
|
||||
protected $insertSql = 'INSERT INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%';
|
||||
protected $insertAllSql = 'INSERT INTO %TABLE% (%FIELD%) %DATA% %COMMENT%';
|
||||
|
||||
/**
|
||||
* order分析
|
||||
* @access protected
|
||||
* @param mixed $order
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function parseOrder($order, $options = [])
|
||||
{
|
||||
if (is_array($order)) {
|
||||
$array = [];
|
||||
foreach ($order as $key => $val) {
|
||||
if (is_numeric($key)) {
|
||||
if (false === strpos($val, '(')) {
|
||||
$array[] = $this->parseKey($val, $options);
|
||||
} elseif ('[rand]' == $val) {
|
||||
$array[] = $this->parseRand();
|
||||
} else {
|
||||
$array[] = $val;
|
||||
}
|
||||
} else {
|
||||
$sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : '';
|
||||
$array[] = $this->parseKey($key, $options) . ' ' . $sort;
|
||||
}
|
||||
}
|
||||
$order = implode(',', $array);
|
||||
}
|
||||
return !empty($order) ? ' ORDER BY ' . $order : ' ORDER BY rand()';
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机排序
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function parseRand()
|
||||
{
|
||||
return 'rand()';
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段和表名处理
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key, $options = [])
|
||||
{
|
||||
$key = trim($key);
|
||||
if (strpos($key, '.') && !preg_match('/[,\'\"\(\)\[\s]/', $key)) {
|
||||
list($table, $key) = explode('.', $key, 2);
|
||||
if ('__TABLE__' == $table) {
|
||||
$table = $this->query->getTable();
|
||||
}
|
||||
if (isset($options['alias'][$table])) {
|
||||
$table = $options['alias'][$table];
|
||||
}
|
||||
}
|
||||
if (!is_numeric($key) && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) {
|
||||
$key = '[' . $key . ']';
|
||||
}
|
||||
if (isset($table)) {
|
||||
$key = '[' . $table . '].' . $key;
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* limit
|
||||
* @access protected
|
||||
* @param mixed $limit
|
||||
* @return string
|
||||
*/
|
||||
protected 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;
|
||||
}
|
||||
|
||||
public function selectInsert($fields, $table, $options)
|
||||
{
|
||||
$this->selectSql = $this->selectInsertSql;
|
||||
return parent::selectInsert($fields, $table, $options);
|
||||
}
|
||||
|
||||
}
|
126
thinkphp/library/think/db/connector/Mysql.php
Executable file
126
thinkphp/library/think/db/connector/Mysql.php
Executable file
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\connector;
|
||||
|
||||
use PDO;
|
||||
use think\db\Connection;
|
||||
use think\Log;
|
||||
|
||||
/**
|
||||
* mysql数据库驱动
|
||||
*/
|
||||
class Mysql extends Connection
|
||||
{
|
||||
|
||||
protected $builder = '\\think\\db\\builder\\Mysql';
|
||||
|
||||
/**
|
||||
* 解析pdo连接的dsn信息
|
||||
* @access protected
|
||||
* @param array $config 连接信息
|
||||
* @return string
|
||||
*/
|
||||
protected function parseDsn($config)
|
||||
{
|
||||
if (!empty($config['socket'])) {
|
||||
$dsn = 'mysql:unix_socket=' . $config['socket'];
|
||||
} elseif (!empty($config['hostport'])) {
|
||||
$dsn = 'mysql:host=' . $config['hostname'] . ';port=' . $config['hostport'];
|
||||
} else {
|
||||
$dsn = 'mysql:host=' . $config['hostname'];
|
||||
}
|
||||
$dsn .= ';dbname=' . $config['database'];
|
||||
|
||||
if (!empty($config['charset'])) {
|
||||
$dsn .= ';charset=' . $config['charset'];
|
||||
}
|
||||
return $dsn;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得数据表的字段信息
|
||||
* @access public
|
||||
* @param string $tableName
|
||||
* @return array
|
||||
*/
|
||||
public function getFields($tableName)
|
||||
{
|
||||
list($tableName) = explode(' ', $tableName);
|
||||
if (false === strpos($tableName, '`')) {
|
||||
if (strpos($tableName, '.')) {
|
||||
$tableName = str_replace('.', '`.`', $tableName);
|
||||
}
|
||||
$tableName = '`' . $tableName . '`';
|
||||
}
|
||||
$sql = 'SHOW COLUMNS FROM ' . $tableName;
|
||||
$pdo = $this->query($sql, [], false, true);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
if ($result) {
|
||||
foreach ($result as $key => $val) {
|
||||
$val = array_change_key_case($val);
|
||||
$info[$val['field']] = [
|
||||
'name' => $val['field'],
|
||||
'type' => $val['type'],
|
||||
'notnull' => (bool) ('' === $val['null']), // not null is empty, null is yes
|
||||
'default' => $val['default'],
|
||||
'primary' => (strtolower($val['key']) == 'pri'),
|
||||
'autoinc' => (strtolower($val['extra']) == 'auto_increment'),
|
||||
];
|
||||
}
|
||||
}
|
||||
return $this->fieldCase($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得数据库的表信息
|
||||
* @access public
|
||||
* @param string $dbName
|
||||
* @return array
|
||||
*/
|
||||
public function getTables($dbName = '')
|
||||
{
|
||||
$sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
|
||||
$pdo = $this->query($sql, [], false, true);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
foreach ($result as $key => $val) {
|
||||
$info[$key] = current($val);
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL性能分析
|
||||
* @access protected
|
||||
* @param string $sql
|
||||
* @return array
|
||||
*/
|
||||
protected function getExplain($sql)
|
||||
{
|
||||
$pdo = $this->linkID->query("EXPLAIN " . $sql);
|
||||
$result = $pdo->fetch(PDO::FETCH_ASSOC);
|
||||
$result = array_change_key_case($result);
|
||||
if (isset($result['extra'])) {
|
||||
if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) {
|
||||
Log::record('SQL:' . $this->queryStr . '[' . $result['extra'] . ']', 'warn');
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function supportSavepoint()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
103
thinkphp/library/think/db/connector/Pgsql.php
Executable file
103
thinkphp/library/think/db/connector/Pgsql.php
Executable file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\connector;
|
||||
|
||||
use PDO;
|
||||
use think\db\Connection;
|
||||
|
||||
/**
|
||||
* Pgsql数据库驱动
|
||||
*/
|
||||
class Pgsql extends Connection
|
||||
{
|
||||
protected $builder = '\\think\\db\\builder\\Pgsql';
|
||||
|
||||
/**
|
||||
* 解析pdo连接的dsn信息
|
||||
* @access protected
|
||||
* @param array $config 连接信息
|
||||
* @return string
|
||||
*/
|
||||
protected function parseDsn($config)
|
||||
{
|
||||
$dsn = 'pgsql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
|
||||
if (!empty($config['hostport'])) {
|
||||
$dsn .= ';port=' . $config['hostport'];
|
||||
}
|
||||
return $dsn;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得数据表的字段信息
|
||||
* @access public
|
||||
* @param string $tableName
|
||||
* @return array
|
||||
*/
|
||||
public function getFields($tableName)
|
||||
{
|
||||
|
||||
list($tableName) = explode(' ', $tableName);
|
||||
$sql = 'select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\'' . $tableName . '\');';
|
||||
|
||||
$pdo = $this->query($sql, [], false, true);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
if ($result) {
|
||||
foreach ($result as $key => $val) {
|
||||
$val = array_change_key_case($val);
|
||||
$info[$val['field']] = [
|
||||
'name' => $val['field'],
|
||||
'type' => $val['type'],
|
||||
'notnull' => (bool) ('' !== $val['null']),
|
||||
'default' => $val['default'],
|
||||
'primary' => !empty($val['key']),
|
||||
'autoinc' => (0 === strpos($val['extra'], 'nextval(')),
|
||||
];
|
||||
}
|
||||
}
|
||||
return $this->fieldCase($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得数据库的表信息
|
||||
* @access public
|
||||
* @param string $dbName
|
||||
* @return array
|
||||
*/
|
||||
public function getTables($dbName = '')
|
||||
{
|
||||
$sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'";
|
||||
$pdo = $this->query($sql, [], false, true);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
foreach ($result as $key => $val) {
|
||||
$info[$key] = current($val);
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL性能分析
|
||||
* @access protected
|
||||
* @param string $sql
|
||||
* @return array
|
||||
*/
|
||||
protected function getExplain($sql)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function supportSavepoint()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
104
thinkphp/library/think/db/connector/Sqlite.php
Executable file
104
thinkphp/library/think/db/connector/Sqlite.php
Executable file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\connector;
|
||||
|
||||
use PDO;
|
||||
use think\db\Connection;
|
||||
|
||||
/**
|
||||
* Sqlite数据库驱动
|
||||
*/
|
||||
class Sqlite extends Connection
|
||||
{
|
||||
|
||||
protected $builder = '\\think\\db\\builder\\Sqlite';
|
||||
|
||||
/**
|
||||
* 解析pdo连接的dsn信息
|
||||
* @access protected
|
||||
* @param array $config 连接信息
|
||||
* @return string
|
||||
*/
|
||||
protected function parseDsn($config)
|
||||
{
|
||||
$dsn = 'sqlite:' . $config['database'];
|
||||
return $dsn;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得数据表的字段信息
|
||||
* @access public
|
||||
* @param string $tableName
|
||||
* @return array
|
||||
*/
|
||||
public function getFields($tableName)
|
||||
{
|
||||
list($tableName) = explode(' ', $tableName);
|
||||
$sql = 'PRAGMA table_info( ' . $tableName . ' )';
|
||||
|
||||
$pdo = $this->query($sql, [], false, true);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
if ($result) {
|
||||
foreach ($result as $key => $val) {
|
||||
$val = array_change_key_case($val);
|
||||
$info[$val['name']] = [
|
||||
'name' => $val['name'],
|
||||
'type' => $val['type'],
|
||||
'notnull' => 1 === $val['notnull'],
|
||||
'default' => $val['dflt_value'],
|
||||
'primary' => '1' == $val['pk'],
|
||||
'autoinc' => '1' == $val['pk'],
|
||||
];
|
||||
}
|
||||
}
|
||||
return $this->fieldCase($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得数据库的表信息
|
||||
* @access public
|
||||
* @param string $dbName
|
||||
* @return array
|
||||
*/
|
||||
public function getTables($dbName = '')
|
||||
{
|
||||
|
||||
$sql = "SELECT name FROM sqlite_master WHERE type='table' "
|
||||
. "UNION ALL SELECT name FROM sqlite_temp_master "
|
||||
. "WHERE type='table' ORDER BY name";
|
||||
|
||||
$pdo = $this->query($sql, [], false, true);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
foreach ($result as $key => $val) {
|
||||
$info[$key] = current($val);
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL性能分析
|
||||
* @access protected
|
||||
* @param string $sql
|
||||
* @return array
|
||||
*/
|
||||
protected function getExplain($sql)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function supportSavepoint()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
122
thinkphp/library/think/db/connector/Sqlsrv.php
Executable file
122
thinkphp/library/think/db/connector/Sqlsrv.php
Executable file
@ -0,0 +1,122 @@
|
||||
<?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\connector;
|
||||
|
||||
use PDO;
|
||||
use think\db\Connection;
|
||||
|
||||
/**
|
||||
* Sqlsrv数据库驱动
|
||||
*/
|
||||
class Sqlsrv extends Connection
|
||||
{
|
||||
// PDO连接参数
|
||||
protected $params = [
|
||||
PDO::ATTR_CASE => PDO::CASE_NATURAL,
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_STRINGIFY_FETCHES => false,
|
||||
];
|
||||
protected $builder = '\\think\\db\\builder\\Sqlsrv';
|
||||
/**
|
||||
* 解析pdo连接的dsn信息
|
||||
* @access protected
|
||||
* @param array $config 连接信息
|
||||
* @return string
|
||||
*/
|
||||
protected function parseDsn($config)
|
||||
{
|
||||
$dsn = 'sqlsrv:Database=' . $config['database'] . ';Server=' . $config['hostname'];
|
||||
if (!empty($config['hostport'])) {
|
||||
$dsn .= ',' . $config['hostport'];
|
||||
}
|
||||
return $dsn;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得数据表的字段信息
|
||||
* @access public
|
||||
* @param string $tableName
|
||||
* @return array
|
||||
*/
|
||||
public function getFields($tableName)
|
||||
{
|
||||
list($tableName) = explode(' ', $tableName);
|
||||
$sql = "SELECT column_name, data_type, column_default, is_nullable
|
||||
FROM information_schema.tables AS t
|
||||
JOIN information_schema.columns AS c
|
||||
ON t.table_catalog = c.table_catalog
|
||||
AND t.table_schema = c.table_schema
|
||||
AND t.table_name = c.table_name
|
||||
WHERE t.table_name = '$tableName'";
|
||||
|
||||
$pdo = $this->query($sql, [], false, true);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
if ($result) {
|
||||
foreach ($result as $key => $val) {
|
||||
$val = array_change_key_case($val);
|
||||
$info[$val['column_name']] = [
|
||||
'name' => $val['column_name'],
|
||||
'type' => $val['data_type'],
|
||||
'notnull' => (bool) ('' === $val['is_nullable']), // not null is empty, null is yes
|
||||
'default' => $val['column_default'],
|
||||
'primary' => false,
|
||||
'autoinc' => false,
|
||||
];
|
||||
}
|
||||
}
|
||||
$sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'";
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$pdo = $this->linkID->query($sql);
|
||||
// 调试结束
|
||||
$this->debug(false, $sql);
|
||||
$result = $pdo->fetch(PDO::FETCH_ASSOC);
|
||||
if ($result) {
|
||||
$info[$result['column_name']]['primary'] = true;
|
||||
}
|
||||
return $this->fieldCase($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得数据表的字段信息
|
||||
* @access public
|
||||
* @param string $dbName
|
||||
* @return array
|
||||
*/
|
||||
public function getTables($dbName = '')
|
||||
{
|
||||
$sql = "SELECT TABLE_NAME
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE TABLE_TYPE = 'BASE TABLE'
|
||||
";
|
||||
|
||||
$pdo = $this->query($sql, [], false, true);
|
||||
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
||||
$info = [];
|
||||
foreach ($result as $key => $val) {
|
||||
$info[$key] = current($val);
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL性能分析
|
||||
* @access protected
|
||||
* @param string $sql
|
||||
* @return array
|
||||
*/
|
||||
protected function getExplain($sql)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
BIN
thinkphp/library/think/db/connector/code.jpg
Executable file
BIN
thinkphp/library/think/db/connector/code.jpg
Executable file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
117
thinkphp/library/think/db/connector/pgsql.sql
Executable file
117
thinkphp/library/think/db/connector/pgsql.sql
Executable file
@ -0,0 +1,117 @@
|
||||
CREATE OR REPLACE FUNCTION pgsql_type(a_type varchar) RETURNS varchar AS
|
||||
$BODY$
|
||||
DECLARE
|
||||
v_type varchar;
|
||||
BEGIN
|
||||
IF a_type='int8' THEN
|
||||
v_type:='bigint';
|
||||
ELSIF a_type='int4' THEN
|
||||
v_type:='integer';
|
||||
ELSIF a_type='int2' THEN
|
||||
v_type:='smallint';
|
||||
ELSIF a_type='bpchar' THEN
|
||||
v_type:='char';
|
||||
ELSE
|
||||
v_type:=a_type;
|
||||
END IF;
|
||||
RETURN v_type;
|
||||
END;
|
||||
$BODY$
|
||||
LANGUAGE PLPGSQL;
|
||||
|
||||
CREATE TYPE "public"."tablestruct" AS (
|
||||
"fields_key_name" varchar(100),
|
||||
"fields_name" VARCHAR(200),
|
||||
"fields_type" VARCHAR(20),
|
||||
"fields_length" BIGINT,
|
||||
"fields_not_null" VARCHAR(10),
|
||||
"fields_default" VARCHAR(500),
|
||||
"fields_comment" VARCHAR(1000)
|
||||
);
|
||||
|
||||
CREATE OR REPLACE FUNCTION "public"."table_msg" (a_schema_name varchar, a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS
|
||||
$body$
|
||||
DECLARE
|
||||
v_ret tablestruct;
|
||||
v_oid oid;
|
||||
v_sql varchar;
|
||||
v_rec RECORD;
|
||||
v_key varchar;
|
||||
BEGIN
|
||||
SELECT
|
||||
pg_class.oid INTO v_oid
|
||||
FROM
|
||||
pg_class
|
||||
INNER JOIN pg_namespace ON (pg_class.relnamespace = pg_namespace.oid AND lower(pg_namespace.nspname) = a_schema_name)
|
||||
WHERE
|
||||
pg_class.relname=a_table_name;
|
||||
IF NOT FOUND THEN
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
v_sql='
|
||||
SELECT
|
||||
pg_attribute.attname AS fields_name,
|
||||
pg_attribute.attnum AS fields_index,
|
||||
pgsql_type(pg_type.typname::varchar) AS fields_type,
|
||||
pg_attribute.atttypmod-4 as fields_length,
|
||||
CASE WHEN pg_attribute.attnotnull THEN ''not null''
|
||||
ELSE ''''
|
||||
END AS fields_not_null,
|
||||
pg_attrdef.adsrc AS fields_default,
|
||||
pg_description.description AS fields_comment
|
||||
FROM
|
||||
pg_attribute
|
||||
INNER JOIN pg_class ON pg_attribute.attrelid = pg_class.oid
|
||||
INNER JOIN pg_type ON pg_attribute.atttypid = pg_type.oid
|
||||
LEFT OUTER JOIN pg_attrdef ON pg_attrdef.adrelid = pg_class.oid AND pg_attrdef.adnum = pg_attribute.attnum
|
||||
LEFT OUTER JOIN pg_description ON pg_description.objoid = pg_class.oid AND pg_description.objsubid = pg_attribute.attnum
|
||||
WHERE
|
||||
pg_attribute.attnum > 0
|
||||
AND attisdropped <> ''t''
|
||||
AND pg_class.oid = ' || v_oid || '
|
||||
ORDER BY pg_attribute.attnum' ;
|
||||
|
||||
FOR v_rec IN EXECUTE v_sql LOOP
|
||||
v_ret.fields_name=v_rec.fields_name;
|
||||
v_ret.fields_type=v_rec.fields_type;
|
||||
IF v_rec.fields_length > 0 THEN
|
||||
v_ret.fields_length:=v_rec.fields_length;
|
||||
ELSE
|
||||
v_ret.fields_length:=NULL;
|
||||
END IF;
|
||||
v_ret.fields_not_null=v_rec.fields_not_null;
|
||||
v_ret.fields_default=v_rec.fields_default;
|
||||
v_ret.fields_comment=v_rec.fields_comment;
|
||||
SELECT constraint_name INTO v_key FROM information_schema.key_column_usage WHERE table_schema=a_schema_name AND table_name=a_table_name AND column_name=v_rec.fields_name;
|
||||
IF FOUND THEN
|
||||
v_ret.fields_key_name=v_key;
|
||||
ELSE
|
||||
v_ret.fields_key_name='';
|
||||
END IF;
|
||||
RETURN NEXT v_ret;
|
||||
END LOOP;
|
||||
RETURN ;
|
||||
END;
|
||||
$body$
|
||||
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
|
||||
|
||||
COMMENT ON FUNCTION "public"."table_msg"(a_schema_name varchar, a_table_name varchar)
|
||||
IS '获得表信息';
|
||||
|
||||
---重载一个函数
|
||||
CREATE OR REPLACE FUNCTION "public"."table_msg" (a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS
|
||||
$body$
|
||||
DECLARE
|
||||
v_ret tablestruct;
|
||||
BEGIN
|
||||
FOR v_ret IN SELECT * FROM table_msg('public',a_table_name) LOOP
|
||||
RETURN NEXT v_ret;
|
||||
END LOOP;
|
||||
RETURN;
|
||||
END;
|
||||
$body$
|
||||
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
|
||||
|
||||
COMMENT ON FUNCTION "public"."table_msg"(a_table_name varchar)
|
||||
IS '获得表信息';
|
35
thinkphp/library/think/db/exception/BindParamException.php
Executable file
35
thinkphp/library/think/db/exception/BindParamException.php
Executable file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\exception;
|
||||
|
||||
use think\exception\DbException;
|
||||
|
||||
/**
|
||||
* PDO参数绑定异常
|
||||
*/
|
||||
class BindParamException extends DbException
|
||||
{
|
||||
|
||||
/**
|
||||
* BindParamException constructor.
|
||||
* @param string $message
|
||||
* @param array $config
|
||||
* @param string $sql
|
||||
* @param array $bind
|
||||
* @param int $code
|
||||
*/
|
||||
public function __construct($message, $config, $sql, $bind, $code = 10502)
|
||||
{
|
||||
$this->setData('Bind Param', $bind);
|
||||
parent::__construct($message, $config, $sql, $code);
|
||||
}
|
||||
}
|
43
thinkphp/library/think/db/exception/DataNotFoundException.php
Executable file
43
thinkphp/library/think/db/exception/DataNotFoundException.php
Executable file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\exception;
|
||||
|
||||
use think\exception\DbException;
|
||||
|
||||
class DataNotFoundException extends DbException
|
||||
{
|
||||
protected $table;
|
||||
|
||||
/**
|
||||
* DbException constructor.
|
||||
* @param string $message
|
||||
* @param string $table
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct($message, $table = '', array $config = [])
|
||||
{
|
||||
$this->message = $message;
|
||||
$this->table = $table;
|
||||
|
||||
$this->setData('Database Config', $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据表名
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getTable()
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
}
|
43
thinkphp/library/think/db/exception/ModelNotFoundException.php
Executable file
43
thinkphp/library/think/db/exception/ModelNotFoundException.php
Executable file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\exception;
|
||||
|
||||
use think\exception\DbException;
|
||||
|
||||
class ModelNotFoundException extends DbException
|
||||
{
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* @param string $message
|
||||
* @param string $model
|
||||
*/
|
||||
public function __construct($message, $model = '', array $config = [])
|
||||
{
|
||||
$this->message = $message;
|
||||
$this->model = $model;
|
||||
|
||||
$this->setData('Database Config', $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模型类名
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getModel()
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user