Skip to content
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"DockerRun.DisableDockerrc": true
}
74 changes: 74 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2873,6 +2873,7 @@ extern "SQL" {
insert_after: Bool,
) -> Arr::Out;

/// Checks whether the JSON path returns any item for the specified JSON value.
/// Builds a possibly-heterogeneously-typed JSON array out of a variadic argument list. Each argument is converted as per to_json.
///
///
Expand All @@ -2888,6 +2889,31 @@ extern "SQL" {
/// #
/// # #[cfg(feature = "serde_json")]
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::jsonb_path_exists;
/// # use diesel::sql_types::{Jsonb, Nullable};
/// # use serde_json::{json, Value};
/// # let connection = &mut establish_connection();
///
/// let result = diesel::select(jsonb_path_exists::<Jsonb, _>(
/// json!({"a": [1, 2, 3, 4, 5]}),
/// "$.a[*] ? (@ > 2)"
/// ))
/// .get_result::<bool>(connection)?;
/// assert_eq!(true, result);
///
/// let result = diesel::select(jsonb_path_exists::<Jsonb, _>(
/// json!({"a": [1, 2, 3]}),
/// "$.b"
/// ))
/// .get_result::<bool>(connection)?;
/// assert_eq!(false, result);
///
/// let result = diesel::select(jsonb_path_exists::<Nullable<Jsonb>, _>(
/// None::<Value>,
/// "$.a"
/// ))
/// .get_result::<Option<bool>>(connection)?;
/// assert_eq!(None, result);
/// # use diesel::dsl::{json_build_array_1, json_build_array_2, json_build_array_0};
/// # use diesel::sql_types::{Jsonb, Array, Json, Nullable, Text, Integer, Record, Double};
/// # use serde_json::{json,Value};
Expand Down Expand Up @@ -2935,6 +2961,14 @@ extern "SQL" {
/// # Ok(())
/// # }
/// ```
fn jsonb_path_exists<E: JsonbOrNullableJsonb + SingleValue + MaybeNullableValue<Bool>>(
target: E,
path: Text,
) -> E::Out;

/// Returns the result of a JSON path predicate check for the specified JSON value.
/// Only the first item of the result is taken into account.
/// If the result is not Boolean, then NULL is returned.
#[cfg(feature = "postgres_backend")]
#[sql_name = "json_build_array"]
#[variadic(1)]
Expand All @@ -2955,6 +2989,42 @@ extern "SQL" {
/// #
/// # #[cfg(feature = "serde_json")]
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::jsonb_path_match;
/// # use diesel::sql_types::{Jsonb, Nullable};
/// # use serde_json::{json, Value};
/// # let connection = &mut establish_connection();
///
/// // Returns true because the predicate evaluates to true
/// let result = diesel::select(jsonb_path_match::<Jsonb, _>(
/// json!({"a": [1, 2, 3, 4, 5]}),
/// "$.a[*] > 2"
/// ))
/// .get_result::<Option<bool>>(connection)?;
/// assert_eq!(Some(true), result);
///
/// // Returns false because first element (1) is not > 2
/// let result = diesel::select(jsonb_path_match::<Jsonb, _>(
/// json!({"a": [1, 2, 3]}),
/// "$.a[0] > 2"
/// ))
/// .get_result::<Option<bool>>(connection)?;
/// assert_eq!(Some(false), result);
///
/// // Returns NULL because path doesn't return a boolean
/// let result = diesel::select(jsonb_path_match::<Jsonb, _>(
/// json!({"a": 123}),
/// "$.a"
/// ))
/// .get_result::<Option<bool>>(connection)?;
/// assert_eq!(None, result);
///
/// // Nullable input returns NULL
/// let result = diesel::select(jsonb_path_match::<Nullable<Jsonb>, _>(
/// None::<Value>,
/// "$.a > 1"
/// ))
/// .get_result::<Option<bool>>(connection)?;
/// assert_eq!(None, result);
/// # use diesel::dsl::{jsonb_build_array_1, jsonb_build_array_2, jsonb_build_array_0};
/// # use diesel::sql_types::{Jsonb, Array, Json, Nullable, Text, Integer, Record, Double};
/// # use serde_json::{json,Value};
Expand Down Expand Up @@ -3003,6 +3073,10 @@ extern "SQL" {
/// # }
/// ```
#[cfg(feature = "postgres_backend")]
fn jsonb_path_match<E: JsonbOrNullableJsonb + SingleValue>(
target: E,
path: Text,
) -> Nullable<Bool>;
#[sql_name = "jsonb_build_array"]
#[variadic(1)]
fn jsonb_build_array<V: SingleValue>(value: V) -> Jsonb;
Expand Down
Loading
Loading