25 protected static function uParse($db, $className, &$ucondition, $quote, &$fields =
null) {
29 foreach ($expressions as $expression) {
30 $expressionArray = \explode(
".", $expression);
31 self::uParseExpression($db, $className, $expression, $expressionArray, $condition, $ucondition, $aliases, $quote, $fields);
36 protected static function uParseExpression($db, $className, $expression, &$expressionArray, &$condition, &$ucondition, &$aliases, $quote, &$fields =
null) {
38 $field = \array_shift($expressionArray);
39 if (isset ($relations [$field])) {
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];
46 self::uParseExpression($db, $jSQL [
'class'], $expression, $expressionArray, $condition, $ucondition, $aliases, $quote, $fields);
52 if (!isset (self::$annotFieldsInRelations [$className])) {
55 return self::$annotFieldsInRelations [$className];
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]);
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);
91 public static function uCount($className, $ucondition =
'', $parameters =
null) {
92 $db = self::getDb($className);
94 $condition =
self::uParse($db, $className, $ucondition, $quote);
96 if ($ucondition !=
'') {
97 $ucondition = SqlUtils::checkWhere($ucondition);
99 return $db->prepareAndFetchColumn(
"SELECT COUNT(*) FROM {$quote}{$tableName}{$quote} " . $condition . $ucondition, $parameters, 0);
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);
117 if (\is_array($groupBy)) {
118 $ucondition .=
' GROUP BY ' . \implode(
', ', $groupBy);
121 $firstPart =
self::uParse($db, $className, $ucondition, $quote, $fieldsMap);
123 $field = $fieldsMap[$field] ?? $field;
125 $field =
'DISTINCT ' . $field;
127 $fieldsInSelect = $function .
"($field) AS result";
129 if (\is_array($groupBy)) {
130 foreach ($groupBy as $index => $field) {
131 if (\is_int($index)) {
132 $fieldsInSelect .=
',' . ($fieldsMap[$field] ?? $field);
134 $fieldsInSelect .=
',' . ($fieldsMap[$field] ?? $field) .
" AS {$quote}{$index}{$quote}";
139 if ($ucondition !=
'' && !\str_starts_with($ucondition,
' GROUP BY')) {
140 $ucondition = SqlUtils::checkWhere($ucondition);
142 return $db->prepareAndFetchAll(
"SELECT {$fieldsInSelect} FROM {$quote}{$tableName}{$quote} " . $firstPart . $ucondition, $parameters, 0);
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);
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);
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);
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);
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);
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);
220 if (\is_array($parameters)) {
221 $conditionParser->setParams($parameters);
223 return self::_getOne($db, $className, $conditionParser, $included, $useCache);
Ubiquity Generic database class.
Object/relational mapping utilities.
static getTableName($class)
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 $annotFieldsInRelations
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)
static getAnnotFieldsInRelations($class)
Ubiquity\orm\traits This class is part of Ubiquity.