Skip to content

Use DateTimeColumn #355

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

Merged
merged 5 commits into from
May 16, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- Enh #350, #351: Use `DbArrayHelper::arrange()` instead of `DbArrayHelper::index()` method (@Tigrov)
- New #348: Realize `Schema::loadResultColumn()` method (@Tigrov)
- New #354: Add `FOR` clause to query (@vjik)
- New #355: Use `DateTimeColumn` class for datetime column types (@Tigrov)

## 1.2.0 March 21, 2024

Expand Down
12 changes: 8 additions & 4 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
'varchar',
'text',
'blob',
'time',
'datetime',
'timestamp',
'datetime',
'datetimetz',
'time',
'timetz',
];

protected const TYPES_WITH_SCALE = [
Expand Down Expand Up @@ -87,10 +89,12 @@ protected function getDbType(ColumnInterface $column): string
ColumnType::TEXT => 'text',
ColumnType::BINARY => 'blob',
ColumnType::UUID => 'blob(16)',
ColumnType::DATETIME => 'datetime',
ColumnType::TIMESTAMP => 'timestamp',
ColumnType::DATE => 'date',
ColumnType::DATETIME => 'datetime',
ColumnType::DATETIMETZ => 'datetimetz',
ColumnType::TIME => 'time',
ColumnType::TIMETZ => 'timetz',
ColumnType::DATE => 'date',
ColumnType::ARRAY => 'json',
ColumnType::STRUCTURED => 'json',
ColumnType::JSON => 'json',
Expand Down
10 changes: 9 additions & 1 deletion src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ final class ColumnFactory extends AbstractColumnFactory
'longtext' => ColumnType::TEXT,
'text' => ColumnType::TEXT,
'blob' => ColumnType::BINARY,
'year' => ColumnType::DATE,
'year' => ColumnType::SMALLINT,
'date' => ColumnType::DATE,
'time' => ColumnType::TIME,
'timetz' => ColumnType::TIMETZ,
'datetime' => ColumnType::DATETIME,
'datetimetz' => ColumnType::DATETIMETZ,
'timestamp' => ColumnType::TIMESTAMP,
'json' => ColumnType::JSON,
];
Expand All @@ -56,6 +58,12 @@ protected function getType(string $dbType, array $info = []): string
'bit', 'tinyint' => isset($info['size']) && $info['size'] === 1
? ColumnType::BOOLEAN
: parent::getType($dbType, $info),
'text' => match ($info['defaultValueRaw'] ?? null) {
'CURRENT_TIMESTAMP' => ColumnType::DATETIMETZ,
'CURRENT_DATE' => ColumnType::DATE,
'CURRENT_TIME' => ColumnType::TIMETZ,
default => parent::getType($dbType, $info),
},
default => parent::getType($dbType, $info),
};
}
Expand Down
60 changes: 29 additions & 31 deletions tests/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Db\Sqlite\Tests;

use DateTimeImmutable;
use DateTimeZone;
use PDO;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Schema\Column\BinaryColumn;
Expand All @@ -12,20 +14,23 @@
use Yiisoft\Db\Schema\Column\IntegerColumn;
use Yiisoft\Db\Schema\Column\JsonColumn;
use Yiisoft\Db\Schema\Column\StringColumn;
use Yiisoft\Db\Sqlite\Column\ColumnBuilder;
use Yiisoft\Db\Sqlite\Connection;
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Tests\AbstractColumnTest;
use Yiisoft\Db\Tests\Common\CommonColumnTest;

use function str_repeat;

/**
* @group sqlite
*/
final class ColumnTest extends AbstractColumnTest
final class ColumnTest extends CommonColumnTest
{
use TestTrait;

protected const COLUMN_BUILDER = ColumnBuilder::class;

private function insertTypeValues(Connection $db): void
{
$command = $db->createCommand();
Expand All @@ -39,6 +44,7 @@ private function insertTypeValues(Connection $db): void
'float_col' => 1.234,
'blob_col' => "\x10\x11\x12",
'timestamp_col' => '2023-07-11 14:50:23',
'timestamp_default' => new DateTimeImmutable('2023-07-11 14:50:23'),
'bool_col' => false,
'bit_col' => 0b0110_0110, // 102
'json_col' => [['a' => 1, 'b' => null, 'c' => [1, 3, 5]]],
Expand All @@ -48,18 +54,24 @@ private function insertTypeValues(Connection $db): void
$command->execute();
}

private function assertResultValues(array $result): void
private function assertTypecastedValues(array $result, bool $allTypecasted = false): void
{
$this->assertSame(1, $result['int_col']);
$this->assertSame(str_repeat('x', 100), $result['char_col']);
$this->assertNull($result['char_col3']);
$this->assertSame(1.234, $result['float_col']);
$this->assertSame("\x10\x11\x12", $result['blob_col']);
$this->assertSame('2023-07-11 14:50:23', $result['timestamp_col']);
$this->assertEquals(new DateTimeImmutable('2023-07-11 14:50:23', new DateTimeZone('UTC')), $result['timestamp_col']);
$this->assertEquals(new DateTimeImmutable('2023-07-11 14:50:23'), $result['timestamp_default']);
$this->assertFalse($result['bool_col']);
$this->assertSame(0b0110_0110, $result['bit_col']);
$this->assertSame([['a' => 1, 'b' => null, 'c' => [1, 3, 5]]], $result['json_col']);
$this->assertSame('[1,2,3,"string",null]', $result['json_text_col']);

if ($allTypecasted) {
$this->assertSame([1, 2, 3, 'string', null], $result['json_text_col']);
} else {
$this->assertSame('[1,2,3,"string",null]', $result['json_text_col']);
}
}

public function testQueryWithTypecasting(): void
Expand All @@ -72,11 +84,11 @@ public function testQueryWithTypecasting(): void

$result = $query->one();

$this->assertResultValues($result);
$this->assertTypecastedValues($result);

$result = $query->all();

$this->assertResultValues($result[0]);
$this->assertTypecastedValues($result[0]);

$db->close();
}
Expand All @@ -91,11 +103,11 @@ public function testCommandWithPhpTypecasting(): void

