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