1#[macro_use]
8mod query_id;
9#[macro_use]
10mod clause_macro;
11
12pub(crate) mod ast_pass;
13pub mod bind_collector;
14mod collected_query;
15pub(crate) mod combination_clause;
16mod debug_query;
17mod delete_statement;
18mod distinct_clause;
19pub(crate) mod from_clause;
20pub(crate) mod functions;
21pub(crate) mod group_by_clause;
22mod having_clause;
23pub(crate) mod insert_statement;
24pub(crate) mod limit_clause;
25pub(crate) mod limit_offset_clause;
26pub(crate) mod locking_clause;
27pub(crate) mod nodes;
28pub(crate) mod offset_clause;
29pub(crate) mod order_clause;
30pub(crate) mod select_clause;
31pub(crate) mod select_statement;
32mod sql_query;
33pub(crate) mod update_statement;
34pub(crate) mod upsert;
35pub(crate) mod where_clause;
36
37#[doc(inline)]
38pub use self::ast_pass::AstPass;
39#[doc(inline)]
40pub use self::bind_collector::{BindCollector, MoveableBindCollector};
41#[doc(inline)]
42pub use self::collected_query::CollectedQuery;
43#[doc(inline)]
44pub use self::debug_query::DebugQuery;
45#[doc(inline)]
46pub use self::delete_statement::{
47 BoxedCloneDeleteStatement, BoxedDeleteStatement, DeleteStatement,
48};
49#[cfg(any(feature = "mysql_backend", feature = "mariadb_backend"))]
50#[doc(inline)]
51pub use self::insert_statement::SingleRowInsertValues;
52#[doc(inline)]
53pub use self::insert_statement::{
54 IncompleteInsertOrIgnoreStatement, IncompleteInsertStatement, IncompleteReplaceStatement,
55 InsertOrIgnoreStatement, InsertStatement, ReplaceStatement,
56};
57#[doc(inline)]
58pub use self::query_id::QueryId;
59#[doc(inline)]
60pub use self::sql_query::{BoxedCloneSqlQuery, BoxedSqlQuery, SqlQuery};
61#[doc(inline)]
62pub use self::upsert::into_conflict_clause::IntoConflictValueClause;
63#[doc(inline)]
64pub use self::upsert::on_conflict_target::{ConflictTarget, OnConflictTarget};
65#[doc(inline)]
66pub use self::upsert::on_conflict_target_decorations::DecoratableTarget;
67
68#[doc(inline)]
69pub use self::update_statement::changeset::AsChangeset;
70#[doc(inline)]
71pub use self::update_statement::target::{IntoUpdateTarget, UpdateTarget};
72#[doc(inline)]
73pub use self::update_statement::{
74 BoxedCloneUpdateStatement, BoxedUpdateStatement, UpdateStatement,
75};
76
77#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
78pub use self::combination_clause::{
79 All, Distinct, Except, Intersect, ParenthesisWrapper, SupportsCombinationClause, Union,
80};
81#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
82pub use self::limit_clause::{LimitClause, NoLimitClause};
83#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
84pub use self::limit_offset_clause::{
85 BoxedCloneLimitOffsetClause, BoxedLimitOffsetClause, LimitOffsetClause,
86};
87#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
88pub use self::offset_clause::{NoOffsetClause, OffsetClause};
89#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
90pub use self::order_clause::{NoOrderClause, OrderClause};
91
92#[diesel_derives::__diesel_public_if(
93 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
94)]
95#[doc(inline)]
96pub(crate) use self::insert_statement::batch_insert::BatchInsert;
97#[diesel_derives::__diesel_public_if(
98 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
99)]
100pub(crate) use self::insert_statement::{UndecoratedInsertRecord, ValuesClause};
101
102#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
103#[doc(inline)]
104pub use self::insert_statement::{DefaultValues, InsertOrIgnore, Replace};
105
106#[cfg(not(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"))]
107pub(crate) mod returning;
108#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
109pub mod returning;
110
111#[doc(inline)]
112#[diesel_derives::__diesel_public_if(
113 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
114)]
115pub(crate) use self::ast_pass::AstPassToSqlOptions;
116
117#[doc(inline)]
118#[diesel_derives::__diesel_public_if(
119 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
120)]
121pub(crate) use self::select_clause::SelectClauseExpression;
122
123#[doc(inline)]
124#[diesel_derives::__diesel_public_if(
125 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
126)]
127pub(crate) use self::from_clause::{FromClause, NoFromClause};
128#[cfg_attr(
129 not(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"),
130 allow(unused_imports)
131)]
132#[diesel_derives::__diesel_public_if(
133 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
134)]
135#[doc(inline)]
136pub(crate) use self::group_by_clause::{GroupByClause, NoGroupByClause};
137#[diesel_derives::__diesel_public_if(
138 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
139)]
140#[doc(inline)]
141pub(crate) use self::select_statement::BoxedCloneSelectStatement;
142#[diesel_derives::__diesel_public_if(
143 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
144)]
145#[doc(inline)]
146pub(crate) use self::select_statement::BoxedSelectStatement;
147
148#[diesel_derives::__diesel_public_if(
149 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
150)]
151#[doc(inline)]
152pub(crate) use self::select_statement::SelectStatement;
153
154pub(crate) use self::insert_statement::ColumnList;
155
156#[cfg(feature = "postgres_backend")]
157pub use crate::pg::query_builder::only::Only;
158
159#[cfg(feature = "postgres_backend")]
160pub use crate::pg::query_builder::tablesample::{Tablesample, TablesampleMethod};
161
162pub(crate) use self::bind_collector::BindData;
163#[cfg(feature = "postgres_backend")]
164pub(crate) use self::bind_collector::ByteWrapper;
165use crate::backend::Backend;
166use crate::result::QueryResult;
167use alloc::boxed::Box;
168use alloc::string::String;
169use alloc::vec::Vec;
170use core::error::Error;
171
172pub(crate) use self::private::NotSpecialized;
173
174#[doc(hidden)]
175pub type Binds = Vec<Option<Vec<u8>>>;
176pub type BuildQueryResult = Result<(), Box<dyn Error + Send + Sync>>;
178
179pub trait QueryBuilder<DB: Backend> {
186 fn push_sql(&mut self, sql: &str);
188
189 fn push_identifier(&mut self, identifier: &str) -> QueryResult<()>;
192
193 fn push_bind_param(&mut self);
196
197 fn push_bind_param_value_only(&mut self) {}
200
201 fn finish(self) -> String;
203}
204
205pub trait Query {
217 type SqlType;
223}
224
225impl<T: Query> Query for &T {
226 type SqlType = T::SqlType;
227}
228
229pub trait SelectQuery {
238 type SqlType;
240}
241
242#[diagnostic::on_unimplemented(
252 message = "`{Self}` is no valid SQL fragment for the `{DB}` backend",
253 note = "this usually means that the `{DB}` database system does not support \n\
254 this SQL syntax"
255)]
256pub trait QueryFragment<DB: Backend, SP = self::private::NotSpecialized> {
257 fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()>;
263
264 #[diesel_derives::__diesel_public_if(
268 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
269 )]
270 fn to_sql(&self, out: &mut DB::QueryBuilder, backend: &DB) -> QueryResult<()> {
271 let mut options = AstPassToSqlOptions::default();
272 self.walk_ast(AstPass::to_sql(out, &mut options, backend))
273 }
274
275 #[diesel_derives::__diesel_public_if(
282 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
283 )]
284 fn collect_binds<'b>(
285 &'b self,
286 out: &mut DB::BindCollector<'b>,
287 metadata_lookup: &mut DB::MetadataLookup,
288 backend: &'b DB,
289 ) -> QueryResult<()> {
290 self.walk_ast(AstPass::collect_binds(out, metadata_lookup, backend))
291 }
292
293 #[diesel_derives::__diesel_public_if(
309 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
310 )]
311 fn is_safe_to_cache_prepared(&self, backend: &DB) -> QueryResult<bool> {
312 let mut result = true;
313 self.walk_ast(AstPass::is_safe_to_cache_prepared(&mut result, backend))?;
314 Ok(result)
315 }
316
317 #[diesel_derives::__diesel_public_if(
319 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
320 )]
321 fn is_noop(&self, backend: &DB) -> QueryResult<bool> {
322 let mut result = true;
323 self.walk_ast(AstPass::is_noop(&mut result, backend))?;
324 Ok(result)
325 }
326
327 #[diesel_derives::__diesel_public_if(
329 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
330 )]
331 fn count_binds(&self, backend: &DB) -> QueryResult<usize> {
332 let mut count = 0;
333 self.walk_ast(AstPass::count_binds(&mut count, backend))?;
334 Ok(count)
335 }
336}
337
338impl<T: ?Sized, DB> QueryFragment<DB> for Box<T>
339where
340 DB: Backend,
341 T: QueryFragment<DB>,
342{
343 fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
344 QueryFragment::walk_ast(&**self, pass)
345 }
346}
347
348impl<T: ?Sized, DB> QueryFragment<DB> for alloc::rc::Rc<T>
349where
350 DB: Backend,
351 T: QueryFragment<DB>,
352{
353 fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
354 QueryFragment::walk_ast(&**self, pass)
355 }
356}
357
358impl<T: ?Sized, DB> QueryFragment<DB> for alloc::sync::Arc<T>
359where
360 DB: Backend,
361 T: QueryFragment<DB>,
362{
363 fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
364 QueryFragment::walk_ast(&**self, pass)
365 }
366}
367
368impl<T: ?Sized, DB> QueryFragment<DB> for &T
369where
370 DB: Backend,
371 T: QueryFragment<DB>,
372{
373 fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
374 QueryFragment::walk_ast(&**self, pass)
375 }
376}
377
378impl<DB: Backend> QueryFragment<DB> for () {
379 fn walk_ast<'b>(&'b self, _: AstPass<'_, 'b, DB>) -> QueryResult<()> {
380 Ok(())
381 }
382}
383
384impl<T, DB> QueryFragment<DB> for Option<T>
385where
386 DB: Backend,
387 T: QueryFragment<DB>,
388{
389 fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
390 match *self {
391 Some(ref c) => c.walk_ast(out),
392 None => Ok(()),
393 }
394 }
395}
396
397#[diagnostic::on_unimplemented(
401 note = "this usually means that `{Self}` is no valid SQL for `{DB}`"
402)]
403pub trait IntoBoxedClause<'a, DB> {
404 type BoxedClause;
406
407 fn into_boxed(self) -> Self::BoxedClause;
409}
410
411#[diagnostic::on_unimplemented(
415 note = "this usually means that `{Self}` is no valid SQL for `{DB}`"
416)]
417pub trait IntoBoxedCloneClause<'a, DB> {
418 type BoxedCloneClause;
420
421 fn into_boxed_clone(self) -> Self::BoxedCloneClause;
423}
424
425pub trait AsQuery {
432 type SqlType;
434
435 type Query: Query<SqlType = Self::SqlType>;
437
438 #[allow(clippy::wrong_self_convention)]
445 fn as_query(self) -> Self::Query;
446}
447
448impl<T: Query> AsQuery for T {
449 type SqlType = <T as Query>::SqlType;
450 type Query = T;
451
452 fn as_query(self) -> <T as AsQuery>::Query {
453 self
454 }
455}
456
457pub fn debug_query<DB, T>(query: &T) -> DebugQuery<'_, T, DB> {
509 DebugQuery::new(query)
510}
511
512mod private {
513 #[allow(missing_debug_implementations, missing_copy_implementations)]
514 pub struct NotSpecialized;
515}
516
517pub(crate) mod has_query;