Skip to content

Commit

Permalink
added option convertDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 28, 2024
1 parent a8f561e commit 0d87fc7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/Database/Drivers/Engines/MySQLEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ public function resolveColumnConverter(array $meta, TypeConverter $converter): ?
'TINY' => $meta['length'] === 1 && $converter->convertBoolean
? $converter->toBool(...)
: $converter->toInt(...),
'TIME' => $converter->toInterval(...),
'DATE', 'DATETIME', 'TIMESTAMP' => fn($value): ?\DateTimeInterface => str_starts_with($value, '0000-00')
? null
: $converter->toDateTime($value),
'TIME' => $converter->convertDateTime ? $converter->toInterval(...) : null,
'DATE', 'DATETIME', 'TIMESTAMP' => $converter->convertDateTime ?
(fn($value): ?\DateTimeInterface => str_starts_with($value, '0000-00') ? null : $converter->toDateTime($value))
: null,
default => $converter->resolve($meta['nativeType']),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Drivers/Engines/SQLiteEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function getForeignKeys(string $table): array

public function resolveColumnConverter(array $meta, TypeConverter $converter): ?\Closure
{
return in_array($meta['nativeType'], ['DATE', 'DATETIME'], true)
return $converter->convertDateTime && in_array($meta['nativeType'], ['DATE', 'DATETIME'], true)
? (fn($value): \DateTimeInterface => is_int($value) ? (new DateTime)->setTimestamp($value) : new DateTime($value))
: $converter->resolve($meta['nativeType']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function createDriverFromDsn(
public function createTypeConverter(array &$options): TypeConverter
{
$converter = new TypeConverter;
foreach (['convertBoolean', 'newDateTime'] as $opt) {
foreach (['convertBoolean', 'convertDateTime', 'newDateTime'] as $opt) {
if (isset($options[$opt])) {
$converter->$opt = (bool) $options[$opt];
unset($options[$opt]);
Expand Down
17 changes: 6 additions & 11 deletions src/Database/TypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class TypeConverter
];

public bool $convertBoolean = true;
public bool $convertDateTime = true;
public bool $newDateTime = true;


Expand All @@ -63,12 +64,12 @@ public function resolve(string $nativeType): ?\Closure
{
return match ($this->detectType($nativeType)) {
self::Integer => $this->toInt(...),
self::Decimal => $this->toDecimal(...),
self::Float => $this->toFloat(...),
self::Float
self::Decimal => $this->toFloat(...),

Check failure on line 68 in src/Database/TypeConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected ',' on line 68

Check failure on line 68 in src/Database/TypeConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected T_DOUBLE_ARROW on line 68

Check failure on line 68 in src/Database/TypeConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected T_STRING, expecting T_DOUBLE_ARROW on line 68
self::Boolean => $this->convertBoolean ? $this->toBool(...) : null,

Check failure on line 69 in src/Database/TypeConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected ',' on line 69

Check failure on line 69 in src/Database/TypeConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected T_DOUBLE_ARROW on line 69
self::DateTime, self::Date => $this->toDateTime(...),
self::Time => $this->toTime(...),
self::Interval => self::toInterval(...),
self::DateTime, self::Date => $this->convertDateTime ? $this->toDateTime(...) : null,

Check failure on line 70 in src/Database/TypeConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected ',' on line 70

Check failure on line 70 in src/Database/TypeConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected T_DOUBLE_ARROW on line 70
self::Time => $this->convertDateTime ? $this->toTime(...) : null,

Check failure on line 71 in src/Database/TypeConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected ',' on line 71

Check failure on line 71 in src/Database/TypeConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected T_DOUBLE_ARROW on line 71
self::Interval => $this->convertDateTime ? self::toInterval(...) : null,

Check failure on line 72 in src/Database/TypeConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected ',' on line 72
default => null,
};
}
Expand All @@ -86,12 +87,6 @@ public function toFloat(float|string $value): float
}


public function toDecimal(float|string $value): float
{
return (float) $value;
}


public function toBool(bool|int|string $value): bool
{
return (bool) $value;
Expand Down
21 changes: 21 additions & 0 deletions tests/Database/connection.options.mysql.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test('custom sqlmode', function () {
Assert::equal($desiredMode, $field);
});


test('default convertBoolean', function () {
$connection = connectToDB(['convertBoolean' => null])->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . '/files/mysql-nette_test3.sql');
Expand All @@ -52,6 +53,7 @@ test('convertBoolean = false', function () {
Assert::equal(1, $row->bool);
});


test('default newDateTime', function () {
$connection = connectToDB(['newDateTime' => null])->getConnection();
$field = $connection->fetchField('SELECT NOW()');
Expand All @@ -69,3 +71,22 @@ test('newDateTime = true', function () {
$field = $connection->fetchField('SELECT NOW()');
Assert::type(Nette\Database\DateTime::class, $field);
});


test('default convertDateTime', function () {
$connection = connectToDB(['convertDateTime' => null])->getConnection();
$field = $connection->fetchField('SELECT NOW()');
Assert::type(Nette\Database\DateTime::class, $field);
});

test('convertDateTime = false', function () {
$connection = connectToDB(['convertDateTime' => false])->getConnection();
$field = $connection->fetchField('SELECT NOW()');
Assert::type('string', $field);
});

test('convertDateTime = true', function () {
$connection = connectToDB(['convertDateTime' => true])->getConnection();
$field = $connection->fetchField('SELECT NOW()');
Assert::type(Nette\Database\DateTime::class, $field);
});
24 changes: 24 additions & 0 deletions tests/Database/connection.options.sqlite.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,27 @@ test('formatDateTime', function () {
$engine = $connection->getDatabaseEngine();
Assert::equal('1978-01-23', $engine->formatDateTime(new DateTime('1978-01-23 00:00:00')));
});


test('default convertDateTime', function () {
$connection = connectToDB(['convertDateTime' => null])->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . '/files/sqlite-nette_test3.sql');
$row = $connection->fetch('SELECT * FROM types');
Assert::type(Nette\Database\DateTime::class, $row->date);
Assert::type(Nette\Database\DateTime::class, $row->datetime);
});

test('convertDateTime = false', function () {
$connection = connectToDB(['convertDateTime' => false])->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . '/files/sqlite-nette_test3.sql');
$row = $connection->fetch('SELECT * FROM types');
Assert::type('int', $row->date);
Assert::type('int', $row->datetime);
});

test('convertDateTime = true', function () {
$connection = connectToDB(['convertDateTime' => true])->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . '/files/sqlite-nette_test3.sql');
$row = $connection->fetch('SELECT * FROM types');
Assert::type(Nette\Database\DateTime::class, $row->date);
});

0 comments on commit 0d87fc7

Please sign in to comment.