51 public static function getDb($model) {
65 public static function getAll($className, $condition =
'', $included =
true, $parameters =
null, $useCache = NULL) {
67 return static::_getAll (
$db, $className,
new ConditionParser ( $condition,
null, $parameters ), $included, $useCache );
80 public static function getAllByIds($className, $keyValues = [ ], $included =
true, $condition =
'', $useCache = NULL): array {
83 $countK = \count ( $keyValues );
85 $nCondition = $key .
' IN (' . \str_repeat (
'?,', $countK - 1 ) .
'?)';
86 if ($condition !=
null) {
87 $nCondition .=
' AND ' . $condition;
89 return static::_getAll (
$db, $className,
new ConditionParser ( $nCondition,
null, $keyValues ), $included, $useCache );
95 public static function paginate($className, $page = 1, $rowsPerPage = 20, $condition =
null, $included =
true) {
96 return self::getAll ( $className, ($condition ??
'1=1') .
' LIMIT ' . $rowsPerPage .
' OFFSET ' . (($page - 1) * $rowsPerPage), $included );
111 public static function orderBy(
string $className,
string $field,
string $order =
'ASC',
string $condition =
'',
bool $included =
true, $parameters =
null, $useCache = NULL): array {
112 return self::getAll ( $className, ($condition ??
'1=1') .
' ORDER BY ' . $field .
' ' . $order, $included, $parameters, $useCache );
116 $tableName = OrmUtils::getTableName ( $className );
117 $db = self::getDb ( $className );
119 self::parseKey ( $ids, $className, $quote );
120 $condition = SqlUtils::getCondition ( $ids, $className );
121 $keyFields = OrmUtils::getKeyFields ( $className );
122 if (\is_array ( $keyFields )) {
123 $keys = \implode (
',', $keyFields );
127 return $db->getRowNum ( $tableName, $keys, $condition );
138 public static function count($className, $condition =
'', $parameters =
null) {
139 $tableName = OrmUtils::getTableName ( $className );
140 if ($condition !=
'') {
141 $condition = SqlUtils::checkWhere($condition);
143 $db = self::getDb ( $className );
145 return $db->prepareAndFetchColumn (
'SELECT COUNT(*) FROM ' . $quote . $tableName . $quote . $condition, $parameters );
156 public static function exists($className, $condition =
'', $parameters =
null): bool {
157 $tableName =
OrmUtils::getTableName ( $className );
158 if ($condition !=
'') {
159 $condition = SqlUtils::checkWhere($condition);
161 $db = self::getDb ( $className );
163 return (1 == $db->prepareAndFetchColumn (
"SELECT EXISTS(SELECT 1 FROM {$quote}{$tableName}{$quote}{$condition})", $parameters ));
177 public static function getOne($className, $condition, $included =
true, $parameters =
null, $useCache = NULL) {
178 $db = self::getDb ( $className );
180 if (! isset ( $parameters )) {
181 $conditionParser->addKeyValues ( $condition, $className );
182 } elseif (! is_array ( $condition )) {
183 $conditionParser->setCondition ( $condition );
184 $conditionParser->setParams ( $parameters );
186 throw new DAOException (
"The \$condition parameter should not be an array if \$parameters is not null" );
188 return static::_getOne ( $db, $className, $conditionParser, $included, $useCache );
200 public static function getById($className, $keyValues, $included =
true, $useCache = NULL) {
201 return static::_getOne ( self::getDatabase ( self::$modelsDatabase [$className] ??
'default'), $className, self::getConditionParser ( $className, $keyValues ), $included, $useCache );
205 if (! isset ( self::$conditionParsers [$className] )) {
207 $conditionParser->addKeyValues ( $keyValues, $className );
208 self::$conditionParsers [$className] = $conditionParser;
210 self::$conditionParsers [$className]->setKeyValues ( $keyValues );
212 return self::$conditionParsers [$className];
230 public static function connect($offset, $wrapper, $dbType, $dbName, $serverName =
'127.0.0.1', $port =
'3306', $user =
'root', $password =
'', $options = [ ], $cache =
false) {
231 self::$db [$offset] =
new Database ( $wrapper, $dbType, $dbName, $serverName, $port, $user, $password, $options, $cache, self::$pool );
233 self::$db [$offset]->connect ();
234 }
catch ( \Exception $e ) {
235 Logger::error (
"DAO", $e->getMessage () );
236 throw new DAOException ( $e->getMessage (), $e->getCode (), $e->getPrevious () );
246 $db = $offset ? ($config [
'database'] [$offset] ?? ($config [
'database'] ?? [ ])) : ($config [
'database'] [
'default'] ?? $config [
'database']);
247 if ($db [
'dbName'] !==
'') {
248 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);
253 return $offset ? ($config [
'database'] [$offset] ?? ($config [
'database'] ?? [ ])) : ($config [
'database'] [
'default'] ?? $config [
'database']);
262 $db = self::$db [$offset] ?? false;
263 return ($db instanceof
Database) && $db->isConnected ();
272 self::$transformerOp = $op;
278 public static function closeDb($offset =
'default') {
279 $db = self::$db [$offset] ??
false;
292 self::$modelsDatabase [$model] = $database;
301 self::$modelsDatabase = $modelsDatabase;
311 if (! isset ( self::$db [$offset] )) {
312 self::startDatabase ( Startup::$config, $offset );
314 SqlUtils::$quote = self::$db [$offset]->quote??
'';
315 return self::$db [$offset]??
null;
319 $config =
Startup::getConfig ();
320 if (isset ( $config [
'database'] )) {
321 if (isset ( $config [
'database'] [
'dbName'] )) {
322 return [
'default' ];
324 return \array_keys ( $config [
'database'] );
331 if ($offset ===
'default') {
332 if (isset ( $config [
'database'] [$offset] )) {
333 foreach ( $parameters as $k => $param ) {
334 $config [
'database'] [$offset] [$k] = $param;
337 foreach ( $parameters as $k => $param ) {
338 $config [
'database'] [$k] = $param;
342 if (isset ( $config [
'database'] [$offset] )) {
343 foreach ( $parameters as $k => $param ) {
344 $config [
'database'] [$offset] [$k] = $param;
351 self::$modelsDatabase = CacheManager::getModelsDatabases ();
355 $db = static::$db [self::$modelsDatabase [$model] ??
'default'];
356 return $db->getCacheInstance ();
359 public static function warmupCache($className, $condition =
'', $included =
false, $parameters = [ ]) {
360 $objects = self::getAll ( $className, $condition, $included, $parameters );
361 foreach ( $objects as $o ) {
362 self::$cache->store ( $className, OrmUtils::getKeyValues ( $o ), $o );
364 self::$cache->optimize ();
365 $offset = self::$modelsDatabase [$className] ??
'default';
366 $db = self::$db [$offset];
368 unset ( self::$db [$offset] );
372 self::$cache = $cache;
380 return static::$cache;
387 return self::$modelsDatabase;
Manager for caches (Router, Rest, models).
Ubiquity\cache\dao$AbstractDAOCache This class is part of Ubiquity.
Ubiquity Generic database class.
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Gateway class between database and object model.
static getConditionParser($className, $keyValues)
static count($className, $condition='', $parameters=null)
Returns the number of objects of $className from the database respecting the condition possibly passe...
static paginate($className, $page=1, $rowsPerPage=20, $condition=null, $included=true)
static connect($offset, $wrapper, $dbType, $dbName, $serverName='127.0.0.1', $port='3306', $user='root', $password='', $options=[], $cache=false)
Establishes the connection to the database using the past parameters.
static warmupCache($className, $condition='', $included=false, $parameters=[])
static orderBy(string $className, string $field, string $order='ASC', string $condition='', bool $included=true, $parameters=null, $useCache=NULL)
Returns an array of $className objects from the database ordered by $field.
static getDbOffset(&$config, $offset=null)
static getOne($className, $condition, $included=true, $parameters=null, $useCache=NULL)
Returns an instance of $className from the database, from $keyvalues values of the primary key or wit...
static closeDb($offset='default')
Closes the active pdo connection to the database.
static getAllByIds($className, $keyValues=[], $included=true, $condition='', $useCache=NULL)
Returns an array of $className objects loaded by id from the database.
static updateDatabaseParams(array &$config, array $parameters, $offset='default')
static startDatabase(&$config, $offset=null)
Establishes the connection to the database using the $config array.
static getById($className, $keyValues, $included=true, $useCache=NULL)
Returns an instance of $className from the database, from $keyvalues values of the primary key.
static getDatabase($offset='default')
Returns the database instance defined at $offset key in config.
static exists($className, $condition='', $parameters=null)
Tests the existence of objects of $className from the database respecting the condition possibly pass...
static setModelDatabase($model, $database='default')
Defines the database connection to use for $model class.
static setModelsDatabases($modelsDatabase)
Defines the database connections to use for models classes.
static getRownum($className, $ids)
static getModelsDatabase()
static setTransformerOp(string $op)
Sets the transformer operation.
static isConnected($offset='default')
Returns true if the connection to the database is established.
static getDbCacheInstance($model)
static setCache(AbstractDAOCache $cache)
static getAll($className, $condition='', $included=true, $parameters=null, $useCache=NULL)
Returns an array of $className objects from the database.
Object/relational mapping utilities.
Represents a query condition.
Ubiquity\orm\traits$DAOBulkUpdatesTrait This class is part of Ubiquity.
Ubiquity\orm\traits$DAOPreparedTrait This class is part of Ubiquity.
Used by DAO class, realize relations assignments.
Used by DAO class, prepare relations for loading.
Ubiquity\orm\traits$DAOUQueries This class is part of Ubiquity.
static getFirstKey($class)
Class Configuration \config.