Skip to content

Instantly share code, notes, and snippets.

@simolus3
Last active February 24, 2020 17:14
Show Gist options
  • Save simolus3/d9a18cec5b98f43f283e9664b08109af to your computer and use it in GitHub Desktop.
Save simolus3/d9a18cec5b98f43f283e9664b08109af to your computer and use it in GitHub Desktop.
Moor: Sent CREATE TABLE IF NOT EXISTS blood_sugars (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, value INTEGER NOT NULL, date INTEGER NOT NULL, type VARCHAR NOT NULL); with args []
Moor: Sent INSERT INTO blood_sugars (id, value, date, type) VALUES (?, ?, ?, ?) with args [2, 13, 1578779185, some type]
Moor: Sent SELECT * FROM blood_sugars WHERE (CAST(strftime("%Y", date, "unixepoch") AS INTEGER)) = ? AND (CAST(strftime("%m", date, "unixepoch") AS INTEGER)) = ? AND (CAST(strftime("%d", date, "unixepoch") AS INTEGER)) = ?; with args [2020, 1, 11]
[BloodSugar(id: 2, value: 13, date: 2020-01-11 22:46:25.000, type: some type)]
import 'package:moor/moor.dart';
import 'package:moor_ffi/moor_ffi.dart';
part 'repro.g.dart';
class BloodSugars extends Table {
IntColumn get id => integer().autoIncrement()();
IntColumn get value => integer()();
DateTimeColumn get date => dateTime()();
TextColumn get type => text()();
}
@UseMoor(tables: [BloodSugars])
class Database extends _$Database {
Database(QueryExecutor e) : super(e);
@override
int get schemaVersion => 1;
Future<void> insertTestBloodSugar() async {
await into(bloodSugars).insert(BloodSugarsCompanion(
id: Value(2),
value: Value(13),
type: Value('some type'),
date: Value(DateTime.parse('2020-01-11T21:46:25Z')),
));
}
Stream<List<BloodSugar>> watchBloodSugarsInDate2(DateTime dateTime) =>
(select(bloodSugars)
..where((row) {
final date = row.date;
return date.year.equals(dateTime.year) &
date.month.equals(dateTime.month) &
date.day.equals(dateTime.day);
}))
.watch();
}
void main() async {
final db = Database(VmDatabase.memory());
await db.insertTestBloodSugar();
final found = await db
.watchBloodSugarsInDate2(DateTime.parse('2020-01-11T21:46:25Z'))
.first;
print(found);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment