#[cfg(not(all(target_family = "wasm", target_os = "unknown")))] extern crate libsqlite3_sys as ffi; #[cfg(all(target_family = "wasm", target_os = "unknown"))] use sqlite_wasm_rs as ffi; mod bind_collector; mod functions; mod owned_row; mod raw; mod row; mod serialized_database; mod sqlite_value; mod statement_iterator; mod stmt; pub(in crate::sqlite) use self::bind_collector::SqliteBindCollector; pub use self::bind_collector::SqliteBindValue; pub use self::serialized_database::SerializedDatabase; pub use self::sqlite_value::SqliteValue; use std::os::raw as libc; use self::raw::RawConnection; use self::statement_iterator::*; use self::stmt::{Statement, StatementUse}; use super::SqliteAggregateFunction; use crate::connection::instrumentation::{DynInstrumentation, StrQueryHelper}; use crate::connection::statement_cache::StatementCache; use crate::connection::*; use crate::deserialize::{FromSqlRow, StaticallySizedRow}; use crate::expression::QueryMetadata; use crate::query_builder::*; use crate::result::*; use crate::serialize::ToSql; use crate::sql_types::{HasSqlType, TypeMetadata}; use crate::sqlite::Sqlite; /// Connections for the SQLite backend. Unlike other backends, SQLite supported /// connection URLs are: /// /// - File paths (`test.db`) /// - [URIs](https://sqlite.org/uri.html) (`file://test.db`) /// - Special identifiers (`:memory:`) /// /// # Supported loading model implementations /// /// * [`DefaultLoadingMode`] /// /// As `SqliteConnection` only supports a single loading mode implementation, /// it is **not required** to explicitly specify a loading mode /// when calling [`RunQueryDsl::load_iter()`] or [`LoadConnection::load`] /// /// [`RunQueryDsl::load_iter()`]: crate::query_dsl::RunQueryDsl::load_iter /// /// ## DefaultLoadingMode /// /// `SqliteConnection` only supports a single loading mode, which loads /// values row by row from the result set. /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use schema::users; /// # let connection = &mut establish_connection(); /// use diesel::connection::DefaultLoadingMode; /// { /// // scope to restrict the lifetime of the iterator /// let iter1 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?; /// /// for r in iter1 { /// let (id, name) = r?; /// println!("Id: {} Name: {}", id, name); /// } /// } /// /// // works without specifying the loading mode /// let iter2 = users::table.load_iter::<(i32, String), _>(connection)?; /// /// for r in iter2 { /// let (id, name) = r?; /// println!("Id: {} Name: {}", id, name); /// } /// # Ok(()) /// # } /// ``` /// /// This mode does **not support** creating /// multiple iterators using the same connection. /// /// ```compile_fail /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use schema::users; /// # let connection = &mut establish_connection(); /// use diesel::connection::DefaultLoadingMode; /// /// let iter1 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?; /// let iter2 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?; /// /// for r in iter1 { /// let (id, name) = r?; /// println!("Id: {} Name: {}", id, name); /// } /// /// for r in iter2 { /// let (id, name) = r?; /// println!("Id: {} Name: {}", id, name); /// } /// # Ok(()) /// # } /// ``` /// /// # Concurrency /// /// By default, when running into a database lock, the operation will abort with a /// `Database locked` error. However, it's possible to configure it for greater concurrency, /// trading latency for not having to deal with retries yourself. /// /// You can use this example as blue-print for which statements to run after establishing a connection. /// It is **important** to run each `PRAGMA` in a single statement to make sure all of them apply /// correctly. In addition the order of the `PRAGMA` statements is relevant to prevent timeout /// issues for the later `PRAGMA` statements. /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use schema::users; /// use diesel::connection::SimpleConnection; /// let conn = &mut establish_connection(); /// // see https://fractaledmind.github.io/2023/09/07/enhancing-rails-sqlite-fine-tuning/ /// // sleep if the database is busy, this corresponds to up to 2 seconds sleeping time. /// conn.batch_execute("PRAGMA busy_timeout = 2000;")?; /// // better write-concurrency /// conn.batch_execute("PRAGMA journal_mode = WAL;")?; /// // fsync only in critical moments /// conn.batch_execute("PRAGMA synchronous = NORMAL;")?; /// // write WAL changes back every 1000 pages, for an in average 1MB WAL file. /// // May affect readers if number is increased /// conn.batch_execute("PRAGMA wal_autocheckpoint = 1000;")?; /// // free some space by truncating possibly massive WAL files from the last run /// conn.batch_execute("PRAGMA wal_checkpoint(TRUNCATE);")?; /// # Ok(()) /// # } /// ``` #[allow(missing_debug_implementations)] #[cfg(feature = "sqlite")] pub struct SqliteConnection { // statement_cache needs to be before raw_connection // otherwise we will get errors about open statements before closing the // connection itself statement_cache: StatementCache, raw_connection: RawConnection, transaction_state: AnsiTransactionManager, // this exists for the sole purpose of implementing `WithMetadataLookup` trait // and avoiding static mut which will be deprecated in 2024 edition metadata_lookup: (), instrumentation: DynInstrumentation, // We potentially need to store a serialized // database in here to make sure the database bytes // live as long as the connection // This is used by SqliteConnection::deserialize_readonly_database_from_buffer // only // This field needs to come after the RawConnection // as we need to make sure the data are still there until the // connection is dropped // // We are not allowed to modify the inner buffer until the database connection is dropped serialized_data: Vec>, } // This relies on the invariant that RawConnection or Statement are never // leaked. If a reference to one of those was held on a different thread, this // would not be thread safe. #[allow(unsafe_code)] unsafe impl Send for SqliteConnection {} impl SimpleConnection for SqliteConnection { fn batch_execute(&mut self, query: &str) -> QueryResult<()> { self.instrumentation .on_connection_event(InstrumentationEvent::StartQuery { query: &StrQueryHelper::new(query), }); let resp = self.raw_connection.exec(query); self.instrumentation .on_connection_event(InstrumentationEvent::FinishQuery { query: &StrQueryHelper::new(query), error: resp.as_ref().err(), }); resp } } impl ConnectionSealed for SqliteConnection {} impl Connection for SqliteConnection { type Backend = Sqlite; type TransactionManager = AnsiTransactionManager; /// Establish a connection to the database specified by `database_url`. /// /// See [SqliteConnection] for supported `database_url`. /// /// If the database does not exist, this method will try to /// create a new database and then establish a connection to it. /// /// ## WASM support /// /// If you plan to use this connection type on the `wasm32-unknown-unknown` target please /// make sure to read the following notes: /// /// * The database is stored in memory by default. /// * Persistent VFS (Virtual File Systems) is optional, /// see for details fn establish(database_url: &str) -> ConnectionResult { let mut instrumentation = DynInstrumentation::default_instrumentation(); instrumentation.on_connection_event(InstrumentationEvent::StartEstablishConnection { url: database_url, }); let establish_result = Self::establish_inner(database_url); instrumentation.on_connection_event(InstrumentationEvent::FinishEstablishConnection { url: database_url, error: establish_result.as_ref().err(), }); let mut conn = establish_result?; conn.instrumentation = instrumentation; Ok(conn) } fn execute_returning_count(&mut self, source: &T) -> QueryResult where T: QueryFragment + QueryId, { let statement_use = self.prepared_query(source)?; statement_use.run().and_then(|_| { self.raw_connection .rows_affected_by_last_query() .map_err(Error::DeserializationError) }) } fn transaction_state(&mut self) -> &mut AnsiTransactionManager where Self: Sized, { &mut self.transaction_state } fn instrumentation(&mut self) -> &mut dyn Instrumentation { &mut *self.instrumentation } fn set_instrumentation(&mut self, instrumentation: impl Instrumentation) { self.instrumentation = instrumentation.into(); } fn set_prepared_statement_cache_size(&mut self, size: CacheSize) { self.statement_cache.set_cache_size(size); } } impl LoadConnection for SqliteConnection { type Cursor<'conn, 'query> = StatementIterator<'conn, 'query>; type Row<'conn, 'query> = self::row::SqliteRow<'conn, 'query>; fn load<'conn, 'query, T>( &'conn mut self, source: T, ) -> QueryResult> where T: Query + QueryFragment + QueryId + 'query, Self::Backend: QueryMetadata, { let statement = self.prepared_query(source)?; Ok(StatementIterator::new(statement)) } } impl WithMetadataLookup for SqliteConnection { fn metadata_lookup(&mut self) -> &mut ::MetadataLookup { &mut self.metadata_lookup } } #[cfg(feature = "r2d2")] impl crate::r2d2::R2D2Connection for crate::sqlite::SqliteConnection { fn ping(&mut self) -> QueryResult<()> { use crate::RunQueryDsl; crate::r2d2::CheckConnectionQuery.execute(self).map(|_| ()) } fn is_broken(&mut self) -> bool { AnsiTransactionManager::is_broken_transaction_manager(self) } } impl MultiConnectionHelper for SqliteConnection { fn to_any<'a>( lookup: &mut ::MetadataLookup, ) -> &mut (dyn std::any::Any + 'a) { lookup } fn from_any( lookup: &mut dyn std::any::Any, ) -> Option<&mut ::MetadataLookup> { lookup.downcast_mut() } } impl SqliteConnection { /// Run a transaction with `BEGIN IMMEDIATE` /// /// This method will return an error if a transaction is already open. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # let mut conn = SqliteConnection::establish(":memory:").unwrap(); /// conn.immediate_transaction(|conn| { /// // Do stuff in a transaction /// Ok(()) /// }) /// # } /// ``` pub fn immediate_transaction(&mut self, f: F) -> Result where F: FnOnce(&mut Self) -> Result, E: From, { self.transaction_sql(f, "BEGIN IMMEDIATE") } /// Run a transaction with `BEGIN EXCLUSIVE` /// /// This method will return an error if a transaction is already open. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # let mut conn = SqliteConnection::establish(":memory:").unwrap(); /// conn.exclusive_transaction(|conn| { /// // Do stuff in a transaction /// Ok(()) /// }) /// # } /// ``` pub fn exclusive_transaction(&mut self, f: F) -> Result where F: FnOnce(&mut Self) -> Result, E: From, { self.transaction_sql(f, "BEGIN EXCLUSIVE") } fn transaction_sql(&mut self, f: F, sql: &str) -> Result where F: FnOnce(&mut Self) -> Result, E: From, { AnsiTransactionManager::begin_transaction_sql(&mut *self, sql)?; match f(&mut *self) { Ok(value) => { AnsiTransactionManager::commit_transaction(&mut *self)?; Ok(value) } Err(e) => { AnsiTransactionManager::rollback_transaction(&mut *self)?; Err(e) } } } fn prepared_query<'conn, 'query, T>( &'conn mut self, source: T, ) -> QueryResult> where T: QueryFragment + QueryId + 'query, { self.instrumentation .on_connection_event(InstrumentationEvent::StartQuery { query: &crate::debug_query(&source), }); let raw_connection = &self.raw_connection; let cache = &mut self.statement_cache; let statement = match cache.cached_statement( &source, &Sqlite, &[], raw_connection, Statement::prepare, &mut *self.instrumentation, ) { Ok(statement) => statement, Err(e) => { self.instrumentation .on_connection_event(InstrumentationEvent::FinishQuery { query: &crate::debug_query(&source), error: Some(&e), }); return Err(e); } }; StatementUse::bind(statement, source, &mut *self.instrumentation) } #[doc(hidden)] pub fn register_sql_function( &mut self, fn_name: &str, deterministic: bool, mut f: F, ) -> QueryResult<()> where F: FnMut(Args) -> Ret + std::panic::UnwindSafe + Send + 'static, Args: FromSqlRow + StaticallySizedRow, Ret: ToSql, Sqlite: HasSqlType, { functions::register( &self.raw_connection, fn_name, deterministic, move |_, args| f(args), ) } #[doc(hidden)] pub fn register_noarg_sql_function( &self, fn_name: &str, deterministic: bool, f: F, ) -> QueryResult<()> where F: FnMut() -> Ret + std::panic::UnwindSafe + Send + 'static, Ret: ToSql, Sqlite: HasSqlType, { functions::register_noargs(&self.raw_connection, fn_name, deterministic, f) } #[doc(hidden)] pub fn register_aggregate_function( &mut self, fn_name: &str, ) -> QueryResult<()> where A: SqliteAggregateFunction + 'static + Send + std::panic::UnwindSafe, Args: FromSqlRow + StaticallySizedRow, Ret: ToSql, Sqlite: HasSqlType, { functions::register_aggregate::<_, _, _, _, A>(&self.raw_connection, fn_name) } /// Register a collation function. /// /// `collation` must always return the same answer given the same inputs. /// If `collation` panics and unwinds the stack, the process is aborted, since it is used /// across a C FFI boundary, which cannot be unwound across and there is no way to /// signal failures via the SQLite interface in this case.. /// /// If the name is already registered it will be overwritten. /// /// This method will return an error if registering the function fails, either due to an /// out-of-memory situation or because a collation with that name already exists and is /// currently being used in parallel by a query. /// /// The collation needs to be specified when creating a table: /// `CREATE TABLE my_table ( str TEXT COLLATE MY_COLLATION )`, /// where `MY_COLLATION` corresponds to name passed as `collation_name`. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # let mut conn = SqliteConnection::establish(":memory:").unwrap(); /// // sqlite NOCASE only works for ASCII characters, /// // this collation allows handling UTF-8 (barring locale differences) /// conn.register_collation("RUSTNOCASE", |rhs, lhs| { /// rhs.to_lowercase().cmp(&lhs.to_lowercase()) /// }) /// # } /// ``` pub fn register_collation(&mut self, collation_name: &str, collation: F) -> QueryResult<()> where F: Fn(&str, &str) -> std::cmp::Ordering + Send + 'static + std::panic::UnwindSafe, { self.raw_connection .register_collation_function(collation_name, collation) } /// Serialize the current SQLite database into a byte buffer. /// /// The serialized data is identical to the data that would be written to disk if the database /// was saved in a file. /// /// # Returns /// /// This function returns a byte slice representing the serialized database. pub fn serialize_database_to_buffer(&mut self) -> SerializedDatabase { self.raw_connection.serialize() } /// Deserialize an SQLite database from a byte buffer. /// /// This function takes a byte slice and attempts to deserialize it into a SQLite database. /// If successful, the database is loaded into the connection. If the deserialization fails, /// an error is returned. /// /// The database is opened in READONLY mode. /// /// # Example /// /// ```no_run /// # use diesel::sqlite::SerializedDatabase; /// # use diesel::sqlite::SqliteConnection; /// # use diesel::result::QueryResult; /// # use diesel::sql_query; /// # use diesel::Connection; /// # use diesel::RunQueryDsl; /// # fn main() { /// let connection = &mut SqliteConnection::establish(":memory:").unwrap(); /// /// sql_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)") /// .execute(connection).unwrap(); /// sql_query("INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com'), ('Jane Doe', 'jane.doe@example.com')") /// .execute(connection).unwrap(); /// /// // Serialize the database to a byte vector /// let serialized_db: SerializedDatabase = connection.serialize_database_to_buffer(); /// /// // Create a new in-memory SQLite database /// let connection = &mut SqliteConnection::establish(":memory:").unwrap(); /// /// // Deserialize the byte vector into the new database /// connection.deserialize_readonly_database_from_buffer(serialized_db.as_slice()).unwrap(); /// # /// # } /// ``` // TODO: Diesel 3.0 This signature needs to change, we want to expose more options (schema name, readonly) // and also ensure that this is not as unsafe as the current construct anymore. Maybe just accept a owned buffer or static pointer // only instead? (So `Cow<'static, [u8]>`?) #[allow(unsafe_code)] pub fn deserialize_readonly_database_from_buffer(&mut self, data: &[u8]) -> QueryResult<()> { // we copy the buffer here // to make sure the underlying buffer lives as long as the connection self.serialized_data.push(data.to_vec()); let last = self .serialized_data .last() .expect("We literally pushed it above, so it's there"); unsafe { // SAFETY: We store the buffer inside of the connection and we never touch it until // we drop the connection self.raw_connection.deserialize(last) } } fn register_diesel_sql_functions(&self) -> QueryResult<()> { use crate::sql_types::{Integer, Text}; functions::register::( &self.raw_connection, "diesel_manage_updated_at", false, |conn, table_name: String| { conn.exec(&format!( include_str!("diesel_manage_updated_at.sql"), table_name = table_name )) .expect("Failed to create trigger"); 0 // have to return *something* }, ) } fn establish_inner(database_url: &str) -> Result { use crate::result::ConnectionError::CouldntSetupConfiguration; let raw_connection = RawConnection::establish(database_url)?; let conn = Self { statement_cache: StatementCache::new(), raw_connection, transaction_state: AnsiTransactionManager::default(), metadata_lookup: (), instrumentation: DynInstrumentation::none(), serialized_data: Vec::new(), }; conn.register_diesel_sql_functions() .map_err(CouldntSetupConfiguration)?; Ok(conn) } } fn error_message(err_code: libc::c_int) -> &'static str { ffi::code_to_str(err_code) } #[cfg(test)] mod tests { use super::*; use crate::dsl::sql; use crate::prelude::*; use crate::sql_types::{Integer, Text}; fn connection() -> SqliteConnection { SqliteConnection::establish(":memory:").unwrap() } #[declare_sql_function] extern "SQL" { fn fun_case(x: Text) -> Text; fn my_add(x: Integer, y: Integer) -> Integer; fn answer() -> Integer; fn add_counter(x: Integer) -> Integer; #[aggregate] fn my_sum(expr: Integer) -> Integer; #[aggregate] fn range_max(expr1: Integer, expr2: Integer, expr3: Integer) -> Nullable; } #[diesel_test_helper::test] fn database_serializes_and_deserializes_successfully() { let expected_users = vec![ ( 1, "John Doe".to_string(), "john.doe@example.com".to_string(), ), ( 2, "Jane Doe".to_string(), "jane.doe@example.com".to_string(), ), ]; let conn1 = &mut connection(); let _ = crate::sql_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)") .execute(conn1); let _ = crate::sql_query("INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com'), ('Jane Doe', 'jane.doe@example.com')") .execute(conn1); for _i in 0..2 { let serialized_database = conn1.serialize_database_to_buffer(); let conn2 = &mut connection(); conn2 .deserialize_readonly_database_from_buffer(serialized_database.as_slice()) .unwrap(); let query = sql::<(Integer, Text, Text)>("SELECT id, name, email FROM users ORDER BY id"); let actual_users = query.load::<(i32, String, String)>(conn2).unwrap(); assert_eq!(expected_users, actual_users); // drop the database here // and requery the database to make sure the database owns // required data std::mem::drop(serialized_database); let query = sql::<(Integer, Text, Text)>("SELECT id, name, email FROM users ORDER BY id"); let actual_users = query.load::<(i32, String, String)>(conn2).unwrap(); assert_eq!(expected_users, actual_users); } } #[diesel_test_helper::test] fn database_deserialize_random_bytes() { let buffer = vec![0, 1, 2, 3, 4]; let conn = &mut SqliteConnection::establish(":memory:").unwrap(); conn.deserialize_readonly_database_from_buffer(&buffer) .unwrap(); let r = sql::("SELECT id FROM users").load::(conn); assert!(r.is_err()); assert_eq!(r.unwrap_err().to_string(), "file is not a database"); let conn = &mut SqliteConnection::establish(":memory:").unwrap(); let _ = crate::sql_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)") .execute(conn); let _ = crate::sql_query("INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com'), ('Jane Doe', 'jane.doe@example.com')") .execute(conn); let db = conn.serialize_database_to_buffer(); // only get a valid header, but append garbage let mut bad_buffer = db[..100].to_vec(); bad_buffer.extend(b"whatever"); conn.deserialize_readonly_database_from_buffer(&bad_buffer) .unwrap(); let r = sql::("SELECT id FROM users").load::(conn); assert!(r.is_err()); assert_eq!( r.unwrap_err().to_string(), "database disk image is malformed" ); // only get a valid header, but append garbage let mut size_fitting_bad_buffer = db[..100].to_vec(); size_fitting_bad_buffer.extend( core::iter::repeat(b"abcdefghij") .flatten() .take(db.len() - 100), ); let r = conn.deserialize_readonly_database_from_buffer(&size_fitting_bad_buffer); assert!(r.is_err()); assert_eq!( r.unwrap_err().to_string(), "database disk image is malformed" ); } #[diesel_test_helper::test] fn register_custom_function() { let connection = &mut connection(); fun_case_utils::register_impl(connection, |x: String| { x.chars() .enumerate() .map(|(i, c)| { if i % 2 == 0 { c.to_lowercase().to_string() } else { c.to_uppercase().to_string() } }) .collect::() }) .unwrap(); let mapped_string = crate::select(fun_case("foobar")) .get_result::(connection) .unwrap(); assert_eq!("fOoBaR", mapped_string); } #[diesel_test_helper::test] fn register_multiarg_function() { let connection = &mut connection(); my_add_utils::register_impl(connection, |x: i32, y: i32| x + y).unwrap(); let added = crate::select(my_add(1, 2)).get_result::(connection); assert_eq!(Ok(3), added); } #[diesel_test_helper::test] fn register_noarg_function() { let connection = &mut connection(); answer_utils::register_impl(connection, || 42).unwrap(); let answer = crate::select(answer()).get_result::(connection); assert_eq!(Ok(42), answer); } #[diesel_test_helper::test] fn register_nondeterministic_noarg_function() { let connection = &mut connection(); answer_utils::register_nondeterministic_impl(connection, || 42).unwrap(); let answer = crate::select(answer()).get_result::(connection); assert_eq!(Ok(42), answer); } #[diesel_test_helper::test] fn register_nondeterministic_function() { let connection = &mut connection(); let mut y = 0; add_counter_utils::register_nondeterministic_impl(connection, move |x: i32| { y += 1; x + y }) .unwrap(); let added = crate::select((add_counter(1), add_counter(1), add_counter(1))) .get_result::<(i32, i32, i32)>(connection); assert_eq!(Ok((2, 3, 4)), added); } #[derive(Default)] struct MySum { sum: i32, } impl SqliteAggregateFunction for MySum { type Output = i32; fn step(&mut self, expr: i32) { self.sum += expr; } fn finalize(aggregator: Option) -> Self::Output { aggregator.map(|a| a.sum).unwrap_or_default() } } table! { my_sum_example { id -> Integer, value -> Integer, } } #[diesel_test_helper::test] fn register_aggregate_function() { use self::my_sum_example::dsl::*; let connection = &mut connection(); crate::sql_query( "CREATE TABLE my_sum_example (id integer primary key autoincrement, value integer)", ) .execute(connection) .unwrap(); crate::sql_query("INSERT INTO my_sum_example (value) VALUES (1), (2), (3)") .execute(connection) .unwrap(); my_sum_utils::register_impl::(connection).unwrap(); let result = my_sum_example .select(my_sum(value)) .get_result::(connection); assert_eq!(Ok(6), result); } #[diesel_test_helper::test] fn register_aggregate_function_returns_finalize_default_on_empty_set() { use self::my_sum_example::dsl::*; let connection = &mut connection(); crate::sql_query( "CREATE TABLE my_sum_example (id integer primary key autoincrement, value integer)", ) .execute(connection) .unwrap(); my_sum_utils::register_impl::(connection).unwrap(); let result = my_sum_example .select(my_sum(value)) .get_result::(connection); assert_eq!(Ok(0), result); } #[derive(Default)] struct RangeMax { max_value: Option, } impl SqliteAggregateFunction<(T, T, T)> for RangeMax { type Output = Option; fn step(&mut self, (x0, x1, x2): (T, T, T)) { let max = if x0 >= x1 && x0 >= x2 { x0 } else if x1 >= x0 && x1 >= x2 { x1 } else { x2 }; self.max_value = match self.max_value { Some(current_max_value) if max > current_max_value => Some(max), None => Some(max), _ => self.max_value, }; } fn finalize(aggregator: Option) -> Self::Output { aggregator?.max_value } } table! { range_max_example { id -> Integer, value1 -> Integer, value2 -> Integer, value3 -> Integer, } } #[diesel_test_helper::test] fn register_aggregate_multiarg_function() { use self::range_max_example::dsl::*; let connection = &mut connection(); crate::sql_query( r#"CREATE TABLE range_max_example ( id integer primary key autoincrement, value1 integer, value2 integer, value3 integer )"#, ) .execute(connection) .unwrap(); crate::sql_query( "INSERT INTO range_max_example (value1, value2, value3) VALUES (3, 2, 1), (2, 2, 2)", ) .execute(connection) .unwrap(); range_max_utils::register_impl::, _, _, _>(connection).unwrap(); let result = range_max_example .select(range_max(value1, value2, value3)) .get_result::>(connection) .unwrap(); assert_eq!(Some(3), result); } table! { my_collation_example { id -> Integer, value -> Text, } } #[diesel_test_helper::test] fn register_collation_function() { use self::my_collation_example::dsl::*; let connection = &mut connection(); connection .register_collation("RUSTNOCASE", |rhs, lhs| { rhs.to_lowercase().cmp(&lhs.to_lowercase()) }) .unwrap(); crate::sql_query( "CREATE TABLE my_collation_example (id integer primary key autoincrement, value text collate RUSTNOCASE)", ).execute(connection) .unwrap(); crate::sql_query( "INSERT INTO my_collation_example (value) VALUES ('foo'), ('FOo'), ('f00')", ) .execute(connection) .unwrap(); let result = my_collation_example .filter(value.eq("foo")) .select(value) .load::(connection); assert_eq!( Ok(&["foo".to_owned(), "FOo".to_owned()][..]), result.as_ref().map(|vec| vec.as_ref()) ); let result = my_collation_example .filter(value.eq("FOO")) .select(value) .load::(connection); assert_eq!( Ok(&["foo".to_owned(), "FOo".to_owned()][..]), result.as_ref().map(|vec| vec.as_ref()) ); let result = my_collation_example .filter(value.eq("f00")) .select(value) .load::(connection); assert_eq!( Ok(&["f00".to_owned()][..]), result.as_ref().map(|vec| vec.as_ref()) ); let result = my_collation_example .filter(value.eq("F00")) .select(value) .load::(connection); assert_eq!( Ok(&["f00".to_owned()][..]), result.as_ref().map(|vec| vec.as_ref()) ); let result = my_collation_example .filter(value.eq("oof")) .select(value) .load::(connection); assert_eq!(Ok(&[][..]), result.as_ref().map(|vec| vec.as_ref())); } // regression test for https://github.com/diesel-rs/diesel/issues/3425 #[diesel_test_helper::test] fn test_correct_serialization_of_owned_strings() { use crate::prelude::*; #[derive(Debug, crate::expression::AsExpression)] #[diesel(sql_type = diesel::sql_types::Text)] struct CustomWrapper(String); impl crate::serialize::ToSql for CustomWrapper { fn to_sql<'b>( &'b self, out: &mut crate::serialize::Output<'b, '_, Sqlite>, ) -> crate::serialize::Result { out.set_value(self.0.to_string()); Ok(crate::serialize::IsNull::No) } } let connection = &mut connection(); let res = crate::select( CustomWrapper("".into()) .into_sql::() .nullable(), ) .get_result::>(connection) .unwrap(); assert_eq!(res, Some(String::new())); } #[diesel_test_helper::test] fn test_correct_serialization_of_owned_bytes() { use crate::prelude::*; #[derive(Debug, crate::expression::AsExpression)] #[diesel(sql_type = diesel::sql_types::Binary)] struct CustomWrapper(Vec); impl crate::serialize::ToSql for CustomWrapper { fn to_sql<'b>( &'b self, out: &mut crate::serialize::Output<'b, '_, Sqlite>, ) -> crate::serialize::Result { out.set_value(self.0.clone()); Ok(crate::serialize::IsNull::No) } } let connection = &mut connection(); let res = crate::select( CustomWrapper(Vec::new()) .into_sql::() .nullable(), ) .get_result::>>(connection) .unwrap(); assert_eq!(res, Some(Vec::new())); } #[diesel_test_helper::test] fn correctly_handle_empty_query() { let check_empty_query_error = |r: crate::QueryResult| { assert!(r.is_err()); let err = r.unwrap_err(); assert!( matches!(err, crate::result::Error::QueryBuilderError(ref b) if b.is::()), "Expected a query builder error, but got {err}" ); }; let connection = &mut SqliteConnection::establish(":memory:").unwrap(); check_empty_query_error(crate::sql_query("").execute(connection)); check_empty_query_error(crate::sql_query(" ").execute(connection)); check_empty_query_error(crate::sql_query("\n\t").execute(connection)); check_empty_query_error(crate::sql_query("-- SELECT 1;").execute(connection)); } #[diesel_test_helper::test] fn aggregate_function_works_with_aligned_data() { #[derive(Debug, Default)] #[repr(align(64))] struct OverAligned; impl SqliteAggregateFunction for OverAligned { type Output = i64; fn step(&mut self, _value: i32) { let need = core::mem::align_of::(); let got = core::mem::align_of_val(self); assert_eq!(need, got); } fn finalize(_agg: Option) -> i64 { 0 } } #[declare_sql_function] extern "SQL" { #[aggregate] fn over_aligned_sum(x: Integer) -> diesel::sql_types::BigInt; } let mut conn = SqliteConnection::establish(":memory:").unwrap(); over_aligned_sum_utils::register_impl::(&mut conn).unwrap(); diesel::select(over_aligned_sum(1)) .execute(&mut conn) .unwrap(); } #[diesel_test_helper::test] fn sum_twice() { #[derive(Default)] struct Sum(i32); impl SqliteAggregateFunction for Sum { type Output = i32; fn step(&mut self, value: i32) { self.0 += value; } fn finalize(agg: Option) -> i32 { agg.map(|s| s.0).unwrap_or_default() } } #[declare_sql_function] extern "SQL" { #[aggregate] fn my_sum(x: Integer) -> Integer; } let mut conn = SqliteConnection::establish(":memory:").unwrap(); my_sum_utils::register_impl::(&mut conn).unwrap(); conn.batch_execute( " CREATE TABLE test(key1 INTEGER, key2 INTEGER); INSERT INTO test(key1, key2) VALUES (1, 2), (2, 4), (3, 6); ", ) .unwrap(); table! { test (key1, key2) { key1 -> Integer, key2 -> Integer, } } let (first_res, second_res) = test::table .select((my_sum(test::key1), my_sum(test::key2))) .get_result::<(i32, i32)>(&mut conn) .unwrap(); assert_eq!(first_res, 6); assert_eq!(second_res, 12); conn.batch_execute("DELETE FROM test").unwrap(); let (first_res, second_res) = test::table .select((my_sum(test::key1), my_sum(test::key2))) .get_result::<(i32, i32)>(&mut conn) .unwrap(); assert_eq!(first_res, 0); assert_eq!(second_res, 0); } #[diesel_test_helper::test] fn test_injection() { diesel::table! { #[sql_name = "quote'table"] quote_table (id) { id -> Nullable, name -> Nullable, } } let mut conn = SqliteConnection::establish(":memory:").unwrap(); conn.batch_execute("CREATE TABLE \"quote'table\" (id INTEGER PRIMARY KEY, name TEXT);") .unwrap(); diesel::insert_into(quote_table::table) .values((quote_table::id.eq(1), quote_table::name.eq("Jane"))) .execute(&mut conn) .unwrap(); let data = quote_table::table .load::<(Option, Option)>(&mut conn) .unwrap(); assert_eq!(data, [(Some(1), Some("Jane".to_owned()))]); } }