$result = $command->queryOne();

$this->assertResultValues($result);
$this->assertTypecastedValues($result);

$result = $command->queryAll();

$this->assertResultValues($result[0]);
$this->assertTypecastedValues($result[0]);

$db->close();
}
Expand Down Expand Up @@ -130,33 +142,19 @@ public function testPhpTypeCast(): void
{
$db = $this->getConnection(true);
$schema = $db->getSchema();
$tableSchema = $schema->getTableSchema('type');
$columns = $schema->getTableSchema('type')->getColumns();

$this->insertTypeValues($db);

$query = (new Query($db))->from('type')->one();

$intColPhpType = $tableSchema->getColumn('int_col')?->phpTypecast($query['int_col']);
$charColPhpType = $tableSchema->getColumn('char_col')?->phpTypecast($query['char_col']);
$charCol3PhpType = $tableSchema->getColumn('char_col3')?->phpTypecast($query['char_col3']);
$floatColPhpType = $tableSchema->getColumn('float_col')?->phpTypecast($query['float_col']);
$blobColPhpType = $tableSchema->getColumn('blob_col')?->phpTypecast($query['blob_col']);
$timestampColPhpType = $tableSchema->getColumn('timestamp_col')?->phpTypecast($query['timestamp_col']);
$boolColPhpType = $tableSchema->getColumn('bool_col')?->phpTypecast($query['bool_col']);
$bitColPhpType = $tableSchema->getColumn('bit_col')?->phpTypecast($query['bit_col']);
$jsonColPhpType = $tableSchema->getColumn('json_col')?->phpTypecast($query['json_col']);
$jsonTextColPhpType = $tableSchema->getColumn('json_text_col')?->phpTypecast($query['json_text_col']);

$this->assertSame(1, $intColPhpType);
$this->assertSame(str_repeat('x', 100), $charColPhpType);
$this->assertNull($charCol3PhpType);
$this->assertSame(1.234, $floatColPhpType);
$this->assertSame("\x10\x11\x12", $blobColPhpType);
$this->assertSame('2023-07-11 14:50:23', $timestampColPhpType);
$this->assertFalse($boolColPhpType);
$this->assertSame(0b0110_0110, $bitColPhpType);
$this->assertSame([['a' => 1, 'b' => null, 'c' => [1, 3, 5]]], $jsonColPhpType);
$this->assertSame([1, 2, 3, 'string', null], $jsonTextColPhpType);
$result = [];

foreach ($columns as $columnName => $column) {
$result[$columnName] = $column->phpTypecast($query[$columnName]);
}

$this->assertTypecastedValues($result, true);

$db->close();
}
Expand Down
13 changes: 8 additions & 5 deletions tests/Provider/ColumnFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Yiisoft\Db\Schema\Column\BinaryColumn;
use Yiisoft\Db\Schema\Column\BitColumn;
use Yiisoft\Db\Schema\Column\BooleanColumn;
use Yiisoft\Db\Schema\Column\DatetimeColumn;
use Yiisoft\Db\Schema\Column\DoubleColumn;
use Yiisoft\Db\Schema\Column\IntegerColumn;
use Yiisoft\Db\Schema\Column\JsonColumn;
Expand Down Expand Up @@ -42,11 +43,13 @@ public static function dbTypes(): array
['longtext', ColumnType::TEXT, StringColumn::class],
['text', ColumnType::TEXT, StringColumn::class],
['blob', ColumnType::BINARY, BinaryColumn::class],
['year', ColumnType::DATE, StringColumn::class],
['date', ColumnType::DATE, StringColumn::class],
['time', ColumnType::TIME, StringColumn::class],
['datetime', ColumnType::DATETIME, StringColumn::class],
['timestamp', ColumnType::TIMESTAMP, StringColumn::class],
['year', ColumnType::SMALLINT, IntegerColumn::class],
['date', ColumnType::DATE, DatetimeColumn::class],
['time', ColumnType::TIME, DatetimeColumn::class],
['timetz', ColumnType::TIMETZ, DatetimeColumn::class],
['datetime', ColumnType::DATETIME, DatetimeColumn::class],
['datetimetz', ColumnType::DATETIMETZ, DatetimeColumn::class],
['timestamp', ColumnType::TIMESTAMP, DatetimeColumn::class],
['json', ColumnType::JSON, JsonColumn::class],
];
}
Expand Down
38 changes: 27 additions & 11 deletions tests/Provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Yiisoft\Db\Sqlite\Tests\Provider;

