From 4549dd9d6cd28984689cf1aa7d8b9ec03108c9a8 Mon Sep 17 00:00:00 2001 From: jkomyno <12381818+jkomyno@users.noreply.github.com> Date: Sat, 4 Jan 2025 15:38:21 +0100 Subject: [PATCH 1/3] feat(driver-adapters): add "version()" support to QuaintQueryable --- quaint/src/visitor.rs | 5 +++++ quaint/src/visitor/sqlite.rs | 4 ++++ query-engine/driver-adapters/src/queryable.rs | 20 +++++++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/quaint/src/visitor.rs b/quaint/src/visitor.rs index badd9a86a2fc..608b925c7600 100644 --- a/quaint/src/visitor.rs +++ b/quaint/src/visitor.rs @@ -68,6 +68,11 @@ pub trait Visitor<'a> { /// Write to the query. fn write(&mut self, s: D) -> Result; + /// Name of the function used to view the version of the database. + fn version_fn(&self) -> &'static str { + "version" + } + /// A point to modify an incoming query to make it compatible with the /// underlying database. fn compatibility_modifications(&self, query: Query<'a>) -> Query<'a> { diff --git a/quaint/src/visitor/sqlite.rs b/quaint/src/visitor/sqlite.rs index 7292aa2eca64..f177e2080879 100644 --- a/quaint/src/visitor/sqlite.rs +++ b/quaint/src/visitor/sqlite.rs @@ -95,6 +95,10 @@ impl<'a> Visitor<'a> for Sqlite<'a> { Ok(()) } + fn version_fn(&self) -> &'static str { + "sqlite_version" + } + fn visit_raw_value(&mut self, value: Value<'a>) -> visitor::Result { let res = match &value.typed { ValueType::Int32(i) => i.map(|i| self.write(i)), diff --git a/query-engine/driver-adapters/src/queryable.rs b/query-engine/driver-adapters/src/queryable.rs index 7746d6137f18..98eddebfa60e 100644 --- a/query-engine/driver-adapters/src/queryable.rs +++ b/query-engine/driver-adapters/src/queryable.rs @@ -130,9 +130,25 @@ impl QuaintQueryable for JsBaseQueryable { .await } + // Note: Needed by the Wasm Schema Engine only. async fn version(&self) -> quaint::Result> { - // Note: JS Connectors don't use this method. - Ok(None) + let version_fn: &'static str = match self.provider { + #[cfg(feature = "mysql")] + AdapterFlavour::Mysql => visitor::Mysql::version_fn(), + #[cfg(feature = "postgresql")] + AdapterFlavour::Postgres => visitor::Postgres::version_fn(), + #[cfg(feature = "sqlite")] + AdapterFlavour::Sqlite => visitor::Sqlite::version_fn(), + }; + + let query = format!(r#"SELECT {}() AS version"#, version_fn); + let rows = self.query_raw(query, &[]).await?; + + let version_string = rows + .first() + .and_then(|row| row.get("version").and_then(|version| version.to_string())); + + Ok(version_string) } fn is_healthy(&self) -> bool { From e90eba9eee9e2936bfcd90e5671f82cf6d46f4f4 Mon Sep 17 00:00:00 2001 From: jkomyno <12381818+jkomyno@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:53:43 +0100 Subject: [PATCH 2/3] feat(driver-adapters): add "version()" support to QuaintQueryable --- quaint/src/visitor.rs | 5 ----- quaint/src/visitor/mysql.rs | 5 +++++ quaint/src/visitor/postgres.rs | 5 +++++ quaint/src/visitor/sqlite.rs | 9 +++++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/quaint/src/visitor.rs b/quaint/src/visitor.rs index 608b925c7600..badd9a86a2fc 100644 --- a/quaint/src/visitor.rs +++ b/quaint/src/visitor.rs @@ -68,11 +68,6 @@ pub trait Visitor<'a> { /// Write to the query. fn write(&mut self, s: D) -> Result; - /// Name of the function used to view the version of the database. - fn version_fn(&self) -> &'static str { - "version" - } - /// A point to modify an incoming query to make it compatible with the /// underlying database. fn compatibility_modifications(&self, query: Query<'a>) -> Query<'a> { diff --git a/quaint/src/visitor/mysql.rs b/quaint/src/visitor/mysql.rs index 77979b8b7f64..63ed01612e0a 100644 --- a/quaint/src/visitor/mysql.rs +++ b/quaint/src/visitor/mysql.rs @@ -16,6 +16,11 @@ pub struct Mysql<'a> { } impl<'a> Mysql<'a> { + /// Name of the function used to view the version of the database. + pub const fn version_fn() -> &'static str { + "version" + } + fn visit_regular_equality_comparison(&mut self, left: Expression<'a>, right: Expression<'a>) -> visitor::Result { self.visit_expression(left)?; self.write(" = ")?; diff --git a/quaint/src/visitor/postgres.rs b/quaint/src/visitor/postgres.rs index c119ab319ded..d347402f73f2 100644 --- a/quaint/src/visitor/postgres.rs +++ b/quaint/src/visitor/postgres.rs @@ -18,6 +18,11 @@ pub struct Postgres<'a> { } impl<'a> Postgres<'a> { + /// Name of the function used to view the version of the database. + pub const fn version_fn() -> &'static str { + "version" + } + fn visit_json_build_obj_expr(&mut self, expr: Expression<'a>) -> crate::Result<()> { match expr.kind() { ExpressionKind::Column(col) => match (col.type_family.as_ref(), col.native_type.as_deref()) { diff --git a/quaint/src/visitor/sqlite.rs b/quaint/src/visitor/sqlite.rs index f177e2080879..06d7c86f9876 100644 --- a/quaint/src/visitor/sqlite.rs +++ b/quaint/src/visitor/sqlite.rs @@ -16,6 +16,11 @@ pub struct Sqlite<'a> { } impl<'a> Sqlite<'a> { + /// Name of the function used to view the version of the database. + pub const fn version_fn() -> &'static str { + "sqlite_version" + } + fn returning(&mut self, returning: Option>>) -> visitor::Result { if let Some(returning) = returning { if !returning.is_empty() { @@ -95,10 +100,6 @@ impl<'a> Visitor<'a> for Sqlite<'a> { Ok(()) } - fn version_fn(&self) -> &'static str { - "sqlite_version" - } - fn visit_raw_value(&mut self, value: Value<'a>) -> visitor::Result { let res = match &value.typed { ValueType::Int32(i) => i.map(|i| self.write(i)), From 3d71c338d0dbd61de8d3997fa7ecdaf30516354d Mon Sep 17 00:00:00 2001 From: jkomyno <12381818+jkomyno@users.noreply.github.com> Date: Mon, 6 Jan 2025 15:07:41 +0100 Subject: [PATCH 3/3] chore: fix lint --- query-engine/driver-adapters/src/queryable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/query-engine/driver-adapters/src/queryable.rs b/query-engine/driver-adapters/src/queryable.rs index 98eddebfa60e..9c64b48ba820 100644 --- a/query-engine/driver-adapters/src/queryable.rs +++ b/query-engine/driver-adapters/src/queryable.rs @@ -142,7 +142,7 @@ impl QuaintQueryable for JsBaseQueryable { }; let query = format!(r#"SELECT {}() AS version"#, version_fn); - let rows = self.query_raw(query, &[]).await?; + let rows = self.query_raw(query.as_str(), &[]).await?; let version_string = rows .first()