Struct 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() -> Self

Construct Self with default options.

See the source of this method for the current defaults.

Source

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

Sets the name of the database file.

Source

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

Set the enforcement of foreign key constraints.

By default, this is enabled.

Source

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

Set the SQLITE_OPEN_SHAREDCACHE flag.

By default, this is disabled.

Source

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

Sets the journal mode for the database connection.

The default journal mode is WAL. For most use cases this can be significantly faster but there are disadvantages.

Source

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

Sets the locking mode for the database connection.

The default locking mode is NORMAL.

Source

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

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

Source

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

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) -> Self

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) -> Self

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) -> Self

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) -> Self

Sets the auto_vacuum setting for the database connection.

The default auto_vacuum setting is NONE.

Source

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

Sets the page_size setting for the database connection.

The default page_size setting is 4096.

Source

pub fn pragma<K, V>(self, key: K, value: V) -> Self
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) -> Self
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 immutable(self, immutable: bool) -> Self

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) -> Self

Sets the threading mode for the database connection.

The default setting is false corersponding to using OPEN_NOMUTEX, if 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, ) -> Self

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) -> Self

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) -> Self

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.

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

Source§

type Connection = SqliteConnection

Source§

fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
where Self::Connection: Sized,

Establish a new database connection with the options specified by self.
Source§

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

Log executed statements with the specified level
Source§

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

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

Formats the value using the given formatter. Read more
Source§

impl Default for SqliteConnectOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<SqliteConnectOptions> for AnyConnectOptions

Source§

fn from(options: SqliteConnectOptions) -> Self

Converts to this type from the input type.
Source§

impl FromStr for SqliteConnectOptions

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(url: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl TryFrom<AnyConnectOptions> for SqliteConnectOptions

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyConnectOptions) -> Result<Self, Self::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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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>,

Source§

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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T