Debian Codegraph @

src/pg/query_builder/query_fragment_impls.rs

raw
1 use crate::expression::array_comparison::{In, InExpression, Many, NotIn};
2 use crate::pg::backend::PgStyleArrayComparison;
3 use crate::pg::types::sql_types::Array;
4 use crate::pg::Pg;
5 use crate::query_builder::locking_clause::{
6 ForKeyShare, ForNoKeyUpdate, ForShare, ForUpdate, NoModifier, NoWait, SkipLocked,
7 };
8 use crate::query_builder::upsert::into_conflict_clause::OnConflictSelectWrapper;
9 use crate::query_builder::upsert::on_conflict_target_decorations::DecoratedConflictTarget;
10 use crate::query_builder::{AstPass, QueryFragment};
11 use crate::result::QueryResult;
12 use crate::serialize::ToSql;
13 use crate::sql_types::{HasSqlType, SingleValue};
14
15 impl QueryFragment<Pg> for ForUpdate {
16 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
17 out.push_sql(" FOR UPDATE");
18 Ok(())
19 }
20 }
21
22 impl QueryFragment<Pg> for ForNoKeyUpdate {
23 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
24 out.push_sql(" FOR NO KEY UPDATE");
25 Ok(())
26 }
27 }
28
29 impl QueryFragment<Pg> for ForShare {
30 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
31 out.push_sql(" FOR SHARE");
32 Ok(())
33 }
34 }
35
36 impl QueryFragment<Pg> for ForKeyShare {
37 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
38 out.push_sql(" FOR KEY SHARE");
39 Ok(())
40 }
41 }
42
43 impl QueryFragment<Pg> for NoModifier {
44 fn walk_ast<'b>(&'b self, _out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
45 Ok(())
46 }
47 }
48
49 impl QueryFragment<Pg> for SkipLocked {
50 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
51 out.push_sql(" SKIP LOCKED");
52 Ok(())
53 }
54 }
55
56 impl QueryFragment<Pg> for NoWait {
57 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
58 out.push_sql(" NOWAIT");
59 Ok(())
60 }
61 }
62
63 impl<T, U> QueryFragment<Pg, PgStyleArrayComparison> for In<T, U>
64 where
65 T: QueryFragment<Pg>,
66 U: QueryFragment<Pg> + InExpression,
67 {
68 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
69 if self.values.is_array() {
70 self.walk_ansi_ast(out)?;
71 } else {
72 self.left.walk_ast(out.reborrow())?;
73 out.push_sql(" = ANY(");
74 self.values.walk_ast(out.reborrow())?;
75 out.push_sql(")");
76 }
77 Ok(())
78 }
79 }
80
81 impl<T, U> QueryFragment<Pg, PgStyleArrayComparison> for NotIn<T, U>
82 where
83 T: QueryFragment<Pg>,
84 U: QueryFragment<Pg> + InExpression,
85 {
86 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
87 if self.values.is_array() {
88 self.walk_ansi_ast(out)?;
89 } else {
90 self.left.walk_ast(out.reborrow())?;
91 out.push_sql(" != ALL(");
92 self.values.walk_ast(out.reborrow())?;
93 out.push_sql(")");
94 }
95 Ok(())
96 }
97 }
98
99 impl<ST, I> QueryFragment<Pg, PgStyleArrayComparison> for Many<ST, I>
100 where
101 ST: SingleValue,
102 Vec<I>: ToSql<Array<ST>, Pg>,
103 I: ToSql<ST, Pg>,
104 Pg: HasSqlType<ST>,
105 {
106 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
107 if ST::IS_ARRAY {
108 self.walk_ansi_ast(out)
109 } else {
110 out.push_bind_param::<Array<ST>, Vec<I>>(&self.values)
111 }
112 }
113 }
114
115 impl<T, U> QueryFragment<Pg, crate::pg::backend::PgOnConflictClause>
116 for DecoratedConflictTarget<T, U>
117 where
118 T: QueryFragment<Pg>,
119 U: QueryFragment<Pg>,
120 {
121 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
122 self.target.walk_ast(out.reborrow())?;
123 self.where_clause.walk_ast(out.reborrow())?;
124 Ok(())
125 }
126 }
127
128 impl<S> QueryFragment<crate::pg::Pg> for OnConflictSelectWrapper<S>
129 where
130 S: QueryFragment<crate::pg::Pg>,
131 {
132 fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, crate::pg::Pg>) -> QueryResult<()> {
133 self.0.walk_ast(out)
134 }
135 }