From 4123509d24aa4dede1e864b46351bf2790323b69 Mon Sep 17 00:00:00 2001 From: Alberto Schiabel Date: Tue, 7 Jan 2025 11:43:53 +0100 Subject: [PATCH] feat(driver-adapters): add "version()" support to QuaintQueryable (#5103) * feat(driver-adapters): add "version()" support to QuaintQueryable * feat(driver-adapters): add "version()" support to QuaintQueryable * chore: fix lint --------- Co-authored-by: jkomyno <12381818+jkomyno@users.noreply.github.com> --- quaint/src/visitor/mysql.rs | 5 +++++ quaint/src/visitor/postgres.rs | 5 +++++ quaint/src/visitor/sqlite.rs | 5 +++++ query-engine/driver-adapters/src/queryable.rs | 20 +++++++++++++++++-- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/quaint/src/visitor/mysql.rs b/quaint/src/visitor/mysql.rs index 77979b8b7f6..63ed01612e0 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 c119ab319de..d347402f73f 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 7292aa2eca6..06d7c86f987 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() { diff --git a/query-engine/driver-adapters/src/queryable.rs b/query-engine/driver-adapters/src/queryable.rs index 7746d6137f1..9c64b48ba82 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.as_str(), &[]).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 {