#[cfg(not(feature = "std"))]
use crate::prelude::*;
use crate::values::PostgresValue;
use drizzle_core::ToSQL;
use drizzle_core::expr::{Expr, NonNull, SQLExpr, Scalar};
use drizzle_core::sql::{SQL, SQLChunk};
use drizzle_types::postgres::types::Boolean;
pub struct PgArray<T>(pub Vec<T>);
impl<'a, T> ToSQL<'a, PostgresValue<'a>> for PgArray<T>
where
T: Into<PostgresValue<'a>> + Clone,
{
fn to_sql(&self) -> SQL<'a, PostgresValue<'a>> {
let array: Vec<PostgresValue<'a>> = self.0.iter().map(|v| v.clone().into()).collect();
SQL::param(PostgresValue::Array(array))
}
}
pub fn array_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 array_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 array_overlaps<'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 trait ArrayExprExt<'a>: Expr<'a, PostgresValue<'a>> + Sized {
fn array_contains<R>(self, other: R) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
where
R: ToSQL<'a, PostgresValue<'a>>,
{
array_contains(self, other)
}
fn array_contained<R>(
self,
other: R,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
where
R: ToSQL<'a, PostgresValue<'a>>,
{
array_contained(self, other)
}
fn array_overlaps<R>(self, other: R) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar>
where
R: ToSQL<'a, PostgresValue<'a>>,
{
array_overlaps(self, other)
}
}
impl<'a, E: Expr<'a, PostgresValue<'a>>> ArrayExprExt<'a> for E {}