1use crate::arguments::IntoArguments;
2use crate::column::ColumnIndex;
3use crate::database::{Database, HasArguments, HasStatement};
4use crate::error::Error;
5use crate::from_row::FromRow;
6use crate::query::Query;
7use crate::query_as::QueryAs;
8use crate::query_scalar::QueryScalar;
9use either::Either;
10
11pub trait Statement<'q>: Send + Sync {
20 type Database: Database;
21
22 fn to_owned(&self) -> <Self::Database as HasStatement<'static>>::Statement;
25
26 fn sql(&self) -> &str;
28
29 fn parameters(&self) -> Option<Either<&[<Self::Database as Database>::TypeInfo], usize>>;
34
35 fn columns(&self) -> &[<Self::Database as Database>::Column];
37
38 fn column<I>(&self, index: I) -> &<Self::Database as Database>::Column
48 where
49 I: ColumnIndex<Self>,
50 {
51 self.try_column(index).unwrap()
52 }
53
54 fn try_column<I>(&self, index: I) -> Result<&<Self::Database as Database>::Column, Error>
56 where
57 I: ColumnIndex<Self>,
58 {
59 Ok(&self.columns()[index.index(self)?])
60 }
61
62 fn query(&self) -> Query<'_, Self::Database, <Self::Database as HasArguments<'_>>::Arguments>;
63
64 fn query_with<'s, A>(&'s self, arguments: A) -> Query<'s, Self::Database, A>
65 where
66 A: IntoArguments<'s, Self::Database>;
67
68 fn query_as<O>(
69 &self,
70 ) -> QueryAs<'_, Self::Database, O, <Self::Database as HasArguments<'_>>::Arguments>
71 where
72 O: for<'r> FromRow<'r, <Self::Database as Database>::Row>;
73
74 fn query_as_with<'s, O, A>(&'s self, arguments: A) -> QueryAs<'s, Self::Database, O, A>
75 where
76 O: for<'r> FromRow<'r, <Self::Database as Database>::Row>,
77 A: IntoArguments<'s, Self::Database>;
78
79 fn query_scalar<O>(
80 &self,
81 ) -> QueryScalar<'_, Self::Database, O, <Self::Database as HasArguments<'_>>::Arguments>
82 where
83 (O,): for<'r> FromRow<'r, <Self::Database as Database>::Row>;
84
85 fn query_scalar_with<'s, O, A>(&'s self, arguments: A) -> QueryScalar<'s, Self::Database, O, A>
86 where
87 (O,): for<'r> FromRow<'r, <Self::Database as Database>::Row>,
88 A: IntoArguments<'s, Self::Database>;
89}
90
91macro_rules! impl_statement_query {
92 ($A:ty) => {
93 #[inline]
94 fn query(&self) -> crate::query::Query<'_, Self::Database, $A> {
95 crate::query::query_statement(self)
96 }
97
98 #[inline]
99 fn query_with<'s, A>(&'s self, arguments: A) -> crate::query::Query<'s, Self::Database, A>
100 where
101 A: crate::arguments::IntoArguments<'s, Self::Database>,
102 {
103 crate::query::query_statement_with(self, arguments)
104 }
105
106 #[inline]
107 fn query_as<O>(
108 &self,
109 ) -> crate::query_as::QueryAs<
110 '_,
111 Self::Database,
112 O,
113 <Self::Database as crate::database::HasArguments<'_>>::Arguments,
114 >
115 where
116 O: for<'r> crate::from_row::FromRow<
117 'r,
118 <Self::Database as crate::database::Database>::Row,
119 >,
120 {
121 crate::query_as::query_statement_as(self)
122 }
123
124 #[inline]
125 fn query_as_with<'s, O, A>(
126 &'s self,
127 arguments: A,
128 ) -> crate::query_as::QueryAs<'s, Self::Database, O, A>
129 where
130 O: for<'r> crate::from_row::FromRow<
131 'r,
132 <Self::Database as crate::database::Database>::Row,
133 >,
134 A: crate::arguments::IntoArguments<'s, Self::Database>,
135 {
136 crate::query_as::query_statement_as_with(self, arguments)
137 }
138
139 #[inline]
140 fn query_scalar<O>(
141 &self,
142 ) -> crate::query_scalar::QueryScalar<
143 '_,
144 Self::Database,
145 O,
146 <Self::Database as crate::database::HasArguments<'_>>::Arguments,
147 >
148 where
149 (O,): for<'r> crate::from_row::FromRow<
150 'r,
151 <Self::Database as crate::database::Database>::Row,
152 >,
153 {
154 crate::query_scalar::query_statement_scalar(self)
155 }
156
157 #[inline]
158 fn query_scalar_with<'s, O, A>(
159 &'s self,
160 arguments: A,
161 ) -> crate::query_scalar::QueryScalar<'s, Self::Database, O, A>
162 where
163 (O,): for<'r> crate::from_row::FromRow<
164 'r,
165 <Self::Database as crate::database::Database>::Row,
166 >,
167 A: crate::arguments::IntoArguments<'s, Self::Database>,
168 {
169 crate::query_scalar::query_statement_scalar_with(self, arguments)
170 }
171 };
172}