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
/// 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].
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>
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> {
/// Returns an expression that is true if this expression is strictly bigger
/// 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
/// timestamp. The hour, minute and second fields will be set to 0.
const Expression<DateTime, DateTimeType> currentDate =
CustomExpression("strftime('%s', CURRENT_DATE)");
const DateTimeExpression currentDate =
_CustomDateTimeExpression("strftime('%s', CURRENT_DATE)");
/// A sql expression that evaluates to the current date and time, similar to
/// [DateTime.now]. Timestamps are stored with a second accuracy.
const Expression<DateTime, DateTimeType> currentDateAndTime =
CustomExpression("strftime('%s', CURRENT_TIMESTAMP)");
const DateTimeExpression currentDateAndTime =
_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
/// 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>
with ComparableExpr
implements DateTimeColumn {
GeneratedDateTimeColumn(
String $name,

View File

@ -15,4 +15,11 @@ void main() {
expect(nullableQuery.sql, equals('name INTEGER 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)');
});
}