Skip to content

Commit 1c9aa5d

Browse files
authored
Merge pull request #75 from ydb-platform/check-support-data-types
Check support data types
2 parents 3258ddd + 6417e26 commit 1c9aa5d

File tree

5 files changed

+215
-6
lines changed

5 files changed

+215
-6
lines changed

src/QueryResult.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace YdbPlatform\Ydb;
44

5+
use DateTime;
6+
57
class QueryResult
68
{
79
protected $columns = [];
@@ -192,7 +194,7 @@ protected function fillRows($rows)
192194
break;
193195

194196
case 'TIMESTAMP':
195-
$_row[$column['name']] = is_numeric($value) ? date('Y-m-d H:i:s', $value / 1000000) : $value;
197+
$_row[$column['name']] = is_numeric($value) ? date('Y-m-d H:i:s', $value/1000000) : $value;
196198
break;
197199

198200
case 'DATETIME':

src/Traits/TypeValueHelpersTrait.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
use YdbPlatform\Ydb\Types\ListType;
2424
use YdbPlatform\Ydb\Types\UintType;
2525
use YdbPlatform\Ydb\Types\Utf8Type;
26+
use YdbPlatform\Ydb\Types\Int8Type;
27+
use YdbPlatform\Ydb\Types\Int16Type;
2628
use YdbPlatform\Ydb\Types\Int64Type;
2729
use YdbPlatform\Ydb\Types\FloatType;
2830
use YdbPlatform\Ydb\Types\TupleType;
2931
use YdbPlatform\Ydb\Types\DoubleType;
32+
use YdbPlatform\Ydb\Types\Uint8Type;
33+
use YdbPlatform\Ydb\Types\Uint16Type;
3034
use YdbPlatform\Ydb\Types\Uint64Type;
3135
use YdbPlatform\Ydb\Types\StringType;
3236
use YdbPlatform\Ydb\Types\StructType;
@@ -356,4 +360,4 @@ protected function convertKeyRange(array $ranges = [])
356360
}
357361
return $ranges;
358362
}
359-
}
363+
}

src/Types/IntType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ protected function getYdbValue()
9494
*/
9595
protected function normalizeValue($value)
9696
{
97-
if ($value < 0)
98-
{
99-
$this->unsigned = true;
100-
}
97+
// if ($value < 0)
98+
// {
99+
// $this->unsigned = true;
100+
// }
101101
return (int)$value;
102102
}
103103

