1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ClickHouseDB\Query\Degeneration; |
6
|
|
|
|
7
|
|
|
use ClickHouseDB\Exception\UnsupportedValueType; |
|
|
|
|
8
|
|
|
use ClickHouseDB\Query\Degeneration; |
9
|
|
|
use ClickHouseDB\Quote\ValueFormatter; |
10
|
|
|
use ClickHouseDB\Type\UInt64; |
|
|
|
|
11
|
|
|
use DateTimeInterface; |
|
|
|
|
12
|
|
|
use function array_map; |
13
|
|
|
use function implode; |
14
|
|
|
use function is_array; |
15
|
|
|
use function is_bool; |
|
|
|
|
16
|
|
|
use function is_callable; |
|
|
|
|
17
|
|
|
use function is_float; |
|
|
|
|
18
|
|
|
use function is_int; |
|
|
|
|
19
|
|
|
use function is_object; |
|
|
|
|
20
|
|
|
use function is_string; |
|
|
|
|
21
|
|
|
use function sprintf; |
|
|
|
|
22
|
|
|
|
23
|
|
|
class Bindings implements Degeneration |
24
|
|
|
{ |
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @var array |
|
|
|
|
27
|
|
|
*/ |
28
|
|
|
protected $bindings = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $bindings |
|
|
|
|
32
|
|
|
*/ |
33
|
51 |
|
public function bindParams(array $bindings) |
|
|
|
|
34
|
|
|
{ |
35
|
51 |
|
$this->bindings = []; |
36
|
51 |
|
foreach ($bindings as $column => $value) { |
37
|
22 |
|
$this->bindParam($column, $value); |
38
|
|
|
} |
39
|
51 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $column |
43
|
|
|
* @param mixed $value |
44
|
|
|
*/ |
45
|
22 |
|
public function bindParam($column, $value) |
|
|
|
|
46
|
|
|
{ |
47
|
22 |
|
$this->bindings[$column] = $value; |
48
|
22 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Binds a list of values to the corresponding parameters. |
52
|
|
|
* This is similar to [[bindValue()]] except that it binds multiple values at a time. |
53
|
|
|
* |
54
|
|
|
* @param string $sql |
55
|
|
|
* @param array $binds |
|
|
|
|
56
|
|
|
* @param string $pattern |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
49 |
|
public function compile_binds($sql, $binds,$pattern) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
return preg_replace_callback($pattern, function($m) use ($binds){ |
|
|
|
|
62
|
22 |
|
if(isset($binds[$m[1]])){ // If it exists in our array |
|
|
|
|
63
|
21 |
|
return $binds[$m[1]]; // Then replace it from our array |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
return $m[0]; // Otherwise return the whole match (basically we won't change it) |
67
|
49 |
|
}, $sql); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Compile Bindings |
72
|
|
|
* |
73
|
|
|
* @param string $sql |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
50 |
|
public function process($sql) |
|
|
|
|
77
|
|
|
{ |
78
|
50 |
|
$bindFormatted=[]; |
|
|
|
|
79
|
50 |
|
$bindRaw=[]; |
|
|
|
|
80
|
50 |
|
foreach ($this->bindings as $key => $value) { |
81
|
22 |
|
if (is_array($value)) { |
82
|
6 |
|
$valueSet = implode(', ', $value); |
83
|
|
|
|
84
|
6 |
|
$values = array_map( |
85
|
|
|
function ($value) { |
86
|
6 |
|
return ValueFormatter::formatValue($value); |
87
|
6 |
|
}, |
88
|
6 |
|
$value |
89
|
|
|
); |
90
|
|
|
|
91
|
6 |
|
$formattedParameter = implode(',', $values); |
92
|
|
|
} else { |
93
|
18 |
|
$valueSet = $value; |
94
|
18 |
|
$formattedParameter = ValueFormatter::formatValue($value); |
95
|
|
|
} |
96
|
|
|
|
97
|
21 |
|
if ($formattedParameter !== null) { |
98
|
21 |
|
$bindFormatted[$key]=$formattedParameter; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
21 |
|
if ($valueSet !== null) { |
|
|
|
|
102
|
21 |
|
$bindRaw[$key]=$valueSet; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
49 |
|
for ($loop=0;$loop<2;$loop++) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
// dipping in binds |
109
|
|
|
// example ['A' => '{B}' , 'B'=>':C','C'=>123] |
110
|
49 |
|
$sql=$this->compile_binds($sql,$bindRaw,'#{([\w+]+)}#'); |
|
|
|
|
111
|
|
|
} |
112
|
49 |
|
$sql=$this->compile_binds($sql,$bindFormatted,'#:([\w+]+)#'); |
|
|
|
|
113
|
|
|
|
114
|
49 |
|
return $sql; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|