use crate::values::PostgresValue;
use drizzle_core::expr::{Expr, NonNull, SQLExpr};
use drizzle_core::scope::Arg;
use drizzle_core::sql::{SQL, SQLChunk};
use drizzle_types::postgres::types::Boolean;
#[allow(clippy::type_complexity)]
pub fn regex_match<'a, E>(
expr: E,
pattern: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, E::Aggregate, Arg<E::Nullable, E::Sources>>
where
E: Expr<'a, PostgresValue<'a>>,
E::SQLType: drizzle_types::Textual,
{
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("~".into()))
.append(SQL::param(PostgresValue::Text(pattern.into()))),
)
}
#[allow(clippy::type_complexity)]
pub fn regex_match_ci<'a, E>(
expr: E,
pattern: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, E::Aggregate, Arg<E::Nullable, E::Sources>>
where
E: Expr<'a, PostgresValue<'a>>,
E::SQLType: drizzle_types::Textual,
{
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("~*".into()))
.append(SQL::param(PostgresValue::Text(pattern.into()))),
)
}
#[allow(clippy::type_complexity)]
pub fn regex_not_match<'a, E>(
expr: E,
pattern: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, E::Aggregate, Arg<E::Nullable, E::Sources>>
where
E: Expr<'a, PostgresValue<'a>>,
E::SQLType: drizzle_types::Textual,
{
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("!~".into()))
.append(SQL::param(PostgresValue::Text(pattern.into()))),
)
}
#[allow(clippy::type_complexity)]
pub fn regex_not_match_ci<'a, E>(
expr: E,
pattern: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, E::Aggregate, Arg<E::Nullable, E::Sources>>
where
E: Expr<'a, PostgresValue<'a>>,
E::SQLType: drizzle_types::Textual,
{
SQLExpr::new(
expr.to_sql()
.push(SQLChunk::Raw("!~*".into()))
.append(SQL::param(PostgresValue::Text(pattern.into()))),
)
}
pub trait RegexExprExt<'a>: Expr<'a, PostgresValue<'a>> + Sized {
#[allow(clippy::type_complexity)]
fn regex_match(
self,
pattern: &'a str,
) -> SQLExpr<
'a,
PostgresValue<'a>,
Boolean,
NonNull,
Self::Aggregate,
Arg<Self::Nullable, Self::Sources>,
>
where
Self::SQLType: drizzle_types::Textual,
{
regex_match(self, pattern)
}
#[allow(clippy::type_complexity)]
fn regex_match_ci(
self,
pattern: &'a str,
) -> SQLExpr<
'a,
PostgresValue<'a>,
Boolean,
NonNull,
Self::Aggregate,
Arg<Self::Nullable, Self::Sources>,
>
where
Self::SQLType: drizzle_types::Textual,
{
regex_match_ci(self, pattern)
}
#[allow(clippy::type_complexity)]
fn regex_not_match(
self,
pattern: &'a str,
) -> SQLExpr<
'a,
PostgresValue<'a>,
Boolean,
NonNull,
Self::Aggregate,
Arg<Self::Nullable, Self::Sources>,
>
where
Self::SQLType: drizzle_types::Textual,
{
regex_not_match(self, pattern)
}
#[allow(clippy::type_complexity)]
fn regex_not_match_ci(
self,
pattern: &'a str,
) -> SQLExpr<
'a,
PostgresValue<'a>,
Boolean,
NonNull,
Self::Aggregate,
Arg<Self::Nullable, Self::Sources>,
>
where
Self::SQLType: drizzle_types::Textual,
{
regex_not_match_ci(self, pattern)
}
}
impl<'a, E: Expr<'a, PostgresValue<'a>>> RegexExprExt<'a> for E {}