Skip to content

Commit 54b0440

Browse files
committed
working on json_path_exists
1 parent 2b394ca commit 54b0440

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

diesel/src/pg/expression/functions.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2191,3 +2191,8 @@ define_sql_function! {
21912191
values: Arr2
21922192
) -> Arr2::Out;
21932193
}
2194+
2195+
#[cfg(feature = "postgres_backend")]
2196+
define_sql_function!{
2197+
fn jsonb_path_exists<J: JsonbOrNullableJsonb + SingleValue, >(jsonb:J,path:Jsonb);
2198+
}

diesel/src/pg/types/json_path.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::fmt::{Debug, Display};
2+
use std::io::Write;
3+
use std::str;
4+
use std::str::FromStr;
5+
use crate::{deserialize, serialize, sql_types};
6+
use crate::deserialize::FromSql;
7+
use crate::pg::Pg;
8+
use crate::serialize::{Output, ToSql};
9+
10+
#[cfg(all(feature = "postgres_backend", feature = "serde_json"))]
11+
impl FromSql<sql_types::Jsonpath, Pg> for jsonpath_rust::JsonPath {
12+
fn from_sql(value: Pg::RawValue<'_>) -> deserialize::Result<Self> {
13+
let str = str::from_utf8(value.as_bytes()).map_err(|_| "Invalid path".into())?;
14+
jsonpath_rust::JsonPath::from_str(str).map_err(|_| "Invalid path".into())
15+
}
16+
}
17+
18+
impl ToSql<sql_types::Jsonpath, Pg> for jsonpath_rust::JsonPath {
19+
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
20+
21+
}
22+
}
23+
24+
25+
26+
#[cfg(test)]
27+
mod tests {
28+
use jsonpath_rust::JsonPath;
29+
30+
use crate::pg::Pg;
31+
use crate::query_builder::bind_collector::ByteWrapper;
32+
use crate::serialize::{Output, ToSql};
33+
use crate::sql_types;
34+
35+
#[test]
36+
fn json_to_sql() {
37+
let mut buffer = Vec::new();
38+
let mut bytes = Output::test(ByteWrapper(&mut buffer));
39+
let test_json_path = jsonpath_rust::path!("$.abc");
40+
ToSql::<sql_types::Jsonpath, Pg>::to_sql(&test_json_path, &mut bytes).unwrap();
41+
assert_eq!(buffer, b"$.abc");
42+
}
43+
}

diesel/src/pg/types/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod ranges;
2222
mod record;
2323
#[cfg(feature = "uuid")]
2424
mod uuid;
25+
mod json_path;
2526

2627
/// PostgreSQL specific SQL types
2728
///
@@ -681,6 +682,13 @@ pub mod sql_types {
681682
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
682683
#[diesel(postgres_type(name = "citext"))]
683684
pub struct Citext;
685+
686+
687+
/// The [`Jsonpath`] SQL type. This is a PostgreSQL specific type.
688+
#[cfg(feature = "postgres_backend")]
689+
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
690+
#[diesel(postgres_type(name = "jsonpath"))]
691+
pub struct Jsonpath;
684692
}
685693

686694
mod ops {

0 commit comments

Comments
 (0)