Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DAOUQueries.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\orm\traits;
4
9
19 protected static $annotFieldsInRelations = [];
20
21 abstract protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = null);
22
23 abstract protected static function _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache);
24
25 protected static function uParse($db, $className, &$ucondition, $quote, &$fields = null) {
26 $expressions = self::uGetExpressions($ucondition);
27 $condition = "";
28 $aliases = [];
29 foreach ($expressions as $expression) {
30 $expressionArray = \explode(".", $expression);
31 self::uParseExpression($db, $className, $expression, $expressionArray, $condition, $ucondition, $aliases, $quote, $fields);
32 }
33 return $condition;
34 }
35
36 protected static function uParseExpression($db, $className, $expression, &$expressionArray, &$condition, &$ucondition, &$aliases, $quote, &$fields = null) {
37 $relations = self::getAnnotFieldsInRelations($className);
38 $field = \array_shift($expressionArray);
39 if (isset ($relations [$field])) {
40 $jSQL = OrmUtils::getUJoinSQL($db, $className, $relations [$field], $field, $aliases, $quote);
41 $condition .= ' ' . $jSQL ['sql'];
42 if (\count($expressionArray) === 1) {
43 $ucondition = \preg_replace('/(^|\s|\‍()' . $expression . '/', "\$1{$jSQL['alias']}." . $expressionArray [0], $ucondition);
44 $fields[$expression] = $jSQL['alias'] . '.' . $expressionArray[0];
45 } else {
46 self::uParseExpression($db, $jSQL ['class'], $expression, $expressionArray, $condition, $ucondition, $aliases, $quote, $fields);
47 }
48 }
49 }
50
51 protected static function getAnnotFieldsInRelations($className) {
52 if (!isset (self::$annotFieldsInRelations [$className])) {
53 return self::$annotFieldsInRelations [$className] = OrmUtils::getAnnotFieldsInRelations($className);
54 }
55 return self::$annotFieldsInRelations [$className];
56 }
57
58 protected static function uGetExpressions($condition) {
59 $condition = \preg_replace('@(["\']([^"\']|""|\'\')*["\'])@', "%values%", $condition);
60 \preg_match_all('@[a-zA-Z_$][a-zA-Z_$0-9]*(?:\.[a-zA-Z_$\*][a-zA-Z_$0-9\*]*)+@', $condition, $matches);
61 if (\count($matches) > 0) {
62 return \array_unique($matches [0]);
63 }
64 return [];
65 }
66
77 public static function uGetAll($className, $ucondition = '', $included = true, $parameters = null, $useCache = null) {
78 $db = self::getDb($className);
79 $firstPart = self::uParse($db, $className, $ucondition, $db->quote);
80 return self::_getAll($db, $className, new ConditionParser ($ucondition, $firstPart, $parameters), $included, $useCache);
81 }
82
91 public static function uCount($className, $ucondition = '', $parameters = null) {
92 $db = self::getDb($className);
93 $quote = $db->quote;
94 $condition = self::uParse($db, $className, $ucondition, $quote);
95 $tableName = OrmUtils::getTableName($className);
96 if ($ucondition != '') {
97 $ucondition = SqlUtils::checkWhere($ucondition);
98 }
99 return $db->prepareAndFetchColumn("SELECT COUNT(*) FROM {$quote}{$tableName}{$quote} " . $condition . $ucondition, $parameters, 0);
100 }
101
102
113 public static function uAggregate(string $className, string $ucondition = '', ?array $parameters = null, ?array $groupBy = null, string $function = 'COUNT', string $field = '*', bool $distinct = false): array {
114 $db = self::getDb($className);
115 $quote = $db->quote;
116
117 if (\is_array($groupBy)) {
118 $ucondition .= ' GROUP BY ' . \implode(', ', $groupBy);
119 }
120 $fieldsMap = [];
121 $firstPart = self::uParse($db, $className, $ucondition, $quote, $fieldsMap);
122
123 $field = $fieldsMap[$field] ?? $field;
124 if ($distinct) {
125 $field = 'DISTINCT ' . $field;
126 }
127 $fieldsInSelect = $function . "($field) AS result";
128
129 if (\is_array($groupBy)) {
130 foreach ($groupBy as $index => $field) {
131 if (\is_int($index)) {
132 $fieldsInSelect .= ',' . ($fieldsMap[$field] ?? $field);
133 } else {
134 $fieldsInSelect .= ',' . ($fieldsMap[$field] ?? $field) . " AS {$quote}{$index}{$quote}";
135 }
136 }
137 }
138 $tableName = OrmUtils::getTableName($className);
139 if ($ucondition != '' && !\str_starts_with($ucondition, ' GROUP BY')) {
140 $ucondition = SqlUtils::checkWhere($ucondition);
141 }
142 return $db->prepareAndFetchAll("SELECT {$fieldsInSelect} FROM {$quote}{$tableName}{$quote} " . $firstPart . $ucondition, $parameters, 0);
143 }
144
154 public static function uCountGroupBy(string $className, string $ucondition = '', ?array $parameters = null, ?array $groupBy = null, string $countField = '*', bool $distinct = false): array {
155 return self::uAggregate($className, $ucondition, $parameters, $groupBy, 'COUNT', $countField, $distinct);
156 }
157
166 public static function uAvgGroupBy(string $className, string $avgField, string $ucondition = '', ?array $parameters = null, ?array $groupBy = null) {
167 return self::uAggregate($className, $ucondition, $parameters, $groupBy, 'AVG', $avgField, false);
168 }
169
178 public static function uSumGroupBy(string $className, string $sumField, string $ucondition = '', ?array $parameters = null, ?array $groupBy = null) {
179 return self::uAggregate($className, $ucondition, $parameters, $groupBy, 'SUM', $sumField, false);
180 }
181
190 public static function uMinGroupBy(string $className, string $minField, string $ucondition = '', ?array $parameters = null, ?array $groupBy = null) {
191 return self::uAggregate($className, $ucondition, $parameters, $groupBy, 'MIN', $minField, false);
192 }
193
202 public static function uMaxGroupBy(string $className, string $maxField, string $ucondition = '', ?array $parameters = null, ?array $groupBy = null) {
203 return self::uAggregate($className, $ucondition, $parameters, $groupBy, 'MAX', $maxField, false);
204 }
205
216 public static function uGetOne($className, $ucondition, $included = true, $parameters = null, $useCache = null) {
217 $db = self::getDb($className);
218 $condition = self::uParse($db, $className, $ucondition, $db->quote);
219 $conditionParser = new ConditionParser ($ucondition, $condition);
220 if (\is_array($parameters)) {
221 $conditionParser->setParams($parameters);
222 }
223 return self::_getOne($db, $className, $conditionParser, $included, $useCache);
224 }
225}
226
Ubiquity Generic database class.
Definition Database.php:25
SQL utilities.
Definition SqlUtils.php:13
Object/relational mapping utilities.
Definition OrmUtils.php:17
static getTableName($class)
Definition OrmUtils.php:45
Represents a query condition.
Ubiquity\orm\traits$DAOUQueries This class is part of Ubiquity.
static uParse($db, $className, &$ucondition, $quote, &$fields=null)
static uGetOne($className, $ucondition, $included=true, $parameters=null, $useCache=null)
Returns an instance of $className from the database, from $keyvalues values of the primary key.
static uCountGroupBy(string $className, string $ucondition='', ?array $parameters=null, ?array $groupBy=null, string $countField=' *', bool $distinct=false)
static uGetAll($className, $ucondition='', $included=true, $parameters=null, $useCache=null)
Returns an array of $className objects from the database.
static _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache)
static uSumGroupBy(string $className, string $sumField, string $ucondition='', ?array $parameters=null, ?array $groupBy=null)
static uGetExpressions($condition)
static uCount($className, $ucondition='', $parameters=null)
Returns the number of objects of $className from the database respecting the condition possibly passe...
static getAnnotFieldsInRelations($className)
static uAvgGroupBy(string $className, string $avgField, string $ucondition='', ?array $parameters=null, ?array $groupBy=null)
static uAggregate(string $className, string $ucondition='', ?array $parameters=null, ?array $groupBy=null, string $function='COUNT', string $field=' *', bool $distinct=false)
static _getAll(Database $db, $className, ConditionParser $conditionParser, $included=true, $useCache=null)
static uMinGroupBy(string $className, string $minField, string $ucondition='', ?array $parameters=null, ?array $groupBy=null)
static uParseExpression($db, $className, $expression, &$expressionArray, &$condition, &$ucondition, &$aliases, $quote, &$fields=null)
static uMaxGroupBy(string $className, string $maxField, string $ucondition='', ?array $parameters=null, ?array $groupBy=null)
static getUJoinSQL($db, $model, $arrayAnnot, $field, &$aliases, $quote)
Ubiquity\orm\traits This class is part of Ubiquity.