NativeDatabase.opened constructor
- Database database, {
- bool logStatements = false,
- DatabaseSetup? setup,
- bool closeUnderlyingOnClose = true,
- bool enableMigrations = true,
- bool cachePreparedStatements = _cacheStatementsByDefault,
Creates a drift executor for an opened database
from the sqlite3
package.
When the closeUnderlyingOnClose
argument is set (which is the default),
calling QueryExecutor.close on the returned NativeDatabase will also
CommonDatabase.dispose
the database
passed to this constructor.
Using NativeDatabase.opened may be useful when you want to use the same
underlying Database
in multiple drift connections. Drift uses this
internally when running integration tests for migrations.
If logStatements
is true (defaults to false
), generated sql statements
will be printed before executing. This can be useful for debugging.
The cachePreparedStatements
flag (defaults to false
) controls whether
drift will cache prepared statement objects, which improves performance as
sqlite3 doesn't have to parse statements that are frequently used multiple
times. This will be the default in the next minor drift version.
The optional setup
function can be used to perform a setup just after
the database is opened, before drift is fully ready. This can be used to
add custom user-defined sql functions or to provide encryption keys in
SQLCipher implementations.
By default, drift runs migrations defined in your database class to create
tables when the database is first opened or to alter when when your schema
changes. This uses the user_version
sqlite3 pragma, which is compared
against the schemaVersion
getter of the database.
If you want to manage migrations independently or don't need them at all,
you can disable migrations in drift with the enableMigrations
parameter.
Implementation
factory NativeDatabase.opened(
Database database, {
bool logStatements = false,
DatabaseSetup? setup,
bool closeUnderlyingOnClose = true,
bool enableMigrations = true,
bool cachePreparedStatements = _cacheStatementsByDefault,
}) {
return NativeDatabase._(
_NativeDelegate.opened(
database,
setup,
closeUnderlyingOnClose,
cachePreparedStatements,
enableMigrations,
),
logStatements);
}