Skip to content

Commit

Permalink
Merge pull request #189 from antanasja/feat/post-type-bindings
Browse files Browse the repository at this point in the history
Post type bindings support
  • Loading branch information
isublimity committed May 24, 2023
2 parents 026953b + 87b6cc8 commit 14606e4
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,14 @@ public function partitions(string $table, int $limit = null, bool $active = null
return $this->select(<<<CLICKHOUSE
SELECT *
FROM system.parts
WHERE like(table,'%$table%') AND database='$database'$whereActiveClause
WHERE table={tbl:String} AND database = {db:String}
$whereActiveClause
ORDER BY max_date $limitClause
CLICKHOUSE
CLICKHOUSE,
[
'db'=>$database,
'tbl'=>$table
]
)->rowsAsTree('name');
}

Expand Down
1 change: 1 addition & 0 deletions src/Query/Degeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ interface Degeneration
{
public function process($sql);
public function bindParams(array $bindings);
public function getBind():array;
}
5 changes: 5 additions & 0 deletions src/Query/Degeneration/Bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function bindParams(array $bindings)
}
}

public function getBind(): array
{
return $this->bindings;
}

/**
* @param string $column
* @param mixed $value
Expand Down
4 changes: 4 additions & 0 deletions src/Query/Degeneration/Conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function bindParams(array $bindings)
}
}

public function getBind(): array
{
return $this->bindings;
}

static function __ifsets($matches, $markers)
{
Expand Down
28 changes: 27 additions & 1 deletion src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,36 @@ private function applyFormatQuery()
*/
public function getFormat()
{

return $this->format;
}

public function isUseInUrlBindingsParams():bool
{
// 'query=select {p1:UInt8} + {p2:UInt8}' -F "param_p1=3" -F "param_p2=4"
return preg_match('#{[\w+]+:[\w+()]+}#',$this->sql);

}
public function getUrlBindingsParams():array
{
$out=[];
if (sizeof($this->degenerations)) {
foreach ($this->degenerations as $degeneration) {
if ($degeneration instanceof Degeneration) {
$params=$degeneration->getBind();
break;
// need first response
}
}
}
if (sizeof($params)) {
foreach ($params as $key=>$value)
{
$out['param_'.$key]=$value;
}
}
return $out;
}

public function toSql()
{
if ($this->format !== null) {
Expand Down
4 changes: 4 additions & 0 deletions src/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ private function makeRequest(Query $query, array $urlParams = [], bool $query_as
* Build URL after request making, since URL may contain auth data. This will not matter after the
* implantation of the todo in the `HTTP:newRequest()` method.
*/

if ($query->isUseInUrlBindingsParams()) {
$urlParams=array_replace_recursive($query->getUrlBindingsParams());
}
$url = $this->getUrl($urlParams);
$new->url($url);

Expand Down
72 changes: 72 additions & 0 deletions tests/BindingsPostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace ClickHouseDB\Tests;
use PHPUnit\Framework\TestCase;

/**
* @group BindingsPost
*/
final class BindingsPostTest extends TestCase
{
use WithClient;


public function testSelectPostParams()
{
$xpx1=time();
$result = $this->client->select(
'SELECT number+{num_num:UInt8} as numbe_r, {xpx1:UInt32} as xpx1,{zoza:String} as zoza FROM system.numbers LIMIT 6',
[
'num_num'=>123,
'xpx1'=>$xpx1,
'zoza'=>'ziza'
]
);
$this->assertEquals(null,$result->fetchRow('x')); //0
$this->assertEquals(null,$result->fetchRow('y')); //1
$this->assertEquals($xpx1,$result->fetchRow('xpx1')); //2
$this->assertEquals('ziza',$result->fetchRow('zoza'));//3
$this->assertEquals(127,$result->fetchRow('numbe_r')); // 123+4
$this->assertEquals(128,$result->fetchRow('numbe_r')); // 123+5 item
}

public function testSelectAsKeys()
{
// chr(0....255);
$this->client->settings()->set('max_block_size', 100);

$bind['k1']=1;
$bind['k2']=2;

$select=[];
for($z=0;$z<4;$z++)
{
$bind['k'.$z]=$z;
$select[]="{k{$z}:UInt16} as k{$z}";
}
$rows=$this->client->select("SELECT ".implode(",\n",$select),$bind)->rows();

$this->assertNotEmpty($rows);

$row=$rows[0];

for($z=10;$z<4;$z++) {
$this->assertArrayHasKey('k'.$z,$row);
$this->assertEquals($z,$row['k'.$z]);

}
}

public function testArrayAsPostParam()
{
$arr = [1,3,6];
$result = $this->client->select(
'SELECT {arr:Array(UInt8)} as arr',
[
'arr'=>json_encode($arr)
]
);
$this->assertEquals($arr, $result->fetchRow('arr'));
}

}
6 changes: 6 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,12 @@ public function testSelectAsync()
$this->assertEquals(2, $state2->fetchOne('ping'));
}

public function testPartsInfo()
{
$this->create_table_summing_url_views();
$this->insert_data_table_summing_url_views();
$this->assertIsArray($this->client->partitions('summing_url_views'));
}
/**
*
*/
Expand Down

0 comments on commit 14606e4

Please sign in to comment.