Support `INT64` type for bigints in drift files

https://github.com/simolus3/drift/issues/2955
This commit is contained in:
Simon Binder 2024-04-15 21:16:32 +02:00
parent ca0dee4f83
commit ac4947f266
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
5 changed files with 29 additions and 4 deletions

View File

@ -116,8 +116,10 @@ to determine the column type based on the declared type name.
Additionally, columns that have the type name `BOOLEAN` or `DATETIME` will have
`bool` or `DateTime` as their Dart counterpart.
Booleans are stored as `INTEGER` (either `0` or `1`). Datetimes are stored as
unix timestamps (`INTEGER`) or ISO-8601 (`TEXT`) depending on a configurable
build option.
unix timestamps (`INTEGER`) or ISO-8601 (`TEXT`) [depending on a configurable build option]({{ '../Dart API/tables.md#datetime-options' | pageUrl }}).
For integers that should be represented as a `BigInt` in Dart (i.e. to have better compatibility with large numbers when compiling to JS),
define the column with the `INT64` type.
Dart enums can automatically be stored by their index by using an `ENUM()` type
referencing the Dart enum class:

View File

@ -290,4 +290,21 @@ class MyType implements CustomSqlType<String> {}
expect(column.sqlType.custom?.dartType.toString(), 'String');
expect(column.sqlType.custom?.expression.toString(), 'MyType()');
});
test('recognizes bigint columns', () async {
final state = TestBackend.inTest({
'a|lib/a.drift': '''
CREATE TABLE foo (
bar INT64 NOT NULL
);
''',
});
final file = await state.analyze('package:a/a.drift');
state.expectNoErrors();
final table = file.analyzedElements.single as DriftTable;
final column = table.columns.single;
expect(column.sqlType.builtin, DriftSqlType.bigInt);
});
}

View File

@ -4,6 +4,7 @@
- Expand support for `IN` expressions, they now support tuples on the left-hand
side and the shorthand syntax for table references and table-valued functions.
- Drift extensions: Allow custom class names for `CREATE VIEW` statements.
- Drift extensions: Support the `INT64` hint for `CREATE TABLE` statements.
## 0.34.1

View File

@ -157,7 +157,11 @@ class SchemaFromCreateTable {
final upper = typeName.toUpperCase();
if (upper.contains('INT')) {
return const ResolvedType(type: BasicType.int);
if (driftExtensions && upper.contains('INT64')) {
return const ResolvedType(type: BasicType.int, hints: [IsBigInt()]);
} else {
return const ResolvedType(type: BasicType.int);
}
}
if (upper.contains('CHAR') ||
upper.contains('CLOB') ||

View File

@ -103,7 +103,7 @@ void main() {
SqlEngine(EngineOptions(driftOptions: const DriftSqlOptions()));
final stmt = engine.parse('''
CREATE TABLE foo (
a BOOL, b DATETIME, c DATE, d BOOLEAN NOT NULL
a BOOL, b DATETIME, c DATE, d BOOLEAN NOT NULL, e INT64
)
''').rootNode;
@ -114,6 +114,7 @@ void main() {
ResolvedType(type: BasicType.int, hints: [IsDateTime()], nullable: true),
ResolvedType(type: BasicType.int, hints: [IsDateTime()], nullable: true),
ResolvedType(type: BasicType.int, hints: [IsBoolean()], nullable: false),
ResolvedType(type: BasicType.int, hints: [IsBigInt()], nullable: true),
]);
});