1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soupmix; |
4
|
|
|
/* |
5
|
|
|
SQL Adapter |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Doctrine\DBAL\DriverManager; |
9
|
|
|
use Doctrine\DBAL\Schema\Table; |
10
|
|
|
use Doctrine\DBAL\Schema\Column; |
11
|
|
|
use Doctrine\DBAL\Types\Type; |
12
|
|
|
use Doctrine\DBAL\Schema\Index; |
13
|
|
|
|
14
|
|
|
class SQL implements Base |
15
|
|
|
{ |
16
|
|
|
public $conn = null; |
17
|
|
|
private $defaults = [ |
18
|
|
|
'db_name' => 'default', |
19
|
|
|
'user_name' => '', |
20
|
|
|
'password' => '', |
21
|
|
|
'host' => '127.0.0.1', |
22
|
|
|
'port' => 3306, |
23
|
|
|
'charset' => 'utf8', |
24
|
|
|
'driver' => 'pdo_mysql', |
25
|
|
|
]; |
26
|
|
|
private static $columnDefaults = [ |
27
|
|
|
'name' => null, |
28
|
|
|
'type' => 'string', |
29
|
|
|
'type_info' => null, |
30
|
|
|
'maxLength' => 255, |
31
|
|
|
'default' => null, |
32
|
|
|
'index' => null, |
33
|
|
|
'index_type'=> null, |
34
|
|
|
] ; |
35
|
|
|
|
36
|
2 |
|
public function __construct($config) |
37
|
|
|
{ |
38
|
2 |
|
$config = array_merge($this->defaults, $config); |
39
|
2 |
|
$this->connect($config); |
40
|
2 |
|
} |
41
|
|
|
|
42
|
2 |
|
public function connect($config) |
43
|
|
|
{ |
44
|
|
|
$connectionParams = array( |
45
|
2 |
|
'dbname' => $config['db_name'], |
46
|
2 |
|
'user' => $config['user_name'], |
47
|
2 |
|
'password' => $config['password'], |
48
|
2 |
|
'host' => $config['host'], |
49
|
2 |
|
'port' => $config['port'], |
50
|
2 |
|
'charset' => $config['charset'], |
51
|
2 |
|
'driver' => $config['driver'], |
52
|
2 |
|
); |
53
|
2 |
|
$this->conn = DriverManager::getConnection($connectionParams); |
54
|
2 |
|
} |
55
|
|
|
|
56
|
2 |
|
public function create($collection, $fields) |
57
|
|
|
{ |
58
|
2 |
|
$columns = []; |
59
|
2 |
|
$indexes = []; |
60
|
2 |
|
$schemaManager = $this->conn->getSchemaManager(); |
61
|
2 |
|
$columns[] = new Column('id', Type::getType('integer'), ['unsigned' => true, 'autoincrement' => true] ); |
62
|
2 |
|
$indexes[] = new Index($collection.'_PK', ['id'], false, true); |
63
|
2 |
|
$tmpIndexes = []; |
64
|
2 |
|
foreach ($fields as $field){ |
65
|
2 |
|
$field = array_merge(self::$columnDefaults, $field); |
66
|
2 |
|
$options = []; |
67
|
2 |
|
if ($field['type'] == 'integer' && $field['type_info'] == 'unsigned') { |
68
|
|
|
$options['unsigned'] = true; |
69
|
|
|
} |
70
|
2 |
|
$options['length'] = $field['maxLength']; |
71
|
2 |
|
$options['default'] = $field['default']; |
72
|
2 |
View Code Duplication |
if ($field['index'] !== null) { |
|
|
|
|
73
|
2 |
|
if ( $field['index_type'] == 'unique' ) { |
74
|
2 |
|
$indexes[] = new Index($collection . '_' . $field['name'] . '_UNQ', [$field['name']], true, false); |
75
|
2 |
|
} else { |
76
|
2 |
|
$tmpIndexes[] = $field['name']; |
77
|
|
|
} |
78
|
2 |
|
} |
79
|
|
|
|
80
|
2 |
|
$columns[] = new Column($field['name'], Type::getType($field['type']), $options ); |
81
|
2 |
|
} |
82
|
2 |
View Code Duplication |
if(count($tmpIndexes)>0){ |
|
|
|
|
83
|
2 |
|
$indexes[] = new Index($collection . '_IDX', $tmpIndexes, false, false); |
84
|
2 |
|
} |
85
|
2 |
|
$table = new Table($collection, $columns, $indexes); |
86
|
2 |
|
return $schemaManager->createTable($table); |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
public function drop($collection) |
90
|
|
|
{ |
91
|
2 |
|
$schemaManager = $this->conn->getSchemaManager(); |
92
|
2 |
|
if ($schemaManager->tablesExist([$collection])) { |
93
|
1 |
|
return $schemaManager->dropTable($collection); |
94
|
|
|
} else { |
95
|
1 |
|
return null; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function truncate($collection) |
100
|
|
|
{ |
101
|
|
|
return $this->client->conn->query('TRUNCATE TABLE `' . $collection . '`'); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function createIndexes($collection, $indexes) |
105
|
|
|
{ |
106
|
|
|
$schemaManager = $this->conn->getSchemaManager(); |
107
|
|
|
|
108
|
|
|
$tmpIndexes = []; |
109
|
|
|
foreach ($indexes as $field){ |
110
|
|
|
$field = array_merge(self::$columnDefaults, $field); |
111
|
|
View Code Duplication |
if ($field['index'] !== null) { |
|
|
|
|
112
|
|
|
if ( $field['index_type'] == 'unique' ) { |
113
|
|
|
$indexes[] = new Index($collection . '_' . $field['name'] . '_UNQ', [$field['name']], true, false); |
114
|
|
|
} else { |
115
|
|
|
$tmpIndexes[] = $field['name']; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
View Code Duplication |
if (count($tmpIndexes) > 0) { |
|
|
|
|
120
|
|
|
$indexes[] = new Index($collection . '_IDX', $tmpIndexes, false, false); |
121
|
|
|
} |
122
|
|
|
foreach ($indexes as $index) { |
123
|
|
|
$schemaManager->createIndex($index, $collection); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
public function insert($collection, $values) |
130
|
|
|
{ |
131
|
2 |
|
$insertion = $this->conn->insert($collection, $values); |
132
|
2 |
|
if($insertion !== 0) { |
133
|
2 |
|
return $this->conn->lastInsertId(); |
134
|
|
|
} |
135
|
|
|
return null; |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
public function update($collection, $filter, $values) |
139
|
|
|
{ |
140
|
|
|
return $this->conn->update($collection, $values, $filter); |
141
|
1 |
|
} |
142
|
|
|
|
143
|
2 |
|
public function delete($collection, $filter) |
144
|
|
|
{ |
145
|
2 |
|
$numberOfDeletedItems = $this->conn->delete($collection, $filter); |
146
|
2 |
|
if ($numberOfDeletedItems>0) { |
147
|
2 |
|
return 1; |
148
|
|
|
} |
149
|
|
|
return 0; |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
public function get($collection, $docId) |
153
|
|
|
{ |
154
|
1 |
|
return $this->conn->fetchAssoc('SELECT * FROM '.$collection.' WHERE id = ?', array($docId)); |
155
|
|
|
} |
156
|
|
|
|
157
|
2 |
|
public function find($collection, $filters, $fields = null, $sort = null, $start = 0, $limit = 25, $debug = false) |
158
|
|
|
{ |
159
|
1 |
|
$result = null; |
160
|
1 |
|
$queryBuilder = $this->conn->createQueryBuilder(); |
161
|
2 |
|
$queryBuilder->from($collection); |
162
|
1 |
|
if ($filters !== null) { |
163
|
1 |
|
foreach ($filters as $key => $value) { |
164
|
1 |
|
if (strpos($key, '__') === false && is_array($value)) { |
165
|
1 |
|
foreach ($value as $orValue) { |
166
|
1 |
|
$subKey = array_keys($orValue)[0]; |
167
|
1 |
|
$subValue = $orValue[$subKey]; |
168
|
1 |
|
$sqlOptions = self::buildFilter([$subKey=>$subValue]); |
169
|
1 |
View Code Duplication |
if(in_array($sqlOptions['method'], ['in','notIn',''])){ |
|
|
|
|
170
|
|
|
$queryBuilder->orWhere( |
171
|
|
|
$queryBuilder->expr()->{$sqlOptions['method']}( $sqlOptions['key'], $sqlOptions['value']) |
172
|
1 |
|
); |
173
|
|
|
} |
174
|
1 |
|
else{ |
175
|
1 |
|
$queryBuilder->orWhere( |
176
|
1 |
|
$sqlOptions['key'] |
177
|
1 |
|
. ' ' . $sqlOptions['operand'] |
178
|
1 |
|
. ' ' . $queryBuilder->createNamedParameter($sqlOptions['value'])); |
179
|
|
|
} |
180
|
1 |
|
} |
181
|
1 |
|
} else { |
182
|
1 |
|
$sqlOptions = self::buildFilter([$key=>$value]); |
183
|
1 |
View Code Duplication |
if(in_array($sqlOptions['method'], ['in', 'notIn', ''])){ |
|
|
|
|
184
|
|
|
$queryBuilder->andWhere( |
185
|
|
|
$queryBuilder->expr()->{$sqlOptions['method']}( $sqlOptions['key'], $sqlOptions['value']) |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
else{ |
189
|
1 |
|
$queryBuilder->andWhere( |
190
|
1 |
|
$sqlOptions['key'] |
191
|
1 |
|
. ' ' . $sqlOptions['operand'] |
192
|
1 |
|
. ' ' . $queryBuilder->createNamedParameter($sqlOptions['value'])); |
193
|
|
|
} |
194
|
|
|
} |
195
|
1 |
|
} |
196
|
1 |
|
} |
197
|
1 |
|
if ($sort !== null) { |
198
|
|
|
$params['sort'] = ''; |
|
|
|
|
199
|
|
|
foreach ($sort as $sort_key => $sort_dir) { |
200
|
|
|
if ($params['sort']!='') { |
201
|
|
|
$params['sort'] .= ','; |
202
|
|
|
} |
203
|
|
|
$queryBuilder->addOrderBy($sort_key, $sort_dir); |
204
|
|
|
} |
205
|
|
|
} |
206
|
1 |
|
$queryBuilderCount = clone $queryBuilder; |
207
|
1 |
|
$queryBuilderCount->select(" COUNT(*) AS total "); |
208
|
1 |
|
$stmt = $this->conn->executeQuery($queryBuilderCount->getSql(), $queryBuilderCount->getParameters()); |
209
|
1 |
|
$count = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
210
|
1 |
|
$numberOfSet = 0; |
211
|
1 |
|
if (isset($count[0]['total']) && ($count[0]['total']>0)) { |
212
|
1 |
|
$numberOfSet = $count[0]['total']; |
213
|
1 |
|
$fields = ($fields === null) ? "*" : $fields; |
214
|
1 |
|
$queryBuilder->select($fields) |
215
|
1 |
|
->setFirstResult($start) |
216
|
1 |
|
->setMaxResults($limit); |
217
|
1 |
|
$stmt = $this->conn->executeQuery($queryBuilder->getSql(), $queryBuilder->getParameters()); |
218
|
1 |
|
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
219
|
1 |
|
} |
220
|
1 |
|
return ['total' => $numberOfSet, 'data' => $result]; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function query($query) |
224
|
|
|
{ |
225
|
|
|
// reserved |
226
|
|
|
} |
227
|
|
|
|
228
|
1 |
|
public static function buildFilter($filter) |
229
|
|
|
{ |
230
|
1 |
|
$key = array_keys($filter)[0]; |
231
|
1 |
|
$value = $filter[$key]; |
232
|
1 |
|
$operator = ' = '; |
233
|
1 |
|
$method = 'eq'; |
234
|
|
|
|
235
|
|
|
$methods = [ |
236
|
1 |
|
'gte' => 'gte', |
237
|
1 |
|
'gt' => 'gt', |
238
|
1 |
|
'lte' => 'lte', |
239
|
1 |
|
'lt' => 'lt', |
240
|
1 |
|
'in' => 'in', |
241
|
1 |
|
'!in' => 'notIn', |
242
|
1 |
|
'not' => 'not', |
243
|
1 |
|
'wildchard' => 'like', |
244
|
1 |
|
'prefix' => 'like', |
245
|
1 |
|
]; |
246
|
|
|
$operands = [ |
247
|
1 |
|
'gte' => ' >= ', |
248
|
1 |
|
'gt' => ' > ', |
249
|
1 |
|
'lte' => ' <= ', |
250
|
1 |
|
'lt' => ' < ', |
251
|
1 |
|
'in' => ' IN ', |
252
|
1 |
|
'!in' => ' NOT IN', |
253
|
1 |
|
'not' => ' NOT', |
254
|
1 |
|
'wildchard' => ' LIKE ', |
255
|
1 |
|
'prefix' => ' LIKE ', |
256
|
1 |
|
]; |
257
|
|
|
|
258
|
1 |
|
if (strpos($key, '__')!==false) { |
259
|
1 |
|
preg_match('/__(.*?)$/i', $key, $matches); |
260
|
1 |
|
$key = str_replace($matches[0], '', $key); |
261
|
1 |
|
$operator = $matches[1]; |
262
|
1 |
|
$method = $methods[$operator]; |
263
|
1 |
|
$operator = $operands[$operator]; |
264
|
|
|
switch ($operator) { |
265
|
1 |
|
case 'wildcard': |
266
|
|
|
$value = str_replace(array('?', '*'), array('_', '%'), $value); |
267
|
|
|
break; |
268
|
1 |
|
case 'prefix': |
269
|
|
|
$value = $value.'%'; |
270
|
|
|
break; |
271
|
|
|
} |
272
|
1 |
|
} |
273
|
|
|
return [ |
274
|
1 |
|
'key' => $key, |
275
|
1 |
|
'operand' => $operator, |
276
|
1 |
|
'method' => $method, |
277
|
|
|
'value' => $value |
278
|
1 |
|
]; |
279
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.