2019-03-09 07:37:22 -08:00
|
|
|
import 'package:moor/moor.dart';
|
2019-02-14 07:01:57 -08:00
|
|
|
|
2019-02-14 07:53:52 -08:00
|
|
|
part 'todos.g.dart';
|
|
|
|
|
2019-02-14 07:01:57 -08:00
|
|
|
@DataClassName('TodoEntry')
|
|
|
|
class TodosTable extends Table {
|
2019-02-14 07:53:52 -08:00
|
|
|
@override
|
|
|
|
String get tableName => 'todos';
|
|
|
|
|
2019-02-14 07:01:57 -08:00
|
|
|
IntColumn get id => integer().autoIncrement()();
|
2019-02-14 07:53:52 -08:00
|
|
|
TextColumn get title => text().withLength(min: 4, max: 16).nullable()();
|
2019-02-14 07:01:57 -08:00
|
|
|
TextColumn get content => text()();
|
2019-06-04 13:04:21 -07:00
|
|
|
@JsonKey('target_date')
|
2019-02-14 11:46:25 -08:00
|
|
|
DateTimeColumn get targetDate => dateTime().nullable()();
|
2019-02-14 07:01:57 -08:00
|
|
|
|
2019-02-14 07:53:52 -08:00
|
|
|
IntColumn get category => integer().nullable()();
|
|
|
|
}
|
|
|
|
|
|
|
|
class Users extends Table {
|
|
|
|
IntColumn get id => integer().autoIncrement()();
|
|
|
|
TextColumn get name => text().withLength(min: 6, max: 32)();
|
2019-05-09 06:30:17 -07:00
|
|
|
BoolColumn get isAwesome => boolean().withDefault(const Constant(true))();
|
2019-03-10 11:45:32 -07:00
|
|
|
|
|
|
|
BlobColumn get profilePicture => blob()();
|
2019-04-01 06:19:21 -07:00
|
|
|
DateTimeColumn get creationTime =>
|
|
|
|
dateTime().withDefault(currentDateAndTime)();
|
2019-02-14 07:53:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@DataClassName('Category')
|
|
|
|
class Categories extends Table {
|
|
|
|
IntColumn get id => integer().autoIncrement()();
|
2019-03-27 10:37:29 -07:00
|
|
|
TextColumn get description =>
|
|
|
|
text().named('desc').customConstraint('NOT NULL UNIQUE')();
|
2019-02-14 07:53:52 -08:00
|
|
|
}
|
|
|
|
|
2019-03-06 12:43:16 -08:00
|
|
|
class SharedTodos extends Table {
|
|
|
|
IntColumn get todo => integer()();
|
|
|
|
IntColumn get user => integer()();
|
|
|
|
|
|
|
|
@override
|
|
|
|
Set<Column> get primaryKey => {todo, user};
|
2019-04-19 11:38:58 -07:00
|
|
|
|
|
|
|
@override
|
|
|
|
List<String> get customConstraints => [
|
|
|
|
'FOREIGN KEY (todo) REFERENCES todos(id)',
|
|
|
|
'FOREIGN KEY (user) REFERENCES users(id)'
|
|
|
|
];
|
2019-03-06 12:43:16 -08:00
|
|
|
}
|
|
|
|
|
2019-04-29 09:04:40 -07:00
|
|
|
class TableWithoutPK extends Table {
|
|
|
|
IntColumn get notReallyAnId => integer()();
|
2019-05-12 01:52:02 -07:00
|
|
|
RealColumn get someFloat => real()();
|
2019-07-18 03:02:16 -07:00
|
|
|
|
|
|
|
TextColumn get custom => text().map(const CustomConverter())();
|
2019-04-29 09:04:40 -07:00
|
|
|
}
|
|
|
|
|
2019-07-02 13:42:49 -07:00
|
|
|
class PureDefaults extends Table {
|
|
|
|
IntColumn get id => integer().autoIncrement()();
|
|
|
|
TextColumn get txt => text().nullable()();
|
|
|
|
}
|
|
|
|
|
2019-07-18 03:02:16 -07:00
|
|
|
// example object used for custom mapping
|
|
|
|
class MyCustomObject {
|
|
|
|
final String data;
|
|
|
|
MyCustomObject(this.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
class CustomConverter extends TypeConverter<MyCustomObject, String> {
|
|
|
|
const CustomConverter();
|
|
|
|
@override
|
|
|
|
MyCustomObject mapToDart(String fromDb) {
|
|
|
|
return fromDb == null ? null : MyCustomObject(fromDb);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String mapToSql(MyCustomObject value) {
|
|
|
|
return value?.data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:41:27 -07:00
|
|
|
@UseMoor(
|
2019-07-02 13:42:49 -07:00
|
|
|
tables: [
|
|
|
|
TodosTable,
|
|
|
|
Categories,
|
|
|
|
Users,
|
|
|
|
SharedTodos,
|
|
|
|
TableWithoutPK,
|
|
|
|
PureDefaults,
|
|
|
|
],
|
2019-06-29 06:23:14 -07:00
|
|
|
daos: [SomeDao],
|
2019-06-30 04:35:13 -07:00
|
|
|
queries: {
|
|
|
|
'allTodosWithCategory': 'SELECT t.*, c.id as catId, c."desc" as catDesc '
|
|
|
|
'FROM todos t INNER JOIN categories c ON c.id = t.category',
|
2019-06-30 13:16:58 -07:00
|
|
|
'deleteTodoById': 'DELETE FROM todos WHERE id = ?',
|
2019-07-10 09:25:18 -07:00
|
|
|
'withIn': 'SELECT * FROM todos WHERE title = ?2 OR id IN ? OR title = ?1',
|
|
|
|
'search':
|
|
|
|
'SELECT * FROM todos WHERE CASE WHEN -1 = :id THEN 1 ELSE id = :id END',
|
2019-07-27 15:02:17 -07:00
|
|
|
'findCustom': 'SELECT custom FROM table_without_p_k WHERE some_float < 10',
|
2019-06-30 04:35:13 -07:00
|
|
|
},
|
2019-06-28 14:41:27 -07:00
|
|
|
)
|
2019-02-14 07:53:52 -08:00
|
|
|
class TodoDb extends _$TodoDb {
|
|
|
|
TodoDb(QueryExecutor e) : super(e);
|
|
|
|
|
|
|
|
@override
|
|
|
|
MigrationStrategy get migration => MigrationStrategy();
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get schemaVersion => 1;
|
2019-02-14 08:55:31 -08:00
|
|
|
}
|
2019-06-29 06:23:14 -07:00
|
|
|
|
|
|
|
@UseDao(
|
|
|
|
tables: [Users, SharedTodos, TodosTable],
|
2019-06-30 04:35:13 -07:00
|
|
|
queries: {
|
|
|
|
'todosForUser': 'SELECT t.* FROM todos t '
|
|
|
|
'INNER JOIN shared_todos st ON st.todo = t.id '
|
|
|
|
'INNER JOIN users u ON u.id = st.user '
|
|
|
|
'WHERE u.id = :user'
|
|
|
|
},
|
2019-06-29 06:23:14 -07:00
|
|
|
)
|
|
|
|
class SomeDao extends DatabaseAccessor<TodoDb> with _$SomeDaoMixin {
|
|
|
|
SomeDao(TodoDb db) : super(db);
|
|
|
|
}
|