From af0e22110f85f08807cf0dc3cca69d0baf632c41 Mon Sep 17 00:00:00 2001 From: Ilya Kharev Date: Thu, 22 Jun 2023 14:04:24 +0300 Subject: [PATCH 1/2] Added OptionalType --- src/Traits/TypeValueHelpersTrait.php | 6 ++ src/Types/OptionalType.php | 86 ++++++++++++++++++++++++++++ tests/CheckTypeTest.php | 26 ++++----- 3 files changed, 103 insertions(+), 15 deletions(-) create mode 100644 src/Types/OptionalType.php diff --git a/src/Traits/TypeValueHelpersTrait.php b/src/Traits/TypeValueHelpersTrait.php index c7826387..e253f9f6 100644 --- a/src/Traits/TypeValueHelpersTrait.php +++ b/src/Traits/TypeValueHelpersTrait.php @@ -21,6 +21,7 @@ use YdbPlatform\Ydb\Types\DateType; use YdbPlatform\Ydb\Types\JsonType; use YdbPlatform\Ydb\Types\ListType; +use YdbPlatform\Ydb\Types\OptionalType; use YdbPlatform\Ydb\Types\UintType; use YdbPlatform\Ydb\Types\Utf8Type; use YdbPlatform\Ydb\Types\Int8Type; @@ -161,6 +162,11 @@ public function valueOfType($value, $type) return (new TupleType($value))->itemTypes(trim(substr($type, 6, -1))); } + else if (substr($_type, 0, 8) === 'OPTIONAL') + { + return (new OptionalType($value))->itemType(trim(substr($type, 9, -1))); + } + throw new Exception('YDB: Unknown [' . $type . '] type.'); } diff --git a/src/Types/OptionalType.php b/src/Types/OptionalType.php new file mode 100644 index 00000000..78130573 --- /dev/null +++ b/src/Types/OptionalType.php @@ -0,0 +1,86 @@ +typeValue($value, $this->itemType)->normalizeValue($value); + } + + /** + * @param string $type + * @return $this + */ + public function itemType($type) + { + $this->itemType = $type; + return $this; + } + + /** + * @inherit + */ + public function toYdbValue() + { + return $this->typeValue($this->value, $this->itemType)->toYdbValue(); + } + + /** + * @inherit + */ + public function getYdbType() + { + $type_id = $this->convertType($this->itemType); + + if ($type_id) + { + return new Type([ + 'optional_type' => new \Ydb\OptionalType([ + 'item' => new Type([ + 'type_id' => $type_id, + ]), + ]), + ]); + } + else + { + $value = $this->typeValue('', $this->itemType); + return new Type([ + 'optional_type' => new \Ydb\OptionalType([ + 'item' => $value->getYdbType(), + ]), + ]); + } + } + + /** + * @inherit + */ + public function toYdbType() + { + return $this->getYdbType(); + } + + /** + * @inherit + */ + protected function getYqlString() + { + $value = $this->typeValue($this->value, $this->itemType)->toYqlString(); + + return '(' . $value . ')'; + } + +} diff --git a/tests/CheckTypeTest.php b/tests/CheckTypeTest.php index e37d2227..3600da01 100644 --- a/tests/CheckTypeTest.php +++ b/tests/CheckTypeTest.php @@ -45,12 +45,6 @@ public function test(){ ]; $checkTypes = [ - "Timestamp" => [ - "class" => TimestampType::class, - "values" => [ - "2023-06-14 17:12:15.000001" - ] - ], "Bool" => [ "class" => BoolType::class, "values" => [ @@ -161,27 +155,29 @@ public function test(){ $table = $ydb->table(); $session = $table->createSession(); + $query = "DECLARE \$v as Optional; SELECT \$v as val;"; + $prepared = $session->prepare($query); + $result = $prepared->execute([ + 'v' => null, + ]); + + $query = "DECLARE \$v as Optional; SELECT \$v as val;"; + $prepared = $session->prepare($query); + $result = $prepared->execute([ + 'v' => 4, + ]); $query = "DECLARE \$v as Struct; SELECT \$v as val;"; $prepared = $session->prepare($query); $result = $prepared->execute([ 'v' => ["x"=>2], ]); - print_r($result); $query = "DECLARE \$v as List; SELECT \$v as val;"; $prepared = $session->prepare($query); $result = $prepared->execute([ 'v' => [2], ]); - print_r($result); - -// $query = "DECLARE \$v as Optional; SELECT \$v as val;"; -// $prepared = $session->prepare($query); -// $result = $prepared->execute([ -// 'v' => 2, -// ]); -// print_r($result); foreach ($checkTypes as $type=>$data) { $query = "DECLARE \$v as $type; SELECT \$v as val;"; From c09bc313a275b3aeb55000f97c491f67bee49ed1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <38153753+ilyakharev@users.noreply.github.com> Date: Thu, 22 Jun 2023 14:11:33 +0300 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42c310b6..9fb4d68f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* added optional type in prepare statment + ## 1.5.6 * added support of php 7.2