Skip to content

Commit

Permalink
Merge branch 'feat/sql-query-connector-on-wasm32-unknown-unknown' int…
Browse files Browse the repository at this point in the history
…o feat/query-core-on-wasm32-unknown-unknown
  • Loading branch information
jkomyno authored Nov 15, 2023
2 parents 9c41dc1 + 414ae2b commit d0f783d
Show file tree
Hide file tree
Showing 24 changed files with 373 additions and 385 deletions.
8 changes: 4 additions & 4 deletions quaint/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ pub(crate) mod postgres;
#[cfg(feature = "postgresql-native")]
pub use postgres::native::*;
#[cfg(feature = "postgresql")]
pub use postgres::wasm::common::*;
pub use postgres::*;

#[cfg(feature = "mysql")]
pub(crate) mod mysql;
#[cfg(feature = "mysql-native")]
pub use mysql::native::*;
#[cfg(feature = "mysql")]
pub use mysql::wasm::common::*;
pub use mysql::*;

#[cfg(feature = "sqlite")]
pub(crate) mod sqlite;
#[cfg(feature = "sqlite-native")]
pub use sqlite::native::*;
#[cfg(feature = "sqlite")]
pub use sqlite::wasm::common::*;
pub use sqlite::*;

#[cfg(feature = "mssql")]
pub(crate) mod mssql;
#[cfg(feature = "mssql-native")]
pub use mssql::native::*;
#[cfg(feature = "mssql")]
pub use mssql::wasm::common::*;
pub use mssql::*;
7 changes: 4 additions & 3 deletions quaint/src/connector/mssql.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub use wasm::common::MssqlUrl;
//! Wasm-compatible definitions for the MSSQL connector.
//! This module is only available with the `mssql` feature.
pub(crate) mod url;

#[cfg(feature = "mssql")]
pub(crate) mod wasm;
pub use url::*;

#[cfg(feature = "mssql-native")]
pub(crate) mod native;
19 changes: 1 addition & 18 deletions quaint/src/connector/mssql/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
mod conversion;
mod error;

pub(crate) use crate::connector::mssql::wasm::common::MssqlUrl;
pub(crate) use crate::connector::mssql::MssqlUrl;
use crate::connector::{timeout, IsolationLevel, Transaction, TransactionOptions};

use crate::{
Expand Down Expand Up @@ -237,20 +237,3 @@ impl Queryable for Mssql {
true
}
}

#[cfg(test)]
mod tests {
use crate::tests::test_api::mssql::CONN_STR;
use crate::{error::*, single::Quaint};

#[tokio::test]
async fn should_map_wrong_credentials_error() {
let url = CONN_STR.replace("user=SA", "user=WRONG");

let res = Quaint::new(url.as_str()).await;
assert!(res.is_err());

let err = res.unwrap_err();
assert!(matches!(err.kind(), ErrorKind::AuthenticationFailed { user } if user == &Name::available("WRONG")));
}
}
File renamed without changes.
5 changes: 0 additions & 5 deletions quaint/src/connector/mssql/wasm/mod.rs

This file was deleted.

10 changes: 6 additions & 4 deletions quaint/src/connector/mysql.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub use wasm::common::MysqlUrl;
pub use wasm::error::MysqlError;
//! Wasm-compatible definitions for the MySQL connector.
//! This module is only available with the `mysql` feature.
pub(crate) mod error;
pub(crate) mod url;

#[cfg(feature = "mysql")]
pub(crate) mod wasm;
pub use error::MysqlError;
pub use url::*;

#[cfg(feature = "mysql-native")]
pub(crate) mod native;
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::error::{DatabaseConstraint, Error, ErrorKind};
use thiserror::Error;

