use crate::values::PostgresValue;
use drizzle_core::expr::{Expr, NonNull, SQLExpr, Scalar};
use drizzle_core::sql::{SQL, SQLChunk};
use drizzle_types::postgres::types::Boolean;
pub fn regex_match<'a, E>(
expr: E,
pattern: &'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(pattern.into()))),
)
}
pub fn regex_match_ci<'a, E>(
expr: E,
pattern: &'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(pattern.into()))),
)
}
pub fn regex_not_match<'a, E>(
expr: E,
pattern: &'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(pattern.into()))),
)
}
pub fn regex_not_match_ci<'a, E>(
expr: E,
pattern: &'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(pattern.into()))),
)
}
pub trait RegexExprExt<'a>: Expr<'a, PostgresValue<'a>> + Sized {
fn regex_match(
self,
pattern: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar> {
regex_match(self, pattern)
}
fn regex_match_ci(
self,
pattern: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar> {
regex_match_ci(self, pattern)
}
fn regex_not_match(
self,
pattern: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar> {
regex_not_match(self, pattern)
}
fn regex_not_match_ci(
self,
pattern: &'a str,
) -> SQLExpr<'a, PostgresValue<'a>, Boolean, NonNull, Scalar> {
regex_not_match_ci(self, pattern)
}
}
impl<'a, E: Expr<'a, PostgresValue<'a>>> RegexExprExt<'a> for E {}