Skip to content

Commit

Permalink
feat(driver-adapters): add "version()" support to QuaintQueryable (#5103
Browse files Browse the repository at this point in the history
)

* feat(driver-adapters): add "version()" support to QuaintQueryable

* feat(driver-adapters): add "version()" support to QuaintQueryable

* chore: fix lint

---------

Co-authored-by: jkomyno <[email protected]>
  • Loading branch information
jkomyno and jkomyno authored Jan 7, 2025
1 parent 51db5cf commit 4123509
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions quaint/src/visitor/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(" = ")?;
Expand Down
5 changes: 5 additions & 0 deletions quaint/src/visitor/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
5 changes: 5 additions & 0 deletions quaint/src/visitor/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Column<'a>>>) -> visitor::Result {
if let Some(returning) = returning {
if !returning.is_empty() {
Expand Down
20 changes: 18 additions & 2 deletions query-engine/driver-adapters/src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,25 @@ impl QuaintQueryable for JsBaseQueryable {
.await
}

// Note: Needed by the Wasm Schema Engine only.
async fn version(&self) -> quaint::Result<Option<String>> {
// 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 {
Expand Down

0 comments on commit 4123509

Please sign in to comment.