1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soupmix; |
4
|
|
|
/* |
5
|
|
|
SQL Adapter |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Doctrine\DBAL\DriverManager; |
9
|
|
|
|
10
|
|
|
class SQL implements Base |
11
|
|
|
{ |
12
|
|
|
public $conn = null; |
13
|
|
|
private $defaults = [ |
14
|
|
|
'db_name' => 'default', |
15
|
|
|
'user_name' => '', |
16
|
|
|
'password' => '', |
17
|
|
|
'host' => '127.0.0.1', |
18
|
|
|
'port' => 3306, |
19
|
|
|
'charset' => 'utf8', |
20
|
|
|
'driver' => 'pdo_mysql', |
21
|
|
|
]; |
22
|
|
|
|
23
|
2 |
|
public function __construct($config) |
24
|
|
|
{ |
25
|
2 |
|
$config = array_merge($this->defaults, $config); |
26
|
2 |
|
$this->connect($config); |
27
|
2 |
|
} |
28
|
|
|
|
29
|
2 |
|
public function connect($config) |
30
|
|
|
{ |
31
|
|
|
$connectionParams = array( |
32
|
2 |
|
'dbname' => $config['db_name'], |
33
|
2 |
|
'user' => $config['user_name'], |
34
|
2 |
|
'password' => $config['password'], |
35
|
2 |
|
'host' => $config['host'], |
36
|
2 |
|
'port' => $config['port'], |
37
|
2 |
|
'charset' => $config['charset'], |
38
|
2 |
|
'driver' => $config['driver'], |
39
|
2 |
|
); |
40
|
2 |
|
$this->conn = DriverManager::getConnection($connectionParams); |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
public function create($collection) |
44
|
|
|
{ |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function drop($collection) |
48
|
|
|
{ |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function truncate($collection) |
53
|
|
|
{ |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function createIndexes($collection, $indexes) |
57
|
|
|
{ |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
public function insert($collection, $values) |
61
|
|
|
{ |
62
|
2 |
|
$insertion = $this->conn->insert($collection, $values); |
63
|
2 |
|
if($insertion !== 0) { |
64
|
2 |
|
return $this->conn->lastInsertId(); |
65
|
|
|
} |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function update($collection, $filter, $values) |
70
|
|
|
{ |
71
|
|
|
return $this->conn->update($collection, $values, $filter); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public function delete($collection, $filter) |
75
|
|
|
{ |
76
|
2 |
|
$numberOfDeletedItems = $this->conn->delete($collection, $filter); |
77
|
2 |
|
if ($numberOfDeletedItems>0) { |
78
|
2 |
|
return 1; |
79
|
|
|
} |
80
|
|
|
return 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function get($collection, $docId) |
84
|
|
|
{ |
85
|
1 |
|
return $this->conn->fetchAssoc('SELECT * FROM '.$collection.' WHERE id = ?', array($docId)); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
public function find($collection, $filters, $fields = null, $sort = null, $start = 0, $limit = 25, $debug = false) |
89
|
|
|
{ |
90
|
1 |
|
$result = null; |
91
|
1 |
|
$queryBuilder = $this->conn->createQueryBuilder(); |
92
|
1 |
|
$queryBuilder->from($collection); |
93
|
1 |
|
if ($filters !== null) { |
94
|
1 |
|
foreach ($filters as $key => $value) { |
95
|
1 |
|
if (strpos($key, '__') === false && is_array($value)) { |
96
|
1 |
|
foreach ($value as $orValue) { |
97
|
1 |
|
$subKey = array_keys($orValue)[0]; |
98
|
1 |
|
$subValue = $orValue[$subKey]; |
99
|
1 |
|
$sqlOptions = self::buildFilter([$subKey=>$subValue]); |
100
|
1 |
View Code Duplication |
if(in_array($sqlOptions['method'], ['in','notIn',''])){ |
|
|
|
|
101
|
|
|
$queryBuilder->orWhere( |
102
|
|
|
$queryBuilder->expr()->{$sqlOptions['method']}( $sqlOptions['key'], $sqlOptions['value']) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
else{ |
106
|
1 |
|
$queryBuilder->orWhere( |
107
|
1 |
|
$sqlOptions['key'] |
108
|
1 |
|
. ' ' . $sqlOptions['operand'] |
109
|
1 |
|
. ' ' . $queryBuilder->createNamedParameter($sqlOptions['value'])); |
110
|
|
|
} |
111
|
1 |
|
} |
112
|
1 |
|
} else { |
113
|
1 |
|
$sqlOptions = self::buildFilter([$key=>$value]); |
114
|
1 |
View Code Duplication |
if(in_array($sqlOptions['method'], ['in', 'notIn', ''])){ |
|
|
|
|
115
|
|
|
$queryBuilder->andWhere( |
116
|
|
|
$queryBuilder->expr()->{$sqlOptions['method']}( $sqlOptions['key'], $sqlOptions['value']) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
else{ |
120
|
1 |
|
$queryBuilder->andWhere( |
121
|
1 |
|
$sqlOptions['key'] |
122
|
1 |
|
. ' ' . $sqlOptions['operand'] |
123
|
1 |
|
. ' ' . $queryBuilder->createNamedParameter($sqlOptions['value'])); |
124
|
|
|
} |
125
|
|
|
} |
126
|
1 |
|
} |
127
|
1 |
|
} |
128
|
1 |
|
if ($sort !== null) { |
129
|
|
|
$params['sort'] = ''; |
|
|
|
|
130
|
|
|
foreach ($sort as $sort_key => $sort_dir) { |
131
|
|
|
if ($params['sort']!='') { |
132
|
|
|
$params['sort'] .= ','; |
133
|
|
|
} |
134
|
|
|
$queryBuilder->addOrderBy($sort_key, $sort_dir); |
135
|
|
|
} |
136
|
|
|
} |
137
|
1 |
|
$queryBuilderCount = clone $queryBuilder; |
138
|
2 |
|
$queryBuilderCount->select(" COUNT(*) AS total "); |
139
|
1 |
|
$stmt = $this->conn->executeQuery($queryBuilderCount->getSql(), $queryBuilderCount->getParameters()); |
140
|
1 |
|
$count = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
141
|
2 |
|
$numberOfSet = 0; |
142
|
1 |
|
if (isset($count[0]['total']) && ($count[0]['total']>0)) { |
143
|
1 |
|
$numberOfSet = $count[0]['total']; |
144
|
1 |
|
$fields = ($fields === null) ? "*" : $fields; |
145
|
1 |
|
$queryBuilder->select($fields) |
146
|
1 |
|
->setFirstResult($start) |
147
|
1 |
|
->setMaxResults($limit); |
148
|
1 |
|
$stmt = $this->conn->executeQuery($queryBuilder->getSql(), $queryBuilder->getParameters()); |
149
|
1 |
|
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
150
|
1 |
|
} |
151
|
1 |
|
return ['total' => $numberOfSet, 'data' => $result]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function query($query) |
155
|
|
|
{ |
156
|
|
|
// reserved |
157
|
|
|
} |
158
|
|
|
|
159
|
2 |
|
public static function buildFilter($filter) |
160
|
|
|
{ |
161
|
2 |
|
$key = array_keys($filter)[0]; |
162
|
1 |
|
$value = $filter[$key]; |
163
|
1 |
|
$operator = ' = '; |
164
|
1 |
|
$method = 'eq'; |
165
|
|
|
|
166
|
|
|
$methods = [ |
167
|
1 |
|
'gte' => 'gte', |
168
|
1 |
|
'gt' => 'gt', |
169
|
1 |
|
'lte' => 'lte', |
170
|
1 |
|
'lt' => 'lt', |
171
|
1 |
|
'in' => 'in', |
172
|
2 |
|
'!in' => 'notIn', |
173
|
1 |
|
'not' => 'not', |
174
|
1 |
|
'wildchard' => 'like', |
175
|
1 |
|
'prefix' => 'like', |
176
|
1 |
|
]; |
177
|
|
|
$operands = [ |
178
|
1 |
|
'gte' => ' >= ', |
179
|
1 |
|
'gt' => ' > ', |
180
|
1 |
|
'lte' => ' <= ', |
181
|
1 |
|
'lt' => ' < ', |
182
|
1 |
|
'in' => ' IN ', |
183
|
1 |
|
'!in' => ' NOT IN', |
184
|
1 |
|
'not' => ' NOT', |
185
|
1 |
|
'wildchard' => ' LIKE ', |
186
|
1 |
|
'prefix' => ' LIKE ', |
187
|
1 |
|
]; |
188
|
|
|
|
189
|
1 |
|
if (strpos($key, '__')!==false) { |
190
|
1 |
|
preg_match('/__(.*?)$/i', $key, $matches); |
191
|
1 |
|
$key = str_replace($matches[0], '', $key); |
192
|
1 |
|
$operator = $matches[1]; |
193
|
1 |
|
$method = $methods[$operator]; |
194
|
1 |
|
$operator = $operands[$operator]; |
195
|
|
|
switch ($operator) { |
196
|
1 |
|
case 'wildcard': |
197
|
|
|
$value = str_replace(array('?', '*'), array('_', '%'), $value); |
198
|
|
|
break; |
199
|
1 |
|
case 'prefix': |
200
|
|
|
$value = $value.'%'; |
201
|
|
|
break; |
202
|
|
|
} |
203
|
1 |
|
} |
204
|
|
|
return [ |
205
|
1 |
|
'key' => $key, |
206
|
1 |
|
'operand' => $operator, |
207
|
1 |
|
'method' => $method, |
208
|
|
|
'value' => $value |
209
|
1 |
|
]; |
210
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
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.