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.6.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Idiorm; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Idiorm\Statements\Criteria; |
18
|
|
|
use Quantum\Libraries\Database\Idiorm\Statements\Result; |
19
|
|
|
use Quantum\Libraries\Database\Idiorm\Statements\Model; |
20
|
|
|
use Quantum\Libraries\Database\Idiorm\Statements\Query; |
21
|
|
|
use Quantum\Libraries\Database\Idiorm\Statements\Join; |
22
|
|
|
use Quantum\Libraries\Database\DbalInterface; |
23
|
|
|
use RecursiveIteratorIterator; |
24
|
|
|
use RecursiveArrayIterator; |
25
|
|
|
use PDO; |
26
|
|
|
use ORM; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class IdiormDbal |
31
|
|
|
* @package Quantum\Libraries\Database |
32
|
|
|
*/ |
33
|
|
|
class IdiormDbal implements DbalInterface |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
use Model; |
37
|
|
|
use Result; |
38
|
|
|
use Criteria; |
39
|
|
|
use Join; |
40
|
|
|
use Query; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Type array |
44
|
|
|
*/ |
45
|
|
|
const TYPE_ARRAY = 1; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Type object |
49
|
|
|
*/ |
50
|
|
|
const TYPE_OBJECT = 2; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The database table associated with model |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $table; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Id column of table |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $idColumn; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Foreign keys |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
private $foreignKeys = []; |
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Idiorm Patch object |
72
|
|
|
* @var \Quantum\Libraries\Database\Idiorm\IdiormPatch |
73
|
|
|
*/ |
74
|
|
|
private $ormPatch = null; |
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* ORM Model |
78
|
|
|
* @var object |
79
|
|
|
*/ |
80
|
|
|
private $ormModel; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Active connection |
84
|
|
|
* @var array|null |
85
|
|
|
*/ |
86
|
|
|
private static $connection = null; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* ORM Class |
90
|
|
|
* @var string |
91
|
|
|
*/ |
92
|
|
|
private static $ormClass = ORM::class; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Class constructor |
96
|
|
|
* @param string $table |
97
|
|
|
* @param string $idColumn |
98
|
|
|
*/ |
99
|
|
|
public function __construct(string $table, string $idColumn = 'id') |
100
|
|
|
{ |
101
|
|
|
$this->table = $table; |
102
|
|
|
$this->idColumn = $idColumn; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritDoc |
107
|
|
|
*/ |
108
|
|
|
public static function connect(array $config) |
109
|
|
|
{ |
110
|
|
|
$configuration = [ |
111
|
|
|
'connection_string' => self::buildConnectionString($config), |
112
|
|
|
'logging' => config()->get('debug', false), |
113
|
|
|
'error_mode' => PDO::ERRMODE_EXCEPTION, |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
if ($config['driver'] == 'mysql' || $config['driver'] == 'pgsql') { |
117
|
|
|
$configuration = array_merge($configuration, [ |
118
|
|
|
'username' => $config['username'] ?? null, |
119
|
|
|
'password' => $config['password'] ?? null, |
120
|
|
|
'driver_options' => [ |
121
|
|
|
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8') |
122
|
|
|
] |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
(self::$ormClass)::configure($configuration); |
128
|
|
|
|
129
|
|
|
self::$connection = (self::$ormClass)::get_config(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritDoc |
134
|
|
|
*/ |
135
|
|
|
public static function getConnection(): ?array |
136
|
|
|
{ |
137
|
|
|
return self::$connection; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritDoc |
142
|
|
|
*/ |
143
|
|
|
public static function disconnect() |
144
|
|
|
{ |
145
|
|
|
self::$connection = null; |
146
|
|
|
(self::$ormClass)::reset_db(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritDoc |
151
|
|
|
*/ |
152
|
|
|
public function getTable(): string |
153
|
|
|
{ |
154
|
|
|
return $this->table; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritDoc |
159
|
|
|
*/ |
160
|
|
|
public function select(...$columns): object |
161
|
|
|
{ |
162
|
|
|
array_walk($columns, function (&$column) { |
163
|
|
|
if (is_array($column)) { |
164
|
|
|
$column = array_flip($column); |
165
|
|
|
} |
166
|
|
|
}); |
167
|
|
|
|
168
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($columns)); |
169
|
|
|
$columns = iterator_to_array($iterator, true); |
170
|
|
|
|
171
|
|
|
return $this->getOrmModel()->select_many($columns); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @inheritDoc |
176
|
|
|
*/ |
177
|
|
|
public function orderBy(string $column, string $direction): object |
178
|
|
|
{ |
179
|
|
|
switch (strtolower($direction)) { |
180
|
|
|
case 'asc': |
181
|
|
|
$this->getOrmModel()->order_by_asc($column); |
182
|
|
|
break; |
183
|
|
|
case 'desc': |
184
|
|
|
$this->getOrmModel()->order_by_desc($column); |
185
|
|
|
break; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $this->getOrmModel(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @inheritDoc |
193
|
|
|
*/ |
194
|
|
|
public function groupBy(string $column): object |
195
|
|
|
{ |
196
|
|
|
return $this->getOrmModel()->group_by($column); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @inheritDoc |
201
|
|
|
*/ |
202
|
|
|
public function limit(int $limit): object |
203
|
|
|
{ |
204
|
|
|
return $this->getOrmModel()->limit($limit); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @inheritDoc |
209
|
|
|
*/ |
210
|
|
|
public function offset(int $offset): object |
211
|
|
|
{ |
212
|
|
|
return $this->getOrmModel()->offset($offset); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @inheritDoc |
217
|
|
|
*/ |
218
|
|
|
public function deleteAll(): bool |
219
|
|
|
{ |
220
|
|
|
return $this->getOrmModel()->delete_many(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @inheritDoc |
225
|
|
|
*/ |
226
|
|
|
public function getOrmModel(): object |
227
|
|
|
{ |
228
|
|
|
if (!$this->ormModel) { |
229
|
|
|
$this->ormModel = (self::$ormClass)::for_table($this->table)->use_id_column($this->idColumn); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $this->ormModel; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @inheritDoc |
237
|
|
|
*/ |
238
|
|
|
public function updateOrmModel(object $ormModel) |
239
|
|
|
{ |
240
|
|
|
$this->ormModel = $ormModel; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Builds connection string |
245
|
|
|
* @param array $connectionDetails |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
private static function buildConnectionString(array $connectionDetails): string |
249
|
|
|
{ |
250
|
|
|
$connectionString = $connectionDetails['driver'] . ':'; |
251
|
|
|
|
252
|
|
|
switch ($connectionDetails['driver']) { |
253
|
|
|
case 'sqlite': |
254
|
|
|
$connectionString .= $connectionDetails['database']; |
255
|
|
|
break; |
256
|
|
|
case 'mysql': |
257
|
|
|
case 'pgsql': |
258
|
|
|
$connectionString .= 'host=' . $connectionDetails['host'] . ';'; |
259
|
|
|
|
260
|
|
|
if (isset($connectionDetails['port'])) { |
261
|
|
|
$connectionString .= 'post=' . $connectionDetails['port'] . ';'; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$connectionString .= 'dbname=' . $connectionDetails['dbname'] . ';'; |
265
|
|
|
|
266
|
|
|
if (isset($connectionDetails['charset'])) { |
267
|
|
|
$connectionString .= 'charset=' . $connectionDetails['charset'] . ';'; |
268
|
|
|
} |
269
|
|
|
break; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $connectionString; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
} |
276
|
|
|
|