Passed
Push — master ( 5305f0...4e53b3 )
by Jean-Christophe
11:14
created

DAOUQueries::uCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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