open method
CommonDatabase
open(
- String filename, {
- String? vfs,
- OpenMode mode = OpenMode.readWriteCreate,
- bool uri = false,
- bool? mutex,
inherited
Opens a database file.
The vfs option can be used to set the appropriate virtual file system
implementation. When null, the default file system will be used.
If uri is enabled (defaults to false), the filename will be
interpreted as an uri as according to https://www.sqlite.org/uri.html.
If the mutex parameter is set to true, the SQLITE_OPEN_FULLMUTEX flag
will be set. If it's set to false, SQLITE_OPEN_NOMUTEX will be enabled.
By default, neither parameter will be set.
Implementation
@override
CommonDatabase open(
String filename, {
String? vfs,
OpenMode mode = OpenMode.readWriteCreate,
bool uri = false,
bool? mutex,
}) {
initialize();
int flags;
switch (mode) {
case OpenMode.readOnly:
flags = SqlFlag.SQLITE_OPEN_READONLY;
break;
case OpenMode.readWrite:
flags = SqlFlag.SQLITE_OPEN_READWRITE;
break;
case OpenMode.readWriteCreate:
flags = SqlFlag.SQLITE_OPEN_READWRITE | SqlFlag.SQLITE_OPEN_CREATE;
break;
}
if (uri) {
flags |= SqlFlag.SQLITE_OPEN_URI;
}
if (mutex != null) {
flags |= mutex
? SqlFlag.SQLITE_OPEN_FULLMUTEX
: SqlFlag.SQLITE_OPEN_NOMUTEX;
}
final result = bindings.sqlite3_open_v2(filename, flags, vfs);
if (result.resultCode != SqlError.SQLITE_OK) {
// It's possible for us to have a database even if opening it failed. Only
// if allocating memory for the connection failed would we not have a
// database.
if (result.result case final database?) {
final exception = createExceptionRaw(
bindings,
database,
result.resultCode,
operation: 'opening the database',
);
// Close the database after creating the exception, which needs to read
// the extended error from the database.
database.sqlite3_close_v2();
throw exception;
} else {
throw createExceptionOutsideOfDatabase(bindings, result.resultCode);
}
}
return wrapDatabase(result.result!..sqlite3_extended_result_codes(1));
}