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