1#[macro_export]
21macro_rules! values {
22 ($($x:expr),*) => (
23 Values::from(std::iter::empty() $(.chain(std::iter::once(Row::from($x))))*)
24 );
25}
26
27#[macro_export]
50macro_rules! col {
51 ($e1:expr) => {
52 Expression::from(Column::from($e1))
53 };
54
55 ($e1:expr, $e2:expr) => {
56 Expression::from(Column::from(($e1, $e2)))
57 };
58}
59
60#[macro_export]
83macro_rules! val {
84 ($val:expr) => {
85 Expression::from($val)
86 };
87}
88
89macro_rules! value {
90 ($target:ident: $kind:ty,$paramkind:ident,$that:expr) => {
91 impl<'a> From<$kind> for crate::ast::Value<'a> {
92 fn from(that: $kind) -> Self {
93 let $target = that;
94 crate::ast::Value::$paramkind(Some($that))
95 }
96 }
97
98 impl<'a> From<Option<$kind>> for crate::ast::Value<'a> {
99 fn from(that: Option<$kind>) -> Self {
100 match that {
101 Some(val) => crate::ast::Value::from(val),
102 None => crate::ast::Value::$paramkind(None),
103 }
104 }
105 }
106 };
107}
108
109macro_rules! aliasable {
110 ($($kind:ty),*) => (
111 $(
112 impl<'a> Aliasable<'a> for $kind {
113 type Target = Table<'a>;
114
115 fn alias<T>(self, alias: T) -> Self::Target
116 where
117 T: Into<Cow<'a, str>>,
118 {
119 let table: Table = self.into();
120 table.alias(alias)
121 }
122 }
123 )*
124 );
125}
126
127macro_rules! function {
128 ($($kind:ident),*) => (
129 $(
130 impl<'a> From<$kind<'a>> for Function<'a> {
131 fn from(f: $kind<'a>) -> Self {
132 Function {
133 typ_: FunctionType::$kind(f),
134 alias: None,
135 }
136 }
137 }
138
139 impl<'a> From<$kind<'a>> for Expression<'a> {
140 fn from(f: $kind<'a>) -> Self {
141 Function::from(f).into()
142 }
143 }
144 )*
145 );
146}
147
148macro_rules! expression {
149 ($kind:ident,$paramkind:ident) => {
150 impl<'a> From<$kind<'a>> for Expression<'a> {
151 fn from(that: $kind<'a>) -> Self {
152 Expression {
153 kind: ExpressionKind::$paramkind(that),
154 alias: None,
155 }
156 }
157 }
158 };
159}
160
161#[cfg(test)]
163macro_rules! test_type {
164 ($name:ident($db:ident, $sql_type:literal, $(($input:expr, $output:expr)),+ $(,)?)) => {
165 paste::item! {
166 #[test]
167 fn [< test_type_ $name >] () -> crate::Result<()> {
168 use crate::ast::*;
169 use crate::connector::Queryable;
170 use crate::tests::test_api::TestApi;
171 use tokio::runtime::Builder;
172
173 let rt = Builder::new_multi_thread().enable_all().build().unwrap();
174
175 rt.block_on(async {
176 let mut setup = [< $db _test_api >]().await?;
177 let table = setup.create_type_table($sql_type).await?;
178
179 $(
180 let input = $input;
181 let output = $output;
182
183 let insert = Insert::single_into(&table).value("value", input);
184 setup.conn().insert(insert.into()).await?;
185
186 let select = Select::from_table(&table).column("value").order_by("id".descend());
187 let res = setup.conn().select(select).await?.into_single()?;
188
189 assert_eq!(Some(&output), res.at(0));
190 )+
191
192 Result::<(), crate::error::Error>::Ok(())
193 }).unwrap();
194
195 Ok(())
196 }
197 }
198 };
199
200 ($name:ident($db:ident, $sql_type:literal, $($value:expr),+ $(,)?)) => {
201 paste::item! {
202 #[test]
203 fn [< test_type_ $name >] () -> crate::Result<()> {
204 use crate::ast::*;
205 use crate::connector::Queryable;
206 use crate::tests::test_api::TestApi;
207 use tokio::runtime::Builder;
208
209 let rt = Builder::new_multi_thread().enable_all().build().unwrap();
210
211 rt.block_on(async {
212 let mut setup = [< $db _test_api >]().await?;
213 let table = setup.create_type_table($sql_type).await?;
214
215 $(
216 let value = $value;
217 let insert = Insert::single_into(&table).value("value", value.clone());
218 setup.conn().insert(insert.into()).await?;
219
220 let select = Select::from_table(&table).column("value").order_by("id".descend());
221 let res = setup.conn().select(select).await?.into_single()?;
222
223 assert_eq!(Some(&value), res.at(0));
224 )+
225
226 Result::<(), crate::error::Error>::Ok(())
227 }).unwrap();
228
229 Ok(())
230 }
231 }
232 };
233}