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-12 22:55:54 +02:00
|
|
|
|
2024-05-12 00:08:54 +02:00
|
|
|
|
|
|
|
class HabitDatabase {
|
|
|
|
Database? _database;
|
|
|
|
|
|
|
|
Future<Database?> get database async {
|
|
|
|
if (_database != null) {
|
|
|
|
return _database;
|
|
|
|
}
|
|
|
|
_database = await _initialize();
|
|
|
|
return _database!;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
Future<String> get fullPath async {
|
|
|
|
const name = 'habit.dart';
|
|
|
|
final path = await getDatabasesPath();
|
|
|
|
return join(path, name);
|
|
|
|
}
|
|
|
|
}
|