src/Types/TimestampType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
class TimestampType extends DatetimeType
99
{
10+
11+
protected $ydb_key_name = "uint64_value";
12+
13+
protected $ydb_type = "TIMESTAMP";
1014
/**
1115
* @inherit
1216
*/

tests/CheckTypeTest.php

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
7+
use YdbPlatform\Ydb\Types\BoolType;
8+
use YdbPlatform\Ydb\Types\DatetimeType;
9+
use YdbPlatform\Ydb\Types\DateType;
10+
use YdbPlatform\Ydb\Types\DoubleType;
11+
use YdbPlatform\Ydb\Types\FloatType;
12+
use YdbPlatform\Ydb\Types\Int16Type;
13+
use YdbPlatform\Ydb\Types\Int32Type;
14+
use YdbPlatform\Ydb\Types\Int64Type;
15+
use YdbPlatform\Ydb\Types\Int8Type;
16+
use YdbPlatform\Ydb\Types\JsonType;
17+
use YdbPlatform\Ydb\Types\StringType;
18+
use YdbPlatform\Ydb\Types\TimestampType;
19+
use YdbPlatform\Ydb\Types\Uint16Type;
20+
use YdbPlatform\Ydb\Types\Uint32Type;
21+
use YdbPlatform\Ydb\Types\Uint64Type;
22+
use YdbPlatform\Ydb\Types\Uint8Type;
23+
use YdbPlatform\Ydb\Types\Utf8Type;
24+
use YdbPlatform\Ydb\Ydb;
25+
26+
class CheckTypeTest extends TestCase{
27+
public function test(){
28+
$config = [
29+
30+
// Database path
31+
'database' => '/local',
32+
33+
// Database endpoint
34+
'endpoint' => 'localhost:2136',
35+
36+
// Auto discovery (dedicated server only)
37+
'discovery' => false,
38+
39+
// IAM config
40+
'iam_config' => [
41+
'insecure' => true,
42+
],
43+
44+
'credentials' => new AnonymousAuthentication()
45+
];
46+
47+
$checkTypes = [
48+
"Timestamp" => [
49+
"class" => TimestampType::class,
50+
"values" => [
51+
"2023-06-14 17:12:15.000001"
52+
]
53+
],
54+
"Bool" => [
55+
"class" => BoolType::class,
56+
"values" => [
57+
true, false
58+
]
59+
],
60+
"Int8" => [
61+
"class" => Int8Type::class,
62+
"values" => [
63+
-1*pow(2,7), 0, pow(2,7)-1
64+
]
65+
],
66+
"Uint8" => [
67+
"class" => Uint8Type::class,
68+
"values" => [
69+
0, pow(2,8)-1
70+
]
71+
],
72+
"Int16" => [
73+
"class" => Int16Type::class,
74+
"values" => [
75+
-1*pow(2,15), 0, pow(2,15)-1
76+
]
77+
],
78+
"Uint16" => [
79+
"class" => Uint16Type::class,
80+
"values" => [
81+
0, pow(2,16)-1
82+
]
83+
],
84+
"Int32" => [
85+
"class" => Int32Type::class,
86+
"values" => [
87+
-1*pow(2,31), 0, pow(2,31)-1
88+
]
89+
],
90+
"Uint32" => [
91+
"class" => Uint32Type::class,
92+
"values" => [
93+
0, pow(2,32)-1
94+
]
95+
],
96+
"Int64" => [
97+
"class" => Int64Type::class,
98+
"values" => [
99+
-1*pow(2,63), 0, PHP_INT_MIN, 0x7FFFFFFFFFFFFFFF // 2^63 -1
100+
]
101+
],
102+
"Uint64" => [
103+
"class" => Uint64Type::class,
104+
"values" => [
105+
0, 1<<64 -1 // 2^64 - 1
106+
]
107+
],
108+
"Float" => [
109+
"class" => FloatType::class,
110+
"values" => [
111+
345.34534
112+
]
113+
],
114+
"Double" => [
115+
"class" => DoubleType::class,
116+
"values" => [
117+
-345.3453453745
118+
]
119+
],
120+
"String" => [
121+
"class" => StringType::class,
122+
"values" => [
123+
random_bytes(5)
124+
]
125+
],
126+
"Utf8" => [
127+
"class" => Utf8Type::class,
128+
"values" => [
129+
"", "YDB"
130+
]
131+
],
132+
"Json" => [
133+
"class" => JsonType::class,
134+
"values" => [
135+
[], (object)[
136+
"num" => 1
137+
]
138+
]
139+
],
140+
"Date" => [
141+
"class" => DateType::class,
142+
"values" => [
143+
"2023-06-14"
144+
]
145+
],
146+
"Datetime" => [
147+
"class" => DatetimeType::class,
148+
"values" => [
149+
"2023-06-14 17:12:15"
150+
]
151+
],
152+
"Timestamp" => [
153+
"class" => TimestampType::class,
154+
"values" => [
155+
"2023-06-14 17:12:15"
156+
]
157+
]
158+
];
159+
160+
$ydb = new Ydb($config);
161+
$table = $ydb->table();
162+
$session = $table->createSession();
163+
164+
165+
$query = "DECLARE \$v as Struct<x:Int32>; SELECT \$v as val;";
166+
$prepared = $session->prepare($query);
167+
$result = $prepared->execute([
168+
'v' => ["x"=>2],
169+
]);
170+
print_r($result);
171+
172+
$query = "DECLARE \$v as List<Int32>; SELECT \$v as val;";
173+
$prepared = $session->prepare($query);
174+
$result = $prepared->execute([
175+
'v' => [2],
176+
]);
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);
185+
186+
foreach ($checkTypes as $type=>$data) {
187+
$query = "DECLARE \$v as $type; SELECT \$v as val;";
188+
$prepared = $session->prepare($query);
189+
foreach ($data["values"] as $value) {
190+
$result = $prepared->execute([
191+
'v' => $value,
192+
]);
193+
self::assertEquals(strtoupper($type),strtoupper($result->columns()[0]["type"]));
194+
self::assertEquals($value,$result->rows()[0]["val"]);
195+
}
196+
}
197+
198+
}
199+
}

0 commit comments

Comments
 (0)