| 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; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 10 | use function implode; |
||
| 11 | use function is_array; |
||
| 12 | |||
| 13 | class Bindings implements Degeneration |
||
| 14 | { |
||
| 15 | /** |
||
|
0 ignored issues
–
show
|
|||
| 16 | * @var array |
||
|
0 ignored issues
–
show
|
|||
| 17 | */ |
||
| 18 | protected $bindings = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param array $bindings |
||
|
0 ignored issues
–
show
|
|||
| 22 | */ |
||
| 23 | 56 | public function bindParams(array $bindings) |
|
|
0 ignored issues
–
show
|
|||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 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) |
||
|
0 ignored issues
–
show
|
|||
| 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 |
|
|
0 ignored issues
–
show
|
|||
| 50 | * @param array $binds |
||
|
0 ignored issues
–
show
|
|||
| 51 | 54 | * @param string $pattern |
|
| 52 | 24 | * @return string |
|
| 53 | 23 | */ |
|
| 54 | public function compile_binds($sql, $binds,$pattern) |
||
|
0 ignored issues
–
show
|
|||
| 55 | { |
||
| 56 | 6 | return preg_replace_callback($pattern, function($m) use ($binds){ |
|
|
0 ignored issues
–
show
|
|||
| 57 | 54 | if(isset($binds[$m[1]])){ // If it exists in our array |
|
|
0 ignored issues
–
show
|
|||
| 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 |
|
|
0 ignored issues
–
show
|
|||
| 69 | 55 | * @return mixed |
|
| 70 | 55 | */ |
|
| 71 | 24 | public function process($sql) |
|
|
0 ignored issues
–
show
|
|||
| 72 | 6 | { |
|
| 73 | $bindFormatted=[]; |
||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 1 space but found 0 spaces
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||
| 74 | 6 | $bindRaw=[]; |
|
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 0 spaces
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||
| 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) { |
||
|
0 ignored issues
–
show
|
|||
| 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; |
||
|
0 ignored issues
–
show
|
|||
| 94 | } |
||
| 95 | |||
| 96 | 54 | if ($valueSet !== null) { |
|
|
0 ignored issues
–
show
|
|||
| 97 | $bindRaw[$key]=$valueSet; |
||
|
0 ignored issues
–
show
|
|||
| 98 | } |
||
| 99 | } |
||
| 100 | 54 | ||
| 101 | for ($loop=0;$loop<2;$loop++) |
||
|
0 ignored issues
–
show
|
|||
| 102 | 54 | { |
|
| 103 | // dipping in binds |
||
| 104 | 54 | // example ['A' => '{B}' , 'B'=>':C','C'=>123] |
|
| 105 | $sql=$this->compile_binds($sql,$bindRaw,'#{([\w+]+)}#'); |
||
|
0 ignored issues
–
show
|
|||
| 106 | } |
||
| 107 | $sql=$this->compile_binds($sql,$bindFormatted,'#:([\w+]+)#'); |
||
|
0 ignored issues
–
show
|
|||
| 108 | |||
| 109 | return $sql; |
||
| 110 | } |
||
| 111 | } |
||
| 112 |