Completed
Push — master ( 5f0535...74f4b9 )
by Jean-Christophe
01:50
created

SqlUtils::getSearchWhere()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 4
1
<?php
2
3
namespace Ubiquity\db;
4
5
use Ubiquity\utils\base\UArray;
6
use Ubiquity\orm\OrmUtils;
7
8
/**
9
 * Utilitaires SQL
10
 * @author jc
11
 * @version 1.0.0.4
12
 */
13
class SqlUtils {
14
	
15
	public static $quote='`';
16
17
	private static function getParameters($keyAndValues) {
18
		$ret=array ();
19
		foreach ( $keyAndValues as $key => $value ) {
20
			$ret[]=":" . $key;
21
		}
22
		return $ret;
23
	}
24
25
	private static function getQuotedKeys($keyAndValues) {
26
		$ret=array ();
27
		foreach ( $keyAndValues as $key => $value ) {
28
			$ret[]=self::$quote . $key . self::$quote;
29
		}
30
		return $ret;
31
	}
32
33 View Code Duplication
	public static function getWhere($keyAndValues) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
		$ret=array ();
35
		foreach ( $keyAndValues as $key => $value ) {
36
			$ret[]=self::$quote . $key . self::$quote . "= :" . $key;
37
		}
38
		return implode(" AND ", $ret);
39
	}
40
41 View Code Duplication
	public static function getMultiWhere($values, $field) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
	public static function getSearchWhere($fields, $value,$jokerBefore="%",$jokerAfter="%") {
50
		$ret=array ();
51
		foreach ( $fields as $field ) {
52
			$ret[]=self::$quote . $field . self::$quote . " LIKE '".$jokerBefore . $value . $jokerAfter."'";
53
		}
54
		return implode(" OR ", $ret);
55
	}
56
57
	public static function getInsertFields($keyAndValues) {
58
		return implode(",", self::getQuotedKeys($keyAndValues));
59
	}
60
61
	public static function getInsertFieldsValues($keyAndValues) {
62
		return implode(",", self::getParameters($keyAndValues));
63
	}
64
65 View Code Duplication
	public static function getUpdateFieldsKeyAndValues($keyAndValues) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
		$ret=array ();
67
		foreach ( $keyAndValues as $key => $value ) {
68
			$ret[]=self::$quote . $key . self::$quote . "= :" . $key;
69
		}
70
		return implode(",", $ret);
71
	}
72
73
	public static function checkWhere($condition){
74
		$c=\strtolower($condition);
75
		if ($condition != '' && \strstr($c, " join ")===false){
76
			$condition=" WHERE " . $condition;
77
		}
78
		return $condition;
79
	}
80
81
	public static function getCondition($keyValues,$classname=NULL,$separator=" AND ") {
82
		$retArray=array ();
83
		if (is_array($keyValues)) {
84
			if(!UArray::isAssociative($keyValues)){
85
				if(isset($classname)){
86
					$keys=OrmUtils::getKeyFields($classname);
87
					$keyValues=\array_combine($keys, $keyValues);
88
				}
89
			}
90
			foreach ( $keyValues as $key => $value ) {
91
				$retArray[]=self::$quote . $key . self::$quote . " = '" . $value . "'";
92
			}
93
			$condition=implode($separator, $retArray);
94
		} else
95
			$condition=$keyValues;
96
		return $condition;
97
	}
98
99
	public static function getFieldList($fields,$tableName=false){
100
		if(!\is_array($fields)){
101
			return $fields;
102
		}
103
		$result=[];
104
		$prefix="";
105
		if($tableName)
106
			$prefix=self::$quote.$tableName.self::$quote.".";
107
		foreach ($fields as $field) {
108
			$result[]= $prefix.self::$quote.$field.self::$quote;
109
		}
110
		return \implode(",", $result);
111
	}
112
}
113