Struct sqlx_oldapi::sqlite::SqliteConnectOptions

source ยท
pub struct SqliteConnectOptions { /* private fields */ }
Available on crate feature sqlite only.
Expand description

Options and flags which can be used to configure a SQLite connection.

A value of SqliteConnectOptions can be parsed from a connection URL, as described by SQLite.

URLDescription
sqlite::memory:Open an in-memory database.
sqlite:data.dbOpen the file data.db in the current directory.
sqlite://data.dbOpen the file data.db in the current directory.
sqlite:///data.dbOpen the file data.db from the root (/) directory.
sqlite://data.db?mode=roOpen the file data.db for read-only access.

ยงExample

use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode};
use std::str::FromStr;

let conn = SqliteConnectOptions::from_str("sqlite://data.db")?
    .journal_mode(SqliteJournalMode::Wal)
    .read_only(true)
    .connect().await?;

Implementationsยง

sourceยง

impl SqliteConnectOptions

source

pub fn new() -> SqliteConnectOptions

Construct Self with default options.

See the source of this method for the current defaults.

source

pub fn filename(self, filename: impl AsRef<Path>) -> SqliteConnectOptions

Sets the name of the database file.

source

pub fn foreign_keys(self, on: bool) -> SqliteConnectOptions

Set the enforcement of foreign key constraints.

SQLx chooses to enable this by default so that foreign keys function as expected, compared to other database flavors.

source

pub fn shared_cache(self, on: bool) -> SqliteConnectOptions

Set the SQLITE_OPEN_SHAREDCACHE flag.

By default, this is disabled.

source

pub fn journal_mode(self, mode: SqliteJournalMode) -> SqliteConnectOptions

Sets the journal mode for the database connection.

Journal modes are ephemeral per connection, with the exception of the Write-Ahead Log (WAL) mode.

A database created in WAL mode retains the setting and will apply it to all connections opened against it that donโ€™t set a journal_mode.

Opening a connection to a database created in WAL mode with a different journal_mode will erase the setting on the database, requiring an exclusive lock to do so. You may get a database is locked (corresponding to SQLITE_BUSY) error if another connection is accessing the database file at the same time.

SQLx does not set a journal mode by default, to avoid unintentionally changing a database into or out of WAL mode.

The default journal mode for non-WAL databases is DELETE, or MEMORY for in-memory databases.

For consistency, any commands in sqlx-cli which create a SQLite database will create it in WAL mode.

source

pub fn locking_mode(self, mode: SqliteLockingMode) -> SqliteConnectOptions

Sets the locking mode for the database connection.

The default locking mode is NORMAL.

source

pub fn read_only(self, read_only: bool) -> SqliteConnectOptions

Sets the access mode to open the database for read-only access.

source

pub fn create_if_missing(self, create: bool) -> SqliteConnectOptions

Sets the access mode to create the database file if the file does not exist.

By default, a new file will not be created if one is not found.

source

pub fn statement_cache_capacity(self, capacity: usize) -> SqliteConnectOptions

Sets the capacity of the connectionโ€™s statement cache in a number of stored distinct statements. Caching is handled using LRU, meaning when the amount of queries hits the defined limit, the oldest statement will get dropped.

The default cache capacity is 100 statements.

source

pub fn busy_timeout(self, timeout: Duration) -> SqliteConnectOptions

Sets a timeout value to wait when the database is locked, before returning a busy timeout error.

The default busy timeout is 5 seconds.

source

pub fn synchronous(self, synchronous: SqliteSynchronous) -> SqliteConnectOptions

Sets the synchronous setting for the database connection.

The default synchronous settings is FULL. However, if durability is not a concern, then NORMAL is normally all one needs in WAL mode.

source

pub fn auto_vacuum(self, auto_vacuum: SqliteAutoVacuum) -> SqliteConnectOptions

Sets the auto_vacuum setting for the database connection.

The default auto_vacuum setting is NONE.

For existing databases, a change to this value does not take effect unless a VACUUM command is executed.

