File tree Expand file tree Collapse file tree 4 files changed +104
-13
lines changed
Expand file tree Collapse file tree 4 files changed +104
-13
lines changed Original file line number Diff line number Diff line change 11* added retry function
2+ * added optional type in prepare statment
23
34## 1.5.6
45
Original file line number Diff line number Diff line change 2121use YdbPlatform \Ydb \Types \DateType ;
2222use YdbPlatform \Ydb \Types \JsonType ;
2323use YdbPlatform \Ydb \Types \ListType ;
24+ use YdbPlatform \Ydb \Types \OptionalType ;
2425use YdbPlatform \Ydb \Types \UintType ;
2526use YdbPlatform \Ydb \Types \Utf8Type ;
2627use 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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,6 +155,17 @@ 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 );
@@ -174,13 +179,6 @@ public function test(){
174179 'v ' => [2 ],
175180 ]);
176181
177- // $query = "DECLARE \$v as Optional<Int32>; SELECT \$v as val;";
178- // $prepared = $session->prepare($query);
179- // $result = $prepared->execute([
180- // 'v' => 2,
181- // ]);
182- // print_r($result);
183-
184182 foreach ($ checkTypes as $ type =>$ data ) {
185183 $ query = "DECLARE \$v as $ type; SELECT \$v as val; " ;
186184 $ prepared = $ session ->prepare ($ query );
You can’t perform that action at this time.
0 commit comments