1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Db; |
4: | |
5: | use Salient\Contract\Core\Entity\Readable; |
6: | use Salient\Contract\Core\Chainable; |
7: | use Salient\Core\Concern\ChainableTrait; |
8: | use Salient\Core\Concern\ReadableTrait; |
9: | use Salient\Utility\Get; |
10: | use LogicException; |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | final class SqlQuery implements Chainable, Readable |
18: | { |
19: | use ChainableTrait; |
20: | use ReadableTrait; |
21: | |
22: | public const AND = 'AND'; |
23: | public const OR = 'OR'; |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | public $Where = []; |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | protected $Values = []; |
64: | |
65: | |
66: | protected $ParamCallback; |
67: | |
68: | |
69: | |
70: | |
71: | public static function getReadableProperties(): array |
72: | { |
73: | return ['Values']; |
74: | } |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | public function __construct(callable $paramCallback) |
81: | { |
82: | $this->ParamCallback = $paramCallback; |
83: | } |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | public function addParam(string $name, $value, ?string &$placeholder) |
93: | { |
94: | if (array_key_exists($name, $this->Values)) { |
95: | throw new LogicException(sprintf('Parameter already added: %s', $name)); |
96: | } |
97: | |
98: | $placeholder = ($this->ParamCallback)($name); |
99: | $this->Values[$name] = $value; |
100: | |
101: | return $this; |
102: | } |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | public function where($condition) |
113: | { |
114: | $this->Where[] = is_callable($condition) ? $condition() : $condition; |
115: | |
116: | return $this; |
117: | } |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | |
126: | public function whereValueInList(string $name, ...$value) |
127: | { |
128: | if (!$value) { |
129: | return $this; |
130: | } |
131: | |
132: | foreach ($value as $val) { |
133: | $expr[] = $this->addNextParam($val); |
134: | } |
135: | $this->Where[] = "$name IN (" . implode(',', $expr) . ')'; |
136: | |
137: | return $this; |
138: | } |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | public function getWhere(?array &$values = null): ?string |
147: | { |
148: | $values = $this->Values; |
149: | $where = $this->buildWhere($this->Where); |
150: | |
151: | return $where === '' |
152: | ? null |
153: | : $where; |
154: | } |
155: | |
156: | |
157: | |
158: | |
159: | private function addNextParam($value): string |
160: | { |
161: | $this->addParam('param_' . count($this->Values), $value, $param); |
162: | |
163: | return $param; |
164: | } |
165: | |
166: | |
167: | |
168: | |
169: | private function buildWhere(array $where): string |
170: | { |
171: | $glue = $where['__'] ?? self::AND; |
172: | if (!is_string($glue)) { |
173: | throw new LogicException(sprintf( |
174: | 'Invalid operator in WHERE condition: %s', |
175: | Get::code($glue), |
176: | )); |
177: | } |
178: | unset($where['__']); |
179: | |
180: | foreach ($where as $condition) { |
181: | if (is_array($condition)) { |
182: | $condition = $this->buildWhere($condition); |
183: | if ($condition === '') { |
184: | continue; |
185: | } |
186: | $sql[] = "($condition)"; |
187: | } else { |
188: | $sql[] = $condition; |
189: | } |
190: | } |
191: | |
192: | return implode(" $glue ", $sql ?? []); |
193: | } |
194: | } |
195: | |