sql_middleware/query_builder/
mod.rs1use std::borrow::Cow;
2
3use crate::executor::QueryTarget;
4use crate::pool::MiddlewarePoolConnection;
5use crate::translation::{QueryOptions, TranslationMode, translate_placeholders};
6use crate::types::RowValues;
7
8mod dml;
9mod select;
10
11pub struct QueryBuilder<'conn, 'q> {
13 pub(crate) target: QueryTarget<'conn>,
14 pub(crate) sql: Cow<'q, str>,
15 pub(crate) params: Cow<'q, [RowValues]>,
16 pub(crate) options: QueryOptions,
17}
18
19impl<'conn, 'q> QueryBuilder<'conn, 'q> {
20 pub(crate) fn new(conn: &'conn mut MiddlewarePoolConnection, sql: &'q str) -> Self {
21 Self {
22 target: conn.into(),
23 sql: Cow::Borrowed(sql),
24 params: Cow::Borrowed(&[]),
25 options: QueryOptions::default(),
26 }
27 }
28
29 pub(crate) fn new_target(target: QueryTarget<'conn>, sql: &'q str) -> Self {
30 Self {
31 target,
32 sql: Cow::Borrowed(sql),
33 params: Cow::Borrowed(&[]),
34 options: QueryOptions::default(),
35 }
36 }
37
38 #[must_use]
40 pub fn params(mut self, params: &'q [RowValues]) -> Self {
41 self.params = Cow::Borrowed(params);
42 self
43 }
44
45 #[must_use]
47 pub fn options(mut self, options: QueryOptions) -> Self {
48 self.options = options;
49 self
50 }
51
52 #[must_use]
75 pub fn translation(mut self, translation: TranslationMode) -> Self {
76 self.options.translation = translation;
77 self
78 }
79}
80
81pub(super) fn translate_query_for_target<'a>(
82 target: &QueryTarget<'_>,
83 query: &'a str,
84 params: &[RowValues],
85 options: QueryOptions,
86) -> Cow<'a, str> {
87 if params.is_empty() {
88 return Cow::Borrowed(query);
89 }
90
91 let Some(style) = target.translation_target() else {
92 return Cow::Borrowed(query);
93 };
94
95 let enabled = options.translation.resolve(target.translation_default());
96 translate_placeholders(query, style, enabled)
97}