source

pub fn page_size(self, page_size: u32) -> SqliteConnectOptions

Sets the page_size setting for the database connection.

The default page_size setting is 4096.

For existing databases, a change to this value does not take effect unless a VACUUM command is executed. However, it cannot be changed in WAL mode.

source

pub fn pragma<K, V>(self, key: K, value: V) -> SqliteConnectOptions
where K: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>,

Sets custom initial pragma for the database connection.

source

pub fn collation<N, F>(self, name: N, collate: F) -> SqliteConnectOptions
where N: Into<Arc<str>>, F: Fn(&str, &str) -> Ordering + Send + Sync + 'static,

Add a custom collation for comparing strings in SQL.

If a collation with the same name already exists, it will be replaced.

See sqlite3_create_collation() for details.

Note this excerpt:

The collating function must obey the following properties for all strings A, B, and C:

If A==B then B==A. If A==B and B==C then A==C. If A<B then B>A. If A<B and B<C then A<C.

If a collating function fails any of the above constraints and that collating function is registered and used, then the behavior of SQLite is undefined.

source

pub fn function(self, func: Function) -> SqliteConnectOptions

Add a custom function for use in SQL statements. If a function with the same name already exists, it will be replaced. See sqlite3_create_function_v2() for details.

ยงExample
ยงUnicode handling

By default, SQLite does not handle unicode in functions like lower or upper. To prevent binary bloat, it advises application developers to implement their own unicode-aware functions.

This is how you would implement a unicode-aware lower function:

use std::str::FromStr;
use sqlx::sqlite::{SqliteConnectOptions, SqliteConnection, SqliteFunctionCtx, Function};
let options = SqliteConnectOptions::from_str("sqlite://data.db")?
   .function(Function::new("lower", |ctx: &SqliteFunctionCtx| {
       let s = ctx.get_arg::<String>(0);
       let result = s.to_lowercase();
       ctx.set_result(result);
    }).deterministic());
source

pub fn immutable(self, immutable: bool) -> SqliteConnectOptions

Set to true to signal to SQLite that the database file is on read-only media.

If enabled, SQLite assumes the database file cannot be modified, even by higher privileged processes, and so disables locking and change detection. This is intended to improve performance but can produce incorrect query results or errors if the file does change.

Note that this is different from the SQLITE_OPEN_READONLY flag set by .read_only(), though the documentation suggests that this does imply SQLITE_OPEN_READONLY.

See sqlite3_open (subheading โ€œURI Filenamesโ€) for details.

source

pub fn serialized(self, serialized: bool) -> SqliteConnectOptions

Sets the threading mode for the database connection.

The default setting is false corresponding to using OPEN_NOMUTEX. If set to true then OPEN_FULLMUTEX.

See open for more details.

ยงNote

Setting this to true may help if you are getting access violation errors or segmentation faults, but will also incur a significant performance penalty. You should leave this set to false if at all possible.

If you do end up needing to set this to true for some reason, please open an issue as this may indicate a concurrency bug in SQLx. Please provide clear instructions for reproducing the issue, including a sample database schema if applicable.

source

