use crate::*;
use std::borrow::Cow;
mod mysql;
mod postgres;
mod sqlite;
pub use mysql::*;
pub use postgres::*;
pub use sqlite::*;
mod bound_sql;
mod dialect;
mod json_path;
mod query_builder;
mod table_ref_builder;
pub use self::dialect::{
ArrayOperator, BoundPart, BoundPiece, BoundSql, JsonContainment, JsonExistence, MATCH_NOTHING,
SqlDialect,
};
pub use self::json_path::{JsonPathStep, is_plain_identifier, json_path_text, parse_json_path};
pub use self::query_builder::*;
pub use self::table_ref_builder::*;
pub trait QuotedBuilder {
fn quote(&self) -> Quote;
fn prepare_iden(&self, iden: &DynIden, sql: &mut impl SqlWriter) {
let quote = self.quote();
match &iden.0 {
Cow::Borrowed(name) => {
sql.write_char(quote.left()).unwrap();
sql.write_str(name).unwrap();
sql.write_char(quote.right()).unwrap();
}
Cow::Owned(name) => write_identifier(sql, quote, name),
}
}
}
pub(crate) fn write_identifier(out: &mut impl Write, quote: Quote, name: &str) {
out.write_char(quote.left()).unwrap();
for ch in name.chars() {
if ch == quote.right() {
out.write_char(ch).unwrap();
}
out.write_char(ch).unwrap();
}
out.write_char(quote.right()).unwrap();
}
pub trait EscapeBuilder {
fn needs_escape(&self, s: &str) -> bool {
s.chars().any(|c| {
matches!(
c,
'\r' | '\n' | '\x1a' | '\x09' | '\x08' | '\0' | '\'' | '"' | '\\'
)
})
}
fn escape_string(&self, string: &str) -> String {
let mut escaped = String::with_capacity(string.len() + 8);
self.write_escaped(&mut escaped, string);
escaped
}
fn write_escaped(&self, buffer: &mut impl Write, string: &str) {
for c in string.chars() {
match c {
'\\' => buffer.write_str("\\\\"),
'"' => buffer.write_str("\\\""),
'\'' => buffer.write_str("''"),
'\0' => buffer.write_str("\\0"),
'\x08' => buffer.write_str("\\b"),
'\x09' => buffer.write_str("\\t"),
'\x1a' => buffer.write_str("\\z"),
'\n' => buffer.write_str("\\n"),
'\r' => buffer.write_str("\\r"),
_ => buffer.write_char(c),
}
.unwrap()
}
}
}
pub trait PrecedenceDecider {
fn inner_expr_well_known_greater_precedence(&self, inner: &Expr, outer_oper: &Oper) -> bool {
common_inner_expr_well_known_greater_precedence(inner, outer_oper)
}
}
pub trait OperLeftAssocDecider {
fn well_known_left_associative(&self, op: &BinOper) -> bool {
common_well_known_left_associative(op)
}
}
#[derive(Debug, PartialEq)]
pub enum Oper {
UnOper(UnOper),
BinOper(BinOper),
}
impl From<UnOper> for Oper {
fn from(value: UnOper) -> Self {
Oper::UnOper(value)
}
}
impl From<BinOper> for Oper {
fn from(value: BinOper) -> Self {
Oper::BinOper(value)
}
}
impl Oper {
pub(crate) fn is_logical(&self) -> bool {
matches!(
self,
Oper::UnOper(UnOper::Not) | Oper::BinOper(BinOper::And | BinOper::Or)
)
}
pub(crate) fn is_between(&self) -> bool {
matches!(self, Oper::BinOper(BinOper::Between | BinOper::NotBetween))
}
pub(crate) fn is_like(&self) -> bool {
matches!(self, Oper::BinOper(BinOper::Like | BinOper::NotLike))
}
pub(crate) fn is_in(&self) -> bool {
matches!(self, Oper::BinOper(BinOper::In | BinOper::NotIn))
}
pub(crate) fn is_is(&self) -> bool {
matches!(self, Oper::BinOper(BinOper::Is | BinOper::IsNot))
}
pub(crate) fn is_shift(&self) -> bool {
matches!(self, Oper::BinOper(BinOper::LShift | BinOper::RShift))
}
pub(crate) fn is_arithmetic(&self) -> bool {
match self {
Oper::BinOper(b) => {
matches!(
b,
BinOper::Mul | BinOper::Div | BinOper::Mod | BinOper::Add | BinOper::Sub
)
}
_ => false,
}
}
pub(crate) fn is_comparison(&self) -> bool {
match self {
Oper::BinOper(b) => {
matches!(
b,
BinOper::SmallerThan
| BinOper::SmallerThanOrEqual
| BinOper::Equal
| BinOper::GreaterThanOrEqual
| BinOper::GreaterThan
| BinOper::NotEqual
)
}
_ => false,
}
}
}