Passed
Push — master ( caa32e...4f267a )
by Jean-Christophe
09:15
created

DAOUQueries   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 95.74%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 42
c 2
b 0
f 0
dl 0
loc 101
ccs 45
cts 47
cp 0.9574
rs 10

7 Methods

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