Code Duplication    Length = 79-91 lines in 4 locations

src/Query/Dialects/Firebird/FirebirdWhereTrait.php 1 location

@@ 21-111 (lines=91) @@
18
/**
19
 * @method quote(string $str):string
20
 */
21
trait FirebirdWhereTrait{
22
	use WhereTrait;
23
24
	protected $where = [];
25
	protected $bindValues = [];
26
27
	/**
28
	 * @param        $val1
29
	 * @param null   $val2
30
	 * @param null   $operator
31
	 * @param bool   $bind
32
	 * @param string $join
33
	 *
34
	 * @return $this
35
	 */
36
	protected function _addWhere($val1, $val2 = null, $operator = null, $bind = true, $join = 'AND'){
37
		$operator = strtoupper(trim($operator));
38
		$join = strtoupper(trim($join));
39
40
		$operators = [
41
			'=', '>=', '>', '<=', '<', '<>', '!=',
42
			'|', '&', '<<', '>>', '+', '-', '*', '/', '%', '^', '<=>', '~', '!', 'DIV', 'MOD',
43
			'IS', 'IS NOT', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP', 'BETWEEN', 'NOT BETWEEN', 'EXISTS'
44
		];
45
46
		if(in_array($operator, $operators, true)){
47
			$where = [$this->quote($val1)];
48
			$values = [];
49
50
			if(in_array($operator, ['IN', 'NOT IN'], true)){
51
52
				if(is_array($val2)){
53
					if($bind){
54
						$where[] = 'IN('.implode(',', array_fill(0, count($val2), '?')).')';
55
						$values = array_merge($values, $val2);
56
					}
57
					else{
58
						$where[] = 'IN('.implode(',', $val2).')'; // @todo: quote
59
					}
60
				}
61
				else if($val2 instanceof StatementInterface){
62
					$where[] = 'IN('.$val2->sql().')';
63
					$values = array_merge($values, $val2->bindValues());
64
				}
65
66
			}
67
			else if(in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)){
68
69
			}
70
			else{
71
				$where[] = $operator;
72
73
				if(is_null($val2)){
74
					$where[] = 'NULL';
75
				}
76
				else if(is_bool($val2)){
77
					$where[] = $val2 ? 'TRUE' : 'FALSE';
78
				}
79
				else if(in_array(strtolower($val2), ['null', 'false', 'true', 'unknown'])){
80
					$where[] = strtoupper($val2);
81
				}
82
				else if($val2 instanceof StatementInterface){
83
					$where[] = '('.$val2->sql().')';
84
					$values = array_merge($values, $val2->bindValues());
85
				}
86
				else{
87
					if($bind){
88
						$where[] = '?';
89
						$values[] = $val2;
90
					}
91
					else{
92
						if(!empty($val2)){
93
							$where[] = $this->quote($val2);
94
						}
95
					}
96
				}
97
98
			}
99
100
			$this->bindValues = array_merge($this->bindValues, $values);
101
			$this->where[] = [
102
				'join' => in_array($join, ['AND', 'OR', 'XOR']) ? $join : 'AND',
103
				'stmt' => implode(' ', $where),
104
			];
105
106
		}
107
108
		return $this;
109
	}
110
111
}
112

src/Query/Dialects/MySQL/MySQLWhereTrait.php 1 location

@@ 21-111 (lines=91) @@
18
/**
19
 * @method quote(string $str):string
20
 */
