-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
MysqlExtended.php
251 lines (230 loc) · 8.34 KB
/
MysqlExtended.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
App::uses('Mysql', 'Model/Datasource/Database');
/**
* Overrides the default MySQL database implementation to support the following features:
* - Set query hints to optimize queries
*/
class MysqlExtended extends Mysql
{
const PDO_MAP = [
'integer' => PDO::PARAM_INT,
'float' => PDO::PARAM_STR,
'boolean' => PDO::PARAM_BOOL,
'string' => PDO::PARAM_STR,
'text' => PDO::PARAM_STR
];
/**
* Output MD5 as binary, that is faster and uses less memory
* @param string $value
* @return string
*/
public function cacheMethodHasher($value)
{
return md5($value, true);
}
/**
* Builds and generates an SQL statement from an array. Handles final clean-up before conversion.
*
* @param array $query An array defining an SQL query.
* @param Model $Model The model object which initiated the query.
* @return string An executable SQL statement.
* @see DboSource::renderStatement()
*/
public function buildStatement($query, Model $Model)
{
$query = array_merge($this->_queryDefaults, $query);
if (!empty($query['joins'])) {
$count = count($query['joins']);
for ($i = 0; $i < $count; $i++) {
if (is_array($query['joins'][$i])) {
$query['joins'][$i] = $this->buildJoinStatement($query['joins'][$i]);
}
}
}
return $this->renderStatement('select', array(
'conditions' => $this->conditions($query['conditions'], true, true, $Model),
'fields' => implode(', ', $query['fields']),
'table' => $query['table'],
'alias' => $this->alias . $this->name($query['alias']),
'order' => $this->order($query['order'], 'ASC', $Model),
'limit' => $this->limit($query['limit'], $query['offset']),
'joins' => implode(' ', $query['joins']),
'group' => $this->group($query['group'], $Model),
'having' => $this->having($query['having'], true, $Model),
'lock' => $this->getLockingHint($query['lock']),
'indexHint' => $this->__buildIndexHint($query['forceIndexHint'] ?? null),
'ignoreIndexHint' => $this->__buildIgnoreIndexHint($query['ignoreIndexHint'] ?? null)
));
}
/**
* Builds an SQL statement.
*
* This is merely a convenient wrapper to DboSource::buildStatement().
*
* @param Model $Model The model to build an association query for.
* @param array $queryData An array of queryData information containing keys similar to Model::find().
* @return string String containing an SQL statement.
* @see DboSource::buildStatement()
* @see DboSource::buildAssociationQuery()
*/
public function buildAssociationQuery(Model $Model, $queryData)
{
$queryData = $this->_scrubQueryData($queryData);
return $this->buildStatement(
array(
'fields' => $this->prepareFields($Model, $queryData),
'table' => $this->fullTableName($Model),
'alias' => $Model->alias,
'limit' => $queryData['limit'],
'offset' => $queryData['offset'],
'joins' => $queryData['joins'],
'conditions' => $queryData['conditions'],
'order' => $queryData['order'],
'group' => $queryData['group'],
'having' => $queryData['having'],
'lock' => $queryData['lock'],
'forceIndexHint' => $queryData['forceIndexHint'] ?? null,
'ignoreIndexHint' => $queryData['ignoreIndexHint'] ?? null,
),
$Model
);
}
/**
* Renders a final SQL statement by putting together the component parts in the correct order
*
* Edit: Added support for query hints
*
* @param string $type type of query being run. e.g select, create, update, delete, schema, alter.
* @param array $data Array of data to insert into the query.
* @return string|null Rendered SQL expression to be run, otherwise null.\
* @see DboSource::renderStatement()
*/
public function renderStatement($type, $data)
{
if ($type === 'select') {
extract($data);
$having = !empty($having) ? " $having" : '';
$lock = !empty($lock) ? " $lock" : '';
return rtrim("SELECT {$fields} FROM {$table} {$alias} {$indexHint} {$joins} {$conditions} {$group}{$having} {$order} {$limit}{$lock}");
}
return parent::renderStatement($type, $data);
}
/**
* Builds the force index hint for the query
*
* @param string|null $forceIndexHint INDEX hint
* @return string
*/
private function __buildIndexHint($forceIndexHint = null): ?string
{
return isset($forceIndexHint) ? ('FORCE INDEX (' . $forceIndexHint . ')') : null;
}
/**
* Builds the ignore index hint for the query
*
* @param string|null $ignoreIndexHint INDEX hint
* @return string
*/
private function __buildIgnoreIndexHint($ignoreIndexHint = null): ?string
{
return isset($ignoreIndexHint) ? ('IGNORE INDEX (' . $ignoreIndexHint . ')') : null;
}
/**
* - Do not call microtime when not necessary
* - Count query count even when logging is disabled
*
* @param string $sql
* @param array $options
* @param array $params
* @return mixed
*/
public function execute($sql, $options = [], $params = [])
{
$log = $options['log'] ?? $this->fullDebug;
if ($log) {
$t = microtime(true);
$this->_result = $this->_execute($sql, $params);
$this->took = round((microtime(true) - $t) * 1000);
$this->numRows = $this->affected = $this->lastAffected();
$this->logQuery($sql, $params);
} else {
$this->_result = $this->_execute($sql, $params);
$this->_queriesCnt++;
}
return $this->_result;
}
/**
* Reduce memory usage for insertMulti
*
* @param string $table
* @param array $fields
* @param array $values
* @return bool
*/
public function insertMulti($table, $fields, $values)
{
if (empty($values)) {
return true;
}
$table = $this->fullTableName($table);
$holder = substr(str_repeat('?,', count($fields)), 0, -1);
$fields = implode(',', array_map([$this, 'name'], $fields));
$columnMap = [];
foreach ($values[key($values)] as $key => $val) {
if (is_int($val)) {
$columnMap[$key] = PDO::PARAM_INT;
} elseif (is_bool($val)) {
$columnMap[$key] = PDO::PARAM_BOOL;
} else {
$type = $this->introspectType($val);
$columnMap[$key] = self::PDO_MAP[$type];
}
}
$sql = "INSERT INTO $table ($fields) VALUES ";
$sql .= substr(str_repeat("($holder),", count($values)), 0, -1);
$statement = $this->_connection->prepare($sql);
$valuesList = array();
$i = 0;
foreach ($values as $value) {
foreach ($value as $col => $val) {
if ($this->fullDebug) {
$valuesList[] = $val;
}
$statement->bindValue(++$i, $val, $columnMap[$col]);
}
}
$result = $statement->execute();
$statement->closeCursor();
if ($this->fullDebug) {
$this->logQuery($sql, $valuesList);
}
return $result;
}
/**
* {@inheritDoc}
*/
public function value($data, $column = null, $null = true)
{
// Fast check if data is int, then return value
if (is_int($data)) {
return $data;
}
// No need to quote bool values
if (is_bool($data)) {
return $data ? '1' : '0';
}
// No need to call expensive array_map
if (is_array($data) && !empty($data)) {
$output = [];
foreach ($data as $d) {
if (is_int($d)) {
$output[] = $d;
} else {
$output[] = parent::value($d, $column);
}
}
return $output;
}
return parent::value($data, $column, $null);
}
}