Skip to content

Commit

Permalink
SQLite: column type can be omitted [Closes #315]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 9, 2025
1 parent 5bac4b5 commit c853e49
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Database/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function getColumns(string $table): array
$columns[] = [
'name' => $column,
'table' => $table,
'nativetype' => strtoupper($typeInfo['type']),
'nativetype' => strtoupper($typeInfo['type'] ?? 'BLOB'),
'size' => $typeInfo['length'],
'nullable' => $row['notnull'] == 0,
'default' => $row['dflt_value'],
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,12 @@ public static function findDuplicates(\PDOStatement $statement): string
}


/** @return array{type: string, length: ?null, scale: ?null, parameters: ?string} */
/** @return array{type: ?string, length: ?null, scale: ?null, parameters: ?string} */
public static function parseColumnType(string $type): array
{
preg_match('/^([^(]+)(?:\((?:(\d+)(?:,(\d+))?|([^)]+))\))?/', $type, $m, PREG_UNMATCHED_AS_NULL);
return [
'type' => $m[1],
'type' => $m[1] ?? null,
'length' => isset($m[2]) ? (int) $m[2] : null,
'scale' => isset($m[3]) ? (int) $m[3] : null,
'parameters' => $m[4] ?? null,
Expand Down
4 changes: 4 additions & 0 deletions tests/Database/Helpers.parseColumnType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ Assert::same(['type' => 'DECIMAL', 'length' => 10, 'scale' => 2, 'parameters' =>
// Test type with additional parameters
$result = Helpers::parseColumnType("ENUM('value1','value2')");
Assert::same(['type' => 'ENUM', 'length' => null, 'scale' => null, 'parameters' => "'value1','value2'"], $result);

// Test omitted type
$result = Helpers::parseColumnType('');
Assert::same(['type' => null, 'length' => null, 'scale' => null, 'parameters' => null], $result);
10 changes: 10 additions & 0 deletions tests/Database/Reflection.columns.sqlite.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ $expectedColumns = [
'autoIncrement' => false,
'primary' => false,
],
'omitted' => [
'name' => 'omitted',
'table' => 'types',
'nativeType' => 'BLOB',
'size' => null,
'nullable' => true,
'default' => null,
'autoIncrement' => false,
'primary' => false,
],
];

Assert::same(
Expand Down
3 changes: 3 additions & 0 deletions tests/Database/ResultSet.normalizeRow.sqlite.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Assert::equal([
'boolean' => true,
'date' => new DateTime('2012-10-13'),
'datetime' => new DateTime('2012-10-13 10:10:10'),
'omitted' => 'a',
], (array) $res->fetch());

Assert::equal([
Expand Down Expand Up @@ -76,6 +77,7 @@ Assert::equal([
'boolean' => false,
'date' => new DateTime('1970-01-01'),
'datetime' => new DateTime('1970-01-01 00:00:00'),
'omitted' => '',
], (array) $res->fetch());

Assert::same([
Expand Down Expand Up @@ -106,6 +108,7 @@ Assert::same([
'boolean' => null,
'date' => null,
'datetime' => null,
'omitted' => null,
], (array) $res->fetch());


Expand Down
12 changes: 8 additions & 4 deletions tests/Database/files/sqlite-nette_test3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ CREATE TEMPORARY TABLE types (
[decimal_10_5] DECIMAL(10,5),
[boolean] BOOLEAN,
[date] DATE,
[datetime] DATETIME
[datetime] DATETIME,
[omitted]
);


Expand Down Expand Up @@ -57,7 +58,8 @@ INSERT INTO types VALUES
1.1, --decimal_10_5
1, --boolean
1350079200, --date
1350115810 --datetime
1350115810, --datetime
'a' --omitted
);


Expand Down Expand Up @@ -88,7 +90,8 @@ INSERT INTO types VALUES (
0.5, --decimal_10_5
0, --boolean
-3600, --date
-3600 --datetime
-3600, --datetime
'' --omitted
);


Expand Down Expand Up @@ -119,5 +122,6 @@ INSERT INTO types VALUES (
NULL, --decimal_10_5
NULL, --boolean
NULL, --date
NULL --datetime
NULL, --datetime
NULL --omitted
);

0 comments on commit c853e49

Please sign in to comment.