Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace ClickHouseDB\Query;

use ClickHouseDB\Exception\QueryException;
use ClickHouseDB\Query\Degeneration\Bindings;
use ClickHouseDB\Query\Degeneration\Conditions;
use function sizeof;

class Query
Expand All @@ -12,6 +14,11 @@ class Query
*/
protected $sql;

/**
* @var string
*/
protected $originalSql;

/**
* @var string|null
*/
Expand Down Expand Up @@ -52,7 +59,7 @@ public function __construct($sql, $degenerations = [])
{
throw new QueryException('Empty Query');
}
$this->sql = $sql;
$this->sql = $this->originalSql = $sql;
$this->degenerations = $degenerations;
}

Expand Down Expand Up @@ -107,10 +114,16 @@ public function getFormat()
return $this->format;
}

/**
* Check if the sql contains bindings like {p1:UInt8}.
*
* Check the original SQL before degeneration to prevent data that matches the same regex by accident causing adding bindings to the url
* For backwards compatibility use the degenerated sql when custom degenerations are found
*/
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->hasCustomDegenerations() ? $this->sql : $this->originalSql);

}
public function getUrlBindingsParams():array
Expand Down Expand Up @@ -160,4 +173,11 @@ public function __toString()
{
return $this->toSql();
}

private function hasCustomDegenerations(): bool
{
return count(array_filter($this->degenerations, function (Degeneration $degeneration) {
return !in_array($degeneration::class, [Conditions::class, Bindings::class]);
})) > 0;
}
}
55 changes: 55 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);

namespace ClickHouseDB\Tests;

use ClickHouseDB\Query\Degeneration;
use ClickHouseDB\Query\Query;
use PHPUnit\Framework\TestCase;

class QueryTest extends TestCase
{
/**
* Test that isUseInUrlBindingsParams returns true when SQL contains a binding with the format {param:type}
*/
public function testIsUseInUrlBindingsParamsWithValidBinding(): void
{
$sql = 'SELECT {p1:UInt8} + {p2:UInt8}';
$query = new Query($sql);

$this->assertEquals('SELECT {p1:UInt8} + {p2:UInt8}', $query->toSql());
$this->assertTrue($query->isUseInUrlBindingsParams());
}

/**
* Test that isUseInUrlBindingsParams returns false when SQL doesn't contain a binding with the format {param:type}
*/
public function testIsUseInUrlBindingsParamsWithNoBinding(): void
{
$sql = 'SELECT 1 + :two';
$degeneration = new Degeneration\Bindings();
$degeneration->bindParams([
'two' => 2,
]);
$query = new Query($sql, [$degeneration]);

$this->assertEquals('SELECT 1 + 2', $query->toSql());
$this->assertFalse($query->isUseInUrlBindingsParams());
}

/**
* Test that isUseInUrlBindingsParams returns false when SQL contains a similar pattern in a binding value
*/
public function testIsUseInUrlBindingsParamsWithSimilarPatternInValue(): void
{
$sql = 'INSERT INTO a (b) VALUES (:simple_bind)';
$degeneration = new Degeneration\Bindings();
$degeneration->bindParams([
'simple_bind' => '{foo:bar}',
]);
$query = new Query($sql, [$degeneration]);

$this->assertEquals("INSERT INTO a (b) VALUES ('{foo:bar}')", $query->toSql());
$this->assertFalse($query->isUseInUrlBindingsParams());
}
}