Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check support data types #75

Merged
merged 3 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 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,7 @@ 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) ? date('Y-m-d H:i:s', $value/1000000) : $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
4 changes: 4 additions & 0 deletions src/Types/TimestampType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

class TimestampType extends DatetimeType
{

protected $ydb_key_name = "uint64_value";

protected $ydb_type = "TIMESTAMP";
/**
* @inherit
*/
Expand Down
199 changes: 199 additions & 0 deletions tests/CheckTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?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" => [
"2023-06-14 17:12:15"
]
]
];

$ydb = new Ydb($config);
$table = $ydb->table();
$session = $table->createSession();


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

$query = "DECLARE \$v as List<Int32>; SELECT \$v as val;";
$prepared = $session->prepare($query);
$result = $prepared->execute([
'v' => [2],
]);
print_r($result);

// $query = "DECLARE \$v as Optional<Int32>; SELECT \$v as val;";
// $prepared = $session->prepare($query);
// $result = $prepared->execute([
// 'v' => 2,
// ]);
// print_r($result);

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"]);
}
}

}
}