-
-
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.
Merge pull request #4388 from xuehaonan27/add_sqlite_json_functions
Add sqlite functions `json` and `jsonb`
- Loading branch information
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,107 @@ | ||
//! SQLite specific functions | ||
use crate::expression::functions::define_sql_function; | ||
use crate::sql_types::*; | ||
use crate::sqlite::expression::expression_methods::BinaryOrNullableBinary; | ||
use crate::sqlite::expression::expression_methods::MaybeNullableValue; | ||
use crate::sqlite::expression::expression_methods::TextOrNullableText; | ||
|
||
#[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, Nullable}; | ||
/// # let connection = &mut establish_connection(); | ||
/// | ||
/// let result = diesel::select(json::<Text, _>(r#"{"a": "b", "c": 1}"#)) | ||
/// .get_result::<Value>(connection)?; | ||
/// | ||
/// assert_eq!(json!({"a":"b","c":1}), result); | ||
/// | ||
/// let result = diesel::select(json::<Text, _>(r#"{ "this" : "is", "a": [ "test" ] }"#)) | ||
/// .get_result::<Value>(connection)?; | ||
/// | ||
/// assert_eq!(json!({"a":["test"],"this":"is"}), result); | ||
/// | ||
/// let result = diesel::select(json::<Nullable<Text>, _>(None::<&str>)) | ||
/// .get_result::<Option<Value>>(connection)?; | ||
/// | ||
/// assert!(result.is_none()); | ||
/// | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
fn json<E: TextOrNullableText + MaybeNullableValue<Json>>(e: E) -> E::Out; | ||
} | ||
|
||
#[cfg(feature = "sqlite")] | ||
define_sql_function! { | ||
/// The jsonb(X) function returns the binary JSONB representation of the JSON provided as argument 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::{sql, jsonb}; | ||
/// # use serde_json::{json, Value}; | ||
/// # use diesel::sql_types::{Text, Binary, Nullable}; | ||
/// # let connection = &mut establish_connection(); | ||
/// | ||
/// let version = diesel::select(sql::<Text>("sqlite_version();")) | ||
/// .get_result::<String>(connection)?; | ||
/// | ||
/// // Querying SQLite version should not fail. | ||
/// let version_components: Vec<&str> = version.split('.').collect(); | ||
/// let major: u32 = version_components[0].parse().unwrap(); | ||
/// let minor: u32 = version_components[1].parse().unwrap(); | ||
/// let patch: u32 = version_components[2].parse().unwrap(); | ||
/// | ||
/// if major > 3 || (major == 3 && minor >= 45) { | ||
/// /* Valid sqlite version, do nothing */ | ||
/// } else { | ||
/// println!("SQLite version is too old, skipping the test."); | ||
/// return Ok(()); | ||
/// } | ||
/// | ||
/// let result = diesel::select(jsonb::<Binary, _>(br#"{"a": "b", "c": 1}"#)) | ||
/// .get_result::<Value>(connection)?; | ||
/// | ||
/// assert_eq!(json!({"a": "b", "c": 1}), result); | ||
/// | ||
/// let result = diesel::select(jsonb::<Binary, _>(br#"{"this":"is","a":["test"]}"#)) | ||
/// .get_result::<Value>(connection)?; | ||
/// | ||
/// assert_eq!(json!({"this":"is","a":["test"]}), result); | ||
/// | ||
/// let result = diesel::select(jsonb::<Nullable<Binary>, _>(None::<Vec<u8>>)) | ||
/// .get_result::<Option<Value>>(connection)?; | ||
/// | ||
/// assert!(result.is_none()); | ||
/// | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
fn jsonb<E: BinaryOrNullableBinary + 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