sql_middleware/query_builder/
mod.rs1use std::borrow::Cow;
2
3use crate::executor::QueryTarget;
4use crate::pool::MiddlewarePoolConnection;
5use crate::translation::{PrepareMode, 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 #[must_use]
82 pub fn prepare(mut self) -> Self {
83 self.options.prepare = PrepareMode::Prepared;
84 self
85 }
86}
87
88pub(super) fn translate_query_for_target<'a>(
89 target: &QueryTarget<'_>,
90 query: &'a str,
91 params: &[RowValues],
92 options: QueryOptions,
93) -> Cow<'a, str> {
94 if params.is_empty() {
95 return Cow::Borrowed(query);
96 }
97
98 let Some(style) = target.translation_target() else {
99 return Cow::Borrowed(query);
100 };
101
102 let enabled = options.translation.resolve(target.translation_default());
103 translate_placeholders(query, style, enabled)
104}