use DateTimeImmutable;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Schema\Column\ArrayColumn;
use Yiisoft\Db\Schema\Column\BinaryColumn;
use Yiisoft\Db\Schema\Column\BitColumn;
use Yiisoft\Db\Schema\Column\BooleanColumn;
use Yiisoft\Db\Schema\Column\DatetimeColumn;
use Yiisoft\Db\Schema\Column\DoubleColumn;
use Yiisoft\Db\Schema\Column\IntegerColumn;
use Yiisoft\Db\Schema\Column\JsonColumn;
Expand Down Expand Up @@ -78,11 +80,19 @@ public static function columns(): array
scale: 2,
defaultValue: 33.22,
),
'timestamp_col' => new StringColumn(
'timestamp_col' => new DatetimeColumn(
ColumnType::TIMESTAMP,
dbType: 'timestamp',
notNull: true,
defaultValue: '2002-01-01 00:00:00',
defaultValue: new DateTimeImmutable('2002-01-01 00:00:00'),
hasTimezone: false,
shouldConvertTimezone: true,
),
'timestamp_default' => new DatetimeColumn(
ColumnType::TIMESTAMP,
dbType: 'timestamp',
notNull: true,
defaultValue: new Expression('CURRENT_TIMESTAMP'),
),
'bool_col' => new BooleanColumn(
dbType: 'tinyint',
Expand All @@ -94,12 +104,6 @@ public static function columns(): array
size: 1,
defaultValue: true,
),
'ts_default' => new StringColumn(
ColumnType::TIMESTAMP,
dbType: 'timestamp',
notNull: true,
defaultValue: new Expression('CURRENT_TIMESTAMP'),
),
'bit_col' => new BitColumn(
dbType: 'bit',
notNull: true,
Expand Down Expand Up @@ -146,12 +150,24 @@ public static function columns(): array
notNull: true,
defaultValue: 'CURRENT_TIMESTAMP',
),
'timestamp_text' => new StringColumn(
ColumnType::TEXT,
'timestamp_text' => new DatetimeColumn(
ColumnType::DATETIMETZ,
dbType: 'text',
notNull: true,
defaultValue: new Expression('CURRENT_TIMESTAMP'),
),
'time_text' => new DatetimeColumn(
ColumnType::TIMETZ,
dbType: 'text',
notNull: true,
defaultValue: new Expression('CURRENT_TIME'),
),
'date_text' => new DatetimeColumn(
ColumnType::DATE,
dbType: 'text',
notNull: true,
defaultValue: new Expression('CURRENT_DATE'),
),
],
'timestamp_default',
],
Expand Down Expand Up @@ -266,7 +282,7 @@ public static function resultColumns(): array
'len' => -1,
'precision' => 0,
]],
[new StringColumn(ColumnType::TIMESTAMP, dbType: 'timestamp', name: 'timestamp_col'), [
[new DatetimeColumn(ColumnType::TIMESTAMP, dbType: 'timestamp', name: 'timestamp_col'), [
'native_type' => 'null',
'pdo_type' => 0,
'sqlite:decl_type' => 'timestamp',
Expand Down
6 changes: 4 additions & 2 deletions tests/Support/Fixture/sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ CREATE TABLE "type" (
blob_col blob,
numeric_col decimal(5,2) DEFAULT '33.22',
timestamp_col timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
timestamp_default TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
bool_col tinyint(1) NOT NULL,
bool_col2 tinyint(1) DEFAULT '1',
ts_default TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
bit_col BIT(8) NOT NULL DEFAULT 130, -- 0b1000_0010
json_col json NOT NULL DEFAULT '{"number":10}',
json_text_col text CHECK(json_text_col IS NULL OR json_valid(json_text_col)) -- for STRICT table
Expand Down Expand Up @@ -173,7 +173,9 @@ CREATE TABLE "notauto_pk" (
CREATE TABLE "timestamp_default" (
id INTEGER PRIMARY KEY,
text_col TEXT NOT NULL DEFAULT 'CURRENT_TIMESTAMP',
timestamp_text TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
timestamp_text TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
time_text TEXT NOT NULL DEFAULT CURRENT_TIME,
date_text TEXT NOT NULL DEFAULT CURRENT_DATE
); -- STRICT

CREATE TABLE "json_type" (
Expand Down