1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\orm; |
4
|
|
|
|
5
|
|
|
use Ubiquity\db\Database; |
6
|
|
|
use Ubiquity\log\Logger; |
7
|
|
|
use Ubiquity\db\SqlUtils; |
8
|
|
|
use Ubiquity\orm\traits\DAOUpdatesTrait; |
9
|
|
|
use Ubiquity\orm\traits\DAORelationsTrait; |
10
|
|
|
use Ubiquity\orm\parser\ConditionParser; |
11
|
|
|
use Ubiquity\orm\traits\DAOUQueries; |
12
|
|
|
use Ubiquity\orm\traits\DAOCoreTrait; |
13
|
|
|
use Ubiquity\orm\traits\DAORelationsPrepareTrait; |
14
|
|
|
use Ubiquity\exceptions\DAOException; |
15
|
|
|
use Ubiquity\orm\traits\DAORelationsAssignmentsTrait; |
16
|
|
|
use Ubiquity\orm\traits\DAOTransactionsTrait; |
17
|
|
|
use Ubiquity\controllers\Startup; |
18
|
|
|
use Ubiquity\cache\CacheManager; |
19
|
|
|
use Ubiquity\orm\traits\DAOPooling; |
20
|
|
|
use Ubiquity\orm\traits\DAOBulkUpdatesTrait; |
21
|
|
|
use Ubiquity\orm\traits\DAOPreparedTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Gateway class between database and object model. |
25
|
|
|
* This class is part of Ubiquity |
26
|
|
|
* |
27
|
|
|
* @author jcheron <[email protected]> |
28
|
|
|
* @version 1.2.4 |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
class DAO { |
32
|
|
|
use DAOCoreTrait,DAOUpdatesTrait,DAORelationsTrait,DAORelationsPrepareTrait,DAORelationsAssignmentsTrait, |
33
|
|
|
DAOUQueries,DAOTransactionsTrait,DAOPooling,DAOBulkUpdatesTrait,DAOPreparedTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
* @var Database |
38
|
|
|
*/ |
39
|
|
|
public static $db; |
40
|
|
|
public static $useTransformers = false; |
41
|
|
|
public static $transformerOp = 'transform'; |
42
|
|
|
private static $conditionParsers = [ ]; |
43
|
|
|
protected static $modelsDatabase = [ ]; |
44
|
|
|
|
45
|
69 |
|
public static function getDb($model) { |
46
|
69 |
|
return self::getDatabase ( self::$modelsDatabase [$model] ?? 'default'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns an array of $className objects from the database |
51
|
|
|
* |
52
|
|
|
* @param string $className class name of the model to load |
53
|
|
|
* @param string $condition Part following the WHERE of an SQL statement |
54
|
|
|
* @param boolean|array $included if true, loads associate members with associations, if array, example : ['client.*','commands'] |
55
|
|
|
* @param array|null $parameters |
56
|
|
|
* @param boolean $useCache use the active cache if true |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
24 |
|
public static function getAll($className, $condition = '', $included = true, $parameters = null, $useCache = NULL) { |
60
|
24 |
|
$db = self::getDb ( $className ); |
61
|
24 |
|
return static::_getAll ( $db, $className, new ConditionParser ( $condition, null, $parameters ), $included, $useCache ); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
public static function paginate($className, $page = 1, $rowsPerPage = 20, $condition = null, $included = true) { |
65
|
3 |
|
if (! isset ( $condition )) { |
66
|
1 |
|
$condition = '1=1'; |
67
|
|
|
} |
68
|
3 |
|
return self::getAll ( $className, $condition . ' LIMIT ' . $rowsPerPage . ' OFFSET ' . (($page - 1) * $rowsPerPage), $included ); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
public static function getRownum($className, $ids) { |
72
|
3 |
|
$tableName = OrmUtils::getTableName ( $className ); |
73
|
3 |
|
$db = self::getDb ( $className ); |
74
|
3 |
|
$quote = $db->quote; |
75
|
3 |
|
self::parseKey ( $ids, $className, $quote ); |
76
|
3 |
|
$condition = SqlUtils::getCondition ( $ids, $className ); |
77
|
3 |
|
$keyFields = OrmUtils::getKeyFields ( $className ); |
78
|
3 |
|
if (is_array ( $keyFields )) { |
79
|
3 |
|
$keys = implode ( ',', $keyFields ); |
80
|
|
|
} else { |
81
|
|
|
$keys = '1'; |
82
|
|
|
} |
83
|
3 |
|
return $db->getRowNum ( $tableName, $keys, $condition ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the number of objects of $className from the database respecting the condition possibly passed as parameter |
88
|
|
|
* |
89
|
|
|
* @param string $className complete classname of the model to load |
90
|
|
|
* @param string $condition Part following the WHERE of an SQL statement |
91
|
|
|
* @param array|null $parameters The query parameters |
92
|
|
|
* @return int|false count of objects |
93
|
|
|
*/ |
94
|
20 |
|
public static function count($className, $condition = '', $parameters = null) { |
95
|
20 |
|
$tableName = OrmUtils::getTableName ( $className ); |
96
|
20 |
|
if ($condition != '') { |
97
|
9 |
|
$condition = ' WHERE ' . $condition; |
98
|
|
|
} |
99
|
20 |
|
$db = self::getDb ( $className ); |
100
|
20 |
|
$quote = $db->quote; |
101
|
20 |
|
return $db->prepareAndFetchColumn ( 'SELECT COUNT(*) FROM ' . $quote . $tableName . $quote . $condition, $parameters ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns an instance of $className from the database, from $keyvalues values of the primary key or with a condition |
106
|
|
|
* |
107
|
|
|
* @param String $className complete classname of the model to load |
108
|
|
|
* @param Array|string $condition condition or primary key values |
109
|
|
|
* @param boolean|array $included if true, charges associate members with association |
110
|
|
|
* @param array|null $parameters the request parameters |
111
|
|
|
* @param boolean|null $useCache use cache if true |
112
|
|
|
* @return object the instance loaded or null if not found |
113
|
|
|
*/ |
114
|
17 |
|
public static function getOne($className, $condition, $included = true, $parameters = null, $useCache = NULL) { |
115
|
17 |
|
$db = self::getDb ( $className ); |
116
|
17 |
|
$conditionParser = new ConditionParser (); |
117
|
17 |
|
if (! isset ( $parameters )) { |
118
|
17 |
|
$conditionParser->addKeyValues ( $condition, $className ); |
119
|
|
|
} elseif (! is_array ( $condition )) { |
120
|
|
|
$conditionParser->setCondition ( $condition ); |
121
|
|
|
$conditionParser->setParams ( $parameters ); |
122
|
|
|
} else { |
123
|
|
|
throw new DAOException ( "The \$condition parameter should not be an array if \$parameters is not null" ); |
124
|
|
|
} |
125
|
17 |
|
return static::_getOne ( $db, $className, $conditionParser, $included, $useCache ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns an instance of $className from the database, from $keyvalues values of the primary key |
130
|
|
|
* |
131
|
|
|
* @param String $className complete classname of the model to load |
132
|
|
|
* @param Array|string $keyValues primary key values or condition |
133
|
|
|
* @param boolean|array $included if true, charges associate members with association |
134
|
|
|
* @param array|null $parameters the request parameters |
135
|
|
|
* @param boolean|null $useCache use cache if true |
136
|
|
|
* @return object the instance loaded or null if not found |
137
|
|
|
*/ |
138
|
25 |
|
public static function getById($className, $keyValues, $included = true, $useCache = NULL) { |
139
|
25 |
|
return static::_getOne ( self::getDatabase ( self::$modelsDatabase [$className] ?? 'default'), $className, self::getConditionParser ( $className, $keyValues ), $included, $useCache ); |
140
|
|
|
} |
141
|
|
|
|
142
|
25 |
|
protected static function getConditionParser($className, $keyValues): ConditionParser { |
143
|
25 |
|
if (! isset ( self::$conditionParsers [$className] )) { |
144
|
13 |
|
$conditionParser = new ConditionParser (); |
145
|
13 |
|
$conditionParser->addKeyValues ( $keyValues, $className ); |
146
|
13 |
|
self::$conditionParsers [$className] = $conditionParser; |
147
|
|
|
} else { |
148
|
14 |
|
self::$conditionParsers [$className]->setKeyValues ( $keyValues ); |
149
|
|
|
} |
150
|
25 |
|
return self::$conditionParsers [$className]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Establishes the connection to the database using the past parameters |
155
|
|
|
* |
156
|
|
|
* @param string $offset |
157
|
|
|
* @param string $wrapper |
158
|
|
|
* @param string $dbType |
159
|
|
|
* @param string $dbName |
160
|
|
|
* @param string $serverName |
161
|
|
|
* @param string $port |
162
|
|
|
* @param string $user |
163
|
|
|
* @param string $password |
164
|
|
|
* @param array $options |
165
|
|
|
* @param boolean $cache |
166
|
|
|
*/ |
167
|
74 |
|
public static function connect($offset, $wrapper, $dbType, $dbName, $serverName = '127.0.0.1', $port = '3306', $user = 'root', $password = '', $options = [ ], $cache = false) { |
168
|
74 |
|
self::$db [$offset] = new Database ( $wrapper, $dbType, $dbName, $serverName, $port, $user, $password, $options, $cache, self::$pool ); |
169
|
|
|
try { |
170
|
74 |
|
self::$db [$offset]->connect (); |
171
|
|
|
} catch ( \Exception $e ) { |
172
|
|
|
Logger::error ( "DAO", $e->getMessage () ); |
173
|
|
|
throw new DAOException ( $e->getMessage (), $e->getCode (), $e->getPrevious () ); |
174
|
|
|
} |
175
|
74 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Establishes the connection to the database using the $config array |
179
|
|
|
* |
180
|
|
|
* @param array $config the config array (Startup::getConfig()) |
181
|
|
|
*/ |
182
|
20 |
|
public static function startDatabase(&$config, $offset = null) { |
183
|
20 |
|
$db = $offset ? ($config ['database'] [$offset] ?? ($config ['database'] ?? [ ])) : ($config ['database'] ['default'] ?? $config ['database']); |
184
|
20 |
|
if ($db ['dbName'] !== '') { |
185
|
20 |
|
self::connect ( $offset ?? 'default', $db ['wrapper'] ?? \Ubiquity\db\providers\pdo\PDOWrapper::class, $db ['type'], $db ['dbName'], $db ['serverName'] ?? '127.0.0.1', $db ['port'] ?? 3306, $db ['user'] ?? 'root', $db ['password'] ?? '', $db ['options'] ?? [ ], $db ['cache'] ?? false); |
186
|
|
|
} |
187
|
20 |
|
} |
188
|
|
|
|
189
|
120 |
|
public static function getDbOffset(&$config, $offset = null) { |
190
|
120 |
|
return $offset ? ($config ['database'] [$offset] ?? ($config ['database'] ?? [ ])) : ($config ['database'] ['default'] ?? $config ['database']); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns true if the connection to the database is established |
195
|
|
|
* |
196
|
|
|
* @return boolean |
197
|
|
|
*/ |
198
|
5 |
|
public static function isConnected($offset = 'default') { |
199
|
5 |
|
$db = self::$db [$offset] ?? false; |
200
|
5 |
|
return $db && ($db instanceof Database) && $db->isConnected (); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Sets the transformer operation |
205
|
|
|
* |
206
|
|
|
* @param string $op |
207
|
|
|
*/ |
208
|
|
|
public static function setTransformerOp($op) { |
209
|
|
|
self::$transformerOp = $op; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Closes the active pdo connection to the database |
214
|
|
|
*/ |
215
|
28 |
|
public static function closeDb($offset = 'default') { |
216
|
28 |
|
$db = self::$db [$offset] ?? false; |
217
|
28 |
|
if ($db !== false) { |
218
|
28 |
|
$db->close (); |
219
|
|
|
} |
220
|
28 |
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Defines the database connection to use for $model class |
224
|
|
|
* |
225
|
|
|
* @param string $model a model class |
226
|
|
|
* @param string $database a database connection defined in config.php |
227
|
|
|
*/ |
228
|
|
|
public static function setModelDatabase($model, $database = 'default') { |
229
|
|
|
self::$modelsDatabase [$model] = $database; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Defines the database connections to use for models classes |
234
|
|
|
* |
235
|
|
|
* @param array $modelsDatabase |
236
|
|
|
*/ |
237
|
|
|
public static function setModelsDatabases($modelsDatabase) { |
238
|
|
|
self::$modelsDatabase = $modelsDatabase; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns the database instance defined at $offset key in config |
243
|
|
|
* |
244
|
|
|
* @param string $offset |
245
|
|
|
* @return \Ubiquity\db\Database |
246
|
|
|
*/ |
247
|
71 |
|
public static function getDatabase($offset = 'default') { |
248
|
71 |
|
if (! isset ( self::$db [$offset] )) { |
249
|
18 |
|
self::startDatabase ( Startup::$config, $offset ); |
250
|
|
|
} |
251
|
71 |
|
SqlUtils::$quote = self::$db [$offset]->quote; |
252
|
71 |
|
return self::$db [$offset]; |
253
|
|
|
} |
254
|
|
|
|
255
|
5 |
|
public static function getDatabases() { |
256
|
5 |
|
$config = Startup::getConfig (); |
257
|
5 |
|
if (isset ( $config ['database'] )) { |
258
|
5 |
|
if (isset ( $config ['database'] ['dbName'] )) { |
259
|
|
|
return [ 'default' ]; |
260
|
|
|
} else { |
261
|
5 |
|
return \array_keys ( $config ['database'] ); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
return [ ]; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public static function updateDatabaseParams(array &$config, array $parameters, $offset = 'default') { |
268
|
|
|
if ($offset === 'default') { |
269
|
|
|
if (isset ( $config ['database'] [$offset] )) { |
270
|
|
|
foreach ( $parameters as $k => $param ) { |
271
|
|
|
$config ['database'] [$offset] [$k] = $param; |
272
|
|
|
} |
273
|
|
|
} else { |
274
|
|
|
foreach ( $parameters as $k => $param ) { |
275
|
|
|
$config ['database'] [$k] = $param; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
} else { |
279
|
|
|
if (isset ( $config ['database'] [$offset] )) { |
280
|
|
|
foreach ( $parameters as $k => $param ) { |
281
|
|
|
$config ['database'] [$offset] [$k] = $param; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
37 |
|
public static function start() { |
288
|
37 |
|
self::$modelsDatabase = CacheManager::getModelsDatabases (); |
289
|
37 |
|
} |
290
|
|
|
} |
291
|
|
|
|