Skip to content

Commit

Permalink
Merge pull request #106 from dorantor/master
Browse files Browse the repository at this point in the history
Upgraded Expression proposal
  • Loading branch information
isublimity committed Mar 13, 2019
2 parents 090261c + 74588de commit a58ab5b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 13 deletions.
1 change: 0 additions & 1 deletion include.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
include_once __DIR__ . '/src/Query/WriteToFile.php';
include_once __DIR__ . '/src/Query/WhereInFile.php';
include_once __DIR__ . '/src/Query/Query.php';
include_once __DIR__ . '/src/Query/Expression.php';
// Transport
include_once __DIR__ . '/src/Transport/Http.php';
include_once __DIR__ . '/src/Transport/CurlerRolling.php';
Expand Down
11 changes: 11 additions & 0 deletions src/Query/Expression/Expression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace ClickHouseDB\Query\Expression;

interface Expression
{
public function needsEncoding() : bool;
public function getValue() : string;
}
33 changes: 33 additions & 0 deletions src/Query/Expression/Func/UUIDStringToNum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace ClickHouseDB\Query\Expression\Func;

use ClickHouseDB\Query\Expression\Expression;
use function sprintf;

/**
* Pass expression "as is" to be sent and executed at server.
* P.ex.: `new Expression\Function\UUIDStringToNum('0f372656-6a5b-4727-a4c4-f6357775d926');`
*/
class UUIDStringToNum implements Expression
{
/** @var string */
private $uuid;

public function __construct(string $uuid)
{
$this->uuid = $uuid;
}

public function needsEncoding() : bool
{
return false;
}

public function getValue() : string
{
return sprintf("UUIDStringToNum('%s')", $this->uuid);
}
}
13 changes: 9 additions & 4 deletions src/Query/Expression.php → src/Query/Expression/Raw.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace ClickHouseDB\Query;
namespace ClickHouseDB\Query\Expression;

/**
* Pass expression "as is" to be sent and executed at server.
* P.ex.: `new Expression("UUIDStringToNum('0f372656-6a5b-4727-a4c4-f6357775d926')");`
* P.ex.: `new Expression\Raw("UUIDStringToNum('0f372656-6a5b-4727-a4c4-f6357775d926')");`
*/
class Expression
class Raw implements Expression
{
/** @var string */
private $expression;
Expand All @@ -18,7 +18,12 @@ public function __construct(string $expression)
$this->expression = $expression;
}

public function __toString() : string
public function needsEncoding() : bool
{
return false;
}

public function getValue() : string
{
return $this->expression;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Quote/StrictQuoteLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace ClickHouseDB\Quote;

use ClickHouseDB\Exception\QueryException;
use ClickHouseDB\Query\Expression\Expression;
use ClickHouseDB\Type\NumericType;
use function array_map;
use function is_array;
Expand Down Expand Up @@ -74,6 +75,9 @@ public function quoteValue($row)
if ($value instanceof NumericType) {
$encode = false;
}
if ($value instanceof Expression) {
$encode = $value->needsEncoding();
}

if (is_array($value)) {
// Arrays are formatted as a list of values separated by commas in square brackets.
Expand Down
4 changes: 2 additions & 2 deletions src/Quote/ValueFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace ClickHouseDB\Quote;

use ClickHouseDB\Exception\UnsupportedValueType;
use ClickHouseDB\Query\Expression;
use ClickHouseDB\Query\Expression\Expression;
use ClickHouseDB\Type\Type;
use DateTimeInterface;
use function addslashes;
Expand Down Expand Up @@ -38,7 +38,7 @@ public static function formatValue($value, bool $addQuotes = true)
}

if ($value instanceof Expression) {
return $value;
return $value->getValue();
}

if (is_object($value) && is_callable([$value, '__toString'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,36 @@

declare(strict_types=1);

namespace ClickHouseDB\Tests\Query;
namespace ClickHouseDB\Tests\Query\Expression;

use ClickHouseDB\Query\Expression;
use ClickHouseDB\Quote\FormatLine;
use PHPUnit\Framework\TestCase;

final class ExpressionTest extends TestCase
final class RawTest extends TestCase
{
public function testToString() : void
public function testNeedsEncoding() : void
{
self::assertEquals(
false,
(new Expression\Raw(''))->needsEncoding()
);
}
public function testGetValue() : void
{
$expressionString = "UUIDStringToNum('0f372656-6a5b-4727-a4c4-f6357775d926')";
$expressionObject = new Expression($expressionString);
$expressionObject = new Expression\Raw($expressionString);

self::assertEquals(
$expressionString,
(string) $expressionObject
$expressionObject->getValue()
);
}

public function testExpressionValueForInsert() : void
{
$expressionString = "UUIDStringToNum('0f372656-6a5b-4727-a4c4-f6357775d926')";
$preparedValue = FormatLine::Insert([new Expression($expressionString)]);
$preparedValue = FormatLine::Insert([new Expression\Raw($expressionString)]);

self::assertEquals(
$expressionString,
Expand Down

0 comments on commit a58ab5b

Please sign in to comment.