132 lines
3.4 KiB
Dart
132 lines
3.4 KiB
Dart
import "package:sqflite/sqflite.dart";
|
|
import "package:sqflite/sqlite_api.dart";
|
|
import "package:path/path.dart";
|
|
import 'data_util.dart' as data;
|
|
|
|
class DBHelper {
|
|
DBHelper._();
|
|
|
|
static DBHelper dbHelper = DBHelper._();
|
|
|
|
late Database _database;
|
|
|
|
Future<Database> get database async {
|
|
_database = await _createDatabase();
|
|
|
|
return _database;
|
|
}
|
|
|
|
Future<Database> _createDatabase() async {
|
|
Database database =
|
|
await openDatabase(join(await getDatabasesPath(), 'lists.db'),
|
|
onCreate: (Database db, int version) {
|
|
db.execute("""
|
|
CREATE TABLE List(
|
|
id INTEGER PRIMARY KEY,
|
|
list_name TEXT,
|
|
list_desc TEXT,
|
|
is_template int2
|
|
)
|
|
""");
|
|
db.execute("""
|
|
CREATE TABLE Item(
|
|
id INTEGER PRIMARY KEY,
|
|
check_text TEXT,
|
|
status int2,
|
|
list_id INTEGER
|
|
)
|
|
""");
|
|
db.execute("""
|
|
INSERT INTO List(id, list_name, list_desc, is_template)
|
|
VALUES
|
|
(0, 'test list', 'A list to test', 0),
|
|
(1, 'test template', 'A template to test', 1)
|
|
""");
|
|
db.execute("""
|
|
INSERT INTO Item(id, check_text, status, list_id)
|
|
VALUES
|
|
(0, 'test item', 0, 0),
|
|
(1, 'test item', 1, 0),
|
|
(2, 'test item', 0, 1),
|
|
(3, 'test item', 1, 1)
|
|
""");
|
|
}, version: 2);
|
|
return database;
|
|
}
|
|
|
|
Future<List<Map<String, dynamic>>> getAllLists(data.Page type) async {
|
|
Database db = await database;
|
|
|
|
return db.query("List", where: "is_template = ?", whereArgs: [type.index]);
|
|
}
|
|
|
|
Future<List<Map<String, dynamic>>> getList(int id) async {
|
|
Database db = await database;
|
|
|
|
return db.query("Item", where: "list_id = ?", whereArgs: [id]);
|
|
}
|
|
|
|
Future<List<Map<String, dynamic>>> getListData(int id) async {
|
|
Database db = await database;
|
|
|
|
return db.query("List", where: "id = ?", whereArgs: [id]);
|
|
}
|
|
|
|
Future<int> insertList(data.List l) async {
|
|
Database db = await database;
|
|
|
|
return db.insert("List", l.toMap());
|
|
}
|
|
|
|
Future<int> deleteList(data.List l) async {
|
|
Database db = await database;
|
|
|
|
db.delete("Item", where: 'list_id = ?', whereArgs: [l.id]);
|
|
return db.delete("List", where: 'id = ?', whereArgs: [l.id]);
|
|
}
|
|
|
|
Future<void> copyList(int oldID, int newID) async {
|
|
Database db = await database;
|
|
|
|
var batch = db.batch();
|
|
db.execute("""
|
|
CREATE TEMPORARY TABLE list_copy AS
|
|
SELECT check_text, status, list_id
|
|
FROM Item
|
|
WHERE list_id = ?
|
|
""", [oldID]);
|
|
db.update("list_copy", {'list_id': newID});
|
|
db.rawInsert("""
|
|
INSERT INTO Item(check_text, status, list_id)
|
|
SELECT * FROM list_copy
|
|
""");
|
|
db.execute("""
|
|
DROP TABLE list_copy
|
|
""");
|
|
batch.commit();
|
|
}
|
|
|
|
Future<int> insertItem(data.Check item) async {
|
|
Database db = await database;
|
|
|
|
return db.insert("Item", item.toMap());
|
|
}
|
|
|
|
Future<int> deleteItem(data.Check item) async {
|
|
Database db = await database;
|
|
|
|
return db.delete("Item", where: 'id = ?', whereArgs: [item.id]);
|
|
}
|
|
|
|
Future<int> updateItem(data.Check item) async {
|
|
Database db = await database;
|
|
|
|
return db.update(
|
|
"Item",
|
|
item.toMap(),
|
|
where: 'id = ?',
|
|
whereArgs: [item.id],
|
|
);
|
|
}
|
|
}
|