1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.6 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Adapters\Sleekdb; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Criteria; |
18
|
|
|
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Reducer; |
19
|
|
|
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Result; |
20
|
|
|
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Model; |
21
|
|
|
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Join; |
22
|
|
|
use Quantum\Libraries\Database\Exceptions\DatabaseException; |
23
|
|
|
use Quantum\Libraries\Database\Contracts\DbalInterface; |
24
|
|
|
use SleekDB\Exceptions\InvalidConfigurationException; |
25
|
|
|
use SleekDB\Exceptions\InvalidArgumentException; |
26
|
|
|
use Quantum\Model\Exceptions\ModelException; |
27
|
|
|
use SleekDB\Exceptions\IOException; |
28
|
|
|
use SleekDB\QueryBuilder; |
29
|
|
|
use SleekDB\Store; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class SleekDbal |
33
|
|
|
* @package Quantum\Libraries\Database |
34
|
|
|
*/ |
35
|
|
|
class SleekDbal implements DbalInterface |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
use Model; |
39
|
|
|
use Result; |
40
|
|
|
use Criteria; |
41
|
|
|
use Reducer; |
42
|
|
|
use Join; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Join type join to |
46
|
|
|
*/ |
47
|
|
|
const JOINTO = 'joinTo'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Join type join through |
51
|
|
|
*/ |
52
|
|
|
const JOINTHROUGH = 'joinThrough'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
protected $isNew = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $data = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $modifiedFields = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
protected $criterias = []; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
protected $havings = []; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var array |
81
|
|
|
*/ |
82
|
|
|
protected $selected = []; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var array |
86
|
|
|
*/ |
87
|
|
|
protected $grouped = []; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var array |
91
|
|
|
*/ |
92
|
|
|
protected $ordered = []; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var int |
96
|
|
|
*/ |
97
|
|
|
protected $offset; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var int |
101
|
|
|
*/ |
102
|
|
|
protected $limit; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var array |
106
|
|
|
*/ |
107
|
|
|
protected $joins = []; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* The database table associated with model |
111
|
|
|
* @var string |
112
|
|
|
*/ |
113
|
|
|
private $table; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* ID column of table |
117
|
|
|
* @var string |
118
|
|
|
*/ |
119
|
|
|
private $idColumn; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Foreign keys |
123
|
|
|
* @var array |
124
|
|
|
*/ |
125
|
|
|
private $foreignKeys = []; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Hidden fields |
129
|
|
|
* @var array |
130
|
|
|
*/ |
131
|
|
|
public $hidden = []; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* ORM Model |
135
|
|
|
* @var object|null |
136
|
|
|
*/ |
137
|
|
|
private $ormModel = null; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @var QueryBuilder|null |
141
|
|
|
*/ |
142
|
|
|
private $queryBuilder; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Active connection |
146
|
|
|
* @var array|null |
147
|
|
|
*/ |
148
|
|
|
private static $connection = null; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Operators map |
152
|
|
|
* @var string[] |
153
|
|
|
*/ |
154
|
|
|
private $operators = [ |
|
|
|
|
155
|
|
|
'=', '!=', |
156
|
|
|
'>', '>=', |
157
|
|
|
'<', '<=', |
158
|
|
|
'IN', 'NOT IN', |
159
|
|
|
'LIKE', 'NOT LIKE', |
160
|
|
|
'BETWEEN', 'NOT BETWEEN', |
161
|
|
|
]; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Class constructor |
165
|
|
|
* @param string $table |
166
|
|
|
* @param string $idColumn |
167
|
|
|
* @param array $foreignKeys |
168
|
|
|
* @param array $hidden |
169
|
|
|
*/ |
170
|
|
|
public function __construct(string $table, string $idColumn = 'id', array $foreignKeys = [], array $hidden = []) |
171
|
|
|
{ |
172
|
|
|
$this->table = $table; |
173
|
|
|
$this->idColumn = $idColumn; |
174
|
|
|
$this->foreignKeys = $foreignKeys; |
175
|
|
|
$this->hidden = $hidden; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function __get($key) |
179
|
|
|
{ |
180
|
|
|
return $this->data[$key] ?? null; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @inheritDoc |
185
|
|
|
*/ |
186
|
|
|
public static function connect(array $config) |
187
|
|
|
{ |
188
|
|
|
self::$connection = $config; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function setData(array $data) |
192
|
|
|
{ |
193
|
|
|
$this->data = $data; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function setModifiedFields($modifiedFields) |
197
|
|
|
{ |
198
|
|
|
$this->modifiedFields = $modifiedFields; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function setIsNew($isNew) |
202
|
|
|
{ |
203
|
|
|
$this->isNew = $isNew; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @inheritDoc |
208
|
|
|
*/ |
209
|
|
|
public static function getConnection(): ?array |
210
|
|
|
{ |
211
|
|
|
return self::$connection; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @inheritDoc |
216
|
|
|
*/ |
217
|
|
|
public static function disconnect() |
218
|
|
|
{ |
219
|
|
|
self::$connection = null; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @inheritDoc |
224
|
|
|
*/ |
225
|
|
|
public function getTable(): string |
226
|
|
|
{ |
227
|
|
|
return $this->table; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Gets the ORM model |
232
|
|
|
* @return Store |
233
|
|
|
* @throws DatabaseException |
234
|
|
|
* @throws IOException |
235
|
|
|
* @throws InvalidArgumentException |
236
|
|
|
* @throws InvalidConfigurationException |
237
|
|
|
*/ |
238
|
|
|
public function getOrmModel(): Store |
239
|
|
|
{ |
240
|
|
|
if (!$this->ormModel) { |
241
|
|
|
if (!self::getConnection()) { |
242
|
|
|
throw DatabaseException::missingConfig(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$connection = self::getConnection(); |
246
|
|
|
|
247
|
|
|
if (empty($connection['database_dir'])) { |
248
|
|
|
throw DatabaseException::incorrectConfig(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
$connection['config']['primary_key'] = $this->idColumn; |
252
|
|
|
|
253
|
|
|
$this->ormModel = new Store($this->table, $connection['database_dir'], $connection['config']); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return $this->ormModel; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param array|null $modelData |
261
|
|
|
*/ |
262
|
|
|
public function updateOrmModel(?array $modelData) |
263
|
|
|
{ |
264
|
|
|
$this->data = $modelData; |
265
|
|
|
$this->modifiedFields = $modelData; |
266
|
|
|
$this->isNew = false; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Deletes the table and the data |
271
|
|
|
* @throws DatabaseException |
272
|
|
|
* @throws IOException |
273
|
|
|
* @throws InvalidArgumentException |
274
|
|
|
* @throws InvalidConfigurationException |
275
|
|
|
*/ |
276
|
|
|
public function deleteTable() |
277
|
|
|
{ |
278
|
|
|
$this->getOrmModel()->deleteStore(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Gets the query builder object |
283
|
|
|
* @return QueryBuilder |
284
|
|
|
* @throws DatabaseException |
285
|
|
|
* @throws ModelException |
286
|
|
|
* @throws IOException |
287
|
|
|
* @throws InvalidArgumentException |
288
|
|
|
* @throws InvalidConfigurationException |
289
|
|
|
*/ |
290
|
|
|
public function getBuilder(): QueryBuilder |
291
|
|
|
{ |
292
|
|
|
if (!$this->queryBuilder) { |
293
|
|
|
$this->queryBuilder = $this->getOrmModel()->createQueryBuilder(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
if (!empty($this->selected)) { |
297
|
|
|
$this->queryBuilder->select($this->selected); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if (!empty($this->joins)) { |
301
|
|
|
$this->applyJoins(); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
if (!empty($this->criterias)) { |
305
|
|
|
$this->queryBuilder->where($this->criterias); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if (!empty($this->havings)) { |
309
|
|
|
$this->queryBuilder->having($this->havings); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if (!empty($this->grouped)) { |
313
|
|
|
$this->queryBuilder->groupBy($this->grouped); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
if (!empty($this->ordered)) { |
317
|
|
|
$this->queryBuilder->orderBy($this->ordered); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
if ($this->offset) { |
321
|
|
|
$this->queryBuilder->skip($this->offset); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
if ($this->limit) { |
325
|
|
|
$this->queryBuilder->limit($this->limit); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return $this->queryBuilder; |
329
|
|
|
} |
330
|
|
|
} |