1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: liu21st <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
|
12
|
|
|
namespace think\db\builder; |
13
|
|
|
|
14
|
|
|
use think\db\Builder; |
15
|
|
|
use think\db\Expression; |
16
|
|
|
use think\db\Query; |
17
|
|
|
use think\Exception; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* mysql数据库驱动 |
|
|
|
|
21
|
|
|
*/ |
22
|
|
|
class Mysql extends Builder |
23
|
|
|
{ |
24
|
|
|
// 查询表达式解析 |
25
|
|
|
protected $parser = [ |
26
|
|
|
'parseCompare' => ['=', '<>', '>', '>=', '<', '<='], |
27
|
|
|
'parseLike' => ['LIKE', 'NOT LIKE'], |
28
|
|
|
'parseBetween' => ['NOT BETWEEN', 'BETWEEN'], |
29
|
|
|
'parseIn' => ['NOT IN', 'IN'], |
30
|
|
|
'parseExp' => ['EXP'], |
31
|
|
|
'parseRegexp' => ['REGEXP', 'NOT REGEXP'], |
32
|
|
|
'parseNull' => ['NOT NULL', 'NULL'], |
33
|
|
|
'parseBetweenTime' => ['BETWEEN TIME', 'NOT BETWEEN TIME'], |
34
|
|
|
'parseTime' => ['< TIME', '> TIME', '<= TIME', '>= TIME'], |
35
|
|
|
'parseExists' => ['NOT EXISTS', 'EXISTS'], |
36
|
|
|
'parseColumn' => ['COLUMN'], |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
protected $insertAllSql = '%INSERT% INTO %TABLE% (%FIELD%) VALUES %DATA% %COMMENT%'; |
40
|
|
|
protected $updateSql = 'UPDATE %TABLE% %JOIN% SET %SET% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* 生成insertall SQL |
44
|
|
|
* @access public |
45
|
|
|
* @param Query $query 查询对象 |
46
|
|
|
* @param array $dataSet 数据集 |
47
|
|
|
* @param bool $replace 是否replace |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function insertAll(Query $query, $dataSet, $replace = false) |
51
|
|
|
{ |
52
|
|
|
$options = $query->getOptions(); |
53
|
|
|
|
54
|
|
|
// 获取合法的字段 |
55
|
|
|
if ('*' == $options['field']) { |
56
|
|
|
$allowFields = $this->connection->getTableFields($options['table']); |
57
|
|
|
} else { |
58
|
|
|
$allowFields = $options['field']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// 获取绑定信息 |
62
|
|
|
$bind = $this->connection->getFieldsBind($options['table']); |
63
|
|
|
|
64
|
|
|
foreach ($dataSet as $k => $data) { |
65
|
|
|
$data = $this->parseData($query, $data, $allowFields, $bind); |
66
|
|
|
|
67
|
|
|
$values[] = '( ' . implode(',', array_values($data)) . ' )'; |
68
|
|
|
|
69
|
|
|
if (!isset($insertFields)) { |
70
|
|
|
$insertFields = array_keys($data); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$fields = []; |
75
|
|
|
foreach ($insertFields as $field) { |
|
|
|
|
76
|
|
|
$fields[] = $this->parseKey($query, $field); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return str_replace( |
80
|
|
|
['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'], |
81
|
|
|
[ |
82
|
|
|
$replace ? 'REPLACE' : 'INSERT', |
83
|
|
|
$this->parseTable($query, $options['table']), |
84
|
|
|
implode(' , ', $fields), |
85
|
|
|
implode(' , ', $values), |
|
|
|
|
86
|
|
|
$this->parseComment($query, $options['comment']), |
87
|
|
|
], |
88
|
|
|
$this->insertAllSql); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* 正则查询 |
93
|
|
|
* @access protected |
94
|
|
|
* @param Query $query 查询对象 |
|
|
|
|
95
|
|
|
* @param string $key |
|
|
|
|
96
|
|
|
* @param string $exp |
|
|
|
|
97
|
|
|
* @param mixed $value |
|
|
|
|
98
|
|
|
* @param string $field |
|
|
|
|
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function parseRegexp(Query $query, $key, $exp, $value, $field) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
if ($value instanceof Expression) { |
104
|
|
|
$value = $value->getValue(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $key . ' ' . $exp . ' ' . $value; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* 字段和表名处理 |
112
|
|
|
* @access public |
113
|
|
|
* @param Query $query 查询对象 |
|
|
|
|
114
|
|
|
* @param mixed $key 字段名 |
|
|
|
|
115
|
|
|
* @param bool $strict 严格检测 |
|
|
|
|
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function parseKey(Query $query, $key, $strict = false) |
119
|
|
|
{ |
120
|
|
|
if (is_numeric($key)) { |
121
|
|
|
return $key; |
122
|
|
|
} elseif ($key instanceof Expression) { |
123
|
|
|
return $key->getValue(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$key = trim($key); |
127
|
|
|
|
128
|
|
|
if(strpos($key, '->>') && false === strpos($key, '(')){ |
|
|
|
|
129
|
|
|
// JSON字段支持 |
130
|
|
|
list($field, $name) = explode('->>', $key, 2); |
131
|
|
|
|
132
|
|
|
return $this->parseKey($query, $field, true) . '->>\'$' . (strpos($name, '[') === 0 ? '' : '.') . str_replace('->>', '.', $name) . '\''; |
133
|
|
|
} |
134
|
|
|
elseif (strpos($key, '->') && false === strpos($key, '(')) { |
|
|
|
|
135
|
|
|
// JSON字段支持 |
136
|
|
|
list($field, $name) = explode('->', $key, 2); |
137
|
|
|
|
138
|
|
|
return 'json_extract(' . $this->parseKey($query, $field, true) . ', \'$' . (strpos($name, '[') === 0 ? '' : '.') . str_replace('->', '.', $name) . '\')'; |
139
|
|
|
} elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) { |
140
|
|
|
list($table, $key) = explode('.', $key, 2); |
141
|
|
|
|
142
|
|
|
$alias = $query->getOptions('alias'); |
143
|
|
|
|
144
|
|
|
if ('__TABLE__' == $table) { |
145
|
|
|
$table = $query->getOptions('table'); |
146
|
|
|
$table = is_array($table) ? array_shift($table) : $table; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (isset($alias[$table])) { |
150
|
|
|
$table = $alias[$table]; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) { |
155
|
|
|
throw new Exception('not support data:' . $key); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ('*' != $key && !preg_match('/[,\'\"\*\(\)`.\s]/', $key)) { |
159
|
|
|
$key = '`' . $key . '`'; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (isset($table)) { |
163
|
|
|
if (strpos($table, '.')) { |
164
|
|
|
$table = str_replace('.', '`.`', $table); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$key = '`' . $table . '`.' . $key; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $key; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* 随机排序 |
175
|
|
|
* @access protected |
176
|
|
|
* @param Query $query 查询对象 |
|
|
|
|
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
protected function parseRand(Query $query) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
return 'rand()'; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|