173 lines
4.5 KiB
Dart
173 lines
4.5 KiB
Dart
import 'dart:convert';
|
|
import 'dart:developer';
|
|
|
|
import 'package:hub/shared/helpers/base_storage.dart';
|
|
import 'package:sqflite/sqflite.dart';
|
|
import 'package:sqflite/sqlite_api.dart';
|
|
import 'package:path/path.dart';
|
|
|
|
class SQLiteStorage implements BaseStorage {
|
|
SQLiteStorage._();
|
|
|
|
static final String _dbName = 'database.db';
|
|
static final int _dbVersion = 2;
|
|
|
|
static const String tableKeychain = 'keychain';
|
|
static const String tableLicense = 'license';
|
|
|
|
static final SQLiteStorage instance = SQLiteStorage._();
|
|
|
|
static late final Database _database;
|
|
static Database get database => _database;
|
|
|
|
@override
|
|
Future<void> init() async {
|
|
_database = await openDatabase(
|
|
join(await getDatabasesPath(), _dbName),
|
|
version: _dbVersion,
|
|
onCreate: _onCreate,
|
|
onUpgrade: _onUpgrade,
|
|
onDowngrade: _onDowngrade,
|
|
);
|
|
}
|
|
|
|
_onCreate(Database database, int version) async {
|
|
log('Creating database...');
|
|
await database.execute(_keychain);
|
|
await database.execute(_license);
|
|
await initLicense(database);
|
|
}
|
|
|
|
_onUpgrade(Database database, int oldVersion, int newVersion) async {
|
|
log('Upgrading database from version $oldVersion to $newVersion...');
|
|
if (oldVersion < 2) {
|
|
await database.execute(_license);
|
|
await initLicense(database);
|
|
}
|
|
}
|
|
|
|
_onDowngrade(Database database, int oldVersion, int newVersion) async {}
|
|
|
|
String get _keychain => '''
|
|
CREATE TABLE keychain (
|
|
key TEXT UNIQUE,
|
|
value TEXT,
|
|
type TEXT,
|
|
updateAt TEXT,
|
|
resolvedAt TEXT,
|
|
createdAt TEXT
|
|
);
|
|
''';
|
|
|
|
String get _license => '''
|
|
CREATE TABLE license (
|
|
key TEXT UNIQUE,
|
|
display TEXT,
|
|
expirationDate TEXT,
|
|
startDate TEXT,
|
|
quantity TEXT
|
|
);
|
|
''';
|
|
|
|
static Future<void> initLicense(Database database) async {
|
|
log('initLicense()');
|
|
for (var element in LicenseStorageKey.values.map((e) => e.value)) {
|
|
await database.insert(
|
|
tableLicense,
|
|
{
|
|
'key': element,
|
|
'display': 'VISIVEL',
|
|
'expirationDate': '',
|
|
'startDate': '',
|
|
'quantity': '',
|
|
},
|
|
conflictAlgorithm: ConflictAlgorithm.ignore,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<String?> get(String key) async {
|
|
if (LicenseStorageKey.values.map((e) => e.value).toList().contains(key)) {
|
|
return await _getLicense(key);
|
|
} else {
|
|
return await _getKeychain(key);
|
|
}
|
|
}
|
|
|
|
Future<String?> _getKeychain(String key) async {
|
|
var response = await _database.query(tableKeychain, where: 'key = ?', whereArgs: [key]);
|
|
if (response.isEmpty) {
|
|
return null;
|
|
}
|
|
return response.first['value'].toString();
|
|
}
|
|
|
|
Future<String?> _getLicense(String key) async {
|
|
var response = await _database.query(tableLicense, where: 'key = ?', whereArgs: [key]);
|
|
if (response.isEmpty) {
|
|
return null;
|
|
}
|
|
return response.first['value'].toString();
|
|
}
|
|
|
|
Future<String?> getAll(String table) async {
|
|
var response = await _database.query(table);
|
|
if (response.isEmpty) {
|
|
return null;
|
|
}
|
|
return jsonEncode(response);
|
|
}
|
|
|
|
@override
|
|
Future<void> set(String key, String value) async {
|
|
if (LicenseStorageKey.values.map((e) => e.value).toList().contains(key)) {
|
|
return await _setLicense(key, value);
|
|
} else {
|
|
return await _setKeychain(key, value);
|
|
}
|
|
}
|
|
|
|
Future<void> _setKeychain(String key, String value) async {
|
|
var date = DateTime.now().toIso8601String();
|
|
await _database.insert(
|
|
tableKeychain,
|
|
{
|
|
'key': key,
|
|
'value': value,
|
|
'type': 'local',
|
|
'updateAt': date,
|
|
'resolvedAt': date,
|
|
'createdAt': date,
|
|
},
|
|
conflictAlgorithm: ConflictAlgorithm.replace);
|
|
}
|
|
|
|
Future<void> _setLicense(String key, String value) async {
|
|
log('setLicense($key, $value)');
|
|
Map<String, dynamic> valueMap = jsonDecode(value);
|
|
log('String to Map<String, dynamic>: $value to $valueMap');
|
|
|
|
await _database.insert(
|
|
tableLicense,
|
|
{
|
|
'key': key,
|
|
'display': valueMap['display'],
|
|
'expirationDate': valueMap['expirationDate'],
|
|
'startDate': valueMap['startDate'],
|
|
'quantity': valueMap['quantity'],
|
|
},
|
|
conflictAlgorithm: ConflictAlgorithm.replace);
|
|
}
|
|
|
|
@override
|
|
Future<void> delete(String key) async {
|
|
await _database.delete('keychain', where: 'key = ?', whereArgs: [key]);
|
|
}
|
|
|
|
@override
|
|
Future<void> clearAll() async {
|
|
await _database.delete('keychain');
|
|
}
|
|
}
|