mirror of https://github.com/AMT-Cheif/drift.git
Better integration of floating-point values
This commit is contained in:
parent
02b646524c
commit
9241bbccc1
|
@ -28,7 +28,8 @@ abstract class DateTimeColumn extends Column<DateTime, DateTimeType> {}
|
||||||
abstract class BlobColumn extends Column<Uint8List, BlobType> {}
|
abstract class BlobColumn extends Column<Uint8List, BlobType> {}
|
||||||
|
|
||||||
/// A column that stores floating point numeric values.
|
/// A column that stores floating point numeric values.
|
||||||
abstract class RealColumn extends Column<num, RealType> {}
|
abstract class RealColumn extends Column<double, RealType>
|
||||||
|
implements DoubleExpression {}
|
||||||
|
|
||||||
/// A column builder is used to specify which columns should appear in a table.
|
/// A column builder is used to specify which columns should appear in a table.
|
||||||
/// All of the methods defined in this class and its subclasses are not meant to
|
/// All of the methods defined in this class and its subclasses are not meant to
|
||||||
|
|
|
@ -2,7 +2,10 @@ import 'package:moor/moor.dart';
|
||||||
import 'expression.dart';
|
import 'expression.dart';
|
||||||
|
|
||||||
abstract class IntExpression extends Expression<int, IntType>
|
abstract class IntExpression extends Expression<int, IntType>
|
||||||
with ComparableExpr {}
|
implements ComparableExpr<int, IntType> {}
|
||||||
|
|
||||||
|
abstract class DoubleExpression extends Expression<double, RealType>
|
||||||
|
implements ComparableExpr<double, RealType> {}
|
||||||
|
|
||||||
mixin ComparableExpr<DT, ST extends SqlType<DT>> on Expression<DT, ST> {
|
mixin ComparableExpr<DT, ST extends SqlType<DT>> on Expression<DT, ST> {
|
||||||
/// Returns an expression that is true if this expression is strictly bigger
|
/// Returns an expression that is true if this expression is strictly bigger
|
||||||
|
|
|
@ -37,7 +37,7 @@ class Variable<T, S extends SqlType<T>> extends Expression<T, S> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a variable that holds the specified floating point value.
|
/// Creates a variable that holds the specified floating point value.
|
||||||
static Variable<num, RealType> withReal(num value) {
|
static Variable<double, RealType> withReal(double value) {
|
||||||
return Variable(value);
|
return Variable(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -193,18 +193,25 @@ class GeneratedDateTimeColumn extends GeneratedColumn<DateTime, DateTimeType>
|
||||||
class GeneratedBlobColumn extends GeneratedColumn<Uint8List, BlobType>
|
class GeneratedBlobColumn extends GeneratedColumn<Uint8List, BlobType>
|
||||||
implements BlobColumn {
|
implements BlobColumn {
|
||||||
GeneratedBlobColumn(String $name, String tableName, bool $nullable,
|
GeneratedBlobColumn(String $name, String tableName, bool $nullable,
|
||||||
{String $customConstraints})
|
{String $customConstraints, Expression<Uint8List, BlobType> defaultValue})
|
||||||
: super($name, tableName, $nullable,
|
: super($name, tableName, $nullable,
|
||||||
$customConstraints: $customConstraints);
|
$customConstraints: $customConstraints, defaultValue: defaultValue);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final String typeName = 'BLOB';
|
final String typeName = 'BLOB';
|
||||||
}
|
}
|
||||||
|
|
||||||
class GeneratedRealColumn extends GeneratedColumn<num, RealType>
|
class GeneratedRealColumn extends GeneratedColumn<double, RealType>
|
||||||
|
with ComparableExpr
|
||||||
implements RealColumn {
|
implements RealColumn {
|
||||||
GeneratedRealColumn(String $name, String tableName, bool $nullable)
|
GeneratedRealColumn(
|
||||||
: super($name, tableName, $nullable);
|
String $name,
|
||||||
|
String tableName,
|
||||||
|
bool $nullable, {
|
||||||
|
Expression<double, RealType> defaultValue,
|
||||||
|
String $customConstraints,
|
||||||
|
}) : super($name, tableName, $nullable,
|
||||||
|
defaultValue: defaultValue, $customConstraints: $customConstraints);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get typeName => 'REAL';
|
String get typeName => 'REAL';
|
||||||
|
|
|
@ -112,11 +112,11 @@ class BlobType extends SqlType<Uint8List> {
|
||||||
mapToSqlVariable(content) => content;
|
mapToSqlVariable(content) => content;
|
||||||
}
|
}
|
||||||
|
|
||||||
class RealType extends SqlType<num> {
|
class RealType extends SqlType<double> {
|
||||||
const RealType();
|
const RealType();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
num mapFromDatabaseResponse(response) => response as num;
|
double mapFromDatabaseResponse(response) => (response as num).toDouble();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String mapToSqlConstant(num content) => content.toString();
|
String mapToSqlConstant(num content) => content.toString();
|
||||||
|
|
|
@ -625,26 +625,26 @@ class $SharedTodosTable extends SharedTodos
|
||||||
|
|
||||||
class TableWithoutPKData extends DataClass {
|
class TableWithoutPKData extends DataClass {
|
||||||
final int notReallyAnId;
|
final int notReallyAnId;
|
||||||
final num someFloat;
|
final double someFloat;
|
||||||
TableWithoutPKData({this.notReallyAnId, this.someFloat});
|
TableWithoutPKData({this.notReallyAnId, this.someFloat});
|
||||||
factory TableWithoutPKData.fromData(
|
factory TableWithoutPKData.fromData(
|
||||||
Map<String, dynamic> data, GeneratedDatabase db,
|
Map<String, dynamic> data, GeneratedDatabase db,
|
||||||
{String prefix}) {
|
{String prefix}) {
|
||||||
final effectivePrefix = prefix ?? '';
|
final effectivePrefix = prefix ?? '';
|
||||||
final intType = db.typeSystem.forDartType<int>();
|
final intType = db.typeSystem.forDartType<int>();
|
||||||
final numType = db.typeSystem.forDartType<num>();
|
final doubleType = db.typeSystem.forDartType<double>();
|
||||||
return TableWithoutPKData(
|
return TableWithoutPKData(
|
||||||
notReallyAnId: intType
|
notReallyAnId: intType
|
||||||
.mapFromDatabaseResponse(data['${effectivePrefix}not_really_an_id']),
|
.mapFromDatabaseResponse(data['${effectivePrefix}not_really_an_id']),
|
||||||
someFloat:
|
someFloat: doubleType
|
||||||
numType.mapFromDatabaseResponse(data['${effectivePrefix}some_float']),
|
.mapFromDatabaseResponse(data['${effectivePrefix}some_float']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory TableWithoutPKData.fromJson(Map<String, dynamic> json,
|
factory TableWithoutPKData.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||||
return TableWithoutPKData(
|
return TableWithoutPKData(
|
||||||
notReallyAnId: serializer.fromJson<int>(json['notReallyAnId']),
|
notReallyAnId: serializer.fromJson<int>(json['notReallyAnId']),
|
||||||
someFloat: serializer.fromJson<num>(json['someFloat']),
|
someFloat: serializer.fromJson<double>(json['someFloat']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@override
|
@override
|
||||||
|
@ -652,11 +652,11 @@ class TableWithoutPKData extends DataClass {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||||
return {
|
return {
|
||||||
'notReallyAnId': serializer.toJson<int>(notReallyAnId),
|
'notReallyAnId': serializer.toJson<int>(notReallyAnId),
|
||||||
'someFloat': serializer.toJson<num>(someFloat),
|
'someFloat': serializer.toJson<double>(someFloat),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
TableWithoutPKData copyWith({int notReallyAnId, num someFloat}) =>
|
TableWithoutPKData copyWith({int notReallyAnId, double someFloat}) =>
|
||||||
TableWithoutPKData(
|
TableWithoutPKData(
|
||||||
notReallyAnId: notReallyAnId ?? this.notReallyAnId,
|
notReallyAnId: notReallyAnId ?? this.notReallyAnId,
|
||||||
someFloat: someFloat ?? this.someFloat,
|
someFloat: someFloat ?? this.someFloat,
|
||||||
|
@ -737,7 +737,7 @@ class $TableWithoutPKTable extends TableWithoutPK
|
||||||
map['not_really_an_id'] = Variable<int, IntType>(d.notReallyAnId);
|
map['not_really_an_id'] = Variable<int, IntType>(d.notReallyAnId);
|
||||||
}
|
}
|
||||||
if (d.someFloat != null || includeNulls) {
|
if (d.someFloat != null || includeNulls) {
|
||||||
map['some_float'] = Variable<num, RealType>(d.someFloat);
|
map['some_float'] = Variable<double, RealType>(d.someFloat);
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,18 @@ void main() {
|
||||||
['Implement insert statements']));
|
['Implement insert statements']));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('can insert floating point values', () async {
|
||||||
|
// regression test for https://github.com/simolus3/moor/issues/30
|
||||||
|
await db
|
||||||
|
.into(db.tableWithoutPK)
|
||||||
|
.insert(TableWithoutPKData(notReallyAnId: 42, someFloat: 3.1415));
|
||||||
|
|
||||||
|
verify(executor.runInsert(
|
||||||
|
'INSERT INTO table_without_p_k '
|
||||||
|
'(not_really_an_id, some_float) VALUES (?, ?)',
|
||||||
|
[42, 3.1415]));
|
||||||
|
});
|
||||||
|
|
||||||
test('generates insert or replace statements', () async {
|
test('generates insert or replace statements', () async {
|
||||||
await db.into(db.todosTable).insert(
|
await db.into(db.todosTable).insert(
|
||||||
TodoEntry(
|
TodoEntry(
|
||||||
|
|
|
@ -76,7 +76,7 @@ class SpecifiedColumn {
|
||||||
ColumnType.integer: 'int',
|
ColumnType.integer: 'int',
|
||||||
ColumnType.datetime: 'DateTime',
|
ColumnType.datetime: 'DateTime',
|
||||||
ColumnType.blob: 'Uint8List',
|
ColumnType.blob: 'Uint8List',
|
||||||
ColumnType.real: 'num',
|
ColumnType.real: 'double',
|
||||||
}[type];
|
}[type];
|
||||||
|
|
||||||
/// The column type from the dsl library. For instance, if a table has
|
/// The column type from the dsl library. For instance, if a table has
|
||||||
|
|
Loading…
Reference in New Issue