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