Test Failed
Push — master ( c36a4e...c6936f )
by Jean-Christophe
03:13
created

SqlUtils::checkWhere()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 5
cp 0.6
rs 10
cc 3
nc 2
nop 1
crap 3.576
1
<?php
2
3
namespace Ubiquity\db;
4
5
use Ubiquity\utils\base\UArray;
6
use Ubiquity\orm\OrmUtils;
7
8
/**
9
 * SQL utilities
10
 *
11
 * @author jc
12
 * @version 1.0.2
13
 */
14
class SqlUtils {
15
	public static $quote = '`';
16
17 10
	private static function getParameters($keyAndValues) {
18 10
		$ret = array ();
19 10
		foreach ( $keyAndValues as $key => $value ) {
20 10
			$ret [] = ':' . $key;
21
		}
22 10
		return $ret;
23
	}
24
25 10
	private static function getQuotedKeys($keyAndValues) {
26 10
		$ret = array ();
27 10
		foreach ( $keyAndValues as $key => $value ) {
28 10
			$ret [] = self::$quote . $key . self::$quote;
29
		}
30 10
		return $ret;
31
	}
32
33 9
	public static function getWhere($keyAndValues) {
34 9
		$ret = array ();
35 9
		foreach ( $keyAndValues as $key => $value ) {
36 9
			$ret [] = self::$quote . $key . self::$quote . '= :' . $key;
37
		}
38 9
		return \implode ( ' AND ', $ret );
39
	}
40
41
	public static function getMultiWhere($values, $field) {
42
		$ret = array ();
43
		foreach ( $values as $value ) {
44
			$ret [] = self::$quote . $field . self::$quote . "='" . $value . "'";
45
		}
46
		return \implode ( ' OR ', $ret );
47
	}
48
49 1
	public static function getSearchWhere($fields, $value, $jokerBefore = '%', $jokerAfter = '%') {
50 1
		$ret = array ();
51 1
		foreach ( $fields as $field ) {
52 1
			$ret [] = self::$quote . $field . self::$quote . " LIKE '" . $jokerBefore . $value . $jokerAfter . "'";
53
		}
54 1
		return \implode ( ' OR ', $ret );
55
	}
56
57 10
	public static function getInsertFields($keyAndValues) {
58 10
		return \implode ( ',', self::getQuotedKeys ( $keyAndValues ) );
59
	}
60
61 10
	public static function getInsertFieldsValues($keyAndValues) {
62 10
		return \implode ( ',', self::getParameters ( $keyAndValues ) );
63
	}
64
65 2
	public static function getUpdateFieldsKeyAndParams($keyAndValues) {
66 2
		$ret = array ();
67 2
		foreach ( $keyAndValues as $key => $value ) {
68 2
			$ret [] = self::$quote . $key . self::$quote . '= :' . $key;
69
		}
70 2
		return \implode ( ',', $ret );
71
	}
72
73 62
	public static function getUpdateFieldsKeyAndValues($keyAndValues) {
74 62
		$ret = array ();
75 62
		foreach ( $keyAndValues as $key => $value ) {
76 53
			$ret [] = self::$quote . $key . self::$quote . '= :' . $key;
77
		}
78 62
		return \implode ( ',', $ret );
79
	}
80
81 3
	public static function checkWhere($condition) {
82 3
		$c = \strtolower ( $condition );
83 3
		if ($condition != '' && \strstr ( $c, ' join ' ) === false) {
84
			$condition = ' WHERE ' . $condition;
85
		}
86
		return $condition;
87
	}
88
89
	public static function getCondition($keyValues, $classname = NULL, $separator = ' AND ') {
90
		if (! \is_array ( $keyValues )) {
91
			return $keyValues;
92
		} else {
93
			if (! UArray::isAssociative ( $keyValues )) {
94
				if (isset ( $classname )) {
95
					$keys = OrmUtils::getKeyFields ( $classname );
96
					if (\is_array ( $keys )) {
97
						$keyValues = \array_combine ( $keys, $keyValues );
98
					}
99
				}
100
			}
101
			$retArray = array ();
102
			foreach ( $keyValues as $key => $value ) {
103
				$retArray [] = self::$quote . $key . self::$quote . " = '" . $value . "'";
104
			}
105
			return \implode ( $separator, $retArray );
106
		}
107 19
	}
108 19
109
	/**
110
	 *
111 19
	 * @param array|string $fields
112 19
	 * @param boolean|string $tableName
113 19
	 * @return string
114 19
	 */
115
	public static function getFieldList($fields, $tableName = false) {
116 19
		if (! \is_array ( $fields )) {
117 19
			return $fields;
118
		}
119 19
		$result = [ ];
120
		$prefix = '';
121
		if (\is_string ( $tableName )) {
122
			$prefix = self::$quote . $tableName . self::$quote . '.';
123
		}
124
		foreach ( $fields as $field ) {
125
			$result [] = $prefix . self::$quote . $field . self::$quote;
126
		}
127
		return \implode ( ',', $result );
128
	}
129
}
130