Add BigInt support

This commit is contained in:
westito 2022-05-11 18:51:45 +02:00
parent 5e3de2fb7c
commit 2b4bcc9864
11 changed files with 70 additions and 1 deletions

View File

@ -45,6 +45,9 @@ abstract class Column<T> extends Expression<T> {
/// A column that stores int values.
typedef IntColumn = Column<int?>;
/// A column that stores BigInt values.
typedef BigIntColumn = Column<BigInt?>;
/// A column that stores boolean values. Booleans will be stored as an integer
/// that can either be 0 (false) or 1 (true).
typedef BoolColumn = Column<bool?>;

View File

@ -105,6 +105,14 @@ abstract class Table extends HasResultSet {
@protected
ColumnBuilder<int> integer() => _isGenerated();
/// Use this as the body of a getter to declare a column that holds BigInts.
/// Example (inside the body of a table class):
/// ```
/// BigIntColumn get bigNumber => bigInt()();
/// ```
@protected
ColumnBuilder<BigInt> bigInt() => _isGenerated();
/// Creates a column to store an `enum` class [T].
///
/// In the database, the column will be represented as an integer

View File

@ -31,6 +31,11 @@ class Variable<T> extends Expression<T> {
return Variable(value);
}
/// Creates a variable that holds the specified BigInt.
static Variable<BigInt> withBigInt(BigInt value) {
return Variable(value);
}
/// Creates a variable that holds the specified string.
static Variable<String> withString(String value) {
return Variable(value);

View File

@ -121,6 +121,32 @@ class IntType extends SqlType<int> {
}
}
/// Maps [BigInt] values from and to sql
@_deprecated
class BigIntType extends SqlType<BigInt> {
/// Constant constructor used by the type system
const BigIntType();
@override
String sqlName(SqlDialect dialect) =>
dialect == SqlDialect.sqlite ? 'INTEGER' : 'bigint';
@override
BigInt? mapFromDatabaseResponse(dynamic response) {
if (response == null || response is BigInt?) return response as BigInt?;
if (response is int) return BigInt.from(response);
return BigInt.parse(response.toString());
}
@override
String mapToSqlConstant(BigInt? content) => content?.toString() ?? 'NULL';
@override
BigInt? mapToSqlVariable(BigInt? content) {
return content;
}
}
/// Maps [DateTime] values from and to sql
@_deprecated
class DateTimeType extends SqlType<DateTime> {

View File

@ -158,6 +158,9 @@ extension on TypeProvider {
switch (type) {
case ColumnType.integer:
return intType;
case ColumnType.bigInt:
return intElement.library.getType('BigInt')!.instantiate(
typeArguments: const [], nullabilitySuffix: NullabilitySuffix.none);
case ColumnType.text:
return stringType;
case ColumnType.boolean:

View File

@ -1,6 +1,7 @@
part of 'parser.dart';
const String startInt = 'integer';
const String startBigInt = 'bigInt';
const String startEnum = 'intEnum';
const String startString = 'text';
const String startBool = 'boolean';
@ -10,6 +11,7 @@ const String startReal = 'real';
const Set<String> starters = {
startInt,
startBigInt,
startEnum,
startString,
startBool,
@ -442,6 +444,7 @@ class ColumnParser {
startBool: ColumnType.boolean,
startString: ColumnType.text,
startInt: ColumnType.integer,
startBigInt: ColumnType.bigInt,
startEnum: ColumnType.integer,
startDateTime: ColumnType.datetime,
startBlob: ColumnType.blob,

View File

@ -170,6 +170,7 @@ class ViewParser {
'bool': ColumnType.boolean,
'String': ColumnType.text,
'int': ColumnType.integer,
'BigInt': ColumnType.bigInt,
'DateTime': ColumnType.datetime,
'Uint8List': ColumnType.blob,
'double': ColumnType.real,

View File

@ -1,5 +1,6 @@
import 'package:drift/drift.dart' as m;
import 'package:drift_dev/moor_generator.dart';
import 'package:drift_dev/src/model/column.dart';
import 'package:drift_dev/src/utils/type_converter_hint.dart';
import 'package:sqlparser/sqlparser.dart';
import 'package:sqlparser/utils/find_referenced_tables.dart' as s;
@ -53,6 +54,9 @@ class TypeMapper {
switch (type) {
case ColumnType.integer:
return ResolvedType(type: BasicType.int, hint: overrideHint);
case ColumnType.bigInt:
return ResolvedType(
type: BasicType.int, hint: overrideHint ?? const IsBigInt());
case ColumnType.text:
return ResolvedType(type: BasicType.text, hint: overrideHint);
case ColumnType.boolean:
@ -82,6 +86,8 @@ class TypeMapper {
return ColumnType.boolean;
} else if (type.hint is IsDateTime) {
return ColumnType.datetime;
} else if (type.hint is IsBigInt) {
return ColumnType.bigInt;
}
return ColumnType.integer;
case BasicType.real:

View File

@ -11,7 +11,7 @@ import 'types.dart';
import 'used_type_converter.dart';
/// The column types in sql.
enum ColumnType { integer, text, boolean, datetime, blob, real }
enum ColumnType { integer, bigInt, text, boolean, datetime, blob, real }
/// Name of a column. Contains additional info on whether the name was chosen
/// implicitly (based on the dart getter name) or explicitly (via an named())
@ -122,6 +122,7 @@ class MoorColumn implements HasDeclaration, HasType {
ColumnType.boolean: 'BoolColumn',
ColumnType.text: 'TextColumn',
ColumnType.integer: 'IntColumn',
ColumnType.bigInt: 'BigIntColumn',
ColumnType.datetime: 'DateTimeColumn',
ColumnType.blob: 'BlobColumn',
ColumnType.real: 'RealColumn',
@ -135,6 +136,9 @@ class MoorColumn implements HasDeclaration, HasType {
case ColumnType.integer:
code = 'int';
break;
case ColumnType.bigInt:
code = 'BigInt';
break;
case ColumnType.text:
code = 'String';
break;
@ -161,6 +165,8 @@ class MoorColumn implements HasDeclaration, HasType {
switch (type) {
case ColumnType.integer:
return const IntType();
case ColumnType.bigInt:
return const BigIntType();
case ColumnType.boolean:
return const BoolType();
case ColumnType.datetime:

View File

@ -128,6 +128,7 @@ const Map<ColumnType, String> dartTypeNames = {
ColumnType.boolean: 'bool',
ColumnType.text: 'String',
ColumnType.integer: 'int',
ColumnType.bigInt: 'BigInt',
ColumnType.datetime: 'DateTime',
ColumnType.blob: 'Uint8List',
ColumnType.real: 'double',
@ -139,6 +140,7 @@ const Map<ColumnType, String> createVariable = {
ColumnType.boolean: 'Variable.withBool',
ColumnType.text: 'Variable.withString',
ColumnType.integer: 'Variable.withInt',
ColumnType.bigInt: 'Variable.withBigInt',
ColumnType.datetime: 'Variable.withDateTime',
ColumnType.blob: 'Variable.withBlob',
ColumnType.real: 'Variable.withReal',
@ -148,6 +150,7 @@ const Map<ColumnType, String> sqlTypes = {
ColumnType.boolean: 'BoolType',
ColumnType.text: 'StringType',
ColumnType.integer: 'IntType',
ColumnType.bigInt: 'BigIntType',
ColumnType.datetime: 'DateTimeType',
ColumnType.blob: 'BlobType',
ColumnType.real: 'RealType',

View File

@ -101,6 +101,11 @@ class IsDateTime extends TypeHint {
const IsDateTime();
}
/// Type hint to mark that this type will contain a date time value.
class IsBigInt extends TypeHint {
const IsBigInt();
}
/// Result of resolving a type. This can either have the resolved [type] set,
/// or it can inform the called that it [needsContext] to resolve the type
/// properly. Failure to resolve the type will have the [unknown] flag set.