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