2024-05-12 00:08:54 +02:00
|
|
|
import 'package:cpd/database/todo_db.dart';
|
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
import 'package:path/path.dart';
|
2024-05-28 21:59:44 +02:00
|
|
|
import 'db_interface.dart';
|
2024-05-12 00:08:54 +02:00
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
class HabitDatabase implements HabitDbInterface{
|
2024-05-24 00:01:08 +02:00
|
|
|
late Database _database;
|
2024-05-12 00:08:54 +02:00
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
@override
|
2024-05-24 00:01:08 +02:00
|
|
|
Future<Database> get database async {
|
2024-05-12 00:08:54 +02:00
|
|
|
_database = await _initialize();
|
2024-05-28 21:59:44 +02:00
|
|
|
return _database;
|
2024-05-12 00:08:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Database> _initialize() async {
|
|
|
|
final path = await fullPath;
|
|
|
|
var database = await openDatabase(
|
|
|
|
path,
|
|
|
|
version: 1,
|
|
|
|
onCreate: create,
|
|
|
|
singleInstance: true,
|
|
|
|
);
|
|
|
|
return database;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> create(Database database, int version) async => await TodoDB().createTable(database);
|
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
@override
|
2024-05-12 00:08:54 +02:00
|
|
|
Future<String> get fullPath async {
|
2024-06-09 17:45:17 +02:00
|
|
|
const name = 'habit.db';
|
2024-05-12 00:08:54 +02:00
|
|
|
final path = await getDatabasesPath();
|
|
|
|
return join(path, name);
|
|
|
|
}
|
|
|
|
}
|