-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfb16f1
commit 61c8c38
Showing
7 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
//! SQLite specific functions | ||
use crate::expression::functions::define_sql_function; | ||
use crate::sql_types::*; | ||
use crate::sqlite::expression::expression_methods::JsonOrNullableJsonOrJsonbOrNullableJsonb; | ||
use crate::sqlite::expression::expression_methods::MaybeNullableValue; | ||
|
||
#[cfg(feature = "sqlite")] | ||
define_sql_function! { | ||
/// Verifies that its argument is a valid JSON string or JSONB blob and returns a minified | ||
/// version of that JSON string with all unnecessary whitespace removed. | ||
/// | ||
/// # 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; | ||
/// # use serde_json::{json, Value}; | ||
/// # use diesel::sql_types::{Text, Json, Jsonb, Nullable}; | ||
/// # let connection = &mut establish_connection(); | ||
/// | ||
/// let result = diesel::select(json::<Json, _>(json!({"a": "b", "c": 1}))) | ||
/// .get_result::<String>(connection)?; | ||
/// | ||
/// assert_eq!(r#"{"a":"b","c":1}"#, result); | ||
/// | ||
/// let result = diesel::select(json::<Json, _>(json!({ "this" : "is", "a": [ "test" ] }))) | ||
/// .get_result::<String>(connection)?; | ||
/// | ||
/// assert_eq!(r#"{"a":["test"],"this":"is"}"#, result); | ||
/// | ||
/// let result = diesel::select(json::<Nullable<Json>, _>(None::<Value>)) | ||
/// .get_result::<Option<String>>(connection)?; | ||
/// | ||
/// assert!(result.is_none()); | ||
/// | ||
/// let result = diesel::select(json::<Jsonb, _>(json!({"a": "b", "c": 1}))) | ||
/// .get_result::<String>(connection)?; | ||
/// | ||
/// assert_eq!(r#"{"a":"b","c":1}"#, result); | ||
/// | ||
/// let result = diesel::select(json::<Jsonb, _>(json!({ "this" : "is", "a": [ "test" ] }))) | ||
/// .get_result::<String>(connection)?; | ||
/// | ||
/// assert_eq!(r#"{"a":["test"],"this":"is"}"#, result); | ||
/// | ||
/// let result = diesel::select(json::<Nullable<Jsonb>, _>(None::<Value>)) | ||
/// .get_result::<Option<String>>(connection)?; | ||
/// | ||
/// assert!(result.is_none()); | ||
/// | ||
/// | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
fn json<E: JsonOrNullableJsonOrJsonbOrNullableJsonb + SingleValue + MaybeNullableValue<Text>>(e: E) -> E::Out; | ||
} | ||
|
||
#[cfg(feature = "sqlite")] | ||
define_sql_function! { | ||
/// | ||
/// # 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::jsonb; | ||
/// # use serde_json::{json, Value}; | ||
/// # use diesel::sql_types::{Text, Json, Jsonb, Nullable}; | ||
/// # let connection = &mut establish_connection(); | ||
/// | ||
/// let result = diesel::select(jsonb::<Json, _>(json!({"a": "b", "c": 1}))) | ||
/// .get_result::<Value>(connection)?; | ||
/// | ||
/// assert_eq!(json!({"a": "b", "c": 1}), result); | ||
/// println!("json abc1"); | ||
/// | ||
/// let result = diesel::select(jsonb::<Jsonb, _>(json!({"a": "b", "c": 1}))) | ||
/// .get_result::<Value>(connection)?; | ||
/// | ||
/// assert_eq!(json!({"a": "b", "c": 1}), result); | ||
/// println!("jsonb abc1"); | ||
/// | ||
/// let result = diesel::select(jsonb::<Nullable<Json>, _>(None::<Value>)) | ||
/// .get_result::<Option<Value>>(connection)?; | ||
/// | ||
/// assert!(result.is_none()); | ||
/// println!("json null"); | ||
/// | ||
/// let result = diesel::select(jsonb::<Nullable<Jsonb>, _>(None::<Value>)) | ||
/// .get_result::<Option<Value>>(connection)?; | ||
/// | ||
/// assert!(result.is_none()); | ||
/// println!("jsonb null"); | ||
/// | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
fn jsonb<E: JsonOrNullableJsonOrJsonbOrNullableJsonb + SingleValue + MaybeNullableValue<Jsonb>>(e: E) -> E::Out; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
use crate::dsl::AsExpr; | ||
use crate::dsl::{AsExpr, SqlTypeOf}; | ||
use crate::expression::grouped::Grouped; | ||
|
||
/// The return type of `lhs.is(rhs)`. | ||
pub type Is<Lhs, Rhs> = Grouped<super::operators::Is<Lhs, AsExpr<Rhs, Lhs>>>; | ||
|
||
/// The return type of `lhs.is_not(rhs)`. | ||
pub type IsNot<Lhs, Rhs> = Grouped<super::operators::IsNot<Lhs, AsExpr<Rhs, Lhs>>>; | ||
|
||
/// Return type of [`json(json)`](super::functions::json()) | ||
#[allow(non_camel_case_types)] | ||
#[cfg(feature = "sqlite")] | ||
pub type json<E> = super::functions::json<SqlTypeOf<E>, E>; | ||
|
||
/// Return type of [`jsonb(json)`](super::functions::jsonb()) | ||
#[allow(non_camel_case_types)] | ||
#[cfg(feature = "sqlite")] | ||
pub type jsonb<E> = super::functions::jsonb<SqlTypeOf<E>, E>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters