|
|
|
|
@@ -1,39 +1,32 @@
|
|
|
|
|
import 'package:path/path.dart';
|
|
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
|
|
|
|
|
|
// Models
|
|
|
|
|
import 'package:progetto_m335_flutter/model/note.dart';
|
|
|
|
|
import 'package:progetto_m335_flutter/model/promemoria.dart';
|
|
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
|
|
|
|
|
|
class NoteDatabase {
|
|
|
|
|
static final NoteDatabase instance = NoteDatabase._init();
|
|
|
|
|
static Database? _database;
|
|
|
|
|
|
|
|
|
|
// Zero args constructor needed to extend this class
|
|
|
|
|
NoteDatabase();
|
|
|
|
|
|
|
|
|
|
NoteDatabase._init();
|
|
|
|
|
|
|
|
|
|
Future<Database> get database async {
|
|
|
|
|
if (_database != null) return _database!;
|
|
|
|
|
|
|
|
|
|
_database = await _initDB('note.db');
|
|
|
|
|
return _database!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<Database> _initDB(String filePath) async {
|
|
|
|
|
print("Initializing database");
|
|
|
|
|
final databasePath = await getDatabasesPath();
|
|
|
|
|
|
|
|
|
|
final path = join(databasePath, filePath);
|
|
|
|
|
|
|
|
|
|
return await openDatabase(path, version: 1, onCreate: _createDB);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future _createDB(Database database, int version) async {
|
|
|
|
|
if (await isDatabaseExists()) {
|
|
|
|
|
print("Database already created");
|
|
|
|
|
deleteDatabase();
|
|
|
|
|
print("Database deleted");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await database.execute('''CREATE TABLE promemoria (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
@@ -62,7 +55,6 @@ class NoteDatabase {
|
|
|
|
|
await fillDemoData(database, version);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future fillDemoData(Database database, int version) async {
|
|
|
|
|
// Add fake categories
|
|
|
|
|
await database.execute('''
|
|
|
|
|
@@ -76,11 +68,11 @@ class NoteDatabase {
|
|
|
|
|
'Nota 2',
|
|
|
|
|
'2023-09-28',
|
|
|
|
|
'2023-09-28',
|
|
|
|
|
'',
|
|
|
|
|
'Questo è un esempio di nota 2.'
|
|
|
|
|
)
|
|
|
|
|
''');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await database.execute('''
|
|
|
|
|
INSERT INTO note (
|
|
|
|
|
title,
|
|
|
|
|
@@ -92,6 +84,7 @@ class NoteDatabase {
|
|
|
|
|
'Nota 2',
|
|
|
|
|
'2023-09-28',
|
|
|
|
|
'2023-09-28',
|
|
|
|
|
'',
|
|
|
|
|
'Questo è un esempio di nota 2.'
|
|
|
|
|
)
|
|
|
|
|
''');
|
|
|
|
|
@@ -112,6 +105,7 @@ class NoteDatabase {
|
|
|
|
|
'2023-09-27',
|
|
|
|
|
'2023-09-27',
|
|
|
|
|
'2023-10-05',
|
|
|
|
|
'',
|
|
|
|
|
'Questo è un esempio di promemoria 1.',
|
|
|
|
|
'Alta',
|
|
|
|
|
'Rosso'
|
|
|
|
|
@@ -134,6 +128,7 @@ class NoteDatabase {
|
|
|
|
|
'2023-09-28',
|
|
|
|
|
'2023-09-28',
|
|
|
|
|
'2023-10-10',
|
|
|
|
|
'',
|
|
|
|
|
'Questo è un esempio di promemoria 2.',
|
|
|
|
|
'Media',
|
|
|
|
|
'Verde'
|
|
|
|
|
@@ -160,24 +155,16 @@ class NoteDatabase {
|
|
|
|
|
database.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> deleteDatabase() async {
|
|
|
|
|
try {
|
|
|
|
|
final databasePath = await getDatabasesPath();
|
|
|
|
|
final path = join(databasePath, 'note.db');
|
|
|
|
|
databaseFactory.deleteDatabase(path);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw Exception('DbBase.deleteDatabase: $error');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<bool> isDatabaseExists() async {
|
|
|
|
|
final databasePath = await getDatabasesPath();
|
|
|
|
|
final path = join(databasePath, 'note.db');
|
|
|
|
|
// Apre il database in modalità sola lettura.
|
|
|
|
|
Database database = await openDatabase(path, readOnly: true);
|
|
|
|
|
|
|
|
|
|
// Restituisce true se il database è stato aperto correttamente, altrimenti false.
|
|
|
|
|
return database.isOpen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> createNote(Database database, Note note) async {
|
|
|
|
|
await database.execute('''
|
|
|
|
|
INSERT INTO note (
|
|
|
|
|
@@ -207,7 +194,6 @@ class NoteDatabase {
|
|
|
|
|
return maps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<List<Map>> selectAllNotes() async {
|
|
|
|
|
final db = await database;
|
|
|
|
|
|
|
|
|
|
@@ -216,9 +202,8 @@ class NoteDatabase {
|
|
|
|
|
return maps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> createPromemoria(Database database,
|
|
|
|
|
Promemoria promemoria) async {
|
|
|
|
|
Future<void> createPromemoria(
|
|
|
|
|
Database database, Promemoria promemoria) async {
|
|
|
|
|
await database.execute('''
|
|
|
|
|
INSERT INTO promemoria (
|
|
|
|
|
title,
|
|
|
|
|
@@ -244,7 +229,6 @@ class NoteDatabase {
|
|
|
|
|
print('promemoria $promemoria.title inserted');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<Map<String, Object?>> readPromemoria(int id) async {
|
|
|
|
|
final db = await database;
|
|
|
|
|
|
|
|
|
|
@@ -252,7 +236,6 @@ class NoteDatabase {
|
|
|
|
|
return results.first;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<Map<String, Object?>> readNote(int id) async {
|
|
|
|
|
final db = await database;
|
|
|
|
|
|
|
|
|
|
@@ -268,7 +251,4 @@ class NoteDatabase {
|
|
|
|
|
// Pass the Dog's id as a whereArg to prevent SQL injection.
|
|
|
|
|
// whereArgs: [dog.id],)
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|