Skip to content

Commit

Permalink
feat(schema-engine): let "sql-schema-describer" be wasm-compatible; p…
Browse files Browse the repository at this point in the history
…artial revert of /pull/3093; close ORM-456
  • Loading branch information
jkomyno committed Dec 18, 2024
1 parent e3a93c9 commit 359d660
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 51 deletions.
37 changes: 30 additions & 7 deletions schema-engine/sql-schema-describer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,38 @@ edition = "2021"
name = "sql-schema-describer"
version = "0.1.0"

[features]
postgresql = ["relation_joins", "quaint/postgresql", "psl/postgresql"]
postgresql-native = ["postgresql", "quaint/postgresql-native", "quaint/pooled"]
mysql = ["relation_joins", "quaint/mysql", "psl/mysql"]
mysql-native = ["mysql", "quaint/mysql-native", "quaint/pooled"]
sqlite = ["quaint/sqlite", "psl/sqlite"]
sqlite-native = ["sqlite", "quaint/sqlite-native", "quaint/pooled"]
mssql = ["quaint/mssql"]
mssql-native = ["mssql", "quaint/mssql-native", "quaint/pooled"]
cockroachdb = ["relation_joins", "quaint/postgresql", "psl/cockroachdb"]
cockroachdb-native = [
"cockroachdb",
"quaint/postgresql-native",
"quaint/pooled",
]
vendored-openssl = ["quaint/vendored-openssl"]
all-native = [
"sqlite-native",
"mysql-native",
"postgresql-native",
"mssql-native",
"cockroachdb-native",
]
# TODO: At the moment of writing (rustc 1.77.0), can_have_capability from psl does not eliminate joins
# code from bundle for some reason, so we are doing it explicitly. Check with a newer version of compiler - if elimination
# happens successfully, we don't need this feature anymore
relation_joins = []

[dependencies]
prisma-value = { path = "../../libs/prisma-value" }
psl = { workspace = true, features = ["all"] }
psl.workspace = true
quaint.workspace = true

either = "1.8.0"
async-trait.workspace = true
Expand All @@ -19,12 +48,6 @@ serde.workspace = true
tracing.workspace = true
tracing-error = "0.2"
tracing-futures.workspace = true
quaint = { workspace = true, features = [
"all-native",
"pooled",
"expose-drivers",
"fmt-sql",
] }

[dev-dependencies]
expect-test = "1.2.2"
Expand Down
8 changes: 8 additions & 0 deletions schema-engine/sql-schema-describer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
#![deny(rust_2018_idioms, unsafe_code)]
#![allow(clippy::derive_partial_eq_without_eq)]

#[cfg(feature = "mssql")]
pub mod mssql;

#[cfg(feature = "mysql")]
pub mod mysql;

#[cfg(any(feature = "postgresql", feature = "cockroachdb"))]
pub mod postgres;

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

pub mod walkers;

mod connector_data;
Expand Down
54 changes: 10 additions & 44 deletions schema-engine/sql-schema-describer/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::{
use either::Either;
use indexmap::IndexMap;
use quaint::{
ast::{Value, ValueType},
connector::{ColumnType as QuaintColumnType, GetRow, ToColumnNames},
prelude::ResultRow,
ast::Value,
prelude::{Queryable, ResultRow},
ValueType,
};
use std::{any::type_name, borrow::Cow, collections::BTreeMap, convert::TryInto, fmt::Debug, path::Path};
use tracing::trace;
Expand All @@ -24,44 +24,8 @@ 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>(
&'a self,
sql: &'a str,
params: &'a [quaint::prelude::Value<'a>],
) -> quaint::Result<quaint::prelude::ResultSet> {
quaint::prelude::Queryable::query_raw(self, sql, params).await
}
}

pub struct SqlSchemaDescriber<'a> {
conn: &'a (dyn Connection + Send + Sync),
conn: &'a dyn Queryable,
}

impl Debug for SqlSchemaDescriber<'_> {
Expand Down Expand Up @@ -92,15 +56,17 @@ impl SqlSchemaDescriberBackend for SqlSchemaDescriber<'_> {
}

async fn version(&self) -> DescriberResult<Option<String>> {
Ok(Some(quaint::connector::sqlite_version().to_owned()))
// TODO: implement `SELECT version` via Driver Adapters
// Ok(Some(quaint::connector::sqlite_version().to_owned()))
Ok(None)
}
}

impl Parser for SqlSchemaDescriber<'_> {}

impl<'a> SqlSchemaDescriber<'a> {
/// Constructor.
pub fn new(conn: &'a (dyn Connection + Send + Sync)) -> SqlSchemaDescriber<'a> {
pub fn new(conn: &'a dyn Queryable) -> SqlSchemaDescriber<'a> {
SqlSchemaDescriber { conn }
}

Expand Down Expand Up @@ -330,7 +296,7 @@ async fn push_columns(
table_name: &str,
container_id: Either<TableId, ViewId>,
schema: &mut SqlSchema,
conn: &(dyn Connection + Send + Sync),
conn: &dyn Queryable,
) -> DescriberResult<()> {
let sql = format!(r#"PRAGMA table_info ("{table_name}")"#);
let result_set = conn.query_raw(&sql, &[]).await?;
Expand Down Expand Up @@ -469,7 +435,7 @@ async fn push_indexes(
table: &str,
table_id: TableId,
schema: &mut SqlSchema,
conn: &(dyn Connection + Send + Sync),
conn: &dyn Queryable,
) -> DescriberResult<()> {
let sql = format!(r#"PRAGMA index_list("{table}");"#);
let result_set = conn.query_raw(&sql, &[]).await?;
Expand Down

0 comments on commit 359d660

Please sign in to comment.