Skip to content

Commit

Permalink
Added Timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyakharev committed Jun 15, 2023
1 parent 05a9021 commit f4d09e1
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"require": {
"php": ">=7.3",
"php": ">=7.2",
"ext-bcmath": "*",
"ext-curl": "*",
"ext-grpc": "*",
Expand Down
6 changes: 5 additions & 1 deletion src/QueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace YdbPlatform\Ydb;

use DateTime;

class QueryResult
{
protected $columns = [];
Expand Down Expand Up @@ -192,7 +194,9 @@ protected function fillRows($rows)
break;

case 'TIMESTAMP':
$_row[$column['name']] = is_numeric($value) ? date('Y-m-d H:i:s', $value / 1000000) : $value;
$_row[$column['name']] = is_numeric($value) ?
(DateTime::createFromFormat("U.u", $value/1000000 .".".str_pad($value%1000000,6,"0", STR_PAD_LEFT)))->format('Y-m-d H:i:s.u')
: $value;
break;

case 'DATETIME':
Expand Down
6 changes: 5 additions & 1 deletion src/Traits/TypeValueHelpersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
use YdbPlatform\Ydb\Types\ListType;
use YdbPlatform\Ydb\Types\UintType;
use YdbPlatform\Ydb\Types\Utf8Type;
use YdbPlatform\Ydb\Types\Int8Type;
use YdbPlatform\Ydb\Types\Int16Type;
use YdbPlatform\Ydb\Types\Int64Type;
use YdbPlatform\Ydb\Types\FloatType;
use YdbPlatform\Ydb\Types\TupleType;
use YdbPlatform\Ydb\Types\DoubleType;
use YdbPlatform\Ydb\Types\Uint8Type;
use YdbPlatform\Ydb\Types\Uint16Type;
use YdbPlatform\Ydb\Types\Uint64Type;
use YdbPlatform\Ydb\Types\StringType;
use YdbPlatform\Ydb\Types\StructType;
Expand Down Expand Up @@ -356,4 +360,4 @@ protected function convertKeyRange(array $ranges = [])
}
return $ranges;
}
}
}
8 changes: 4 additions & 4 deletions src/Types/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ protected function getYdbValue()
*/
protected function normalizeValue($value)
{
if ($value < 0)
{
$this->unsigned = true;
}
// if ($value < 0)
// {
// $this->unsigned = true;
// }
return (int)$value;
}

Expand Down
10 changes: 9 additions & 1 deletion src/Types/TimestampType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

class TimestampType extends DatetimeType
{

protected $ydb_key_name = "uint64_value";

protected $ydb_type = "TIMESTAMP";

protected static $datetime_format = 'Y-m-d\TH:i:s.u\Z';
/**
* @inherit
*/
Expand All @@ -21,6 +27,8 @@ protected function getYqlString()
protected function getYdbValue()
{
$value = new DateTime($this->value);
return $value->getTimestamp() * 1000000;
$x = ($value->format("U")."000000");
$y = $value->format("u");
return $x+$y;
}
}
176 changes: 176 additions & 0 deletions tests/CheckTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

namespace YdbPlatform\Ydb\Test;

use PHPUnit\Framework\TestCase;
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
use YdbPlatform\Ydb\Types\BoolType;
use YdbPlatform\Ydb\Types\DatetimeType;
use YdbPlatform\Ydb\Types\DateType;
use YdbPlatform\Ydb\Types\DoubleType;
use YdbPlatform\Ydb\Types\FloatType;
use YdbPlatform\Ydb\Types\Int16Type;
use YdbPlatform\Ydb\Types\Int32Type;
use YdbPlatform\Ydb\Types\Int64Type;
use YdbPlatform\Ydb\Types\Int8Type;
use YdbPlatform\Ydb\Types\JsonType;
use YdbPlatform\Ydb\Types\StringType;
use YdbPlatform\Ydb\Types\TimestampType;
use YdbPlatform\Ydb\Types\Uint16Type;
use YdbPlatform\Ydb\Types\Uint32Type;
use YdbPlatform\Ydb\Types\Uint64Type;
use YdbPlatform\Ydb\Types\Uint8Type;
use YdbPlatform\Ydb\Types\Utf8Type;
use YdbPlatform\Ydb\Ydb;

class CheckTypeTest extends TestCase{
public function test(){
$config = [

// Database path
'database' => '/local',

// Database endpoint
'endpoint' => 'localhost:2136',

// Auto discovery (dedicated server only)
'discovery' => false,

// IAM config
'iam_config' => [
'insecure' => true,
],

'credentials' => new AnonymousAuthentication()
];

$checkTypes = [
"Timestamp" => [
"class" => TimestampType::class,
"values" => [
"2023-06-14 17:12:15.000001"
]
],
"Bool" => [
"class" => BoolType::class,
"values" => [
true, false
]
],
"Int8" => [
"class" => Int8Type::class,
"values" => [
-1*pow(2,7), 0, pow(2,7)-1
]
],
"Uint8" => [
"class" => Uint8Type::class,
"values" => [
0, pow(2,8)-1
]
],
"Int16" => [
"class" => Int16Type::class,
"values" => [
-1*pow(2,15), 0, pow(2,15)-1
]
],
"Uint16" => [
"class" => Uint16Type::class,
"values" => [
0, pow(2,16)-1
]
],
"Int32" => [
"class" => Int32Type::class,
"values" => [
-1*pow(2,31), 0, pow(2,31)-1
]
],
"Uint32" => [
"class" => Uint32Type::class,
"values" => [
0, pow(2,32)-1
]
],
"Int64" => [
"class" => Int64Type::class,
"values" => [
-1*pow(2,63), 0, PHP_INT_MIN, 0x7FFFFFFFFFFFFFFF // 2^63 -1
]
],
"Uint64" => [
"class" => Uint64Type::class,
"values" => [
0, 1<<64 -1 // 2^64 - 1
]
],
"Float" => [
"class" => FloatType::class,
"values" => [
345.34534
]
],
"Double" => [
"class" => DoubleType::class,
"values" => [
-345.3453453745
]
],
"String" => [
"class" => StringType::class,
"values" => [
random_bytes(5)
]
],
"Utf8" => [
"class" => Utf8Type::class,
"values" => [
"", "YDB"
]
],
"Json" => [
"class" => JsonType::class,
"values" => [
[], (object)[
"num" => 1
]
]
],
"Date" => [
"class" => DateType::class,
"values" => [
"2023-06-14"
]
],
"Datetime" => [
"class" => DatetimeType::class,
"values" => [
"2023-06-14 17:12:15"
]
],
// "Timestamp" => [
// "class" => TimestampType::class,
// "values" => [
// new TimestampType(43578634985)
// ]
// ]
];

$ydb = new Ydb($config);
$table = $ydb->table();
$session = $table->createSession();
foreach ($checkTypes as $type=>$data) {
$query = "DECLARE \$v as $type; SELECT \$v as val;";
$prepared = $session->prepare($query);
foreach ($data["values"] as $value) {
$result = $prepared->execute([
'v' => $value,
]);
self::assertEquals(strtoupper($type),strtoupper($result->columns()[0]["type"]));
self::assertEquals($value,$result->rows()[0]["val"]);
}
}

}
}

0 comments on commit f4d09e1

Please sign in to comment.