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

SqlUtils   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 100
Duplicated Lines 21 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 2
dl 21
loc 100
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameters() 0 7 2
A getQuotedKeys() 0 7 2
A getWhere() 7 7 2
A getMultiWhere() 7 7 2
A getSearchWhere() 0 7 2
A getInsertFields() 0 3 1
A getInsertFieldsValues() 0 3 1
A getUpdateFieldsKeyAndValues() 7 7 2
A checkWhere() 0 7 3
B getCondition() 0 17 5
A getFieldList() 0 13 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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