From d65dbc57dee2c57e9911e726d56f51c9d939a1f5 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Mon, 1 Jun 2026 17:36:31 -0500 Subject: [PATCH] Iterate --- rust/src/db.rs | 52 +++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/rust/src/db.rs b/rust/src/db.rs index 973a34fb0a..84c041e5ce 100644 --- a/rust/src/db.rs +++ b/rust/src/db.rs @@ -15,17 +15,34 @@ use pyo3::prelude::*; +#[derive(Copy, Clone, Debug)] +pub enum DatabaseEngine { + Sqlite, + Postgres, +} + +impl DatabaseEngine { + //[inline] + pub fn supports_using_any_list(&self) -> bool { + match self { + DatabaseEngine::Sqlite => false, + DatabaseEngine::Postgres => true, + } + } +} + /// Wrapper for a `LoggingTransaction` from the Python side of Synapse. pub struct LoggingTransactionWrapper<'py> { - /// The underlying LoggingTransaction + /// The underlying `LoggingTransaction` raw: &'py PyAny, database_engine: DatabaseEngine, } impl<'source> FromPyObject<'source> for LoggingTransactionWrapper<'source> { - fn extract(ob: &'source PyAny) -> PyResult { - let database_engine = match ob + /// From Python `LoggingTransaction` + fn extract(logging_transaction_python_object: &'source PyAny) -> PyResult { + let database_engine = match logging_transaction_python_object .getattr("database_engine")? .get_type() .name() @@ -36,7 +53,7 @@ impl<'source> FromPyObject<'source> for LoggingTransactionWrapper<'source> { other => panic!("Unknown engine {other:?}"), }; Ok(Self { - raw: ob, + raw: logging_transaction_python_object, database_engine, }) } @@ -54,8 +71,15 @@ impl<'py> LoggingTransactionWrapper<'py> { sql: &str, args: T, ) -> PyResult> { - let execute_fn = self.raw.getattr(intern!(self.raw.py(), "execute_values"))?; - Ok(execute_fn.call1((sql, args))?.extract()?) + match self.database_engine { + DatabaseEngine::Postgres => { + let execute_fn = self.raw.getattr(intern!(self.raw.py(), "execute_values"))?; + Ok(execute_fn.call1((sql, args))?.extract()?) + } + DatabaseEngine::Sqlite => { + unimplemented!("execute_values is not supported when using SQLite. This is a Synapse programming error"); + } + } } pub fn fetchall + ValidDatabaseReturnType>( @@ -65,19 +89,3 @@ impl<'py> LoggingTransactionWrapper<'py> { Ok(fetch_fn.call0()?.extract()?) } } - -#[derive(Copy, Clone, Debug)] -pub enum DatabaseEngine { - Sqlite, - Postgres, -} - -impl DatabaseEngine { - //[inline] - pub fn supports_using_any_list(&self) -> bool { - match self { - DatabaseEngine::Sqlite => false, - DatabaseEngine::Postgres => true, - } - } -}