Make date time values comparable

Fixes #58
This commit is contained in:
Simon Binder 2019-07-05 18:47:29 +02:00
parent 2de76c65ac
commit 3b09d819f9
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
5 changed files with 23 additions and 5 deletions

View File

@ -22,7 +22,8 @@ abstract class TextColumn extends Column<String, StringType> {
/// A column that stores a [DateTime]. Times will be stored as unix timestamp /// A column that stores a [DateTime]. Times will be stored as unix timestamp
/// and will thus have a second accuracy. /// and will thus have a second accuracy.
abstract class DateTimeColumn extends Column<DateTime, DateTimeType> {} abstract class DateTimeColumn extends Column<DateTime, DateTimeType>
implements DateTimeExpression {}
/// A column that stores arbitrary blobs of data as a [Uint8List]. /// A column that stores arbitrary blobs of data as a [Uint8List].
abstract class BlobColumn extends Column<Uint8List, BlobType> {} abstract class BlobColumn extends Column<Uint8List, BlobType> {}

View File

@ -7,6 +7,9 @@ abstract class IntExpression extends Expression<int, IntType>
abstract class DoubleExpression extends Expression<double, RealType> abstract class DoubleExpression extends Expression<double, RealType>
implements ComparableExpr<double, RealType> {} implements ComparableExpr<double, RealType> {}
abstract class DateTimeExpression extends Expression<DateTime, DateTimeType>
implements ComparableExpr<DateTime, DateTimeType> {}
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
/// than the other expression. /// than the other expression.

View File

@ -35,13 +35,19 @@ Expression<int, IntType> second(Expression<DateTime, DateTimeType> date) =>
/// A sql expression that evaluates to the current date represented as a unix /// A sql expression that evaluates to the current date represented as a unix
/// timestamp. The hour, minute and second fields will be set to 0. /// timestamp. The hour, minute and second fields will be set to 0.
const Expression<DateTime, DateTimeType> currentDate = const DateTimeExpression currentDate =
CustomExpression("strftime('%s', CURRENT_DATE)"); _CustomDateTimeExpression("strftime('%s', CURRENT_DATE)");
/// A sql expression that evaluates to the current date and time, similar to /// A sql expression that evaluates to the current date and time, similar to
/// [DateTime.now]. Timestamps are stored with a second accuracy. /// [DateTime.now]. Timestamps are stored with a second accuracy.
const Expression<DateTime, DateTimeType> currentDateAndTime = const DateTimeExpression currentDateAndTime =
CustomExpression("strftime('%s', CURRENT_TIMESTAMP)"); _CustomDateTimeExpression("strftime('%s', CURRENT_TIMESTAMP)");
class _CustomDateTimeExpression extends CustomExpression<DateTime, DateTimeType>
with ComparableExpr
implements DateTimeExpression {
const _CustomDateTimeExpression(String content) : super(content);
}
/// Expression that extracts components out of a date time by using the builtin /// Expression that extracts components out of a date time by using the builtin
/// sqlite function "strftime" and casting the result to an integer. /// sqlite function "strftime" and casting the result to an integer.

View File

@ -201,6 +201,7 @@ class GeneratedIntColumn extends GeneratedColumn<int, IntType>
} }
class GeneratedDateTimeColumn extends GeneratedColumn<DateTime, DateTimeType> class GeneratedDateTimeColumn extends GeneratedColumn<DateTime, DateTimeType>
with ComparableExpr
implements DateTimeColumn { implements DateTimeColumn {
GeneratedDateTimeColumn( GeneratedDateTimeColumn(
String $name, String $name,

View File

@ -15,4 +15,11 @@ void main() {
expect(nullableQuery.sql, equals('name INTEGER NULL')); expect(nullableQuery.sql, equals('name INTEGER NULL'));
expect(nonNullQuery.sql, equals('name INTEGER NOT NULL')); expect(nonNullQuery.sql, equals('name INTEGER NOT NULL'));
}); });
test('can compare', () {
final ctx = GenerationContext(null, null);
nonNull.isSmallerThan(currentDateAndTime).writeInto(ctx);
expect(ctx.sql, 'name < strftime(\'%s\', CURRENT_TIMESTAMP)');
});
} }