21
trait MySQLWhereTrait{
22
	use WhereTrait;
23
24
	protected $where = [];
25
	protected $bindValues = [];
26
27
	/**
28
	 * @param        $val1
29
	 * @param null   $val2
30
	 * @param null   $operator
31
	 * @param bool   $bind
32
	 * @param string $join
33
	 *
34
	 * @return $this
35
	 */
36
	protected function _addWhere($val1, $val2 = null, $operator = null, $bind = true, $join = 'AND'){
37
		$operator = strtoupper(trim($operator));
38
		$join = strtoupper(trim($join));
39
40
		$operators = [
41
			'=', '>=', '>', '<=', '<', '<>', '!=',
42
			'|', '&', '<<', '>>', '+', '-', '*', '/', '%', '^', '<=>', '~', '!', 'DIV', 'MOD',
43
			'IS', 'IS NOT', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP', 'BETWEEN', 'NOT BETWEEN', 'EXISTS'
44
		];
45
46
		if(in_array($operator, $operators, true)){
47
			$where = [$this->quote($val1)];
48
			$values = [];
49
50
			if(in_array($operator, ['IN', 'NOT IN'], true)){
51
52
				if(is_array($val2)){
53
					if($bind){
54
						$where[] = 'IN('.implode(',', array_fill(0, count($val2), '?')).')';
55
						$values = array_merge($values, $val2);
56
					}
57
					else{
58
						$where[] = 'IN('.implode(',', $val2).')'; // @todo: quote
59
					}
60
				}
61
				else if($val2 instanceof StatementInterface){
62
					$where[] = 'IN('.$val2->sql().')';
63
					$values = array_merge($values, $val2->bindValues());
64
				}
65
66
			}
67
			else if(in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)){
68
69
			}
70
			else{
71
				$where[] = $operator;
72
73
				if(is_null($val2)){
74
					$where[] = 'NULL';
75
				}
76
				else if(is_bool($val2)){
77
					$where[] = $val2 ? 'TRUE' : 'FALSE';
78
				}
79
				else if(in_array(strtolower($val2), ['null', 'false', 'true', 'unknown'])){
80
					$where[] = strtoupper($val2);
81
				}
82
				else if($val2 instanceof StatementInterface){
83
					$where[] = '('.$val2->sql().')';
84
					$values = array_merge($values, $val2->bindValues());
85
				}
86
				else{
87
					if($bind){
88
						$where[] = '?';
89
						$values[] = $val2;
90
					}
91
					else{
92
						if(!empty($val2)){
93
							$where[] = $this->quote($val2);
94
						}
95
					}
96
				}
97
98
			}
99
100
			$this->bindValues = array_merge($this->bindValues, $values);
101
			$this->where[] = [
102
				'join' => in_array($join, ['AND', 'OR', 'XOR']) ? $join : 'AND',
103
				'stmt' => implode(' ', $where),
104
			];
105
106
		}
107
108
		return $this;
109
	}
110
111
}
112

src/Query/Dialects/Postgres/PostgresWhereTrait.php 1 location

@@ 20-98 (lines=79) @@
17
/**
18
 * Trait PostgresWhereTrait
19
 */
20
trait PostgresWhereTrait{
21
	use WhereTrait;
22
23
	protected function _addWhere($val1, $val2 = null, $operator = null, $bind = true, $join = 'AND'){
24
		$operator = strtoupper(trim($operator));
25
		$join = strtoupper(trim($join));
26
27
		$operators = [
28
			'=', '>=', '>', '<=', '<', '<>', '!=',
29
			'|', '&', '<<', '>>', '+', '-', '*', '/', '%', '^', '<=>', '~', '!', 'DIV', 'MOD',
30
			'IS', 'IS NOT', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP', 'BETWEEN', 'NOT BETWEEN', 'EXISTS'
31
		];
32
33
		if(in_array($operator, $operators, true)){
34
			$where = [$this->quote($val1)];
35
			$values = [];
36
37
			if(in_array($operator, ['IN', 'NOT IN'], true)){
38
39
				if(is_array($val2)){
40
					if($bind){
41
						$where[] = 'IN('.implode(',', array_fill(0, count($val2), '?')).')';
42
						$values = array_merge($values, $val2);
43
					}
44
					else{
45
						$where[] = 'IN('.implode(',', $val2).')'; // @todo: quote
46
					}
47
				}
48
				else if($val2 instanceof StatementInterface){
49
					$where[] = 'IN('.$val2->sql().')';
50
					$values = array_merge($values, $val2->bindValues());
51
				}
52
53
			}
54
			else if(in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)){
55
56
			}
57
			else{
58
				$where[] = $operator;
59
60
				if(is_null($val2)){
61
					$where[] = 'NULL';
62
				}
63
				else if(is_bool($val2)){
64
					$where[] = $val2 ? 'TRUE' : 'FALSE';
65
				}
66
				else if(in_array(strtolower($val2), ['null', 'false', 'true', 'unknown'])){
67
					$where[] = strtoupper($val2);
68
				}
69
				else if($val2 instanceof StatementInterface){
70
					$where[] = '('.$val2->sql().')';
71
					$values = array_merge($values, $val2->bindValues());
72
				}
73
				else{
74
					if($bind){
75
						$where[] = '?';
76
						$values[] = $val2;
77
					}
78
					else{
79
						if(!empty($val2)){
80
							$where[] = $this->quote($val2);
81
						}
82
					}
83
				}
84
85
			}
86
87
			$this->bindValues = array_merge($this->bindValues, $values);
88
			$this->where[] = [
89
				'join' => in_array($join, ['AND', 'OR', 'XOR']) ? $join : 'AND',
90
				'stmt' => implode(' ', $where),
91
			];
92
93
		}
94
95
		return $this;
96
	}
