Skip to content

Commit ae53c5d

Browse files
committed
implement jsonb_path_exists
1 parent 561ca49 commit ae53c5d

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

diesel/src/pg/expression/functions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2301,11 +2301,11 @@ define_sql_function! {
23012301
/// # use jsonpath_rust::JsonPath;
23022302
/// # let connection = &mut establish_connection();
23032303
/// let jsonb:Value = serde_json::json!({"a":[1,2,3,4,5]});
2304-
/// let json_path = jsonpath_rust::path!("$.a[*] ? (@ >= 2 && @ <= 4)");
2305-
/// let exists = jsonb_path_exists::<Jsonb,Jsonpath,_,_>(jsonb,json_path);
2304+
/// let json_path = jsonpath_rust::path!("$.a[ ? (@ >= 2 && @ <= 4)]");
2305+
/// let exists = jsonb_path_exists::<Jsonb,Jsonpath,_,_>(jsonb,json_path).get_result::<bool>(connection)?;
23062306
/// assert!(exists);
23072307
/// # Ok(())
23082308
/// # }
23092309
/// ```
2310-
fn jsonb_path_exists<J: JsonbOrNullableJsonb + SingleValue,P:MaybeNullableValue<Jsonpath>>(jsonb:J,path:P)->Bool;
2311-
}
2310+
fn jsonb_path_exists<J: JsonbOrNullableJsonb + SingleValue, P: MaybeNullableValue<Jsonpath>>(jsonb: J, path: P) -> Bool;
2311+
}

diesel/src/pg/types/json_path.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use crate::deserialize::FromSql;
2-
use crate::pg::{backend::Pg, PgValue};
3-
use crate::serialize::{IsNull, Output, ToSql};
4-
use crate::{deserialize, serialize, sql_types};
51
use std::error::Error;
6-
use std::fmt::{Debug, Display};
72
use std::io::Write;
83
use std::str;
94
use std::str::FromStr;
105

6+
use crate::{deserialize, serialize, sql_types};
7+
use crate::deserialize::FromSql;
8+
use crate::pg::{backend::Pg, PgValue};
9+
use crate::serialize::{IsNull, Output, ToSql};
10+
1111
impl FromSql<sql_types::Jsonpath, Pg> for jsonpath_rust::JsonPath {
1212
fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
1313
println!("{:?}", value.as_bytes());
@@ -29,18 +29,19 @@ impl ToSql<sql_types::Jsonpath, Pg> for jsonpath_rust::JsonPath {
2929

3030
#[cfg(test)]
3131
mod tests {
32-
use super::*;
32+
use jsonpath_rust::JsonPath;
33+
3334
use crate::pg::PgValue;
3435
use crate::query_builder::bind_collector::ByteWrapper;
35-
use jsonpath_rust::{path, JsonPath};
36+
37+
use super::*;
3638

3739
#[test]
3840
fn json_to_sql() {
3941
let mut buffer = Vec::new();
4042
let mut bytes = Output::test(ByteWrapper(&mut buffer));
4143
let test_json_path = JsonPath::from_str("$.a[?(@ >= 2 && @ <= 4)]").unwrap();
4244
ToSql::<sql_types::Jsonpath, Pg>::to_sql(&test_json_path, &mut bytes).unwrap();
43-
println!("{}",String::from_utf8(buffer.clone()).unwrap());
4445
assert_eq!(buffer, b"$.'a'[?(@ >= 2 && @ <= 4)]");
4546
}
4647

diesel/src/pg/types/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod integers;
1010
mod ipnet_address;
1111
#[cfg(feature = "serde_json")]
1212
mod json;
13+
mod json_path;
1314
mod mac_addr;
1415
mod mac_addr_8;
1516
#[doc(hidden)]
@@ -23,7 +24,6 @@ mod ranges;
2324
mod record;
2425
#[cfg(feature = "uuid")]
2526
mod uuid;
26-
mod json_path;
2727

2828
/// PostgreSQL specific SQL types
2929
///
@@ -646,11 +646,10 @@ pub mod sql_types {
646646
#[diesel(postgres_type(name = "citext"))]
647647
pub struct Citext;
648648

649-
650649
/// The [`Jsonpath`] SQL type. This is a PostgreSQL specific type.
651650
#[cfg(feature = "postgres_backend")]
652651
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
653-
#[diesel(postgres_type(name = "jsonpath"))]
652+
#[diesel(postgres_type(oid = 4072, array_oid = 4073))]
654653
pub struct Jsonpath;
655654
}
656655

diesel/src/type_impls/json.rs

+7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ use crate::expression::AsExpression;
55
use crate::sql_types::Json;
66
#[cfg(any(feature = "postgres_backend", feature = "sqlite"))]
77
use crate::sql_types::Jsonb;
8+
use crate::sql_types::Jsonpath;
89

910
#[derive(AsExpression, FromSqlRow)]
1011
#[diesel(foreign_derive)]
1112
#[diesel(sql_type = Json)]
1213
#[cfg_attr(any(feature = "postgres_backend", feature = "sqlite"), diesel(sql_type = Jsonb))]
1314
struct SerdeJsonValueProxy(serde_json::Value);
15+
16+
17+
#[derive(AsExpression, FromSqlRow)]
18+
#[diesel(foreign_derive)]
19+
#[cfg_attr(any(feature = "postgres_backend"), diesel(sql_type = Jsonpath))]
20+
struct JsonpathProxy(jsonpath_rust::JsonPath);

0 commit comments

Comments
 (0)