Skip to content

Commit

Permalink
Float type missing & json wrongfully treated as string
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobKlocker committed Mar 8, 2025
1 parent bef8c3e commit ac14c2e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
40 changes: 28 additions & 12 deletions diesel/src/sqlite/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ extern "SQL" {
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::{sql, json_quote};
/// # use serde_json::{json, Value};
/// # use diesel::sql_types::{Text, Integer, Nullable};
/// # use diesel::sql_types::{Text, Json, Integer, Nullable};
/// # let connection = &mut establish_connection();
///
/// let version = diesel::select(sql::<Text>("sqlite_version();"))
Expand All @@ -928,25 +928,41 @@ extern "SQL" {
/// println!("SQLite version is too old, skipping the test.");
/// return Ok(());
/// }
/// let test_value = 42;
/// let result = diesel::select(json_quote::<Integer, _>(test_value))
/// let result = diesel::select(json_quote::<Integer, _>(42))
/// .get_result::<Option<String>>(connection)?;
///
/// assert_eq!(Some("42".to_string()), result);
///
/// let result = diesel::select(json_quote::<Text, _>("verdant"))
/// .get_result::<Option<String>>(connection)?;
/// assert_eq!(Some("\"verdant\"".to_string()), result);
///
/// let result = diesel::select(json_quote::<Text, _>("[1]"))
/// .get_result::<Option<String>>(connection)?;
/// assert_eq!(Some("\"[1]\"".to_string()), result);
///
/// let result = diesel::select(json_quote::<Nullable<Text>, _>(None::<&str>))
/// .get_result::<Option<String>>(connection)?;
/// assert_eq!(Some("null".to_string()), result);
///
// COMENT----------------------------------------

This comment has been minimized.

Copy link
@JakobKlocker

JakobKlocker Mar 8, 2025

Author

Both tests below fail, the first one because the Json seems to be handled as a Text and the second one because I can't find a Value for Real/Float

// BELOW NONE WORKING TESTS, JSON and FLOAT TYPES
// DATATYPE FOR FLOAT NEEDED & JSON IS TREATED AS TEXT
// let result = diesel::select(json_quote::<Json, _>(json!([1])))
// .get_result::<Option<String>>(connection)?;
// assert_eq!(Some("[1]".to_string()), result);
//
// let result = diesel::select(json_quote::<Integer, _>(3.14159))
// .get_result::<Option<String>>(connection)?;
//
// assert_eq!(Some("3.14159".to_string()), result);
// COMENT----------------------------------------
///
/// # Ok(())
/// # }
/// ```
#[sql_name = "json_quote"]
#[cfg(feature = "sqlite")]
fn json_quote<J: MaybeNullableValue<Integer> + MaybeNullableValue<Text>>( // Have to add Supoprt for Real/Floates, how? Existing Datatype?
fn json_quote<J: MaybeNullableValue<Json> + MaybeNullableValue<Integer> + MaybeNullableValue<Text>>( // Have to add Supoprt for Real/Floates, how? Existing Datatype?

This comment has been minimized.

Copy link
@JakobKlocker

JakobKlocker Mar 8, 2025

Author

It seems to me, even thought I call json_quote with a Json, it still gets treated as Text value? Therefore quotes will be added around the json, even thought it's a valid json and this shouldn't happen.

j: J
) -> Nullable<Text>;
}
// double test case
// let test_value = 3.14159;
// let result = diesel::select(json_quote::<Integer, _>(test_value))
// .get_result::<Option<String>>(connection)?;
//
// assert_eq!(Some("3.14159".to_string()), result);

5 changes: 2 additions & 3 deletions diesel/src/sqlite/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ pub type json_type<E> = super::functions::json_type<SqlTypeOf<E>, E>;
#[cfg(feature = "sqlite")]
pub type json_type_with_path<J, P> = super::functions::json_type_with_path<SqlTypeOf<J>, J, P>;

/// Return type of [`json_type(json)`](super::functions::json_type())
/// Return type of [`json_quote(value)`](super::functions::json_quote())
#[allow(non_camel_case_types)]
#[cfg(feature = "sqlite")]
pub type json_quote<J> = super::functions::json_quote<SqlTypeOf<J>, J>;

pub type json_quote<J> = super::functions::json_quote<SqlTypeOf<J>, J>;
1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ fn sqlite_functions() -> _ {
json_type(sqlite_extras::json),
json_type_with_path(sqlite_extras::json, sqlite_extras::text),
json_quote(sqlite_extras::text),
json_quote(sqlite_extras::json),
json_quote(sqlite_extras::id),

This comment has been minimized.

Copy link
@JakobKlocker

JakobKlocker Mar 8, 2025

Author

id seems to be the correct type for Integer, yet I have not found one which supports Reals/Floats.

)
}
Expand Down

0 comments on commit ac14c2e

Please sign in to comment.