From 92ebfc7596c4d91c1a466ef415ad73e3d4cebebf Mon Sep 17 00:00:00 2001 From: Igor Strykhar Date: Fri, 10 Mar 2023 19:29:37 +0300 Subject: [PATCH 1/3] Implement Bindings in Post with types --- src/Query/Degeneration.php | 1 + src/Query/Degeneration/Bindings.php | 5 +++ src/Query/Degeneration/Conditions.php | 4 ++ src/Query/Query.php | 28 ++++++++++++- src/Transport/Http.php | 4 ++ tests/BindingsPostTest.php | 60 +++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/BindingsPostTest.php diff --git a/src/Query/Degeneration.php b/src/Query/Degeneration.php index 08d144d..8ba02d0 100644 --- a/src/Query/Degeneration.php +++ b/src/Query/Degeneration.php @@ -5,4 +5,5 @@ interface Degeneration { public function process($sql); public function bindParams(array $bindings); + public function getBind():array; } \ No newline at end of file diff --git a/src/Query/Degeneration/Bindings.php b/src/Query/Degeneration/Bindings.php index c95be14..8baeda2 100644 --- a/src/Query/Degeneration/Bindings.php +++ b/src/Query/Degeneration/Bindings.php @@ -28,6 +28,11 @@ public function bindParams(array $bindings) } } + public function getBind(): array + { + return $this->bindings; + } + /** * @param string $column * @param mixed $value diff --git a/src/Query/Degeneration/Conditions.php b/src/Query/Degeneration/Conditions.php index d5b3284..348f555 100644 --- a/src/Query/Degeneration/Conditions.php +++ b/src/Query/Degeneration/Conditions.php @@ -22,6 +22,10 @@ public function bindParams(array $bindings) } } + public function getBind(): array + { + return $this->bindings; + } static function __ifsets($matches, $markers) { diff --git a/src/Query/Query.php b/src/Query/Query.php index 75fa7ab..a582c9b 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -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) { diff --git a/src/Transport/Http.php b/src/Transport/Http.php index e0e83ca..be59d49 100644 --- a/src/Transport/Http.php +++ b/src/Transport/Http.php @@ -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); diff --git a/tests/BindingsPostTest.php b/tests/BindingsPostTest.php new file mode 100644 index 0000000..1cc7462 --- /dev/null +++ b/tests/BindingsPostTest.php @@ -0,0 +1,60 @@ +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]); + + } + } + +} From 0458c19cb35b470c0786fdf3bc493becd181e297 Mon Sep 17 00:00:00 2001 From: Igor Strykhar Date: Fri, 10 Mar 2023 19:53:35 +0300 Subject: [PATCH 2/3] Fix #187 Client::partitions --- src/Client.php | 9 +++++++-- tests/ClientTest.php | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index 28c598b..0196260 100644 --- a/src/Client.php +++ b/src/Client.php @@ -817,9 +817,14 @@ public function partitions(string $table, int $limit = null, bool $active = null return $this->select(<<$database, + 'tbl'=>$table + ] )->rowsAsTree('name'); } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index b5040af..2922083 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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')); + } /** * */ From 87b6cc88aa8f1fb149f815f5754c74213710da2c Mon Sep 17 00:00:00 2001 From: Antanas Jasaitis Date: Wed, 24 May 2023 16:39:42 +0300 Subject: [PATCH 3/3] Add array support to post params. --- src/Query/Query.php | 2 +- tests/BindingsPostTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Query/Query.php b/src/Query/Query.php index a582c9b..0c4b979 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -110,7 +110,7 @@ public function getFormat() 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); + return preg_match('#{[\w+]+:[\w+()]+}#',$this->sql); } public function getUrlBindingsParams():array diff --git a/tests/BindingsPostTest.php b/tests/BindingsPostTest.php index 1cc7462..d48ee78 100644 --- a/tests/BindingsPostTest.php +++ b/tests/BindingsPostTest.php @@ -57,4 +57,16 @@ public function testSelectAsKeys() } } + 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')); + } + }