mod coding;
mod normalize;
pub mod strict;
pub(crate) use coding::is_homogeneous;
pub(crate) use normalize::cmp_related;
pub use normalize::normalize_condition;
use crate::json::{Object, Value};
#[derive(Clone, Debug, PartialEq, Default)]
pub struct Ast {
pub schema: Option<String>,
pub table: String,
pub alias: Option<String>,
pub where_: Option<Condition>,
pub related: Option<Vec<CorrelatedSubquery>>,
pub start: Option<Bound>,
pub limit: Option<f64>,
pub order_by: Option<Vec<OrderPart>>,
pub aggregate: Option<Aggregate>,
pub group_by: Option<Vec<String>>,
}
impl Ast {
pub fn new(table: impl Into<String>) -> Self {
Ast {
table: table.into(),
..Ast::default()
}
}
}
macro_rules! string_enum {
($(#[$m:meta])* $name:ident { $($variant:ident = $s:literal),* $(,)? }) => {
$(#[$m])*
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum $name { $(#[doc = concat!("`", $s, "`")] $variant),* }
impl $name {
pub const ALL: &[$name] = &[$($name::$variant),*];
pub fn as_str(self) -> &'static str {
match self { $($name::$variant => $s),* }
}
pub fn parse(s: &str) -> Option<$name> {
match s { $($s => Some($name::$variant),)* _ => None }
}
}
};
}
pub(crate) use string_enum;
string_enum!(
AggregateFunction { Count = "count", Sum = "sum", Avg = "avg", Min = "min", Max = "max" }
);
string_enum!(System { Permissions = "permissions", Client = "client", Test = "test" });
string_enum!(Direction { Asc = "asc", Desc = "desc" });
string_enum!(SimpleOperator {
Eq = "=",
Ne = "!=",
Is = "IS",
IsNot = "IS NOT",
Lt = "<",
Gt = ">",
Le = "<=",
Ge = ">=",
Like = "LIKE",
NotLike = "NOT LIKE",
Ilike = "ILIKE",
NotIlike = "NOT ILIKE",
In = "IN",
NotIn = "NOT IN",
});
string_enum!(CorrelatedSubqueryOperator { Exists = "EXISTS", NotExists = "NOT EXISTS" });
string_enum!(Anchor { AuthData = "authData", PreMutationRow = "preMutationRow" });
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum JsonPathSegment {
Key(String),
Index(u32),
}
impl JsonPathSegment {
pub fn to_json(&self) -> Value {
match self {
JsonPathSegment::Key(k) => Value::String(k.clone()),
JsonPathSegment::Index(i) => Value::Number(f64::from(*i)),
}
}
}
pub const MAX_JSON_PATH_INDEX: u32 = i32::MAX as u32;
#[derive(Clone, Debug, PartialEq)]
pub struct Aggregate {
pub fn_: AggregateFunction,
pub field: Option<String>,
pub path: Option<Vec<JsonPathSegment>>,
pub distinct: Option<Vec<String>>,
}
impl Aggregate {
pub fn new(fn_: AggregateFunction) -> Self {
Aggregate {
fn_,
field: None,
path: None,
distinct: None,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Bound {
pub row: Object,
pub exclusive: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct OrderPart {
pub field: String,
pub direction: Direction,
}
#[derive(Clone, Debug, PartialEq)]
pub enum StaticField {
Single(String),
Path(Vec<String>),
}
impl StaticField {
pub fn to_json(&self) -> Value {
match self {
StaticField::Single(s) => Value::String(s.clone()),
StaticField::Path(p) => crate::decode::strings_json(p),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum ValuePosition {
Literal(Value),
Column(String),
Json {
column: String,
path: Vec<JsonPathSegment>,
},
Static {
anchor: Anchor,
field: StaticField,
},
}
#[derive(Clone, Debug, PartialEq)]
pub enum Condition {
Simple {
op: SimpleOperator,
left: ValuePosition,
right: ValuePosition,
},
And(Vec<Condition>),
Or(Vec<Condition>),
CorrelatedSubquery {
related: CorrelatedSubquery,
op: CorrelatedSubqueryOperator,
flip: Option<bool>,
scalar: Option<bool>,
},
}
impl Condition {
pub fn conditions(&self) -> Option<&[Condition]> {
match self {
Condition::And(c) | Condition::Or(c) => Some(c),
_ => None,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Correlation {
pub parent_field: Vec<String>,
pub child_field: Vec<String>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CorrelatedSubquery {
pub correlation: Correlation,
pub subquery: Box<Ast>,
pub system: Option<System>,
pub hidden: Option<bool>,
pub aggregate: Option<Aggregate>,
}
pub fn json_value_at<'a>(path: &[JsonPathSegment], value: Option<&'a Value>) -> &'a Value {
static NULL: Value = Value::Null;
let mut v = value.unwrap_or(&NULL);
for segment in path {
v = match (v, segment) {
(Value::Object(o), JsonPathSegment::Key(k)) => o.get(k).unwrap_or(&NULL),
(Value::Array(a), JsonPathSegment::Index(i)) => a.get(*i as usize).unwrap_or(&NULL),
_ => &NULL,
};
}
v
}
pub fn format_json_path(column: &str, path: &[JsonPathSegment]) -> String {
let mut out = column.to_owned();
for segment in path {
match segment {
JsonPathSegment::Key(k) => {
out.push('.');
out.push_str(k);
}
JsonPathSegment::Index(i) => out.push_str(&format!("[{i}]")),
}
}
out
}