Skip to content

Commit 92ebfc7

Browse files
committed
Implement Bindings in Post with types
1 parent 026953b commit 92ebfc7

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed

src/Query/Degeneration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ interface Degeneration
55
{
66
public function process($sql);
77
public function bindParams(array $bindings);
8+
public function getBind():array;
89
}

src/Query/Degeneration/Bindings.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public function bindParams(array $bindings)
2828
}
2929
}
3030

31+
public function getBind(): array
32+
{
33+
return $this->bindings;
34+
}
35+
3136
/**
3237
* @param string $column
3338
* @param mixed $value

src/Query/Degeneration/Conditions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public function bindParams(array $bindings)
2222
}
2323
}
2424

25+
public function getBind(): array
26+
{
27+
return $this->bindings;
28+
}
2529

2630
static function __ifsets($matches, $markers)
2731
{

src/Query/Query.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,36 @@ private function applyFormatQuery()
104104
*/
105105
public function getFormat()
106106
{
107-
108107
return $this->format;
109108
}
110109

110+
public function isUseInUrlBindingsParams():bool
111+
{
112+
// 'query=select {p1:UInt8} + {p2:UInt8}' -F "param_p1=3" -F "param_p2=4"
113+
return preg_match('#{[\w+]+:[\w+]+}#',$this->sql);
114+
115+
}
116+
public function getUrlBindingsParams():array
117+
{
118+
$out=[];
119+
if (sizeof($this->degenerations)) {
120+
foreach ($this->degenerations as $degeneration) {
121+
if ($degeneration instanceof Degeneration) {
122+
$params=$degeneration->getBind();
123+
break;
124+
// need first response
125+
}
126+
}
127+
}
128+
if (sizeof($params)) {
129+
foreach ($params as $key=>$value)
130+
{
131+
$out['param_'.$key]=$value;
132+
}
133+
}
134+
return $out;
135+
}
136+
111137
public function toSql()
112138
{
113139
if ($this->format !== null) {

src/Transport/Http.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ private function makeRequest(Query $query, array $urlParams = [], bool $query_as
299299
* Build URL after request making, since URL may contain auth data. This will not matter after the
300300
* implantation of the todo in the `HTTP:newRequest()` method.
301301
*/
302+
303+
if ($query->isUseInUrlBindingsParams()) {
304+
$urlParams=array_replace_recursive($query->getUrlBindingsParams());
305+
}
302306
$url = $this->getUrl($urlParams);
303307
$new->url($url);
304308

tests/BindingsPostTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace ClickHouseDB\Tests;
4+
use PHPUnit\Framework\TestCase;
5+
6+
/**
7+
* @group BindingsPost
8+
*/
9+
final class BindingsPostTest extends TestCase
10+
{
11+
use WithClient;
12+
13+
14+
public function testSelectPostParams()
15+
{
16+
$xpx1=time();
17+
$result = $this->client->select(
18+
'SELECT number+{num_num:UInt8} as numbe_r, {xpx1:UInt32} as xpx1,{zoza:String} as zoza FROM system.numbers LIMIT 6',
19+
[
20+
'num_num'=>123,
21+
'xpx1'=>$xpx1,
22+
'zoza'=>'ziza'
23+
]
24+
);
25+
$this->assertEquals(null,$result->fetchRow('x')); //0
26+
$this->assertEquals(null,$result->fetchRow('y')); //1
27+
$this->assertEquals($xpx1,$result->fetchRow('xpx1')); //2
28+
$this->assertEquals('ziza',$result->fetchRow('zoza'));//3
29+
$this->assertEquals(127,$result->fetchRow('numbe_r')); // 123+4
30+
$this->assertEquals(128,$result->fetchRow('numbe_r')); // 123+5 item
31+
}
32+
33+
public function testSelectAsKeys()
34+
{
35+
// chr(0....255);
36+
$this->client->settings()->set('max_block_size', 100);
37+
38+
$bind['k1']=1;
39+
$bind['k2']=2;
40+
41+
$select=[];
42+
for($z=0;$z<4;$z++)
43+
{
44+
$bind['k'.$z]=$z;
45+
$select[]="{k{$z}:UInt16} as k{$z}";
46+
}
47+
$rows=$this->client->select("SELECT ".implode(",\n",$select),$bind)->rows();
48+
49+
$this->assertNotEmpty($rows);
50+
51+
$row=$rows[0];
52+
53+
for($z=10;$z<4;$z++) {
54+
$this->assertArrayHasKey('k'.$z,$row);
55+
$this->assertEquals($z,$row['k'.$z]);
56+
57+
}
58+
}
59+
60+
}

0 commit comments

Comments
 (0)