mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-23 05:19:22 +00:00
87 lines
3.1 KiB
Dart
87 lines
3.1 KiB
Dart
import 'package:comunic/helpers/database/database_contract.dart';
|
|
import 'package:path/path.dart';
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
/// Database Helper
|
|
///
|
|
/// Manage the connection to the local SQLite database
|
|
///
|
|
/// @author Pierre HUBERT
|
|
|
|
abstract class DatabaseHelper {
|
|
static Database _db;
|
|
|
|
/// Open the database
|
|
static Future<void> open() async {
|
|
if (_db != null && _db.isOpen) return;
|
|
|
|
var databasePath = await getDatabasesPath();
|
|
_db = await openDatabase(
|
|
join(databasePath, DatabaseContract.DATABASE_FILE_NAME),
|
|
version: DatabaseContract.DATABASE_VERSION,
|
|
onUpgrade: _performDatabaseUpdate,
|
|
onCreate: _initializeDatabase);
|
|
}
|
|
|
|
/// Get a database instance
|
|
static Future<Database> get() async {
|
|
await open();
|
|
return _db;
|
|
}
|
|
|
|
/// Perform database update
|
|
///
|
|
/// Currently : delete all the database tables and initialize it again
|
|
static Future<void> _performDatabaseUpdate(
|
|
Database db, int oldVersion, int newVersion) async {
|
|
// Drop users table
|
|
await db.execute("DROP TABLE IF EXISTS ${UserTableContract.TABLE_NAME}");
|
|
|
|
// Drop conversations table
|
|
await db.execute(
|
|
"DROP TABLE IF EXISTS ${ConversationTableContract.TABLE_NAME}");
|
|
|
|
// Drop conversations messages table
|
|
await db.execute(
|
|
"DROP TABLE IF EXISTS ${ConversationsMessagesTableContract.TABLE_NAME}");
|
|
|
|
// Initialize database again
|
|
await _initializeDatabase(db, newVersion);
|
|
}
|
|
|
|
/// Initialize the database
|
|
static Future<void> _initializeDatabase(Database db, int version) async {
|
|
// Create users table
|
|
await db.execute("CREATE TABLE ${UserTableContract.TABLE_NAME} ("
|
|
"${UserTableContract.C_ID} INTEGER PRIMARY KEY, "
|
|
"${UserTableContract.C_FIRST_NAME} TEXT, "
|
|
"${UserTableContract.C_LAST_NAME} TEXT, "
|
|
"${UserTableContract.C_VISIBILITY} TEXT, "
|
|
"${UserTableContract.C_VIRTUAL_DIRECTORY} TEXT, "
|
|
"${UserTableContract.C_ACCOUNT_IMAGE_URL} TEXT"
|
|
")");
|
|
|
|
// Create conversations table
|
|
await db.execute("CREATE TABLE ${ConversationTableContract.TABLE_NAME} ("
|
|
"${ConversationTableContract.C_ID} INTEGER PRIMARY KEY, "
|
|
"${ConversationTableContract.C_OWNER_ID} INTEGER, "
|
|
"${ConversationTableContract.C_LAST_ACTIVE} INTEGER, "
|
|
"${ConversationTableContract.C_NAME} TEXT, "
|
|
"${ConversationTableContract.C_FOLLOWING} INTEGER, "
|
|
"${ConversationTableContract.C_SAW_LAST_MESSAGE} INTEGER, "
|
|
"${ConversationTableContract.C_MEMBERS} TEXT"
|
|
")");
|
|
|
|
// Create conversation messages table
|
|
await db.execute(
|
|
"CREATE TABLE ${ConversationsMessagesTableContract.TABLE_NAME} ("
|
|
"${ConversationsMessagesTableContract.C_ID} INTEGER PRIMARY KEY, "
|
|
"${ConversationsMessagesTableContract.C_CONVERSATION_ID} INTEGER, "
|
|
"${ConversationsMessagesTableContract.C_USER_ID} INTEGER, "
|
|
"${ConversationsMessagesTableContract.C_TIME_INSERT} INTEGER, "
|
|
"${ConversationsMessagesTableContract.C_MESSAGE} TEXT, "
|
|
"${ConversationsMessagesTableContract.C_IMAGE_URL} TEXT"
|
|
")");
|
|
}
|
|
}
|