|
| 1 | +import 'dart:async'; |
| 2 | +import 'package:meta/meta.dart'; |
| 3 | + |
| 4 | +import 'package:sqlite_async/sqlite3_common.dart' as sqlite; |
| 5 | +import 'package:sqlite_async/src/common/mutex.dart'; |
| 6 | +import 'package:sqlite_async/src/sqlite_connection.dart'; |
| 7 | +import 'package:sqlite_async/src/sqlite_options.dart'; |
| 8 | +import 'package:sqlite_async/src/update_notification.dart'; |
| 9 | + |
| 10 | +/// Factory to create new SQLite database connections. |
| 11 | +/// |
| 12 | +/// Since connections are opened in dedicated background isolates, this class |
| 13 | +/// must be safe to pass to different isolates. |
| 14 | +abstract class SqliteOpenFactory<Database extends sqlite.CommonDatabase> { |
| 15 | + String get path; |
| 16 | + |
| 17 | + /// Opens a direct connection to the SQLite database |
| 18 | + FutureOr<Database> open(SqliteOpenOptions options); |
| 19 | + |
| 20 | + /// Opens an asynchronous [SqliteConnection] |
| 21 | + FutureOr<SqliteConnection> openConnection(SqliteOpenOptions options); |
| 22 | +} |
| 23 | + |
| 24 | +class SqliteOpenOptions { |
| 25 | + /// Whether this is the primary write connection for the database. |
| 26 | + final bool primaryConnection; |
| 27 | + |
| 28 | + /// Whether this connection is read-only. |
| 29 | + final bool readOnly; |
| 30 | + |
| 31 | + /// Mutex to use in [SqliteConnection]s |
| 32 | + final Mutex? mutex; |
| 33 | + |
| 34 | + /// Name used in debug logs |
| 35 | + final String? debugName; |
| 36 | + |
| 37 | + /// Stream of external update notifications |
| 38 | + final Stream<UpdateNotification>? updates; |
| 39 | + |
| 40 | + const SqliteOpenOptions( |
| 41 | + {required this.primaryConnection, |
| 42 | + required this.readOnly, |
| 43 | + this.mutex, |
| 44 | + this.debugName, |
| 45 | + this.updates}); |
| 46 | + |
| 47 | + sqlite.OpenMode get openMode { |
| 48 | + if (primaryConnection) { |
| 49 | + return sqlite.OpenMode.readWriteCreate; |
| 50 | + } else if (readOnly) { |
| 51 | + return sqlite.OpenMode.readOnly; |
| 52 | + } else { |
| 53 | + return sqlite.OpenMode.readWrite; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// The default database factory. |
| 59 | +/// |
| 60 | +/// This takes care of opening the database, and running PRAGMA statements |
| 61 | +/// to configure the connection. |
| 62 | +/// |
| 63 | +/// Override the [open] method to customize the process. |
| 64 | +abstract class AbstractDefaultSqliteOpenFactory< |
| 65 | + Database extends sqlite.CommonDatabase> |
| 66 | + implements SqliteOpenFactory<Database> { |
| 67 | + @override |
| 68 | + final String path; |
| 69 | + final SqliteOptions sqliteOptions; |
| 70 | + |
| 71 | + const AbstractDefaultSqliteOpenFactory( |
| 72 | + {required this.path, |
| 73 | + this.sqliteOptions = const SqliteOptions.defaults()}); |
| 74 | + |
| 75 | + List<String> pragmaStatements(SqliteOpenOptions options); |
| 76 | + |
| 77 | + @protected |
| 78 | + |
| 79 | + /// Opens a direct connection to a SQLite database connection |
| 80 | + FutureOr<Database> openDB(SqliteOpenOptions options); |
| 81 | + |
| 82 | + @override |
| 83 | + |
| 84 | + /// Opens a direct connection to a SQLite database connection |
| 85 | + /// and executes setup pragma statements to initialize the DB |
| 86 | + FutureOr<Database> open(SqliteOpenOptions options) async { |
| 87 | + var db = await openDB(options); |
| 88 | + |
| 89 | + // Pragma statements don't have the same BUSY_TIMEOUT behavior as normal statements. |
| 90 | + // We add a manual retry loop for those. |
| 91 | + for (var statement in pragmaStatements(options)) { |
| 92 | + for (var tries = 0; tries < 30; tries++) { |
| 93 | + try { |
| 94 | + db.execute(statement); |
| 95 | + break; |
| 96 | + } on sqlite.SqliteException catch (e) { |
| 97 | + if (e.resultCode == sqlite.SqlError.SQLITE_BUSY && tries < 29) { |
| 98 | + continue; |
| 99 | + } else { |
| 100 | + rethrow; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return db; |
| 106 | + } |
| 107 | + |
| 108 | + @override |
| 109 | + |
| 110 | + /// Opens an asynchronous [SqliteConnection] to a SQLite database |
| 111 | + /// and executes setup pragma statements to initialize the DB |
| 112 | + FutureOr<SqliteConnection> openConnection(SqliteOpenOptions options); |
| 113 | +} |
0 commit comments