use std::marker::PhantomData;
use crate::filter::{CmpOp, Filter, FilterNode, JsonFilterOp};
use crate::order::OrderBy;
use crate::value::{Encodable, Ordered, Value};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JsonPathSegment {
Key(&'static str),
Index(usize),
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct JsonPath(pub Vec<JsonPathSegment>);
impl From<&'static str> for JsonPath {
fn from(key: &'static str) -> Self {
Self(vec![JsonPathSegment::Key(key)])
}
}
impl JsonPath {
#[must_use]
pub fn new(segments: impl IntoIterator<Item = JsonPathSegment>) -> Self {
Self(segments.into_iter().collect())
}
}
#[derive(Clone, PartialEq, Debug)]
pub struct JsonColumn<M, T> {
pub table: &'static str,
pub column: &'static str,
pub path: JsonPath,
pub text: bool,
pub(crate) _marker: PhantomData<fn() -> (M, T)>,
}
impl<M, T: Encodable> JsonColumn<M, T> {
pub fn eq<V: Into<T>>(self, value: V) -> Filter<M> {
let value = value.into().to_value();
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Eq),
value,
})
}
pub fn ne<V: Into<T>>(self, value: V) -> Filter<M> {
let value = value.into().to_value();
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Ne),
value,
})
}
}
impl<M, T: Ordered> JsonColumn<M, T> {
pub fn gt<V: Into<T>>(self, value: V) -> Filter<M> {
let value = value.into().to_value();
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Gt),
value,
})
}
pub fn gte<V: Into<T>>(self, value: V) -> Filter<M> {
let value = value.into().to_value();
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Gte),
value,
})
}
pub fn lt<V: Into<T>>(self, value: V) -> Filter<M> {
let value = value.into().to_value();
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Lt),
value,
})
}
pub fn lte<V: Into<T>>(self, value: V) -> Filter<M> {
let value = value.into().to_value();
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Lte),
value,
})
}
}
impl<M> JsonColumn<M, String> {
pub fn like(self, pattern: &str) -> Filter<M> {
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Like),
value: Value::Str(format!("%{pattern}%").into()),
})
}
pub fn ilike(self, pattern: &str) -> Filter<M> {
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Ilike),
value: Value::Str(format!("%{pattern}%").into()),
})
}
}
impl<M> JsonColumn<M, serde_json::Value> {
pub fn get(mut self, key: &'static str) -> JsonColumn<M, serde_json::Value> {
self.path.0.push(JsonPathSegment::Key(key));
self
}
pub fn get_text(mut self, key: &'static str) -> JsonColumn<M, String> {
self.path.0.push(JsonPathSegment::Key(key));
JsonColumn {
table: self.table,
column: self.column,
path: self.path,
text: true,
_marker: PhantomData,
}
}
pub fn at(mut self, index: usize) -> JsonColumn<M, serde_json::Value> {
self.path.0.push(JsonPathSegment::Index(index));
self
}
pub fn gt<V: Into<serde_json::Value>>(self, value: V) -> Filter<M> {
let value = value.into().to_value();
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Gt),
value,
})
}
pub fn gte<V: Into<serde_json::Value>>(self, value: V) -> Filter<M> {
let value = value.into().to_value();
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Gte),
value,
})
}
pub fn lt<V: Into<serde_json::Value>>(self, value: V) -> Filter<M> {
let value = value.into().to_value();
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Lt),
value,
})
}
pub fn lte<V: Into<serde_json::Value>>(self, value: V) -> Filter<M> {
let value = value.into().to_value();
Filter::new(FilterNode::Json {
table: self.table,
column: self.column,
path: self.path,
text: self.text,
op: JsonFilterOp::Cmp(CmpOp::Lte),
value,
})
}
}
impl<M, T> JsonColumn<M, T> {
pub fn asc(&self) -> OrderBy<M> {
OrderBy {
table: self.table,
column: self.column,
desc: false,
json_path: Some(self.path.clone()),
text: self.text,
spatial_distance: None,
_marker: PhantomData,
}
}
pub fn desc(&self) -> OrderBy<M> {
OrderBy {
table: self.table,
column: self.column,
desc: true,
json_path: Some(self.path.clone()),
text: self.text,
spatial_distance: None,
_marker: PhantomData,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct JsonSet {
pub(crate) column: &'static str,
pub(crate) path: JsonPath,
pub(crate) value: Value,
}