#[cfg(not(feature = "std"))]
use crate::prelude::*;
use crate::values::PostgresValue;
use drizzle_core::ToSQL;
use drizzle_core::expr::{Expr, NonNull, Null, SQLExpr, Scalar};
use drizzle_core::sql::{SQL, SQLChunk};
use drizzle_types::postgres::types::{Boolean, Json, Text};
pub fn json_get<'a, E>(expr: E, key: &'a str) -> SQLExpr<'a, PostgresValue<'a>, Json, Null, Scalar>
where
E: Expr<'a, PostgresValue<'a>>,
{
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("->".into()))
.append(SQL::param(PostgresValue::Text(key.into()))),
)
}
pub fn json_get_idx<'a, E>(
expr: E,
index: i32,
) -> SQLExpr<'a, PostgresValue<'a>, Json, Null, Scalar>
where
E: Expr<'a, PostgresValue<'a>>,
{
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("->".into()))
.append(SQL::param(PostgresValue::Integer(index))),
)
}
pub fn json_get_text<'a, E>(
expr: E,
key: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Text, Null, Scalar>
where
E: Expr<'a, PostgresValue<'a>>,
{
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("->>".into()))
.append(SQL::param(PostgresValue::Text(key.into()))),
)
}
pub fn json_get_text_idx<'a, E>(
expr: E,
index: i32,
) -> SQLExpr<'a, PostgresValue<'a>, Text, Null, Scalar>
where
E: Expr<'a, PostgresValue<'a>>,
{
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("->>".into()))
.append(SQL::param(PostgresValue::Integer(index))),
)
}
pub fn json_get_path<'a, E>(
expr: E,
path: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Json, Null, Scalar>
where
E: Expr<'a, PostgresValue<'a>>,
{
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("#>".into()))
.append(SQL::param(PostgresValue::Text(path.into()))),
)
}
pub fn json_get_path_text<'a, E>(
expr: E,
path: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Text, Null, Scalar>
where
E: Expr<'a, PostgresValue<'a>>,
{
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("#>>".into()))
.append(SQL::param(PostgresValue::Text(path.into()))),
)
}
pub fn jsonb_contains<'a, L, R>(
left: L,
right: R,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
where
L: Expr<'a, PostgresValue<'a>>,
R: ToSQL<'a, PostgresValue<'a>>,
{
SQLExpr::new(
left.to_sql()
.push(SQLChunk::Raw("@>".into()))
.append(right.to_sql()),
)
}
pub fn jsonb_contained<'a, L, R>(
left: L,
right: R,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
where
L: Expr<'a, PostgresValue<'a>>,
R: ToSQL<'a, PostgresValue<'a>>,
{
SQLExpr::new(
left.to_sql()
.push(SQLChunk::Raw("<@".into()))
.append(right.to_sql()),
)
}
pub fn jsonb_exists_key<'a, E>(
expr: E,
key: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
where
E: Expr<'a, PostgresValue<'a>>,
{
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("?".into()))
.append(SQL::param(PostgresValue::Text(key.into()))),
)
}
pub fn jsonb_exists_any<'a, E>(
expr: E,
keys: &[&'a str],
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
where
E: Expr<'a, PostgresValue<'a>>,
{
let arr: Vec<PostgresValue<'a>> = keys
.iter()
.map(|k| PostgresValue::Text((*k).into()))
.collect();
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("?|".into()))
.append(SQL::param(PostgresValue::Array(arr))),
)
}
pub fn jsonb_exists_all<'a, E>(
expr: E,
keys: &[&'a str],
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
where
E: Expr<'a, PostgresValue<'a>>,
{
let arr: Vec<PostgresValue<'a>> = keys
.iter()
.map(|k| PostgresValue::Text((*k).into()))
.collect();
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("?&".into()))
.append(SQL::param(PostgresValue::Array(arr))),
)
}
pub trait JsonExprExt<'a>: Expr<'a, PostgresValue<'a>> + Sized {
fn json_get(self, key: &'a str) -> SQLExpr<'a, PostgresValue<'a>, Json, Null, Scalar> {
json_get(self, key)
}
fn json_get_idx(self, index: i32) -> SQLExpr<'a, PostgresValue<'a>, Json, Null, Scalar> {
json_get_idx(self, index)
}
fn json_get_text(self, key: &'a str) -> SQLExpr<'a, PostgresValue<'a>, Text, Null, Scalar> {
json_get_text(self, key)
}
fn json_get_text_idx(self, index: i32) -> SQLExpr<'a, PostgresValue<'a>, Text, Null, Scalar> {
json_get_text_idx(self, index)
}
fn json_get_path(self, path: &'a str) -> SQLExpr<'a, PostgresValue<'a>, Json, Null, Scalar> {
json_get_path(self, path)
}
fn json_get_path_text(
self,
path: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Text, Null, Scalar> {
json_get_path_text(self, path)
}
fn jsonb_contains<R>(self, other: R) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
where
R: ToSQL<'a, PostgresValue<'a>>,
{
jsonb_contains(self, other)
}
fn jsonb_contained<R>(
self,
other: R,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
where
R: ToSQL<'a, PostgresValue<'a>>,
{
jsonb_contained(self, other)
}
fn jsonb_exists_key(
self,
key: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar> {
jsonb_exists_key(self, key)
}
}
impl<'a, E: Expr<'a, PostgresValue<'a>>> JsonExprExt<'a> for E {}