1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ClickHouseDB\Query\Degeneration; |
6
|
|
|
|
7
|
|
|
use ClickHouseDB\Exception\UnsupportedParameterType; |
8
|
|
|
use ClickHouseDB\Query\Degeneration; |
9
|
|
|
use DateTimeInterface; |
10
|
|
|
use function array_map; |
11
|
|
|
use function implode; |
12
|
|
|
use function is_array; |
13
|
|
|
use function is_bool; |
14
|
|
|
use function is_callable; |
15
|
|
|
use function is_float; |
16
|
|
|
use function is_int; |
17
|
|
|
use function is_object; |
18
|
|
|
use function is_string; |
19
|
|
|
use function sprintf; |
20
|
|
|
|
21
|
|
|
class Bindings implements Degeneration |
22
|
|
|
{ |
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @var array |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
protected $bindings = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $bindings |
|
|
|
|
30
|
|
|
*/ |
31
|
57 |
|
public function bindParams(array $bindings) |
|
|
|
|
32
|
|
|
{ |
33
|
57 |
|
$this->bindings = []; |
34
|
57 |
|
foreach ($bindings as $column => $value) { |
35
|
22 |
|
$this->bindParam($column, $value); |
36
|
|
|
} |
37
|
57 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $column |
41
|
|
|
* @param mixed $value |
42
|
|
|
*/ |
43
|
22 |
|
public function bindParam($column, $value) |
|
|
|
|
44
|
|
|
{ |
45
|
22 |
|
$this->bindings[$column] = $value; |
46
|
22 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Escape an string |
50
|
|
|
* |
51
|
|
|
* @param string $value |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
17 |
|
private function escapeString($value) |
|
|
|
|
55
|
|
|
{ |
56
|
17 |
|
return addslashes($value); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Binds a list of values to the corresponding parameters. |
61
|
|
|
* This is similar to [[bindValue()]] except that it binds multiple values at a time. |
62
|
|
|
* |
63
|
|
|
* @param string $sql |
64
|
|
|
* @param array $binds |
|
|
|
|
65
|
|
|
* @param string $pattern |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
57 |
|
public function compile_binds($sql, $binds,$pattern) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
return preg_replace_callback($pattern, function($m) use ($binds){ |
|
|
|
|
71
|
21 |
|
if(isset($binds[$m[1]])){ // If it exists in our array |
|
|
|
|
72
|
21 |
|
return $binds[$m[1]]; // Then replace it from our array |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
return $m[0]; // Otherwise return the whole match (basically we won't change it) |
76
|
57 |
|
}, $sql); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Compile Bindings |
81
|
|
|
* |
82
|
|
|
* @param string $sql |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
57 |
|
public function process($sql) |
|
|
|
|
86
|
|
|
{ |
87
|
57 |
|
$bindFormatted=[]; |
|
|
|
|
88
|
57 |
|
$bindRaw=[]; |
|
|
|
|
89
|
57 |
|
foreach ($this->bindings as $key => $value) { |
90
|
22 |
|
if (is_array($value)) { |
91
|
6 |
|
$valueSet = implode(', ', $value); |
92
|
|
|
|
93
|
6 |
|
$values = array_map( |
94
|
|
|
function ($value) { |
95
|
6 |
|
return $this->formatParameter($value); |
96
|
6 |
|
}, |
97
|
6 |
|
$value |
98
|
|
|
); |
99
|
|
|
|
100
|
6 |
|
$formattedParameter = implode(',', $values); |
101
|
|
|
} else { |
102
|
18 |
|
$valueSet = $value; |
103
|
18 |
|
$formattedParameter = $this->formatParameter($value); |
104
|
|
|
} |
105
|
|
|
|
106
|
21 |
|
if ($formattedParameter !== null) { |
107
|
21 |
|
$bindFormatted[$key]=$formattedParameter; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
21 |
|
if ($valueSet !== null) { |
|
|
|
|
111
|
21 |
|
$bindRaw[$key]=$valueSet; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
57 |
|
for ($loop=0;$loop<2;$loop++) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
// dipping in binds |
118
|
|
|
// example ['A' => '{B}' , 'B'=>':C','C'=>123] |
119
|
57 |
|
$sql=$this->compile_binds($sql,$bindRaw,'#{([\w+]+)}#'); |
|
|
|
|
120
|
|
|
} |
121
|
57 |
|
$sql=$this->compile_binds($sql,$bindFormatted,'#:([\w+]+)#'); |
|
|
|
|
122
|
|
|
|
123
|
57 |
|
return $sql; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param mixed $value |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
22 |
|
private function formatParameter($value) |
131
|
|
|
{ |
132
|
22 |
|
if ($value instanceof DateTimeInterface) { |
133
|
1 |
|
$value = $value->format('Y-m-d H:i:s'); |
134
|
|
|
} |
135
|
|
|
|
136
|
22 |
|
if (is_float($value) || is_int($value) || is_bool($value) || $value === null) { |
137
|
9 |
|
return $value; |
138
|
|
|
} |
139
|
|
|
|
140
|
18 |
|
if (is_object($value) && is_callable([$value, '__toString'])) { |
141
|
1 |
|
$value = (string) $value; |
142
|
|
|
} |
143
|
|
|
|
144
|
18 |
|
if (is_string($value)) { |
145
|
17 |
|
return $this->formatStringParameter($this->escapeString($value)); |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
throw UnsupportedParameterType::new($value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
17 |
|
private function formatStringParameter($value) |
|
|
|
|
155
|
|
|
{ |
156
|
17 |
|
return sprintf("'%s'", $value); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|