diff options
| author | xengineering <me@xengineering.eu> | 2026-04-06 17:03:46 +0200 |
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2026-04-06 17:03:46 +0200 |
| commit | 877a39e186699164e68ea969012a4f1ec6840ef8 (patch) | |
| tree | 92932f23c5b1d2798a9e63467b0ad4de70caa39f /lib/db.dart | |
| parent | 1809a88c679fcd17f29c13ddd47732bb65db96b2 (diff) | |
| parent | fcfb2a17733a38b690f4e034c78bc414be5527ef (diff) | |
| download | sia-app-877a39e186699164e68ea969012a4f1ec6840ef8.tar sia-app-877a39e186699164e68ea969012a4f1ec6840ef8.tar.zst sia-app-877a39e186699164e68ea969012a4f1ec6840ef8.zip | |
Merge persisting server fully qualified domain name
This allows to save the server fully qualified domain name (FQDN). The
user does not have to insert this on every app start.
Saved is the value of the corresponding text field on the last press on
the connect button.
Diffstat (limited to 'lib/db.dart')
| -rw-r--r-- | lib/db.dart | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/db.dart b/lib/db.dart new file mode 100644 index 0000000..0b2cf7a --- /dev/null +++ b/lib/db.dart @@ -0,0 +1,90 @@ +import 'dart:io'; +import 'dart:async'; + +import 'package:path_provider/path_provider.dart'; +import 'package:sqlite3/sqlite3.dart'; +import 'package:path/path.dart' as p; + +class DB { + final Completer<Database?> _dbCompleter = Completer<Database?>(); + static const List<String> migrations = <String>[ + ''' +PRAGMA user_version = 1; +CREATE TABLE "key_value" ( + "key" TEXT NOT NULL UNIQUE, + "value" TEXT, + PRIMARY KEY("key") +); + ''', + ]; + + Future<void> connect() async { + String path = await _getDbPath(); + Database candidate = sqlite3.open(path); + + migrate(candidate); + + _dbCompleter.complete(candidate); + } + + void dispose() async { + if (_dbCompleter.isCompleted == false) { + return; + } + Database? db = await _dbCompleter.future; + if (db == null) return; + db.close(); + } + + static Future<String> _getDbPath() async { + Directory supportDir = await getApplicationSupportDirectory(); + return p.join(supportDir.path, 'main.sqlite3'); + } + + static void migrate(Database db) { + for (int i=0; i<migrations.length; i++) { + int? userVersion = _getUserVersion(db); + if (userVersion == null) return; + + if (i == userVersion) { + db.execute(migrations[i]); + } + } + } + + static int? _getUserVersion(Database db) { + ResultSet result = db.select('PRAGMA user_version;'); + if (result.length != 1) return null; + return result.first.values.first as int; + } + + Future<String?> getServerFqdn() async { + Database? db = await _dbCompleter.future; + if (db == null) return null; + + ResultSet result = db.select( + 'SELECT value FROM key_value WHERE key = \'server_fqdn\';' + ); + if (result.length != 1) return null; + + return result[0]['value']; + } + + Future<void> setServerFqdn(String value) async { + Database? db = await _dbCompleter.future; + if (db == null) return; + + String? current = await getServerFqdn(); + if (current == null) { + db.execute( + 'INSERT INTO key_value (key, value) VALUES (\'server_fqdn\', ?);', + <Object?>[value], + ); + } else { + db.execute( + 'UPDATE key_value SET value = ? WHERE key = \'server_fqdn\';', + <Object?>[value], + ); + } + } +} |
