Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky committed Jul 22, 2024
1 parent b9e81b7 commit edd0a60
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
6 changes: 3 additions & 3 deletions quaint/src/connector/mysql/native/column_type.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::connector::ColumnType;
use mysql_async::Column;
use mysql_async::Column as MysqlColumn;

impl From<&Column> for ColumnType {
fn from(value: &Column) -> Self {
impl From<&MysqlColumn> for ColumnType {
fn from(value: &MysqlColumn) -> Self {
ColumnType::from_type_identifier(value)
}
}
11 changes: 5 additions & 6 deletions quaint/src/connector/postgres/native/column_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ macro_rules! create_pg_mapping {

// Generate validators
$(
concat_idents::concat_idents!(enum_name = PGColumnValidator, $key {
concat_idents::concat_idents!(struct_name = PGColumnValidator, $key {
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct enum_name;
pub struct struct_name;

impl enum_name {
impl struct_name {
#[inline]
#[allow(clippy::extra_unused_lifetimes)]
pub fn read<'a>(&self, val: $typ) -> $typ {
Expand Down Expand Up @@ -89,9 +89,6 @@ macro_rules! create_pg_mapping {

// Create a mapping between Postgres types and ColumnType and ensures there's a single source of truth.
// ColumnType(<accepted data>) => [PostgresType(s)...]
//
// For the cases where the Postgres type is not directly mappable to ColumnType, use the following:
// [PGColumnType => ColumnType]
create_pg_mapping! {
Boolean(Option<bool>) => [BOOL],
Int32(Option<i32>) => [INT2, INT4],
Expand Down Expand Up @@ -123,6 +120,8 @@ create_pg_mapping! {
UuidArray(impl Iterator<Item = Option<uuid::Uuid>>) => [UUID_ARRAY],
JsonArray(impl Iterator<Item = Option<serde_json::Value>>) => [JSON_ARRAY, JSONB_ARRAY],

// For the cases where the Postgres type is not directly mappable to ColumnType, use the following:
// [PGColumnType => ColumnType]
[Enum => Text],
[EnumArray => TextArray],
[UnknownArray => TextArray],
Expand Down
4 changes: 4 additions & 0 deletions quaint/src/connector/postgres/native/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ impl GetRow for PostgresRow {
let pg_ty = row.columns()[i].type_();
let column_type = PGColumnType::from_pg_type(pg_ty);

// This convoluted nested enum is macro-generated to ensure we have a single source of truth for
// the mapping between Postgres types and ColumnType. The macro is in `./column_type.rs`.
// PGColumnValidator<Type> are used to softly ensure that the correct `ValueType` variants are created.
// If you ever add a new type or change some mapping, please ensure you pass the data through `v.read()`.
let result = match column_type {
PGColumnType::Boolean(ty, v) => match ty {
PGColumnTypeBoolean::BOOL => ValueType::Boolean(v.read(row.try_get(i)?)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ mod query_raw {
let res = run_query_json!(
&runner,
r#"
mutation {
queryRaw(
query: "BEGIN NOT ATOMIC\n INSERT INTO Test VALUES(FLOOR(RAND()*1000));\n SELECT * FROM Test;\n END",
parameters: "[]"
)
}
"#
mutation {
queryRaw(
query: "BEGIN NOT ATOMIC\n INSERT INTO Test VALUES(FLOOR(RAND()*1000));\n SELECT * FROM Test;\n END",
parameters: "[]"
)
}
"#
);
// fmt_execute_raw cannot run this query, doing it directly instead
insta::assert_json_snapshot!(res,
Expand Down Expand Up @@ -53,4 +53,34 @@ mod query_raw {

Ok(())
}

#[connector_test(only(MySQL("mariadb")))]
async fn mysql_call_2(runner: Runner) -> TestResult<()> {
let res = run_query_json!(
&runner,
r#"
mutation {
queryRaw(
query: "BEGIN NOT ATOMIC\n INSERT INTO Test VALUES(FLOOR(RAND()*1000));\n SELECT * FROM Test WHERE 1=0;\n END",
parameters: "[]"
)
}
"#
);

insta::assert_json_snapshot!(res,
@r###"
{
"data": {
"queryRaw": {
"columns": [],
"types": [],
"rows": []
}
}
}
"###);

Ok(())
}
}
6 changes: 4 additions & 2 deletions query-engine/connectors/sql-query-connector/src/ser_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ impl<'a> SerializedTypes<'a> {
}
}

// Client doesn't know how to handle unknown types.
assert!(!types.contains(&ColumnType::Unknown));
if !self.0.is_empty() {
// Client doesn't know how to handle unknown types.
assert!(!types.contains(&ColumnType::Unknown));
}

types
}
Expand Down
1 change: 0 additions & 1 deletion query-engine/driver-adapters/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ macro_rules! js_column_type {
}
}
};

}

js_column_type! {
Expand Down

0 comments on commit edd0a60

Please sign in to comment.