1 Commits

Author SHA1 Message Date
cd5803e6fe work in progress database 2023-09-28 14:23:23 +02:00
4 changed files with 70 additions and 204 deletions

View File

@@ -1,39 +1,32 @@
import 'package:path/path.dart'; import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
// Models // Models
import 'package:progetto_m335_flutter/model/note.dart'; import 'package:progetto_m335_flutter/model/note.dart';
import 'package:progetto_m335_flutter/model/promemoria.dart'; import 'package:progetto_m335_flutter/model/promemoria.dart';
import 'package:sqflite/sqflite.dart';
class NoteDatabase { class NoteDatabase {
static final NoteDatabase instance = NoteDatabase._init(); static final NoteDatabase instance = NoteDatabase._init();
static Database? _database; static Database? _database;
// Zero args constructor needed to extend this class
NoteDatabase();
NoteDatabase._init(); NoteDatabase._init();
Future<Database> get database async { Future<Database> get database async {
if (_database != null) return _database!; if (_database != null) return _database!;
_database = await _initDB('note.db'); _database = await _initDB('note.db');
return _database!; return _database!;
} }
Future<Database> _initDB(String filePath) async { Future<Database> _initDB(String filePath) async {
print("Initializing database");
final databasePath = await getDatabasesPath(); final databasePath = await getDatabasesPath();
final path = join(databasePath, filePath); final path = join(databasePath, filePath);
return await openDatabase(path, version: 1, onCreate: _createDB); return await openDatabase(path, version: 1, onCreate: _createDB);
} }
Future _createDB(Database database, int version) async { Future _createDB(Database database, int version) async {
if (await isDatabaseExists()) {
print("Database already created");
deleteDatabase();
print("Database deleted");
}
await database.execute('''CREATE TABLE promemoria ( await database.execute('''CREATE TABLE promemoria (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -63,23 +56,6 @@ class NoteDatabase {
} }
Future fillDemoData(Database database, int version) async { Future fillDemoData(Database database, int version) async {
var nota = Note();
nota.setTitle("Nota 1");
nota.setCreationDate("2023-09-56");
nota.setLastModificationDate("2023-09-56");
nota.setArrayPromemoria("1,2,3,4,5");
nota.setDescription("Questo è un esempio di nota 1.");
await createNote(database, nota);
print(await readNote(1));
nota.setDescription("ciao");
await updateNote (nota);
print(await readNote (1));
// Add fake categories // Add fake categories
await database.execute(''' await database.execute('''
INSERT INTO note ( INSERT INTO note (
@@ -92,6 +68,7 @@ class NoteDatabase {
'Nota 2', 'Nota 2',
'2023-09-28', '2023-09-28',
'2023-09-28', '2023-09-28',
'',
'Questo è un esempio di nota 2.' 'Questo è un esempio di nota 2.'
) )
'''); ''');
@@ -107,6 +84,7 @@ class NoteDatabase {
'Nota 2', 'Nota 2',
'2023-09-28', '2023-09-28',
'2023-09-28', '2023-09-28',
'',
'Questo è un esempio di nota 2.' 'Questo è un esempio di nota 2.'
) )
'''); ''');
@@ -127,6 +105,7 @@ class NoteDatabase {
'2023-09-27', '2023-09-27',
'2023-09-27', '2023-09-27',
'2023-10-05', '2023-10-05',
'',
'Questo è un esempio di promemoria 1.', 'Questo è un esempio di promemoria 1.',
'Alta', 'Alta',
'Rosso' 'Rosso'
@@ -149,6 +128,7 @@ class NoteDatabase {
'2023-09-28', '2023-09-28',
'2023-09-28', '2023-09-28',
'2023-10-10', '2023-10-10',
'',
'Questo è un esempio di promemoria 2.', 'Questo è un esempio di promemoria 2.',
'Media', 'Media',
'Verde' 'Verde'
@@ -176,19 +156,34 @@ class NoteDatabase {
} }
Future<void> deleteDatabase() async { Future<void> deleteDatabase() async {
final databasePath = await getDatabasesPath(); try {
final path = join(databasePath, 'note.db'); final databasePath = await getDatabasesPath();
databaseFactory.deleteDatabase(path); final path = join(databasePath, 'note.db');
databaseFactory.deleteDatabase(path);
} catch (error) {
throw Exception('DbBase.deleteDatabase: $error');
}
} }
Future<bool> isDatabaseExists() async { Future<void> createNote(Database database, Note note) async {
final databasePath = await getDatabasesPath(); await database.execute('''
final path = join(databasePath, 'note.db'); INSERT INTO note (
// Apre il database in modalità sola lettura. title,
Database database = await openDatabase(path, readOnly: true); creationDate,
lastModificationDate,
arrayPromemoria,
description,
// Restituisce true se il database è stato aperto correttamente, altrimenti false. ) VALUES (
return database.isOpen; '$note.title}',
'$note.creationDate',
'$note.lastModificationDate',
'$note.arrayPromemoria.toString()',
'$note.description',
)
''');
print('note $note.title inserted');
} }
Future<List<Map>> selectAllPromemoria() async { Future<List<Map>> selectAllPromemoria() async {
@@ -207,23 +202,29 @@ class NoteDatabase {
return maps; return maps;
} }
Future<void> createNote(Database database, Note note) async {
await database.insert(
'note',
note.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
print('note $note.title inserted');
}
Future<void> createPromemoria( Future<void> createPromemoria(
Database database, Promemoria promemoria) async { Database database, Promemoria promemoria) async {
await database.insert( await database.execute('''
'promemoria', INSERT INTO promemoria (
promemoria.toMap(), title,
conflictAlgorithm: ConflictAlgorithm.replace, creationDate,
); lastModificationDate,
expirationDate,
arrayPromemoria,
description,
priority,
color
) VALUES (
'$promemoria.title',
'$promemoria.creationDate',
'$promemoria.lastModificationDate',
'$promemoria.expirationDate',
'$promemoria.arrayPromemoria.toString()',
'$promemoria.description',
'$promemoria.priority',
'$promemoria.color'
)
''');
print('promemoria $promemoria.title inserted'); print('promemoria $promemoria.title inserted');
} }
@@ -243,17 +244,11 @@ class NoteDatabase {
return results.first; return results.first;
} }
Future<void> updatePromemoria(Promemoria promemoria) async { // Future<void> updatePromemoria(Promemoria promemoria) async {
final db = await database; // final db = await database;
await db.update('promemoria', promemoria.toMap(), //await db.update('promemoria', promemoria, where: 'id = ?',
where: 'id = ?', whereArgs: [promemoria.getId()]); // Pass the Dog's id as a whereArg to prevent SQL injection.
} // whereArgs: [dog.id],)
//}
Future<void> updateNote(Note note) async {
final db = await database;
await db.update('note', note.toMap(),
where: 'id = ?', whereArgs: [note.getId()]);
}
} }

View File

@@ -9,63 +9,4 @@ class Note extends BaseEntity {
static String lastModificationDate = BaseEntity.getLastEditDate; static String lastModificationDate = BaseEntity.getLastEditDate;
static String arrayPromemoria = ''; static String arrayPromemoria = '';
static String description = ''; static String description = '';
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'creationDate': creationDate,
'lastModificationDate': lastModificationDate,
'arrayPromemoria': arrayPromemoria,
'description': description
};
}
String getId() {
return id;
}
void setId(String id) {
id = id;
}
String getTitle() {
return title;
}
void setTitle(String title) {
title = title;
}
String getCreationDate() {
return creationDate;
}
void setCreationDate(String creationDate) {
creationDate = creationDate;
}
String getLastModificationDate() {
return lastModificationDate;
}
void setLastModificationDate(String lastModificationDate) {
lastModificationDate = lastModificationDate;
}
String getArrayPromemoria() {
return arrayPromemoria;
}
void setArrayPromemoria(String arrayPromemoria) {
arrayPromemoria = arrayPromemoria;
}
String getDescription() {
return description;
}
void setDescription(String description) {
description = description;
}
} }

View File

@@ -13,85 +13,6 @@ class Promemoria extends BaseEntity {
static String arrayPromemoria = ''; static String arrayPromemoria = '';
static String description = ''; static String description = '';
static Priority priority = Priority.none; static Priority priority = Priority.none;
static Color color = Color.none; static Color color = Color.none;
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'creationDate': creationDate,
'lastModificationDate': lastModificationDate,
'expirationDate': expirationDate,
'arrayPromemoria': arrayPromemoria,
'description': description,
'priority': priority,
'color': color,
};
}
String getId(){
return id;
}
void setId(String id) {
id = id;
}
String getTitle() {
return title;
}
void setTitle(String title) {
title = title;
}
String getCreationDate() {
return creationDate;
}
void setCreationDate(String creationDate) {
creationDate = creationDate;
}
String getLastModificationDate() {
return lastModificationDate;
}
void setLastModificationDate(String lastModificationDate) {
lastModificationDate = lastModificationDate;
}
String getExpirationDate() {
return expirationDate;
}
void setExpirationDate(String expirationDate) {
expirationDate = expirationDate;
}
String getArrayPromemoria() {
return arrayPromemoria;
}
void setArrayPromemoria(String arrayPromemoria) {
arrayPromemoria = arrayPromemoria;
}
String getDescription() {
return description;
}
void setDescription(String description) {
description = description;
}
Priority getPriority() {
return priority;
}
void setPriority(Priority priority) {
priority = priority;
}
} }

View File

@@ -29,6 +29,14 @@ class _TestState extends State<Test> {
print("Data printed"); print("Data printed");
} }
Future<void> _deletedatabase() async {
final db = await noteDatabase.database;
print("Deleting database");
await db.delete(noteTable);
print("Database deleted");
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@@ -36,7 +44,8 @@ class _TestState extends State<Test> {
child: Column( child: Column(
children: [ children: [
FloatingActionButton(onPressed: _pressed), FloatingActionButton(onPressed: _pressed),
FloatingActionButton(onPressed: _printdata) FloatingActionButton(onPressed: _printdata),
FloatingActionButton(onPressed: _deletedatabase)
], ],
) )
) )