Skip to content

Commit ed7ae42

Browse files
committed
Support for expressions
1 parent 6a3e657 commit ed7ae42

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

include.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
include_once __DIR__ . '/src/Query/WriteToFile.php';
2121
include_once __DIR__ . '/src/Query/WhereInFile.php';
2222
include_once __DIR__ . '/src/Query/Query.php';
23+
include_once __DIR__ . '/src/Query/Expression.php';
2324
// Transport
2425
include_once __DIR__ . '/src/Transport/Http.php';
2526
include_once __DIR__ . '/src/Transport/CurlerRolling.php';

src/Client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ public function getCountPendingQueue()
463463
/**
464464
* @param mixed[][] $values
465465
* @param string[] $columns
466+
* @return Statement
467+
* @throws Exception\TransportException
466468
*/
467469
public function insert(string $table, array $values, array $columns = []) : Statement
468470
{

src/Query/Expression.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ClickHouseDB\Query;
6+
7+
/**
8+
* Pass expression "as is" to be sent and executed at server.
9+
* P.ex.: `new Expression("UUIDStringToNum('0f372656-6a5b-4727-a4c4-f6357775d926')");`
10+
*/
11+
class Expression
12+
{
13+
/** @var string */
14+
private $expression;
15+
16+
public function __construct(string $expression)
17+
{
18+
$this->expression = $expression;
19+
}
20+
21+
public function __toString() : string
22+
{
23+
return $this->expression;
24+
}
25+
}

src/Quote/ValueFormatter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace ClickHouseDB\Quote;
66

77
use ClickHouseDB\Exception\UnsupportedValueType;
8+
use ClickHouseDB\Query\Expression;
89
use ClickHouseDB\Type\Type;
910
use DateTimeInterface;
1011
use function addslashes;
@@ -36,6 +37,10 @@ public static function formatValue($value, bool $addQuotes = true)
3637
return $value->getValue();
3738
}
3839

40+
if ($value instanceof Expression) {
41+
return $value;
42+
}
43+
3944
if (is_object($value) && is_callable([$value, '__toString'])) {
4045
$value = (string) $value;
4146
}

tests/Query/ExpressionTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ClickHouseDB\Tests\Query;
6+
7+
use ClickHouseDB\Query\Expression;
8+
use ClickHouseDB\Quote\FormatLine;
9+
use PHPUnit\Framework\TestCase;
10+
11+
final class ExpressionTest extends TestCase
12+
{
13+
public function testToString() : void
14+
{
15+
$expressionString = "UUIDStringToNum('0f372656-6a5b-4727-a4c4-f6357775d926')";
16+
$expressionObject = new Expression($expressionString);
17+
18+
self::assertEquals(
19+
$expressionString,
20+
(string) $expressionObject
21+
);
22+
}
23+
24+
public function testExpressionValueForInsert() : void
25+
{
26+
$expressionString = "UUIDStringToNum('0f372656-6a5b-4727-a4c4-f6357775d926')";
27+
$preparedValue = FormatLine::Insert([new Expression($expressionString)]);
28+
29+
self::assertEquals(
30+
$expressionString,
31+
$preparedValue
32+
);
33+
}
34+
}

0 commit comments

Comments
 (0)