Skip to content

Commit f4d09e1

Browse files
committed
Added Timestamp
1 parent 05a9021 commit f4d09e1

File tree

6 files changed

+200
-8
lines changed

6 files changed

+200
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
],
1111
"require": {
12-
"php": ">=7.3",
12+
"php": ">=7.2",
1313
"ext-bcmath": "*",
1414
"ext-curl": "*",
1515
"ext-grpc": "*",

src/QueryResult.php

Lines changed: 5 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,9 @@ 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) ?
198+
(DateTime::createFromFormat("U.u", $value/1000000 .".".str_pad($value%1000000,6,"0", STR_PAD_LEFT)))->format('Y-m-d H:i:s.u')
199+
: $value;
196200
break;
197201

198202
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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
class TimestampType extends DatetimeType
99
{
10+
11+
protected $ydb_key_name = "uint64_value";
12+
13+
protected $ydb_type = "TIMESTAMP";
14+
15+
protected static $datetime_format = 'Y-m-d\TH:i:s.u\Z';
1016
/**
1117
* @inherit
1218
*/
@@ -21,6 +27,8 @@ protected function getYqlString()
2127
protected function getYdbValue()
2228
{
2329
$value = new DateTime($this->value);
24-
return $value->getTimestamp() * 1000000;
30+
$x = ($value->format("U")."000000");
31+
$y = $value->format("u");
32+
return $x+$y;
2533
}
2634
}

tests/CheckTypeTest.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
// new TimestampType(43578634985)
156+
// ]
157+
// ]
158+
];
159+
160+
$ydb = new Ydb($config);
161+
$table = $ydb->table();
162+
$session = $table->createSession();
163+
foreach ($checkTypes as $type=>$data) {
164+
$query = "DECLARE \$v as $type; SELECT \$v as val;";
165+
$prepared = $session->prepare($query);
166+
foreach ($data["values"] as $value) {
167+
$result = $prepared->execute([
168+
'v' => $value,
169+
]);
170+
self::assertEquals(strtoupper($type),strtoupper($result->columns()[0]["type"]));
171+
self::assertEquals($value,$result->rows()[0]["val"]);
172+
}
173+
}
174+
175+
}
176+
}

0 commit comments

Comments
 (0)