97
98
}
99

src/Query/Dialects/SQLite/SQLiteWhereTrait.php 1 location

@@ 20-98 (lines=79) @@
17
/**
18
 * Trait SQLiteWhereTrait
19
 */
20
trait SQLiteWhereTrait{
21
	use WhereTrait;
22
23
	protected function _addWhere($val1, $val2 = null, $operator = null, $bind = true, $join = 'AND'){
24
		$operator = strtoupper(trim($operator));
25
		$join = strtoupper(trim($join));
26
27
		$operators = [
28
			'=', '>=', '>', '<=', '<', '<>', '!=',
29
			'|', '&', '<<', '>>', '+', '-', '*', '/', '%', '^', '<=>', '~', '!', 'DIV', 'MOD',
30
			'IS', 'IS NOT', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP', 'BETWEEN', 'NOT BETWEEN', 'EXISTS'
31
		];
32
33
		if(in_array($operator, $operators, true)){
34
			$where = [$this->quote($val1)];
35
			$values = [];
36
37
			if(in_array($operator, ['IN', 'NOT IN'], true)){
38
39
				if(is_array($val2)){
40
					if($bind){
41
						$where[] = 'IN('.implode(',', array_fill(0, count($val2), '?')).')';
42
						$values = array_merge($values, $val2);
43
					}
44
					else{
45
						$where[] = 'IN('.implode(',', $val2).')'; // @todo: quote
46
					}
47
				}
48
				else if($val2 instanceof StatementInterface){
49
					$where[] = 'IN('.$val2->sql().')';
50
					$values = array_merge($values, $val2->bindValues());
51
				}
52
53
			}
54
			else if(in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)){
55
56
			}
57
			else{
58
				$where[] = $operator;
59
60
				if(is_null($val2)){
61
					$where[] = 'NULL';
62
				}
63
				else if(is_bool($val2)){
64
					$where[] = $val2 ? 'TRUE' : 'FALSE';
65
				}
66
				else if(in_array(strtolower($val2), ['null', 'false', 'true', 'unknown'])){
67
					$where[] = strtoupper($val2);
68
				}
69
				else if($val2 instanceof StatementInterface){
70
					$where[] = '('.$val2->sql().')';
71
					$values = array_merge($values, $val2->bindValues());
72
				}
73
				else{
74
					if($bind){
75
						$where[] = '?';
76
						$values[] = $val2;
77
					}
78
					else{
79
						if(!empty($val2)){
80
							$where[] = $this->quote($val2);
81
						}
82
					}
83
				}
84
85
			}
86
87
			$this->bindValues = array_merge($this->bindValues, $values);
88
			$this->where[] = [
89
				'join' => in_array($join, ['AND', 'OR', 'XOR']) ? $join : 'AND',
90
				'stmt' => implode(' ', $where),
91
			];
92
93
		}
94
95
		return $this;
96
	}
97
98
}
99