|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.7 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Adapters\Idiorm\Statements; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Exceptions\DatabaseException; |
|
18
|
|
|
use Quantum\Libraries\Database\Contracts\DbalInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Trait Criteria |
|
22
|
|
|
* @package Quantum\Libraries\Database |
|
23
|
|
|
*/ |
|
24
|
|
|
trait Criteria |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritDoc |
|
29
|
|
|
* @throws DatabaseException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function criteria(string $column, string $operator, $value = null): DbalInterface |
|
32
|
|
|
{ |
|
33
|
|
|
if (!key_exists($operator, $this->operators)) { |
|
34
|
|
|
throw DatabaseException::operatorNotSupported($operator); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->addCriteria($column, $operator, $value, $this->operators[$operator]); |
|
38
|
|
|
return $this; |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritDoc |
|
43
|
|
|
* @throws DatabaseException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function criterias(...$criterias): DbalInterface |
|
46
|
|
|
{ |
|
47
|
|
|
foreach ($criterias as $criteria) { |
|
48
|
|
|
if (isset($criteria[0]) && is_array($criteria[0])) { |
|
49
|
|
|
$this->orCriteria($criteria); |
|
50
|
|
|
} else { |
|
51
|
|
|
$this->criteria(...$criteria); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritDoc |
|
60
|
|
|
* @throws DatabaseException |
|
61
|
|
|
*/ |
|
62
|
|
|
public function having(string $column, string $operator, string $value = null): DbalInterface |
|
63
|
|
|
{ |
|
64
|
|
|
if (!key_exists($operator, $this->operators)) { |
|
65
|
|
|
throw DatabaseException::operatorNotSupported($operator); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$func = $this->operators[$operator]; |
|
69
|
|
|
$this->getOrmModel()->$func($column, $value); |
|
|
|
|
|
|
70
|
|
|
return $this; |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritDoc |
|
75
|
|
|
* @throws DatabaseException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function isNull(string $column): DbalInterface |
|
78
|
|
|
{ |
|
79
|
|
|
$this->getOrmModel()->where_null($column); |
|
80
|
|
|
return $this; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @inheritDoc |
|
85
|
|
|
* @throws DatabaseException |
|
86
|
|
|
*/ |
|
87
|
|
|
public function isNotNull(string $column): DbalInterface |
|
88
|
|
|
{ |
|
89
|
|
|
$this->getOrmModel()->where_not_null($column); |
|
90
|
|
|
return $this; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Adds Criteria |
|
95
|
|
|
* @param string $column |
|
96
|
|
|
* @param string $operator |
|
97
|
|
|
* @param mixed $value |
|
98
|
|
|
* @param string|null $func |
|
99
|
|
|
* @throws DatabaseException |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function addCriteria(string $column, string $operator, $value, string $func = null) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($operator == '#=#') { |
|
104
|
|
|
$this->getOrmModel()->where_raw($column . ' = ' . $value); |
|
105
|
|
|
} else if (is_array($value) && count($value) == 1 && key($value) == 'fn') { |
|
106
|
|
|
$this->getOrmModel()->where_raw($column . ' ' . $operator . ' ' . $value['fn']); |
|
107
|
|
|
} else { |
|
108
|
|
|
$this->getOrmModel()->$func($column, $value); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Adds one or more OR criteria in brackets |
|
114
|
|
|
* @param array $orCriterias |
|
115
|
|
|
* @throws DatabaseException |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function orCriteria(array $orCriterias) |
|
118
|
|
|
{ |
|
119
|
|
|
$clause = ''; |
|
120
|
|
|
$params = []; |
|
121
|
|
|
|
|
122
|
|
|
foreach ($orCriterias as $index => $criteria) { |
|
123
|
|
|
if ($index == 0) { |
|
124
|
|
|
$clause .= '('; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$clause .= '`' . $criteria[0] . '` ' . $criteria[1] . ' ?'; |
|
128
|
|
|
|
|
129
|
|
|
if ($index == array_key_last($orCriterias)) { |
|
130
|
|
|
$clause .= ')'; |
|
131
|
|
|
} else { |
|
132
|
|
|
$clause .= ' OR '; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$params[] = $criteria[2]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$this->getOrmModel()->where_raw($clause, $params); |
|
139
|
|
|
} |
|
140
|
|
|
} |