Completed
Push — master ( 4f04d3...489696 )
by Jean-Christophe
01:34
created

SqlUtils::getMultiWhere()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
3
namespace Ubiquity\db;
4
5
use Ubiquity\utils\JArray;
6
use Ubiquity\orm\OrmUtils;
7
8
/**
9
 * Utilitaires SQL
10
 * @author jc
11
 * @version 1.0.0.3
12
 */
13
class SqlUtils {
14
15
	private static function getParameters($keyAndValues) {
16
		$ret=array ();
17
		foreach ( $keyAndValues as $key => $value ) {
18
			$ret[]=":" . $key;
19
		}
20
		return $ret;
21
	}
22
23
	private static function getQuotedKeys($keyAndValues, $quote="`") {
24
		$ret=array ();
25
		foreach ( $keyAndValues as $key => $value ) {
26
			$ret[]=$quote . $key . $quote;
27
		}
28
		return $ret;
29
	}
30
31 View Code Duplication
	public static function getWhere($keyAndValues, $quote="`") {
32
		$ret=array ();
33
		foreach ( $keyAndValues as $key => $value ) {
34
			$ret[]=$quote . $key . $quote . "= :" . $key;
35
		}
36
		return implode(" AND ", $ret);
37
	}
38
39 View Code Duplication
	public static function getMultiWhere($values, $field, $quote="`") {
40
		$ret=array ();
41
		foreach ( $values as $value ) {
42
			$ret[]=$quote . $field . $quote . "='" . $value . "'";
43
		}
44
		return implode(" OR ", $ret);
45
	}
46
47
	public static function getInsertFields($keyAndValues) {
48
		return implode(",", self::getQuotedKeys($keyAndValues));
49
	}
50
51
	public static function getInsertFieldsValues($keyAndValues) {
52
		return implode(",", self::getParameters($keyAndValues));
53
	}
54
55 View Code Duplication
	public static function getUpdateFieldsKeyAndValues($keyAndValues, $quote="`") {
56
		$ret=array ();
57
		foreach ( $keyAndValues as $key => $value ) {
58
			$ret[]=$quote . $key . $quote . "= :" . $key;
59
		}
60
		return implode(",", $ret);
61
	}
62
63
	public static function checkWhere($condition){
64
		$c=\strtolower($condition);
65
		if ($condition != '' && \strstr($c, " join ")===false){
66
			$condition=" WHERE " . $condition;
67
		}
68
		return $condition;
69
	}
70
71
	public static function getCondition($keyValues,$classname=NULL,$separator=" AND ") {
72
		$retArray=array ();
73
		if (is_array($keyValues)) {
74
			if(!JArray::isAssociative($keyValues)){
75
				if(isset($classname)){
76
					$keys=OrmUtils::getKeyFields($classname);
77
					$keyValues=\array_combine($keys, $keyValues);
78
				}
79
			}
80
			foreach ( $keyValues as $key => $value ) {
81
				$retArray[]="`" . $key . "` = '" . $value . "'";
82
			}
83
			$condition=implode($separator, $retArray);
84
		} else
85
			$condition=$keyValues;
86
		return $condition;
87
	}
88
}
89