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> {}
|
||||
|
||||
/// 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.
|
||||
/// 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';
|
||||
|
||||
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> {
|
||||
/// 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.
|
||||
static Variable<num, RealType> withReal(num value) {
|
||||
static Variable<double, RealType> withReal(double value) {
|
||||
return Variable(value);
|
||||
}
|
||||
|
||||
|
|
|
@ -193,18 +193,25 @@ class GeneratedDateTimeColumn extends GeneratedColumn<DateTime, DateTimeType>
|
|||
class GeneratedBlobColumn extends GeneratedColumn<Uint8List, BlobType>
|
||||
implements BlobColumn {
|
||||
GeneratedBlobColumn(String $name, String tableName, bool $nullable,
|
||||
{String $customConstraints})
|
||||
{String $customConstraints, Expression<Uint8List, BlobType> defaultValue})
|
||||
: super($name, tableName, $nullable,
|
||||
$customConstraints: $customConstraints);
|
||||
$customConstraints: $customConstraints, defaultValue: defaultValue);
|
||||
|
||||
@override
|
||||
final String typeName = 'BLOB';
|
||||
}
|
||||
|
||||
class GeneratedRealColumn extends GeneratedColumn<num, RealType>
|
||||
class GeneratedRealColumn extends GeneratedColumn<double, RealType>
|
||||
with ComparableExpr
|
||||
implements RealColumn {
|
||||
GeneratedRealColumn(String $name, String tableName, bool $nullable)
|
||||
: super($name, tableName, $nullable);
|
||||
GeneratedRealColumn(
|
||||
String $name,
|
||||
String tableName,
|
||||
bool $nullable, {
|
||||
Expression<double, RealType> defaultValue,
|
||||
String $customConstraints,
|
||||
}) : super($name, tableName, $nullable,
|
||||
defaultValue: defaultValue, $customConstraints: $customConstraints);
|
||||
|
||||
@override
|
||||
String get typeName => 'REAL';
|
||||
|
|
|
@ -112,11 +112,11 @@ class BlobType extends SqlType<Uint8List> {
|
|||
mapToSqlVariable(content) => content;
|
||||
}
|
||||
|
||||
class RealType extends SqlType<num> {
|
||||
class RealType extends SqlType<double> {
|
||||
const RealType();
|
||||
|
||||
@override
|
||||
num mapFromDatabaseResponse(response) => response as num;
|
||||
double mapFromDatabaseResponse(response) => (response as num).toDouble();
|
||||
|
||||
@override
|
||||
String mapToSqlConstant(num content) => content.toString();
|
||||
|
|
|
@ -625,26 +625,26 @@ class $SharedTodosTable extends SharedTodos
|
|||
|
||||
class TableWithoutPKData extends DataClass {
|
||||
final int notReallyAnId;
|
||||
final num someFloat;
|
||||
final double someFloat;
|
||||
TableWithoutPKData({this.notReallyAnId, this.someFloat});
|
||||
factory TableWithoutPKData.fromData(
|
||||
Map<String, dynamic> data, GeneratedDatabase db,
|
||||
{String prefix}) {
|
||||
final effectivePrefix = prefix ?? '';
|
||||
final intType = db.typeSystem.forDartType<int>();
|
||||
final numType = db.typeSystem.forDartType<num>();
|
||||
final doubleType = db.typeSystem.forDartType<double>();
|
||||
return TableWithoutPKData(
|
||||
notReallyAnId: intType
|
||||
.mapFromDatabaseResponse(data['${effectivePrefix}not_really_an_id']),
|
||||
someFloat:
|
||||
numType.mapFromDatabaseResponse(data['${effectivePrefix}some_float']),
|
||||
someFloat: doubleType
|
||||
.mapFromDatabaseResponse(data['${effectivePrefix}some_float']),
|
||||
);
|
||||
}
|
||||
factory TableWithoutPKData.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return TableWithoutPKData(
|
||||
notReallyAnId: serializer.fromJson<int>(json['notReallyAnId']),
|
||||
someFloat: serializer.fromJson<num>(json['someFloat']),
|
||||
someFloat: serializer.fromJson<double>(json['someFloat']),
|
||||
);
|
||||
}
|
||||
@override
|
||||
|
@ -652,11 +652,11 @@ class TableWithoutPKData extends DataClass {
|
|||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return {
|
||||
'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(
|
||||
notReallyAnId: notReallyAnId ?? this.notReallyAnId,
|
||||
someFloat: someFloat ?? this.someFloat,
|
||||
|
@ -737,7 +737,7 @@ class $TableWithoutPKTable extends TableWithoutPK
|
|||
map['not_really_an_id'] = Variable<int, IntType>(d.notReallyAnId);
|
||||
}
|
||||
if (d.someFloat != null || includeNulls) {
|
||||
map['some_float'] = Variable<num, RealType>(d.someFloat);
|
||||
map['some_float'] = Variable<double, RealType>(d.someFloat);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,18 @@ void main() {
|
|||
['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 {
|
||||
await db.into(db.todosTable).insert(
|
||||
TodoEntry(
|
||||
|
|
|
@ -76,7 +76,7 @@ class SpecifiedColumn {
|
|||
ColumnType.integer: 'int',
|
||||
ColumnType.datetime: 'DateTime',
|
||||
ColumnType.blob: 'Uint8List',
|
||||
ColumnType.real: 'num',
|
||||
ColumnType.real: 'double',
|
||||
}[type];
|
||||
|
||||
/// The column type from the dsl library. For instance, if a table has
|
||||
|
|
Loading…
Reference in New Issue