|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\orm\OrmUtils; |
|
6
|
|
|
use Ubiquity\orm\parser\ConditionParser; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* |
|
10
|
|
|
* @author jc |
|
11
|
|
|
* @property \Ubiquity\db\Database $db |
|
12
|
|
|
*/ |
|
13
|
|
|
trait DAOUQueries { |
|
14
|
|
|
protected static $annotFieldsInRelations = [ ]; |
|
15
|
|
|
|
|
16
|
|
|
abstract protected static function _getAll($className, ConditionParser $conditionParser, $included = true, $useCache = NULL); |
|
17
|
|
|
|
|
18
|
|
|
abstract protected static function _getOne($className, ConditionParser $conditionParser, $included, $useCache); |
|
19
|
|
|
|
|
20
|
8 |
|
protected static function uParse($className, &$ucondition) { |
|
21
|
8 |
|
$expressions = self::uGetExpressions ( $ucondition ); |
|
22
|
8 |
|
$condition = ""; |
|
23
|
8 |
|
$aliases = [ ]; |
|
24
|
8 |
|
foreach ( $expressions as $expression ) { |
|
25
|
3 |
|
$expressionArray = explode ( ".", $expression ); |
|
26
|
3 |
|
self::uParseExpression ( $className, $expression, $expressionArray, $condition, $ucondition, $aliases ); |
|
27
|
|
|
} |
|
28
|
8 |
|
return $condition; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
protected static function uParseExpression($className, $expression, &$expressionArray, &$condition, &$ucondition, &$aliases) { |
|
32
|
3 |
|
$relations = self::getAnnotFieldsInRelations ( $className ); |
|
33
|
3 |
|
$field = array_shift ( $expressionArray ); |
|
34
|
3 |
|
if (isset ( $relations [$field] )) { |
|
35
|
3 |
|
$jSQL = OrmUtils::getUJoinSQL ( $className, $relations [$field], $field, $aliases ); |
|
36
|
3 |
|
$condition .= " " . $jSQL ["sql"]; |
|
37
|
3 |
|
if (sizeof ( $expressionArray ) === 1) { |
|
38
|
3 |
|
$ucondition = str_replace ( $expression, "{$jSQL["alias"]}." . $expressionArray [0], $ucondition ); |
|
39
|
|
|
} else { |
|
40
|
|
|
self::uParseExpression ( $jSQL ["class"], $expression, $expressionArray, $condition, $ucondition, $aliases ); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
3 |
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
protected static function getAnnotFieldsInRelations($className) { |
|
46
|
3 |
|
if (! isset ( self::$annotFieldsInRelations [$className] )) { |
|
47
|
1 |
|
return self::$annotFieldsInRelations [$className] = OrmUtils::getAnnotFieldsInRelations ( $className ); |
|
48
|
|
|
} |
|
49
|
2 |
|
return self::$annotFieldsInRelations [$className]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
8 |
|
protected static function uGetExpressions($condition) { |
|
53
|
8 |
|
$condition = preg_replace ( '@(["\']([^"\']|""|\'\')*["\'])@', "%values%", $condition ); |
|
54
|
8 |
|
preg_match_all ( '@[a-zA-Z_$][a-zA-Z_$0-9]*(?:\.[a-zA-Z_$\*][a-zA-Z_$0-9\*]*)+@', $condition, $matches ); |
|
55
|
8 |
|
if (sizeof ( $matches ) > 0) { |
|
56
|
8 |
|
return array_unique ( $matches [0] ); |
|
57
|
|
|
} |
|
58
|
|
|
return [ ]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns an array of $className objects from the database |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $className class name of the model to load |
|
65
|
|
|
* @param string $ucondition UQL condition |
|
66
|
|
|
* @param boolean|array $included if true, loads associate members with associations, if array, example : ["client.*","commands"] |
|
67
|
|
|
* @param array|null $parameters the request parameters |
|
68
|
|
|
* @param boolean $useCache use the active cache if true |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
6 |
|
public static function uGetAll($className, $ucondition = '', $included = true, $parameters = null, $useCache = NULL) { |
|
72
|
6 |
|
$condition = self::uParse ( $className, $ucondition ); |
|
73
|
6 |
|
return self::_getAll ( $className, new ConditionParser ( $ucondition, $condition, $parameters ), $included, $useCache ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns the number of objects of $className from the database respecting the condition possibly passed as parameter |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $className complete classname of the model to load |
|
80
|
|
|
* @param string $ucondition Part following the WHERE of an SQL statement |
|
81
|
|
|
* @param array|null $parameters The query parameters |
|
82
|
|
|
* @return int|boolean count of objects |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public static function uCount($className, $ucondition = '', $parameters = null) { |
|
85
|
1 |
|
$condition = self::uParse ( $className, $ucondition ); |
|
86
|
1 |
|
$tableName = OrmUtils::getTableName ( $className ); |
|
87
|
1 |
|
if ($ucondition != '') { |
|
88
|
1 |
|
$ucondition = " WHERE " . $ucondition; |
|
89
|
|
|
} |
|
90
|
1 |
|
return self::getDb ( $className )->prepareAndFetchColumn ( "SELECT COUNT(*) FROM `" . $tableName . "` " . $condition . $ucondition, $parameters, 0 ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns an instance of $className from the database, from $keyvalues values of the primary key |
|
95
|
|
|
* |
|
96
|
|
|
* @param String $className complete classname of the model to load |
|
97
|
|
|
* @param Array|string $ucondition primary key values or condition (UQL) |
|
98
|
|
|
* @param boolean|array $included if true, charges associate members with association |
|
99
|
|
|
* @param array|null $parameters the request parameters |
|
100
|
|
|
* @param boolean $useCache use cache if true |
|
101
|
|
|
* @return object the instance loaded or null if not found |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public static function uGetOne($className, $ucondition, $included = true, $parameters = null, $useCache = NULL) { |
|
104
|
1 |
|
$condition = self::uParse ( $className, $ucondition ); |
|
105
|
1 |
|
$conditionParser = new ConditionParser ( $ucondition, $condition ); |
|
106
|
1 |
|
if (is_array ( $parameters )) { |
|
107
|
1 |
|
$conditionParser->setParams ( $parameters ); |
|
108
|
|
|
} |
|
109
|
1 |
|
return self::_getOne ( $className, $conditionParser, $included, $useCache ); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|