1 Commits

Author SHA1 Message Date
grata
a58721a6f4 crud 2023-09-28 13:57:21 +02:00
4 changed files with 204 additions and 70 deletions

View File

@@ -1,32 +1,39 @@
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,
@@ -56,6 +63,23 @@ 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 (
@@ -68,7 +92,6 @@ 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.'
) )
'''); ''');
@@ -84,7 +107,6 @@ 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.'
) )
'''); ''');
@@ -105,7 +127,6 @@ 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'
@@ -128,7 +149,6 @@ 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'
@@ -156,34 +176,19 @@ class NoteDatabase {
} }
Future<void> deleteDatabase() async { Future<void> deleteDatabase() async {
try {
final databasePath = await getDatabasesPath(); final databasePath = await getDatabasesPath();
final path = join(databasePath, 'note.db'); final path = join(databasePath, 'note.db');
databaseFactory.deleteDatabase(path); databaseFactory.deleteDatabase(path);
} catch (error) {
throw Exception('DbBase.deleteDatabase: $error');
}
} }
Future<void> createNote(Database database, Note note) async { Future<bool> isDatabaseExists() async {
await database.execute(''' final databasePath = await getDatabasesPath();
INSERT INTO note ( final path = join(databasePath, 'note.db');
title, // Apre il database in modalità sola lettura.
creationDate, Database database = await openDatabase(path, readOnly: true);
lastModificationDate,
arrayPromemoria,
description,
) VALUES ( // Restituisce true se il database è stato aperto correttamente, altrimenti false.
'$note.title}', return database.isOpen;
'$note.creationDate',
'$note.lastModificationDate',
'$note.arrayPromemoria.toString()',
'$note.description',
)
''');
print('note $note.title inserted');
} }
Future<List<Map>> selectAllPromemoria() async { Future<List<Map>> selectAllPromemoria() async {
@@ -202,29 +207,23 @@ 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.execute(''' await database.insert(
INSERT INTO promemoria ( 'promemoria',
title, promemoria.toMap(),
creationDate, conflictAlgorithm: ConflictAlgorithm.replace,
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');
} }
@@ -244,11 +243,17 @@ 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, where: 'id = ?', await db.update('promemoria', promemoria.toMap(),
// Pass the Dog's id as a whereArg to prevent SQL injection. where: 'id = ?', whereArgs: [promemoria.getId()]);
// 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,4 +9,63 @@ 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,6 +13,85 @@ 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,14 +29,6 @@ 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(
@@ -44,8 +36,7 @@ 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)
], ],
) )
) )