Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQLite function json_type #4401

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 252 additions & 0 deletions diesel/src/sqlite/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,255 @@ define_sql_function! {
#[sql_name = "json_pretty"]
fn json_pretty_with_indentation<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Text>>(j: J, indentation: Nullable<Text>) -> J::Out;
}

#[cfg(feature = "sqlite")]
define_sql_function! {
/// Returns the "type" of the outermost element of X.
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # #[cfg(feature = "serde_json")]
/// # run_test().unwrap();
/// # }
/// #
/// # #[cfg(feature = "serde_json")]
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::json_type;
/// # use diesel::sql_types::{Json, Jsonb, Nullable};
/// # use serde_json::{json, Value};
/// # let connection = &mut establish_connection();
///
/// let result = diesel::select(json_type::<Json, _>(json!({"a": "b", "c": 1})))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("object".to_string(), result);
///
/// let result = diesel::select(json_type::<Json, _>(json!([1,2,3])))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("array".to_string(), result);
///
/// let result = diesel::select(json_type::<Json, _>(json!("abc")))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("text".to_string(), result);
///
/// let result = diesel::select(json_type::<Json, _>(json!(-123.4)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("real".to_string(), result);
///
/// let result = diesel::select(json_type::<Json, _>(json!(42)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("integer".to_string(), result);
///
/// let result = diesel::select(json_type::<Json, _>(json!(true)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("true".to_string(), result);
///
/// let result = diesel::select(json_type::<Json, _>(json!(false)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("false".to_string(), result);
///
/// let result = diesel::select(json_type::<Json, _>(json!(null)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("null".to_string(), result);
///
/// let result = diesel::select(json_type::<Nullable<Json>, _>(None::<Value>))
/// .get_result::<Option<String>>(connection)?;
///
/// assert!(result.is_none());
///
///
///
/// let result = diesel::select(json_type::<Jsonb, _>(json!({"a": "b", "c": 1})))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("object".to_string(), result);
///
/// let result = diesel::select(json_type::<Jsonb, _>(json!([1,2,3])))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("array".to_string(), result);
///
/// let result = diesel::select(json_type::<Jsonb, _>(json!("abc")))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("text".to_string(), result);
///
/// let result = diesel::select(json_type::<Jsonb, _>(json!(-123.4)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("real".to_string(), result);
///
/// let result = diesel::select(json_type::<Jsonb, _>(json!(42)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("integer".to_string(), result);
///
/// let result = diesel::select(json_type::<Jsonb, _>(json!(true)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("true".to_string(), result);
///
/// let result = diesel::select(json_type::<Jsonb, _>(json!(false)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("false".to_string(), result);
///
/// let result = diesel::select(json_type::<Jsonb, _>(json!(null)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("null".to_string(), result);
///
/// let result = diesel::select(json_type::<Nullable<Jsonb>, _>(None::<Value>))
/// .get_result::<Option<String>>(connection)?;
///
/// assert!(result.is_none());
///
/// # Ok(())
/// # }
/// ```
fn json_type<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Text>>(j: J) -> J::Out;
}

#[cfg(feature = "sqlite")]
define_sql_function! {
/// Returns the "type" of the element in X that is selected by path P.
/// If the path P in json_type(X,P) selects an element that does not exist in X, then this function returns NULL.
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # #[cfg(feature = "serde_json")]
/// # run_test().unwrap();
/// # }
/// #
/// # #[cfg(feature = "serde_json")]
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::json_type_with_path;
/// # use diesel::sql_types::{Json, Jsonb, Nullable};
/// # use serde_json::{json, Value};
/// # let connection = &mut establish_connection();
///
/// let result = diesel::select(json_type_with_path::<Json, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("object".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Json, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("array".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Json, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[0]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("integer".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Json, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[1]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("real".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Json, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[2]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("true".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Json, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[3]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("false".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Json, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[4]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("null".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Json, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[5]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("text".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Nullable<Json>, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[6]"))
/// .get_result::<Option<String>>(connection)?;
///
/// assert!(result.is_none());
///
/// let result = diesel::select(json_type_with_path::<Nullable<Json>, _, _>(None::<Value>, None::<&str>))
/// .get_result::<Option<String>>(connection)?;
///
/// assert!(result.is_none());
///
///
///
///
///
/// let result = diesel::select(json_type_with_path::<Jsonb, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("object".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Jsonb, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("array".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Jsonb, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[0]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("integer".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Jsonb, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[1]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("real".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Jsonb, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[2]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("true".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Jsonb, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[3]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("false".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Jsonb, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[4]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("null".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Jsonb, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[5]"))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("text".to_string(), result);
///
/// let result = diesel::select(json_type_with_path::<Nullable<Jsonb>, _, _>(json!({"a":[2,3.5,true,false,null,"x"]}), "$.a[6]"))
/// .get_result::<Option<String>>(connection)?;
///
/// assert!(result.is_none());
///
/// let result = diesel::select(json_type_with_path::<Nullable<Jsonb>, _, _>(None::<Value>, None::<&str>))
/// .get_result::<Option<String>>(connection)?;
///
/// assert!(result.is_none());
///
/// # Ok(())
/// # }
/// ```
#[sql_name = "json_type"]
fn json_type_with_path<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Text>>(j: J, path: Nullable<Text>) -> J::Out;
}
10 changes: 10 additions & 0 deletions diesel/src/sqlite/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ pub type json_pretty<E> = super::functions::json_pretty<SqlTypeOf<E>, E>;
#[cfg(feature = "sqlite")]
pub type json_pretty_with_indentation<J, I> =
super::functions::json_pretty_with_indentation<SqlTypeOf<J>, J, I>;

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

/// Return type of [`json_type_with_path(json, path)`](super::functions::json_type_with_path())
#[allow(non_camel_case_types)]
#[cfg(feature = "sqlite")]
pub type json_type_with_path<J, P> = super::functions::json_type_with_path<SqlTypeOf<J>, J, P>;
4 changes: 4 additions & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ fn sqlite_functions() -> _ {
json_pretty(sqlite_extras::jsonb),
json_pretty_with_indentation(sqlite_extras::json, " "),
json_pretty_with_indentation(sqlite_extras::jsonb, " "),
json_type(sqlite_extras::json),
json_type(sqlite_extras::jsonb),
json_type_with_path(sqlite_extras::json, "$"),
json_type_with_path(sqlite_extras::jsonb, "$"),
)
}

Expand Down
Loading