ente/lib/db/public_keys_db.dart

90 lines
2.2 KiB
Dart
Raw Normal View History

2020-10-17 18:16:23 +00:00
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart';
2021-07-21 20:34:53 +00:00
import 'package:path_provider/path_provider.dart';
2020-10-18 21:39:55 +00:00
import 'package:photos/models/public_key.dart';
2020-10-17 18:16:23 +00:00
import 'package:sqflite/sqflite.dart';
class PublicKeysDB {
2022-07-04 06:02:17 +00:00
static const _databaseName = "ente.public_keys.db";
static const _databaseVersion = 1;
2020-10-17 18:16:23 +00:00
2022-07-04 06:02:17 +00:00
static const table = 'public_keys';
2020-10-17 18:16:23 +00:00
2022-07-04 06:02:17 +00:00
static const columnEmail = 'email';
static const columnPublicKey = 'public_key';
2020-10-17 18:16:23 +00:00
PublicKeysDB._privateConstructor();
static final PublicKeysDB instance = PublicKeysDB._privateConstructor();
static Future<Database>? _dbFuture;
2021-07-21 20:34:53 +00:00
2020-10-17 18:16:23 +00:00
Future<Database> get database async {
2021-07-21 20:34:53 +00:00
_dbFuture ??= _initDatabase();
return _dbFuture!;
2020-10-17 18:16:23 +00:00
}
Future<Database> _initDatabase() async {
final Directory documentsDirectory =
await getApplicationDocumentsDirectory();
2022-08-29 14:43:31 +00:00
final String path = join(documentsDirectory.path, _databaseName);
2020-10-17 18:16:23 +00:00
return await openDatabase(
path,
version: _databaseVersion,
onCreate: _onCreate,
);
}
Future _onCreate(Database db, int version) async {
2022-06-11 08:23:52 +00:00
await db.execute(
'''
2020-10-17 18:16:23 +00:00
CREATE TABLE $table (
$columnEmail TEXT PRIMARY KEY NOT NULL,
$columnPublicKey TEXT NOT NULL
)
2022-06-11 08:23:52 +00:00
''',
);
2020-10-17 18:16:23 +00:00
}
2021-03-17 21:07:17 +00:00
Future<void> clearTable() async {
final db = await instance.database;
await db.delete(table);
}
2020-10-18 21:39:55 +00:00
Future<int> setKey(PublicKey key) async {
2020-10-17 18:16:23 +00:00
final db = await instance.database;
2022-06-11 08:23:52 +00:00
return db.insert(
table,
_getRow(key),
conflictAlgorithm: ConflictAlgorithm.replace,
);
2020-10-17 18:16:23 +00:00
}
2020-10-18 21:39:55 +00:00
Future<List<PublicKey>> searchByEmail(String email) async {
2020-10-17 18:16:23 +00:00
final db = await instance.database;
2022-06-11 08:23:52 +00:00
return _convertRows(
await db.query(
table,
where: '$columnEmail LIKE ?',
whereArgs: ['%$email%'],
),
);
2020-10-17 18:16:23 +00:00
}
2020-10-18 21:39:55 +00:00
Map<String, dynamic> _getRow(PublicKey key) {
2022-08-29 14:43:31 +00:00
final row = <String, dynamic>{};
2020-10-18 21:39:55 +00:00
row[columnEmail] = key.email;
row[columnPublicKey] = key.publicKey;
2020-10-17 18:16:23 +00:00
return row;
}
2020-10-18 21:39:55 +00:00
List<PublicKey> _convertRows(List<Map<String, dynamic>> rows) {
2021-07-22 18:41:58 +00:00
final keys = <PublicKey>[];
2020-10-17 18:16:23 +00:00
for (final row in rows) {
2020-10-18 21:39:55 +00:00
keys.add(PublicKey(row[columnEmail], row[columnPublicKey]));
2020-10-17 18:16:23 +00:00
}
return keys;
}
}