// This is a partial copy of the `mysql_async::Error` using only the enum variant used by Prisma.
// This avoids pulling in `mysql_async`, which would break Wasm compilation.
#[derive(Debug, Error)]
enum MysqlAsyncError {
#[error("Server error: `{}'", _0)]
Expand Down
2 changes: 1 addition & 1 deletion quaint/src/connector/mysql/native/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
connector::mysql::wasm::error::MysqlError,
connector::mysql::error::MysqlError,
error::{Error, ErrorKind},
};
use mysql_async as my;
Expand Down
85 changes: 1 addition & 84 deletions quaint/src/connector/mysql/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
mod conversion;
mod error;

pub(crate) use crate::connector::mysql::wasm::common::MysqlUrl;
pub(crate) use crate::connector::mysql::MysqlUrl;
use crate::connector::{timeout, IsolationLevel};

use crate::{
Expand Down Expand Up @@ -295,86 +295,3 @@ impl Queryable for Mysql {
true
}
}

#[cfg(test)]
mod tests {
use super::MysqlUrl;
use crate::tests::test_api::mysql::CONN_STR;
use crate::{error::*, single::Quaint};
use url::Url;

#[test]
fn should_parse_socket_url() {
let url = MysqlUrl::new(Url::parse("mysql://root@localhost/dbname?socket=(/tmp/mysql.sock)").unwrap()).unwrap();
assert_eq!("dbname", url.dbname());
assert_eq!(&Some(String::from("/tmp/mysql.sock")), url.socket());
}

#[test]
fn should_parse_prefer_socket() {
let url =
MysqlUrl::new(Url::parse("mysql://root:root@localhost:3307/testdb?prefer_socket=false").unwrap()).unwrap();
assert!(!url.prefer_socket().unwrap());
}

#[test]
fn should_parse_sslaccept() {
let url =
MysqlUrl::new(Url::parse("mysql://root:root@localhost:3307/testdb?sslaccept=strict").unwrap()).unwrap();
assert!(url.query_params.use_ssl);
assert!(!url.query_params.ssl_opts.skip_domain_validation());
assert!(!url.query_params.ssl_opts.accept_invalid_certs());
}

#[test]
fn should_parse_ipv6_host() {
let url = MysqlUrl::new(Url::parse("mysql://[2001:db8:1234::ffff]:5432/testdb").unwrap()).unwrap();
assert_eq!("2001:db8:1234::ffff", url.host());
}

#[test]
fn should_allow_changing_of_cache_size() {
let url = MysqlUrl::new(Url::parse("mysql:///root:root@localhost:3307/foo?statement_cache_size=420").unwrap())
.unwrap();
assert_eq!(420, url.cache().capacity());
}

#[test]
fn should_have_default_cache_size() {
let url = MysqlUrl::new(Url::parse("mysql:///root:root@localhost:3307/foo").unwrap()).unwrap();
assert_eq!(100, url.cache().capacity());
}

#[tokio::test]
async fn should_map_nonexisting_database_error() {
let mut url = Url::parse(&CONN_STR).unwrap();
url.set_username("root").unwrap();
url.set_path("/this_does_not_exist");

let url = url.as_str().to_string();
let res = Quaint::new(&url).await;

let err = res.unwrap_err();

match err.kind() {
ErrorKind::DatabaseDoesNotExist { db_name } => {
assert_eq!(Some("1049"), err.original_code());
assert_eq!(Some("Unknown database \'this_does_not_exist\'"), err.original_message());
assert_eq!(&Name::available("this_does_not_exist"), db_name)
}
e => panic!("Expected `DatabaseDoesNotExist`, got {:?}", e),
}
}

#[tokio::test]
async fn should_map_wrong_credentials_error() {
let mut url = Url::parse(&CONN_STR).unwrap();
url.set_username("WRONG").unwrap();

let res = Quaint::new(url.as_str()).await;
assert!(res.is_err());

let err = res.unwrap_err();
assert!(matches!(err.kind(), ErrorKind::AuthenticationFailed { user } if user == &Name::available("WRONG")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,86 @@ pub(crate) struct MysqlUrlQueryParams {
#[cfg(feature = "mysql-native")]
pub(crate) ssl_opts: mysql_async::SslOpts,
}

#[cfg(test)]
mod tests {
use super::MysqlUrl;
use crate::tests::test_api::mysql::CONN_STR;
use crate::{error::*, single::Quaint};
use url::Url;

#[test]
fn should_parse_socket_url() {
let url = MysqlUrl::new(Url::parse("mysql://root@localhost/dbname?socket=(/tmp/mysql.sock)").unwrap()).unwrap();
assert_eq!("dbname", url.dbname());
assert_eq!(&Some(String::from("/tmp/mysql.sock")), url.socket());
}

#[test]
fn should_parse_prefer_socket() {
let url =
MysqlUrl::new(Url::parse("mysql://root:root@localhost:3307/testdb?prefer_socket=false").unwrap()).unwrap();
assert!(!url.prefer_socket().unwrap());
}

#[test]
fn should_parse_sslaccept() {
let url =
MysqlUrl::new(Url::parse("mysql://root:root@localhost:3307/testdb?sslaccept=strict").unwrap()).unwrap();
assert!(url.query_params.use_ssl);
assert!(!url.query_params.ssl_opts.skip_domain_validation());
assert!(!url.query_params.ssl_opts.accept_invalid_certs());
}

#[test]
fn should_parse_ipv6_host() {
let url = MysqlUrl::new(Url::parse("mysql://[2001:db8:1234::ffff]:5432/testdb").unwrap()).unwrap();
assert_eq!("2001:db8:1234::ffff", url.host());
}

#[test]
fn should_allow_changing_of_cache_size() {
let url = MysqlUrl::new(Url::parse("mysql:///root:root@localhost:3307/foo?statement_cache_size=420").unwrap())
.unwrap();
assert_eq!(420, url.cache().capacity());
}

#[test]
fn should_have_default_cache_size() {
let url = MysqlUrl::new(Url::parse("mysql:///root:root@localhost:3307/foo").unwrap()).unwrap();
assert_eq!(100, url.cache().capacity());
}

#[tokio::test]
async fn should_map_nonexisting_database_error() {
let mut url = Url::parse(&CONN_STR).unwrap();
url.set_username("root").unwrap();
url.set_path("/this_does_not_exist");

let url = url.as_str().to_string();
let res = Quaint::new(&url).await;

let err = res.unwrap_err();

match err.kind() {
ErrorKind::DatabaseDoesNotExist { db_name } => {
assert_eq!(Some("1049"), err.original_code());
assert_eq!(Some("Unknown database \'this_does_not_exist\'"), err.original_message());
assert_eq!(&Name::available("this_does_not_exist"), db_name)
}
e => panic!("Expected `DatabaseDoesNotExist`, got {:?}", e),
}
}

#[tokio::test]
async fn should_map_wrong_credentials_error() {
let mut url = Url::parse(&CONN_STR).unwrap();
url.set_username("WRONG").unwrap();

let res = Quaint::new(url.as_str()).await;
assert!(res.is_err());

let err = res.unwrap_err();
assert!(matches!(err.kind(), ErrorKind::AuthenticationFailed { user } if user == &Name::available("WRONG")));
}
}
6 changes: 0 additions & 6 deletions quaint/src/connector/mysql/wasm/mod.rs

This file was deleted.

10 changes: 6 additions & 4 deletions quaint/src/connector/postgres.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub use wasm::common::PostgresUrl;
pub use wasm::error::PostgresError;
//! Wasm-compatible definitions for the PostgreSQL connector.
//! This module is only available with the `postgresql` feature.
pub(crate) mod error;
pub(crate) mod url;

#[cfg(feature = "postgresql")]
pub(crate) mod wasm;
pub use error::PostgresError;
pub use url::*;

#[cfg(feature = "postgresql-native")]
pub(crate) mod native;
File renamed without changes.
2 changes: 1 addition & 1 deletion quaint/src/connector/postgres/native/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tokio_postgres::error::DbError;

use crate::{
connector::postgres::wasm::error::PostgresError,
connector::postgres::error::PostgresError,
error::{Error, ErrorKind},
};

Expand Down
Loading

0 comments on commit d0f783d

Please sign in to comment.