pub fn thread_name( self, generator: impl Fn(u64) -> String + Send + Sync + 'static ) -> SqliteConnectOptions

Provide a callback to generate the name of the background worker thread.

The value passed to the callback is an auto-incremented integer for use as the thread ID.

source

pub fn command_buffer_size(self, size: usize) -> SqliteConnectOptions

Set the maximum number of commands to buffer for the worker thread before backpressure is applied.

Given that most commands sent to the worker thread involve waiting for a result, the command channel is unlikely to fill up unless a lot queries are executed in a short period but cancelled before their full resultsets are returned.

source

pub fn row_buffer_size(self, size: usize) -> SqliteConnectOptions

Set the maximum number of rows to buffer back to the calling task when a query is executed.

If the calling task cannot keep up, backpressure will be applied to the worker thread in order to limit CPU and memory usage.

source

pub fn vfs(self, vfs_name: impl Into<Cow<'static, str>>) -> SqliteConnectOptions

Sets the vfs parameter of the database connection.

The default value is empty, and sqlite will use the default VFS object depending on the operating system.

source

pub fn extension( self, extension_name: impl Into<Cow<'static, str>> ) -> SqliteConnectOptions

Load an extension at run-time when the database connection is established, using the default entry point.

Most common SQLite extensions can be loaded using this method, for extensions where you need to specify the entry point, use extension_with_entrypoint instead.

Multiple extensions can be loaded by calling the method repeatedly on the options struct, they will be loaded in the order they are added.

use std::str::FromStr;
use sqlx::sqlite::SqliteConnectOptions;
let options = SqliteConnectOptions::from_str("sqlite://data.db")?
    .extension("vsv")
    .extension("mod_spatialite");
source

pub fn extension_with_entrypoint( self, extension_name: impl Into<Cow<'static, str>>, entry_point: impl Into<Cow<'static, str>> ) -> SqliteConnectOptions

Load an extension with a specified entry point.

Useful when using non-standard extensions, or when developing your own, the second argument specifies where SQLite should expect to find the extension init routine.

Trait Implementationsยง

sourceยง

impl Clone for SqliteConnectOptions

sourceยง

fn clone(&self) -> SqliteConnectOptions

Returns a copy of the value. Read more
1.0.0 ยท sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
sourceยง

impl ConnectOptions for SqliteConnectOptions

ยง

type Connection = SqliteConnection

sourceยง

fn connect( &self ) -> Pin<Box<dyn Future<Output = Result<<SqliteConnectOptions as ConnectOptions>::Connection, Error>> + Send + '_>>

Establish a new database connection with the options specified by self.
sourceยง

fn log_statements(&mut self, level: LevelFilter) -> &mut SqliteConnectOptions

Log executed statements with the specified level
sourceยง

fn log_slow_statements( &mut self, level: LevelFilter, duration: Duration ) -> &mut SqliteConnectOptions

Log executed statements with a duration above the specified duration at the specified level.
sourceยง

fn disable_statement_logging(&mut self) -> &mut Self

Entirely disables statement logging (both slow and regular).
sourceยง

impl Debug for SqliteConnectOptions

sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
sourceยง

impl Default for SqliteConnectOptions

sourceยง

fn default() -> SqliteConnectOptions

Returns the โ€œdefault valueโ€ for a type. Read more
sourceยง

impl From<SqliteConnectOptions> for AnyConnectOptions

sourceยง

fn from(options: SqliteConnectOptions) -> AnyConnectOptions

Converts to this type from the input type.
sourceยง

impl FromStr for SqliteConnectOptions

ยง

type Err = Error

The associated error which can be returned from parsing.
sourceยง

fn from_str( url: &str ) -> Result<SqliteConnectOptions, <SqliteConnectOptions as FromStr>::Err>

Parses a string s to return a value of this type. Read more
sourceยง

impl TryFrom<AnyConnectOptions> for SqliteConnectOptions

ยง

type Error = Error

The type returned in the event of a conversion error.
sourceยง

fn try_from( value: AnyConnectOptions ) -> Result<SqliteConnectOptions, <SqliteConnectOptions as TryFrom<AnyConnectOptions>>::Error>

Performs the conversion.

Auto Trait Implementationsยง

Blanket Implementationsยง

sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
sourceยง

impl<T> From<T> for T

sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

sourceยง

impl<T> IntoEither for T

sourceยง

fn into_either(self, into_left: bool) -> Either<Self, Self> โ“˜

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
sourceยง

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ“˜
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
sourceยง

impl<T> Same for T

ยง

type Output = T

Should always be Self
sourceยง

impl<T> ToOwned for T
where T: Clone,

ยง

type Owned = T

The resulting type after obtaining ownership.
sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

ยง

type Error = Infallible

The type returned in the event of a conversion error.
sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

ยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
sourceยง

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

sourceยง

fn vzip(self) -> V