database
This commit is contained in:
@@ -1,181 +1,36 @@
|
|||||||
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/note.dart';
|
||||||
import 'package:progetto_m335_flutter/model/promemoria.dart';
|
|
||||||
|
|
||||||
class NoteDatabase {
|
class Database{
|
||||||
static final NoteDatabase instance = NoteDatabase._init();
|
static final Database _instance = Database._init();
|
||||||
static Database? _database;
|
static Database? _database;
|
||||||
|
|
||||||
// Zero args constructor needed to extend this class
|
Database._init();
|
||||||
NoteDatabase();
|
|
||||||
|
|
||||||
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('database.db');
|
||||||
_database = await _initDB('note.db');
|
|
||||||
return _database!;
|
return _database!;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Database> _initDB(String filePath) async {
|
Future _createDB(Database database) async{
|
||||||
// On Android, it is typically data/data//databases.
|
const integerPrimaryKeyAutoincrement = 'INTEGER PRIMARY KEY AUTOINCREMENT';
|
||||||
// On iOS and MacOS, it is the Documents directory.
|
const textNotNull = 'TEXT NOT NULL';
|
||||||
final databasePath = await getDatabasesPath();
|
const integerNotNull = 'INTEGER NOT NULL';
|
||||||
// Directory databasePath = await getApplicationDocumentsDirectory();
|
const integer = 'INTEGER';
|
||||||
|
const real = 'REAL';
|
||||||
|
const text = 'TEXT';
|
||||||
|
|
||||||
final path = join(databasePath, filePath);
|
|
||||||
return await openDatabase(path, version: 1, onCreate: _createDB);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future _createDB(Database database, int version) async {
|
await database.execute('''
|
||||||
//check if the database is created
|
CREATE TABLE $Note (
|
||||||
if (database.query(noteTable) != null) {
|
${Note.id} $integerPrimaryKeyAutoincrement,
|
||||||
print("Database already created");
|
|
||||||
|
)
|
||||||
}else{
|
|
||||||
print("demo data inserting");
|
|
||||||
await database.execute('''CREATE TABLE promemoria (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
title TEXT NOT NULL,
|
|
||||||
creationDate TEXT NOT NULL,
|
|
||||||
lastModificationDate TEXT,
|
|
||||||
expirationDate TEXT,
|
|
||||||
description TEXT,
|
|
||||||
priority TEXT,
|
|
||||||
color TEXT
|
|
||||||
);
|
|
||||||
''');
|
''');
|
||||||
|
|
||||||
await database.execute('''CREATE TABLE note (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
title TEXT NOT NULL,
|
|
||||||
creationDate TEXT NOT NULL,
|
|
||||||
lastModificationDate TEXT,
|
|
||||||
description TEXT
|
|
||||||
);
|
|
||||||
''');
|
|
||||||
|
|
||||||
print("database created");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
await fillDemoData(database, version);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future fillDemoData(Database database, int version) async {
|
execute(String s) {
|
||||||
|
|
||||||
print("boh speriamo funzioni");
|
|
||||||
// Add some fake accounts
|
|
||||||
|
|
||||||
|
|
||||||
// Add fake categories
|
|
||||||
await database.execute('''
|
|
||||||
INSERT INTO note (
|
|
||||||
title,
|
|
||||||
creationDate,
|
|
||||||
lastModificationDate,
|
|
||||||
description
|
|
||||||
) VALUES (
|
|
||||||
'Nota 2',
|
|
||||||
'2023-09-28',
|
|
||||||
'2023-09-28',
|
|
||||||
'Questo è un esempio di nota 2.'
|
|
||||||
)
|
|
||||||
''');
|
|
||||||
|
|
||||||
|
|
||||||
await database.execute('''
|
|
||||||
INSERT INTO note (
|
|
||||||
title,
|
|
||||||
creationDate,
|
|
||||||
lastModificationDate,
|
|
||||||
description
|
|
||||||
) VALUES (
|
|
||||||
'Nota 2',
|
|
||||||
'2023-09-28',
|
|
||||||
'2023-09-28',
|
|
||||||
'Questo è un esempio di nota 2.'
|
|
||||||
)
|
|
||||||
''');
|
|
||||||
|
|
||||||
// Add currencies
|
|
||||||
await database.execute('''
|
|
||||||
INSERT INTO promemoria (
|
|
||||||
title,
|
|
||||||
creationDate,
|
|
||||||
lastModificationDate,
|
|
||||||
expirationDate,
|
|
||||||
description,
|
|
||||||
priority,
|
|
||||||
color
|
|
||||||
) VALUES (
|
|
||||||
'Promemoria 1',
|
|
||||||
'2023-09-27',
|
|
||||||
'2023-09-27',
|
|
||||||
'2023-10-05',
|
|
||||||
'Questo è un esempio di promemoria 1.',
|
|
||||||
'Alta',
|
|
||||||
'Rosso'
|
|
||||||
)
|
|
||||||
''');
|
|
||||||
|
|
||||||
// Add fake budgets
|
|
||||||
await database.execute('''
|
|
||||||
INSERT INTO promemoria (
|
|
||||||
title,
|
|
||||||
creationDate,
|
|
||||||
lastModificationDate,
|
|
||||||
expirationDate,
|
|
||||||
description,
|
|
||||||
priority,
|
|
||||||
color
|
|
||||||
) VALUES (
|
|
||||||
'Promemoria 2',
|
|
||||||
'2023-09-28',
|
|
||||||
'2023-09-28',
|
|
||||||
'2023-10-10',
|
|
||||||
'Questo è un esempio di promemoria 2.',
|
|
||||||
'Media',
|
|
||||||
'Verde'
|
|
||||||
)
|
|
||||||
''');
|
|
||||||
print("Demo data inserted");
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Future clearDatabase() async {
|
|
||||||
try {
|
|
||||||
await _database?.transaction((txn) async {
|
|
||||||
var batch = txn.batch();
|
|
||||||
batch.delete(noteTable);
|
|
||||||
batch.delete(promemoriaTable);
|
|
||||||
await batch.commit();
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
throw Exception('DbBase.cleanDatabase: $error');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<List<Map>> selectAllPromemoria() async {
|
|
||||||
final db = await database;
|
|
||||||
|
|
||||||
final List<Map<String, dynamic>> maps = await db.query(promemoriaTable);
|
|
||||||
|
|
||||||
return maps;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future close() async {
|
|
||||||
final database = await instance.database;
|
|
||||||
database.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
// WARNING: FOR DEV/TEST PURPOSES ONLY!!
|
|
||||||
Future<void> deleteDatabase() async {
|
|
||||||
final databasePath = await getDatabasesPath();
|
|
||||||
final path = join(databasePath, 'note.db');
|
|
||||||
databaseFactory.deleteDatabase(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
18
lib/database/promemoria.dart
Normal file
18
lib/database/promemoria.dart
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
import 'base_entity.dart';
|
||||||
|
import 'identifiers/enum/priority.dart';
|
||||||
|
import 'identifiers/enum/color.dart';
|
||||||
|
|
||||||
|
class Note extends BaseEntity{
|
||||||
|
static String id = BaseEntity.getId;
|
||||||
|
static String title = BaseEntity.getTitle;
|
||||||
|
static String creationDate = BaseEntity.getCreationDate;
|
||||||
|
|
||||||
|
static String expirationDate = 'expirationDate';
|
||||||
|
static String description = 'description';
|
||||||
|
Priority priority = Priority.none;
|
||||||
|
Color color = Color.none;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user