Repository: launchbadge/sqlx
File: sqlx-core/src/sqlite/options/connect.rs (Rust)

Code before:
use crate::connection::ConnectOptions;
use crate::error::Error;
use crate::executor::Executor;
use crate::sqlite::connection::establish::establish;
use crate::sqlite::{SqliteConnectOptions, SqliteConnection};
use futures_core::future::BoxFuture;
use log::LevelFilter;
use std::time::Duration;

impl ConnectOptions for SqliteConnectOptions {
    type Connection = SqliteConnection;

    fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
    where
        Self::Connection: Sized,
    {
        Box::pin(async move {
            let mut conn = establish(self).await?;

            // send an initial sql statement comprised of options

            // page_size must be set before any other action on the database.
            let page_size_opt = format!("PRAGMA key = {};", self.page_size);
            conn.execute(&*page_size_opt).await?;

            // apply custom initial_options
            match self.initial_options {
                Some(stmt) => {
                    conn.execute(&*stmt).await?;
                }
                None => (),
            }

            // Note that locking_mode should be set before journal_mode; see
            // https://www.sqlite.org/wal.html#use_of_wal_without_shared_memory .
            let init = format!(
                "PRAGMA locking_mode = {}; PRAGMA journal_mode = {}; PRAGMA foreign_keys = {}; PRAGMA synchronous = {}; PRAGMA auto_vacuum = {}",
                self.locking_mode.as_str(),
                self.journal_mode.as_str(),
                if self.foreign_keys { "ON" } else { "OFF" },
                self.synchronous.as_str(),
                self.auto_vacuum.as_str(),
            );

            conn.execute(&*init).await?;

            Ok(conn)
        })
    }


Review comment (suggestion):
Unless you intended to add something to the `None` branch:

```suggestion
            if let Some(stmt) = self.initial_options {
                conn.execute(&*stmt).await?;
            }
```

Code after:
use crate::connection::ConnectOptions;
use crate::error::Error;
use crate::executor::Executor;
use crate::sqlite::connection::establish::establish;
use crate::sqlite::{SqliteConnectOptions, SqliteConnection};
use futures_core::future::BoxFuture;
use log::LevelFilter;
use std::time::Duration;

impl ConnectOptions for SqliteConnectOptions {
    type Connection = SqliteConnection;

    fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
    where
        Self::Connection: Sized,
    {
        Box::pin(async move {
            let mut conn = establish(self).await?;

            // send an initial sql statement comprised of options
            let mut init = String::new();

            for (key, value) in self.pragmas.iter() {
                use std::fmt::Write;
                write!(init, "PRAGMA {} = {}; ", key, value).ok();
            }

            conn.execute(&*init).await?;

            Ok(conn)
        })
    }

    fn log_statements(&mut self, level: LevelFilter) -> &mut Self {
        self.log_settings.log_statements(level);
        self
    }

    fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self {
        self.log_settings.log_slow_statements(level, duration);
        self
    }
}