ente/lib/db/collections_db.dart

220 lines
6.5 KiB
Dart
Raw Normal View History

2020-11-02 14:38:59 +00:00
import 'dart:convert';
2020-10-10 22:03:25 +00:00
import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:photos/models/collection.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_migration/sqflite_migration.dart';
2020-10-10 22:03:25 +00:00
class CollectionsDB {
static final _databaseName = "ente.collections.db";
2021-01-24 18:58:21 +00:00
static final table = 'collections';
static final tempTable = 'temp_collections';
2021-10-17 16:04:30 +00:00
static final _sqlBoolTrue = 1;
static final _sqlBoolFalse = 0;
2020-10-10 22:03:25 +00:00
static final columnID = 'collection_id';
2020-11-02 14:38:59 +00:00
static final columnOwner = 'owner';
2020-10-10 22:03:25 +00:00
static final columnEncryptedKey = 'encrypted_key';
static final columnKeyDecryptionNonce = 'key_decryption_nonce';
static final columnName = 'name';
static final columnEncryptedName = 'encrypted_name';
static final columnNameDecryptionNonce = 'name_decryption_nonce';
2020-10-10 22:03:25 +00:00
static final columnType = 'type';
2020-10-10 23:44:04 +00:00
static final columnEncryptedPath = 'encrypted_path';
static final columnPathDecryptionNonce = 'path_decryption_nonce';
2021-02-14 18:33:57 +00:00
static final columnVersion = 'version';
2020-11-02 14:38:59 +00:00
static final columnSharees = 'sharees';
2020-10-31 12:48:41 +00:00
static final columnUpdationTime = 'updation_time';
2021-10-12 20:28:40 +00:00
static final columnIsDeleted = 'is_deleted';
2020-10-10 22:03:25 +00:00
2021-01-24 18:58:21 +00:00
static final intitialScript = [...createTable(table)];
2021-01-23 12:36:03 +00:00
static final migrationScripts = [
...alterNameToAllowNULL(),
...addEncryptedName(),
2021-02-14 18:33:57 +00:00
...addVersion(),
2021-10-12 20:28:40 +00:00
...addIsDeleted(),
2021-01-23 12:36:03 +00:00
];
final dbConfig = MigrationConfig(
initializationScript: intitialScript, migrationScripts: migrationScripts);
2020-10-10 22:03:25 +00:00
CollectionsDB._privateConstructor();
2021-10-17 16:04:30 +00:00
2020-10-10 22:03:25 +00:00
static final CollectionsDB instance = CollectionsDB._privateConstructor();
2021-07-21 20:34:53 +00:00
static Future<Database> _dbFuture;
2020-10-10 22:03:25 +00:00
Future<Database> get database async {
2021-07-21 20:34:53 +00:00
_dbFuture ??= _initDatabase();
return _dbFuture;
2020-10-10 22:03:25 +00:00
}
2021-07-21 20:34:53 +00:00
Future<Database> _initDatabase() async {
2020-10-10 22:03:25 +00:00
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
return await openDatabaseWithMigration(path, dbConfig);
2020-10-10 22:03:25 +00:00
}
2021-03-17 21:07:17 +00:00
Future<void> clearTable() async {
final db = await instance.database;
await db.delete(table);
}
2021-01-24 18:58:21 +00:00
static List<String> createTable(String tableName) {
return [
'''
2021-01-24 07:23:42 +00:00
CREATE TABLE $tableName (
$columnID INTEGER PRIMARY KEY NOT NULL,
$columnOwner TEXT NOT NULL,
$columnEncryptedKey TEXT NOT NULL,
$columnKeyDecryptionNonce TEXT,
$columnName TEXT,
$columnType TEXT NOT NULL,
$columnEncryptedPath TEXT,
$columnPathDecryptionNonce TEXT,
$columnSharees TEXT,
$columnUpdationTime TEXT NOT NULL
);
'''
];
2021-01-23 12:36:03 +00:00
}
static List<String> alterNameToAllowNULL() {
return [
2021-01-24 18:58:21 +00:00
...createTable(tempTable),
'''
2021-01-24 18:58:21 +00:00
INSERT INTO $tempTable
2021-01-24 07:23:42 +00:00
SELECT *
2021-01-24 18:58:21 +00:00
FROM $table;
DROP TABLE $table;
ALTER TABLE $tempTable
RENAME TO $table;
'''
];
2021-01-23 12:36:03 +00:00
}
static List<String> addEncryptedName() {
return [
'''
2021-01-24 18:58:21 +00:00
ALTER TABLE $table
2021-01-24 07:23:42 +00:00
ADD COLUMN $columnEncryptedName TEXT;
''',
2021-01-24 18:58:21 +00:00
'''ALTER TABLE $table
2021-01-24 07:23:42 +00:00
ADD COLUMN $columnNameDecryptionNonce TEXT;
'''
];
2021-01-23 12:36:03 +00:00
}
2021-02-14 18:33:57 +00:00
static List<String> addVersion() {
return [
'''
ALTER TABLE $table
2021-10-17 16:55:42 +00:00
ADD COLUMN $columnVersion INTEGER DEFAULT 0;
2021-02-14 18:33:57 +00:00
'''
];
}
2021-10-12 20:28:40 +00:00
static List<String> addIsDeleted() {
return [
'''
ALTER TABLE $table
2021-10-17 16:55:42 +00:00
ADD COLUMN $columnIsDeleted INTEGER DEFAULT $_sqlBoolFalse;
2021-10-12 20:28:40 +00:00
'''
];
}
2020-10-10 22:03:25 +00:00
Future<List<dynamic>> insert(List<Collection> collections) async {
final db = await instance.database;
var batch = db.batch();
for (final collection in collections) {
2021-01-24 18:58:21 +00:00
batch.insert(table, _getRowForCollection(collection),
2020-10-13 20:12:37 +00:00
conflictAlgorithm: ConflictAlgorithm.replace);
}
return await batch.commit();
}
Future<List<Collection>> getAllCollections() async {
2020-10-10 23:44:04 +00:00
final db = await instance.database;
2021-01-24 18:58:21 +00:00
final rows = await db.query(table);
2021-07-22 18:41:58 +00:00
final collections = <Collection>[];
2020-10-10 23:44:04 +00:00
for (final row in rows) {
collections.add(_convertToCollection(row));
}
return collections;
}
2020-10-31 12:48:41 +00:00
Future<int> getLastCollectionUpdationTime() async {
2020-10-10 22:03:25 +00:00
final db = await instance.database;
final rows = await db.query(
2021-01-24 18:58:21 +00:00
table,
2020-10-31 12:48:41 +00:00
orderBy: '$columnUpdationTime DESC',
2020-10-13 20:12:37 +00:00
limit: 1,
);
if (rows.isNotEmpty) {
2020-10-31 12:48:41 +00:00
return int.parse(rows[0][columnUpdationTime]);
2020-10-13 20:12:37 +00:00
} else {
return null;
}
}
2020-10-31 12:48:41 +00:00
Future<int> deleteCollection(int collectionID) async {
final db = await instance.database;
return db.delete(
2021-01-24 18:58:21 +00:00
table,
2020-10-31 12:48:41 +00:00
where: '$columnID = ?',
whereArgs: [collectionID],
);
}
2020-10-10 22:03:25 +00:00
Map<String, dynamic> _getRowForCollection(Collection collection) {
2021-07-22 18:41:58 +00:00
var row = <String, dynamic>{};
2020-10-10 22:03:25 +00:00
row[columnID] = collection.id;
2020-11-02 14:38:59 +00:00
row[columnOwner] = collection.owner.toJson();
2020-10-10 22:03:25 +00:00
row[columnEncryptedKey] = collection.encryptedKey;
row[columnKeyDecryptionNonce] = collection.keyDecryptionNonce;
row[columnName] = collection.name;
2021-01-23 12:36:03 +00:00
row[columnEncryptedName] = collection.encryptedName;
row[columnNameDecryptionNonce] = collection.nameDecryptionNonce;
row[columnType] = Collection.typeToString(collection.type);
2020-10-21 22:22:09 +00:00
row[columnEncryptedPath] = collection.attributes.encryptedPath;
row[columnPathDecryptionNonce] = collection.attributes.pathDecryptionNonce;
2021-02-14 18:33:57 +00:00
row[columnVersion] = collection.attributes.version;
2020-11-02 14:38:59 +00:00
row[columnSharees] =
json.encode(collection.sharees?.map((x) => x?.toMap())?.toList());
2020-10-31 12:48:41 +00:00
row[columnUpdationTime] = collection.updationTime;
2021-10-17 16:04:30 +00:00
if (collection.isDeleted ?? false) {
row[columnIsDeleted] = _sqlBoolTrue;
2021-10-12 20:28:40 +00:00
} else {
2021-10-24 03:58:08 +00:00
row[columnIsDeleted] = _sqlBoolFalse;
2021-10-12 20:28:40 +00:00
}
2020-10-10 22:03:25 +00:00
return row;
}
Collection _convertToCollection(Map<String, dynamic> row) {
return Collection(
row[columnID],
2020-11-02 14:38:59 +00:00
User.fromJson(row[columnOwner]),
2020-10-10 22:03:25 +00:00
row[columnEncryptedKey],
row[columnKeyDecryptionNonce],
row[columnName],
row[columnEncryptedName],
row[columnNameDecryptionNonce],
Collection.typeFromString(row[columnType]),
2020-10-21 22:22:09 +00:00
CollectionAttributes(
2021-02-14 18:33:57 +00:00
encryptedPath: row[columnEncryptedPath],
pathDecryptionNonce: row[columnPathDecryptionNonce],
version: row[columnVersion],
),
2020-11-02 14:38:59 +00:00
List<User>.from((json.decode(row[columnSharees]) as List)
.map((x) => User.fromMap(x))),
2020-10-31 12:48:41 +00:00
int.parse(row[columnUpdationTime]),
2021-10-17 16:04:30 +00:00
// default to False is columnIsDeleted is not set
isDeleted: (row[columnIsDeleted] ?? _sqlBoolFalse) == _sqlBoolTrue,
2020-10-10 22:03:25 +00:00
);
}
}