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