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.5 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Adapters\Idiorm; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Adapters\Idiorm\Statements\Criteria; |
18
|
|
|
use Quantum\Libraries\Database\Adapters\Idiorm\Statements\Reducer; |
19
|
|
|
use Quantum\Libraries\Database\Adapters\Idiorm\Statements\Result; |
20
|
|
|
use Quantum\Libraries\Database\Adapters\Idiorm\Statements\Query; |
21
|
|
|
use Quantum\Libraries\Database\Adapters\Idiorm\Statements\Model; |
22
|
|
|
use Quantum\Libraries\Database\Adapters\Idiorm\Statements\Join; |
23
|
|
|
use Quantum\Libraries\Database\Contracts\RelationalInterface; |
24
|
|
|
use Quantum\Libraries\Database\Exceptions\DatabaseException; |
25
|
|
|
use Quantum\Libraries\Database\Contracts\DbalInterface; |
26
|
|
|
use ORM; |
27
|
|
|
use PDO; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class IdiormDbal |
31
|
|
|
* @package Quantum\Libraries\Database |
32
|
|
|
*/ |
33
|
|
|
class IdiormDbal implements DbalInterface, RelationalInterface |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
use Model; |
37
|
|
|
use Result; |
38
|
|
|
use Criteria; |
39
|
|
|
use Reducer; |
40
|
|
|
use Join; |
41
|
|
|
use Query; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Default charset |
45
|
|
|
*/ |
46
|
|
|
const DEFAULT_CHARSET = 'utf8'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The database table associated with model |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $table; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Id column of table |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
private $idColumn; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Foreign keys |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private $foreignKeys = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Hidden fields |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
private $hidden = []; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Idiorm Patch object |
74
|
|
|
* @var IdiormPatch |
75
|
|
|
*/ |
76
|
|
|
private $ormPatch = null; |
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* ORM Model |
80
|
|
|
* @var object |
81
|
|
|
*/ |
82
|
|
|
private $ormModel; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Active connection |
86
|
|
|
* @var array|null |
87
|
|
|
*/ |
88
|
|
|
private static $connection = null; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Operators map |
92
|
|
|
* @var string[] |
93
|
|
|
*/ |
94
|
|
|
private $operators = [ |
|
|
|
|
95
|
|
|
'=' => 'where_equal', |
96
|
|
|
'!=' => 'where_not_equal', |
97
|
|
|
'>' => 'where_gt', |
98
|
|
|
'>=' => 'where_gte', |
99
|
|
|
'<' => 'where_lt', |
100
|
|
|
'<=' => 'where_lte', |
101
|
|
|
'IN' => 'where_in', |
102
|
|
|
'NOT IN' => 'where_not_in', |
103
|
|
|
'LIKE' => 'where_like', |
104
|
|
|
'NOT LIKE' => 'where_not_like', |
105
|
|
|
'NULL' => 'where_null', |
106
|
|
|
'NOT NULL' => 'where_not_null', |
107
|
|
|
'#=#' => null, |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* ORM Class |
112
|
|
|
* @var string |
113
|
|
|
*/ |
114
|
|
|
private static $ormClass = ORM::class; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Class constructor |
118
|
|
|
* @param string $table |
119
|
|
|
* @param string $idColumn |
120
|
|
|
* @param array $foreignKeys |
121
|
|
|
* @param array $hidden |
122
|
|
|
*/ |
123
|
|
|
public function __construct(string $table, string $idColumn = 'id', array $foreignKeys = [], array $hidden = []) |
124
|
|
|
{ |
125
|
|
|
$this->table = $table; |
126
|
|
|
$this->idColumn = $idColumn; |
127
|
|
|
$this->foreignKeys = $foreignKeys; |
128
|
|
|
$this->hidden = $hidden; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritDoc |
133
|
|
|
*/ |
134
|
|
|
public static function connect(array $config) |
135
|
|
|
{ |
136
|
|
|
$configuration = [ |
137
|
|
|
'connection_string' => self::buildConnectionString($config), |
138
|
|
|
'logging' => config()->get('debug', false), |
139
|
|
|
'error_mode' => PDO::ERRMODE_EXCEPTION, |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
if ($config['driver'] == 'mysql' || $config['driver'] == 'pgsql') { |
143
|
|
|
$configuration = array_merge($configuration, [ |
144
|
|
|
'username' => $config['username'] ?? null, |
145
|
|
|
'password' => $config['password'] ?? null, |
146
|
|
|
'driver_options' => [ |
147
|
|
|
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? self::DEFAULT_CHARSET) |
148
|
|
|
] |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
(self::$ormClass)::configure($configuration); |
154
|
|
|
|
155
|
|
|
self::$connection = (self::$ormClass)::get_config(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @inheritDoc |
160
|
|
|
*/ |
161
|
|
|
public static function getConnection(): ?array |
162
|
|
|
{ |
163
|
|
|
return self::$connection; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @inheritDoc |
168
|
|
|
*/ |
169
|
|
|
public static function disconnect() |
170
|
|
|
{ |
171
|
|
|
self::$connection = null; |
172
|
|
|
(self::$ormClass)::reset_db(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @inheritDoc |
177
|
|
|
*/ |
178
|
|
|
public function getTable(): string |
179
|
|
|
{ |
180
|
|
|
return $this->table; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Gets the ORM model |
185
|
|
|
* @return ORM |
186
|
|
|
* @throws DatabaseException |
187
|
|
|
*/ |
188
|
|
|
public function getOrmModel(): ORM |
189
|
|
|
{ |
190
|
|
|
if (!$this->ormModel) { |
191
|
|
|
if (!self::getConnection()) { |
192
|
|
|
throw DatabaseException::missingConfig(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->ormModel = (self::$ormClass)::for_table($this->table)->use_id_column($this->idColumn); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this->ormModel; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param ORM $ormModel |
203
|
|
|
*/ |
204
|
|
|
protected function updateOrmModel(ORM $ormModel) |
205
|
|
|
{ |
206
|
|
|
$this->ormModel = $ormModel; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Builds connection string |
211
|
|
|
* @param array $connectionDetails |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
protected static function buildConnectionString(array $connectionDetails): string |
215
|
|
|
{ |
216
|
|
|
$connectionString = $connectionDetails['driver'] . ':'; |
217
|
|
|
|
218
|
|
|
switch ($connectionDetails['driver']) { |
219
|
|
|
case 'sqlite': |
220
|
|
|
$connectionString .= $connectionDetails['database']; |
221
|
|
|
break; |
222
|
|
|
case 'mysql': |
223
|
|
|
case 'pgsql': |
224
|
|
|
$connectionString .= 'host=' . $connectionDetails['host'] . ';'; |
225
|
|
|
|
226
|
|
|
if (isset($connectionDetails['port'])) { |
227
|
|
|
$connectionString .= 'post=' . $connectionDetails['port'] . ';'; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$connectionString .= 'dbname=' . $connectionDetails['dbname'] . ';'; |
231
|
|
|
|
232
|
|
|
if (isset($connectionDetails['charset'])) { |
233
|
|
|
$connectionString .= 'charset=' . $connectionDetails['charset'] . ';'; |
234
|
|
|
} |
235
|
|
|
break; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $connectionString; |
239
|
|
|
} |
240
|
|
|
} |