Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added OptionalType for prepared quere #79

Merged
merged 3 commits into from
Jun 23, 2023
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* added optional type in prepare statment

## 1.5.6

* added support of php 7.2
Expand Down
6 changes: 6 additions & 0 deletions src/Traits/TypeValueHelpersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.');
}

Expand Down
86 changes: 86 additions & 0 deletions src/Types/OptionalType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace YdbPlatform\Ydb\Types;

use Ydb\Type;

class OptionalType extends AbstractType
{
/**
* @var string
*/
protected $itemType;

/**
* @inherit
*/
protected function normalizeValue($value)
{
return $this->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 . ')';
}

}
26 changes: 11 additions & 15 deletions tests/CheckTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" => [
Expand Down Expand Up @@ -161,27 +155,29 @@ public function test(){
$table = $ydb->table();
$session = $table->createSession();

$query = "DECLARE \$v as Optional<Int32>; SELECT \$v as val;";
$prepared = $session->prepare($query);
$result = $prepared->execute([
'v' => null,
]);

$query = "DECLARE \$v as Optional<Int32>; SELECT \$v as val;";
$prepared = $session->prepare($query);
$result = $prepared->execute([
'v' => 4,
]);

$query = "DECLARE \$v as Struct<x:Int32>; SELECT \$v as val;";
$prepared = $session->prepare($query);
$result = $prepared->execute([
'v' => ["x"=>2],
]);
print_r($result);

$query = "DECLARE \$v as List<Int32>; SELECT \$v as val;";
$prepared = $session->prepare($query);
$result = $prepared->execute([
'v' => [2],
]);
print_r($result);

// $query = "DECLARE \$v as Optional<Int32>; 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;";
Expand Down