Skip to content

Commit af0e221

Browse files
committed
Added OptionalType
1 parent a8b930d commit af0e221

File tree

3 files changed

+103
-15
lines changed

3 files changed

+103
-15
lines changed

src/Traits/TypeValueHelpersTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use YdbPlatform\Ydb\Types\DateType;
2222
use YdbPlatform\Ydb\Types\JsonType;
2323
use YdbPlatform\Ydb\Types\ListType;
24+
use YdbPlatform\Ydb\Types\OptionalType;
2425
use YdbPlatform\Ydb\Types\UintType;
2526
use YdbPlatform\Ydb\Types\Utf8Type;
2627
use YdbPlatform\Ydb\Types\Int8Type;
@@ -161,6 +162,11 @@ public function valueOfType($value, $type)
161162
return (new TupleType($value))->itemTypes(trim(substr($type, 6, -1)));
162163
}
163164

165+
else if (substr($_type, 0, 8) === 'OPTIONAL')
166+
{
167+
return (new OptionalType($value))->itemType(trim(substr($type, 9, -1)));
168+
}
169+
164170
throw new Exception('YDB: Unknown [' . $type . '] type.');
165171
}
166172

src/Types/OptionalType.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Types;
4+
5+
use Ydb\Type;
6+
7+
class OptionalType extends AbstractType
8+
{
9+
/**
10+
* @var string
11+
*/
12+
protected $itemType;
13+
14+
/**
15+
* @inherit
16+
*/
17+
protected function normalizeValue($value)
18+
{
19+
return $this->typeValue($value, $this->itemType)->normalizeValue($value);
20+
}
21+
22+
/**
23+
* @param string $type
24+
* @return $this
25+
*/
26+
public function itemType($type)
27+
{
28+
$this->itemType = $type;
29+
return $this;
30+
}
31+
32+
/**
33+
* @inherit
34+
*/
35+
public function toYdbValue()
36+
{
37+
return $this->typeValue($this->value, $this->itemType)->toYdbValue();
38+
}
39+
40+
/**
41+
* @inherit
42+
*/
43+
public function getYdbType()
44+
{
45+
$type_id = $this->convertType($this->itemType);
46+
47+
if ($type_id)
48+
{
49+
return new Type([
50+
'optional_type' => new \Ydb\OptionalType([
51+
'item' => new Type([
52+
'type_id' => $type_id,
53+
]),
54+
]),
55+
]);
56+
}
57+
else
58+
{
59+
$value = $this->typeValue('', $this->itemType);
60+
return new Type([
61+
'optional_type' => new \Ydb\OptionalType([
62+
'item' => $value->getYdbType(),
63+
]),
64+
]);
65+
}
66+
}
67+
68+
/**
69+
* @inherit
70+
*/
71+
public function toYdbType()
72+
{
73+
return $this->getYdbType();
74+
}
75+
76+
/**
77+
* @inherit
78+
*/
79+
protected function getYqlString()
80+
{
81+
$value = $this->typeValue($this->value, $this->itemType)->toYqlString();
82+
83+
return '(' . $value . ')';
84+
}
85+
86+
}

tests/CheckTypeTest.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ public function test(){
4545
];
4646

4747
$checkTypes = [
48-
"Timestamp" => [
49-
"class" => TimestampType::class,
50-
"values" => [
51-
"2023-06-14 17:12:15.000001"
52-
]
53-
],
5448
"Bool" => [
5549
"class" => BoolType::class,
5650
"values" => [
@@ -161,27 +155,29 @@ public function test(){
161155
$table = $ydb->table();
162156
$session = $table->createSession();
163157

158+
$query = "DECLARE \$v as Optional<Int32>; SELECT \$v as val;";
159+
$prepared = $session->prepare($query);
160+
$result = $prepared->execute([
161+
'v' => null,
162+
]);
163+
164+
$query = "DECLARE \$v as Optional<Int32>; SELECT \$v as val;";
165+
$prepared = $session->prepare($query);
166+
$result = $prepared->execute([
167+
'v' => 4,
168+
]);
164169

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

172176
$query = "DECLARE \$v as List<Int32>; SELECT \$v as val;";
173177
$prepared = $session->prepare($query);
174178
$result = $prepared->execute([
175179
'v' => [2],
176180
]);
177-
print_r($result);
178-
179-
// $query = "DECLARE \$v as Optional<Int32>; SELECT \$v as val;";
180-
// $prepared = $session->prepare($query);
181-
// $result = $prepared->execute([
182-
// 'v' => 2,
183-
// ]);
184-
// print_r($result);
185181

186182
foreach ($checkTypes as $type=>$data) {
187183
$query = "DECLARE \$v as $type; SELECT \$v as val;";

0 commit comments

Comments
 (0)