2019-04-24 09:24:05 +00:00
|
|
|
import 'package:comunic/helpers/database/database_contract.dart';
|
2020-04-17 09:53:47 +00:00
|
|
|
import 'package:connectivity/connectivity.dart';
|
2019-04-24 09:24:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-17 09:53:47 +00:00
|
|
|
/// Cleanup database
|
|
|
|
static Future<void> cleanUpDatabase() async {
|
|
|
|
// If connected to a network, cleanup user information
|
|
|
|
if (await Connectivity().checkConnectivity() != ConnectivityResult.none)
|
|
|
|
await _db.execute("DELETE FROM ${UserTableContract.TABLE_NAME}");
|
|
|
|
}
|
|
|
|
|
2019-04-24 09:24:05 +00:00
|
|
|
/// Perform database update
|
|
|
|
///
|
|
|
|
/// Currently : delete all the database tables and initialize it again
|
|
|
|
static Future<void> _performDatabaseUpdate(
|
|
|
|
Database db, int oldVersion, int newVersion) async {
|
2019-04-24 12:25:46 +00:00
|
|
|
// Drop users table
|
|
|
|
await db.execute("DROP TABLE IF EXISTS ${UserTableContract.TABLE_NAME}");
|
|
|
|
|
|
|
|
// Drop conversations table
|
2019-04-27 06:42:27 +00:00
|
|
|
await db.execute(
|
|
|
|
"DROP TABLE IF EXISTS ${ConversationTableContract.TABLE_NAME}");
|
|
|
|
|
|
|
|
// Drop conversations messages table
|
|
|
|
await db.execute(
|
|
|
|
"DROP TABLE IF EXISTS ${ConversationsMessagesTableContract.TABLE_NAME}");
|
2019-04-24 12:25:46 +00:00
|
|
|
|
2019-05-04 17:35:03 +00:00
|
|
|
// Drop friends list table
|
2020-04-17 09:53:47 +00:00
|
|
|
await db
|
|
|
|
.execute("DROP TABLE IF EXISTS ${FriendsListTableContract.TABLE_NAME}");
|
2019-05-04 17:35:03 +00:00
|
|
|
|
2019-04-24 12:25:46 +00:00
|
|
|
// Initialize database again
|
2019-04-24 09:24:05 +00:00
|
|
|
await _initializeDatabase(db, newVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize the database
|
|
|
|
static Future<void> _initializeDatabase(Database db, int version) async {
|
2019-04-27 06:42:27 +00:00
|
|
|
// Create users table
|
2019-04-24 09:24:05 +00:00
|
|
|
await db.execute("CREATE TABLE ${UserTableContract.TABLE_NAME} ("
|
|
|
|
"${UserTableContract.C_ID} INTEGER PRIMARY KEY, "
|
|
|
|
"${UserTableContract.C_FIRST_NAME} TEXT, "
|
|
|
|
"${UserTableContract.C_LAST_NAME} TEXT, "
|
2019-04-24 12:25:46 +00:00
|
|
|
"${UserTableContract.C_VISIBILITY} TEXT, "
|
|
|
|
"${UserTableContract.C_VIRTUAL_DIRECTORY} TEXT, "
|
2020-04-28 16:47:47 +00:00
|
|
|
"${UserTableContract.C_ACCOUNT_IMAGE_URL} TEXT, "
|
|
|
|
"${UserTableContract.C_CUSTOM_EMOJIES} TEXT"
|
2019-04-24 09:24:05 +00:00
|
|
|
")");
|
2019-04-24 12:25:46 +00:00
|
|
|
|
2019-04-27 06:42:27 +00:00
|
|
|
// Create conversations table
|
2019-04-24 12:25:46 +00:00
|
|
|
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, "
|
2020-04-26 12:02:57 +00:00
|
|
|
"${ConversationTableContract.C_MEMBERS} TEXT, "
|
|
|
|
"${ConversationTableContract.C_CAN_EVERYONE_ADD_MEMBERS} INTEGER"
|
2019-04-24 12:25:46 +00:00
|
|
|
")");
|
2019-04-27 06:42:27 +00:00
|
|
|
|
|
|
|
// 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"
|
|
|
|
")");
|
2019-05-04 17:35:03 +00:00
|
|
|
|
|
|
|
// Friends list table
|
|
|
|
await db.execute("CREATE TABLE ${FriendsListTableContract.TABLE_NAME} ("
|
|
|
|
"${FriendsListTableContract.C_ID} INTEGER PRIMARY KEY, "
|
|
|
|
"${FriendsListTableContract.C_ACCEPTED} INTEGER, "
|
|
|
|
"${FriendsListTableContract.C_LAST_ACTIVE} INTEGER, "
|
|
|
|
"${FriendsListTableContract.C_FOLLOWING} INTEGER, "
|
|
|
|
"${FriendsListTableContract.C_CAN_POST_TEXTS} INTEGER"
|
|
|
|
")");
|
2019-04-24 09:24:05 +00:00
|
|
|
}
|
|
|
|
}
|