Skip to content

Commit

Permalink
Merge branch 'main' into mff/472
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Fernández authored Nov 8, 2023
2 parents 67a2559 + 2963919 commit 82f5546
Show file tree
Hide file tree
Showing 17 changed files with 475 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-prisma-schema-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Build
run: nix build .#prisma-schema-wasm

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: '20.x'

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/query-engine-driver-adapters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
ref: ${{ github.event.pull_request.head.sha }}

- name: 'Setup Node.js'
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node_version }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,35 @@ mod mysql {
Ok(())
}

fn schema_decimal_vitess() -> String {
let schema = indoc! {
r#"model Model {
#id(id, String, @id, @default(cuid()))
decLarge Decimal @test.Decimal(20, 10)
}"#
};

schema.to_owned()
}

#[connector_test(only(Vitess), schema(schema_decimal_vitess))]
async fn native_decimal_vitess_precision(runner: Runner) -> TestResult<()> {
insta::assert_snapshot!(
run_query!(&runner, r#"mutation {
createOneModel(
data: {
decLarge: "131603421.38724228"
}
) {
decLarge
}
}"#),
@r###"{"data":{"createOneModel":{"decLarge":"131603421.38724228"}}}"###
);

Ok(())
}

fn schema_string() -> String {
let schema = indoc! {
r#"model Model {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
"license": "Apache-2.0",
"dependencies": {
"@libsql/client": "0.3.5",
"@neondatabase/serverless": "^0.6.0",
"@neondatabase/serverless": "0.6.0",
"@planetscale/database": "1.11.0",
"@prisma/adapter-libsql": "workspace:*",
"@prisma/adapter-neon": "workspace:*",
"@prisma/adapter-pg": "workspace:*",
"@prisma/adapter-planetscale": "workspace:*",
"@prisma/driver-adapter-utils": "workspace:*",
"@types/pg": "^8.10.2",
"pg": "^8.11.3",
"undici": "^5.26.5",
"ws": "^8.14.2"
"@types/pg": "8.10.2",
"pg": "8.11.3",
"undici": "5.26.5",
"ws": "8.14.2"
},
"devDependencies": {
"@types/node": "^20.5.1",
"tsup": "^7.2.0",
"@types/node": "20.5.1",
"tsup": "7.2.0",
"typescript": "5.2.2"
}
}
4 changes: 2 additions & 2 deletions query-engine/driver-adapters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"keywords": [],
"author": "",
"devDependencies": {
"@types/node": "^20.5.1",
"tsup": "^7.2.0",
"@types/node": "20.8.10",
"tsup": "7.2.0",
"typescript": "5.2.2",
"esbuild": "0.19.5",
"esbuild-register": "3.5.0"
Expand Down
39 changes: 3 additions & 36 deletions query-engine/driver-adapters/src/conversion.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
pub(crate) mod mysql;
pub(crate) mod postgres;
pub(crate) mod sqlite;

use napi::bindgen_prelude::{FromNapiValue, ToNapiValue};
use napi::NapiValue;
use serde::Serialize;
use serde_json::value::Value as JsonValue;

#[derive(Debug, Serialize)]
#[derive(Debug, PartialEq, Serialize)]
#[serde(untagged)]
pub enum JSArg {
RawString(String),
Value(serde_json::Value),
Buffer(Vec<u8>),
Array(Vec<JSArg>),
Expand All @@ -33,7 +34,6 @@ impl FromNapiValue for JSArg {
impl ToNapiValue for JSArg {
unsafe fn to_napi_value(env: napi::sys::napi_env, value: Self) -> napi::Result<napi::sys::napi_value> {
match value {
JSArg::RawString(s) => ToNapiValue::to_napi_value(env, s),
JSArg::Value(v) => ToNapiValue::to_napi_value(env, v),
JSArg::Buffer(bytes) => {
ToNapiValue::to_napi_value(env, napi::Env::from_raw(env).create_buffer_with_data(bytes)?.into_raw())
Expand All @@ -58,36 +58,3 @@ impl ToNapiValue for JSArg {
}
}
}

pub fn values_to_js_args(values: &[quaint::Value<'_>]) -> serde_json::Result<Vec<JSArg>> {
let mut args = Vec::with_capacity(values.len());

for qv in values {
let res = match &qv.typed {
quaint::ValueType::Json(s) => match s {
Some(ref s) => {
let json_str = serde_json::to_string(s)?;
JSArg::RawString(json_str)
}
None => JsonValue::Null.into(),
},
quaint::ValueType::Bytes(bytes) => match bytes {
Some(bytes) => JSArg::Buffer(bytes.to_vec()),
None => JsonValue::Null.into(),
},
quaint_value @ quaint::ValueType::Numeric(bd) => match bd {
Some(bd) => match bd.to_string().parse::<f64>() {
Ok(double) => JSArg::from(JsonValue::from(double)),
Err(_) => JSArg::from(JsonValue::from(quaint_value.clone())),
},
None => JsonValue::Null.into(),
},
quaint::ValueType::Array(Some(items)) => JSArg::Array(values_to_js_args(items)?),
quaint_value => JSArg::from(JsonValue::from(quaint_value.clone())),
};

args.push(res);
}

Ok(args)
}
107 changes: 107 additions & 0 deletions query-engine/driver-adapters/src/conversion/mysql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use crate::conversion::JSArg;
use serde_json::value::Value as JsonValue;

const DATETIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.f";
const DATE_FORMAT: &str = "%Y-%m-%d";
const TIME_FORMAT: &str = "%H:%M:%S%.f";

#[rustfmt::skip]
pub fn value_to_js_arg(value: &quaint::Value) -> serde_json::Result<JSArg> {
let res = match &value.typed {
quaint::ValueType::Numeric(Some(bd)) => JSArg::Value(JsonValue::String(bd.to_string())),
quaint::ValueType::Json(Some(s)) => JSArg::Value(JsonValue::String(serde_json::to_string(s)?)),
quaint::ValueType::Bytes(Some(bytes)) => JSArg::Buffer(bytes.to_vec()),
quaint::ValueType::Date(Some(d)) => JSArg::Value(JsonValue::String(d.format(DATE_FORMAT).to_string())),
quaint::ValueType::DateTime(Some(dt)) => JSArg::Value(JsonValue::String(dt.format(DATETIME_FORMAT).to_string())),
quaint::ValueType::Time(Some(t)) => JSArg::Value(JsonValue::String(t.format(TIME_FORMAT).to_string())),
quaint::ValueType::Array(Some(ref items)) => JSArg::Array(
items
.iter()
.map(value_to_js_arg)
.collect::<serde_json::Result<Vec<JSArg>>>()?,
),
quaint_value => JSArg::from(JsonValue::from(quaint_value.clone())),
};
Ok(res)
}

#[cfg(test)]
mod test {
use super::*;
use bigdecimal::BigDecimal;
use chrono::*;
use quaint::ValueType;
use std::str::FromStr;

#[test]
#[rustfmt::skip]
fn test_value_to_js_arg() {
let test_cases = vec![
(
ValueType::Numeric(Some(1.into())),
JSArg::Value(JsonValue::String("1".to_string()))
),
(
ValueType::Numeric(Some(BigDecimal::from_str("-1.1").unwrap())),
JSArg::Value(JsonValue::String("-1.1".to_string()))
),
(
ValueType::Numeric(None),
JSArg::Value(JsonValue::Null)
),
(
ValueType::Json(Some(serde_json::json!({"a": 1}))),
JSArg::Value(JsonValue::String("{\"a\":1}".to_string()))
),
(
ValueType::Json(None),
JSArg::Value(JsonValue::Null)
),
(
ValueType::Date(Some(NaiveDate::from_ymd_opt(2020, 2, 29).unwrap())),
JSArg::Value(JsonValue::String("2020-02-29".to_string()))
),
(
ValueType::Date(None),
JSArg::Value(JsonValue::Null)
),
(
ValueType::DateTime(Some(Utc.with_ymd_and_hms(2020, 1, 1, 23, 13, 1).unwrap().with_nanosecond(100).unwrap())),
JSArg::Value(JsonValue::String("2020-01-01 23:13:01.000000100".to_string()))
),
(
ValueType::DateTime(None),
JSArg::Value(JsonValue::Null)
),
(
ValueType::Time(Some(NaiveTime::from_hms_opt(23, 13, 1).unwrap().with_nanosecond(1200).unwrap())),
JSArg::Value(JsonValue::String("23:13:01.000001200".to_string()))
),
(
ValueType::Time(None),
JSArg::Value(JsonValue::Null)
),
(
ValueType::Array(Some(vec!(
ValueType::Numeric(Some(1.into())).into_value(),
ValueType::Numeric(None).into_value(),
ValueType::Time(Some(NaiveTime::from_hms_opt(23, 13, 1).unwrap())).into_value(),
))),
JSArg::Array(vec!(
JSArg::Value(JsonValue::String("1".to_string())),
JSArg::Value(JsonValue::Null),
JSArg::Value(JsonValue::String("23:13:01".to_string()))
))
),
];

let mut errors: Vec<String> = vec![];
for (val, expected) in test_cases {
let actual = value_to_js_arg(&val.clone().into_value()).unwrap();
if actual != expected {
errors.push(format!("transforming: {:?}, expected: {:?}, actual: {:?}", &val, expected, actual));
}
}
assert_eq!(errors.len(), 0, "{}", errors.join("\n"));
}
}
Loading

0 comments on commit 82f5546

Please sign in to comment.