Skip to content

Commit

Permalink
feat(schema-engine-wasm): wasm-compatible sql-schema-describer
Browse files Browse the repository at this point in the history
  • Loading branch information
jkomyno committed Jan 7, 2025
1 parent 4123509 commit bd3416a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Connection {
}

pub(super) async fn describe_schema(&mut self) -> ConnectorResult<SqlSchema> {
// Note: this relies on quaint::connector::rusqlite::Connection, which is exposed by `quaint/expose-drivers`, and is not Wasm-compatible.
describer::SqlSchemaDescriber::new(&self.0)
.describe_impl()
.await
Expand Down
4 changes: 3 additions & 1 deletion schema-engine/sql-introspection-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ edition = "2021"
[dependencies]
schema-connector = { path = "../connectors/schema-connector" }
sql-schema-connector = { path = "../connectors/sql-schema-connector" }
sql-schema-describer = { path = "../sql-schema-describer" }
sql-schema-describer = { path = "../sql-schema-describer", features = [
"all-native",
] }
psl = { workspace = true, features = ["all"] }
test-macros = { path = "../../libs/test-macros" }
user-facing-errors = { path = "../../libs/user-facing-errors", features = [
Expand Down
4 changes: 3 additions & 1 deletion schema-engine/sql-migration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ edition = "2021"
psl = { workspace = true, features = ["all"] }
schema-core = { path = "../core" }
sql-schema-connector = { path = "../connectors/sql-schema-connector" }
sql-schema-describer = { path = "../sql-schema-describer" }
sql-schema-describer = { path = "../sql-schema-describer", features = [
"all-native",
] }
user-facing-errors = { path = "../../libs/user-facing-errors", features = [
"all-native",
] }
Expand Down
9 changes: 6 additions & 3 deletions schema-engine/sql-schema-describer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ edition = "2021"
name = "sql-schema-describer"
version = "0.1.0"

[features]
sqlite-native = ["quaint/pooled", "quaint/sqlite-native", "quaint/expose-drivers"]
all-native = [
"sqlite-native",
]

[dependencies]
prisma-value = { path = "../../libs/prisma-value" }
psl = { workspace = true, features = ["all"] }
Expand All @@ -20,9 +26,6 @@ tracing.workspace = true
tracing-error = "0.2"
tracing-futures.workspace = true
quaint = { workspace = true, features = [
"all-native",
"pooled",
"expose-drivers",
"fmt-sql",
] }

Expand Down
29 changes: 3 additions & 26 deletions schema-engine/sql-schema-describer/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use either::Either;
use indexmap::IndexMap;
use quaint::{
ast::{Value, ValueType},
connector::{ColumnType as QuaintColumnType, GetRow, ToColumnNames},
prelude::ResultRow,
};
use std::{any::type_name, borrow::Cow, collections::BTreeMap, convert::TryInto, fmt::Debug, path::Path};
use tracing::trace;

#[cfg(feature = "sqlite-native")]
pub(crate) mod native;

#[async_trait::async_trait]
pub trait Connection {
async fn query_raw<'a>(
Expand All @@ -24,31 +26,6 @@ pub trait Connection {
) -> quaint::Result<quaint::prelude::ResultSet>;
}

#[async_trait::async_trait]
impl Connection for std::sync::Mutex<quaint::connector::rusqlite::Connection> {
async fn query_raw<'a>(
&'a self,
sql: &'a str,
params: &'a [quaint::prelude::Value<'a>],
) -> quaint::Result<quaint::prelude::ResultSet> {
let conn = self.lock().unwrap();
let mut stmt = conn.prepare_cached(sql)?;
let column_types = stmt.columns().iter().map(QuaintColumnType::from).collect::<Vec<_>>();
let mut rows = stmt.query(quaint::connector::rusqlite::params_from_iter(params.iter()))?;
let column_names = rows.to_column_names();
let mut converted_rows = Vec::new();
while let Some(row) = rows.next()? {
converted_rows.push(row.get_result_row().unwrap());
}

Ok(quaint::prelude::ResultSet::new(
column_names,
column_types,
converted_rows,
))
}
}

#[async_trait::async_trait]
impl Connection for quaint::single::Quaint {
async fn query_raw<'a>(
Expand Down
23 changes: 23 additions & 0 deletions schema-engine/sql-schema-describer/src/sqlite/native/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::sqlite::Connection;

use quaint::{
connector::{rusqlite, ColumnType as QuaintColumnType, GetRow, ToColumnNames},
prelude::{ResultSet, Value},
};

#[async_trait::async_trait]
impl Connection for std::sync::Mutex<rusqlite::Connection> {
async fn query_raw<'a>(&'a self, sql: &'a str, params: &'a [Value<'a>]) -> quaint::Result<ResultSet> {
let conn = self.lock().unwrap();
let mut stmt = conn.prepare_cached(sql)?;
let column_types = stmt.columns().iter().map(QuaintColumnType::from).collect::<Vec<_>>();
let mut rows = stmt.query(rusqlite::params_from_iter(params.iter()))?;
let column_names = rows.to_column_names();
let mut converted_rows = Vec::new();
while let Some(row) = rows.next()? {
converted_rows.push(row.get_result_row().unwrap());
}

Ok(ResultSet::new(column_names, column_types, converted_rows))
}
}

0 comments on commit bd3416a

Please sign in to comment.