sea_query/query/traits.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
use std::fmt::Debug;
use crate::{backend::QueryBuilder, value::Values, SqlWriter, SqlWriterValues, SubQueryStatement};
pub trait QueryStatementBuilder: Debug {
/// Build corresponding SQL statement for certain database backend and collect query parameters into a vector
fn build_any(&self, query_builder: &dyn QueryBuilder) -> (String, Values) {
let (placeholder, numbered) = query_builder.placeholder();
let mut sql = SqlWriterValues::new(placeholder, numbered);
self.build_collect_any_into(query_builder, &mut sql);
sql.into_parts()
}
/// Build corresponding SQL statement for certain database backend and collect query parameters
fn build_collect_any(
&self,
query_builder: &dyn QueryBuilder,
sql: &mut dyn SqlWriter,
) -> String {
self.build_collect_any_into(query_builder, sql);
sql.to_string()
}
/// Build corresponding SQL statement into the SqlWriter for certain database backend and collect query parameters
fn build_collect_any_into(&self, query_builder: &dyn QueryBuilder, sql: &mut dyn SqlWriter);
fn into_sub_query_statement(self) -> SubQueryStatement;
}
pub trait QueryStatementWriter: QueryStatementBuilder {
/// Build corresponding SQL statement for certain database backend and return SQL string
///
/// # Examples
///
/// ```
/// use sea_query::{*, tests_cfg::*};
///
/// let query = Query::select()
/// .column(Glyph::Aspect)
/// .from(Glyph::Table)
/// .and_where(Expr::expr(Expr::col(Glyph::Aspect).if_null(0)).gt(2))
/// .order_by(Glyph::Image, Order::Desc)
/// .order_by((Glyph::Table, Glyph::Aspect), Order::Asc)
/// .to_string(MysqlQueryBuilder);
///
/// assert_eq!(
/// query,
/// r#"SELECT `aspect` FROM `glyph` WHERE IFNULL(`aspect`, 0) > 2 ORDER BY `image` DESC, `glyph`.`aspect` ASC"#
/// );
/// ```
fn to_string<T: QueryBuilder>(&self, query_builder: T) -> String {
let mut sql = String::with_capacity(256);
self.build_collect_any_into(&query_builder, &mut sql);
sql
}
/// Build corresponding SQL statement for certain database backend and collect query parameters into a vector
///
/// # Examples
///
/// ```
/// use sea_query::{*, tests_cfg::*};
///
/// let (query, params) = Query::select()
/// .column(Glyph::Aspect)
/// .from(Glyph::Table)
/// .and_where(Expr::expr(Expr::col(Glyph::Aspect).if_null(0)).gt(2))
/// .order_by(Glyph::Image, Order::Desc)
/// .order_by((Glyph::Table, Glyph::Aspect), Order::Asc)
/// .build(MysqlQueryBuilder);
///
/// assert_eq!(
/// query,
/// r#"SELECT `aspect` FROM `glyph` WHERE IFNULL(`aspect`, ?) > ? ORDER BY `image` DESC, `glyph`.`aspect` ASC"#
/// );
/// assert_eq!(
/// params,
/// Values(vec![Value::Int(Some(0)), Value::Int(Some(2))])
/// );
/// ```
fn build<T: QueryBuilder>(&self, query_builder: T) -> (String, Values) {
let (placeholder, numbered) = query_builder.placeholder();
let mut sql = SqlWriterValues::new(placeholder, numbered);
self.build_collect_into(query_builder, &mut sql);
sql.into_parts()
}
/// Build corresponding SQL statement for certain database backend and collect query parameters
///
/// # Examples
///
/// ```
/// use sea_query::{*, tests_cfg::*};
///
/// let query = Query::select()
/// .column(Glyph::Aspect)
/// .from(Glyph::Table)
/// .and_where(Expr::expr(Expr::col(Glyph::Aspect).if_null(0)).gt(2))
/// .order_by(Glyph::Image, Order::Desc)
/// .order_by((Glyph::Table, Glyph::Aspect), Order::Asc)
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT `aspect` FROM `glyph` WHERE IFNULL(`aspect`, 0) > 2 ORDER BY `image` DESC, `glyph`.`aspect` ASC"#
/// );
///
/// let (placeholder, numbered) = MysqlQueryBuilder.placeholder();
/// let mut sql = SqlWriterValues::new(placeholder, numbered);
///
/// assert_eq!(
/// query.build_collect(MysqlQueryBuilder, &mut sql),
/// r#"SELECT `aspect` FROM `glyph` WHERE IFNULL(`aspect`, ?) > ? ORDER BY `image` DESC, `glyph`.`aspect` ASC"#
/// );
///
/// let (sql, values) = sql.into_parts();
/// assert_eq!(
/// values,
/// Values(vec![Value::Int(Some(0)), Value::Int(Some(2))])
/// );
/// ```
fn build_collect<T: QueryBuilder>(&self, query_builder: T, sql: &mut dyn SqlWriter) -> String {
self.build_collect_into(query_builder, sql);
sql.to_string()
}
fn build_collect_into<T: QueryBuilder>(&self, query_builder: T, sql: &mut dyn SqlWriter);
}