1#![allow(unused_imports)]
2use crate::silent_logs;
3use rust_decimal::{Decimal, prelude::FromPrimitive};
4use std::{
5 borrow::Cow,
6 cell::{Cell, RefCell},
7 i128,
8 pin::pin,
9 sync::{Arc, LazyLock},
10};
11use tank::{
12 Driver, DynQuery, Entity, Executor, FixedDecimal, Query, QueryBuilder, QueryResult, RawQuery,
13 RowsAffected, SqlWriter, expr, stream::TryStreamExt,
14};
15use time::{Date, Time, macros::date};
16use tokio::sync::Mutex;
17use uuid::Uuid;
18
19static MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
20
21#[derive(Entity, PartialEq, Debug)]
22pub struct SimpleFields {
23 #[tank(primary_key)]
24 alpha: u8,
25 bravo: Option<i32>,
26 charlie: Option<i16>,
27 delta: Option<u64>,
28 echo: Option<Uuid>,
29 #[cfg(not(feature = "disable-large-integers"))]
30 foxtrot: Option<i128>,
31 golf: Option<Time>,
32 hotel: Option<Cow<'static, str>>,
33 india: Box<Option<char>>,
34 juliet: Option<bool>,
35 kilo: Option<u32>,
36 lima: Arc<Option<f32>>,
37 mike: Option<Date>,
38 november: Option<Cell<FixedDecimal<4, 2>>>,
39 oscar: Option<RefCell<FixedDecimal<8, 3>>>,
40 papa: Option<FixedDecimal<20, 1>>,
41}
42
43pub async fn simple(executor: &mut impl Executor) {
44 let _lock = MUTEX.lock().await;
45
46 silent_logs! {
48 SimpleFields::drop_table(executor, true, false)
50 .await
51 .expect("Failed to drop SimpleNullFields table");
52 }
53 SimpleFields::create_table(executor, true, true)
54 .await
55 .expect("Failed to create SimpleNullFields table");
56
57 SimpleFields::delete_many(executor, expr!(SimpleFields::alpha == 1))
59 .await
60 .expect("Failed to clear the SimpleNullFields table");
61 let entity = SimpleFields {
62 alpha: 1,
63 bravo: 777.into(),
64 charlie: (-2).into(),
65 delta: 9876543210.into(),
66 echo: None,
67 #[cfg(not(feature = "disable-large-integers"))]
68 foxtrot: i128::MAX.into(),
69 golf: Time::from_hms(12, 0, 10).unwrap().into(),
70 hotel: Some("Hello world!".into()),
71 india: Box::new(None),
72 juliet: true.into(),
73 kilo: None,
74 lima: Arc::new(Some(3.14)),
75 mike: None,
76 november: None,
77 oscar: None,
78 papa: Some(Decimal::from_f32(45.2).unwrap().into()),
79 };
80 entity
81 .save(executor)
82 .await
83 .expect("Failed to save simple 1");
84 let entity = SimpleFields::find_one(executor, expr!(SimpleFields::alpha == 1))
85 .await
86 .expect("Failed to query simple 1")
87 .expect("Failed to find simple 1");
88 assert_eq!(entity.alpha, 1);
89 assert_eq!(entity.bravo, Some(777));
90 assert_eq!(entity.charlie, Some(-2));
91 assert_eq!(entity.delta, Some(9876543210));
92 assert_eq!(entity.echo, None);
93 #[cfg(not(feature = "disable-large-integers"))]
94 assert_eq!(
95 entity.foxtrot,
96 Some(170_141_183_460_469_231_731_687_303_715_884_105_727)
97 );
98 assert_eq!(entity.golf, Some(Time::from_hms(12, 0, 10).unwrap()));
99 assert_eq!(entity.hotel, Some("Hello world!".into()));
100 assert_eq!(*entity.india, None);
101 assert_eq!(entity.juliet, Some(true));
102 assert_eq!(entity.kilo, None);
103 assert_eq!(*entity.lima, Some(3.14));
104 assert_eq!(entity.mike, None);
105 assert_eq!(entity.november, None);
106 assert_eq!(entity.oscar, None);
107 assert_eq!(entity.papa, Some(Decimal::from_f32(45.2).unwrap().into()));
108
109 #[cfg(not(feature = "disable-multiple-statements"))]
111 {
112 let writer = executor.driver().sql_writer();
113 let mut query = DynQuery::default();
114 writer.write_delete::<SimpleFields>(&mut query, false); writer.write_select(
116 &mut query,
117 &QueryBuilder::new()
118 .select(SimpleFields::columns())
119 .from(SimpleFields::table())
120 .where_expr(true),
121 );
122 {
123 let mut stream = pin!(executor.run(query));
124 let result = stream
125 .try_next()
126 .await
127 .expect("Could not process the first query correctly");
128 let Some(QueryResult::Affected(RowsAffected { rows_affected, .. })) = result else {
129 panic!("Unexpected result type:\n{result:#?}");
130 };
131 if let Some(rows_affected) = rows_affected {
132 assert_eq!(rows_affected, 0);
133 }
134 let result = stream
135 .try_next()
136 .await
137 .expect("Could not process the second query correctly");
138 let Some(QueryResult::Row(row)) = result else {
139 panic!("Unexpected result type:\n{result:#?}");
140 };
141 let value =
142 SimpleFields::from_row(row).expect("Could not decode the row into SimpleFields");
143 assert_eq!(
144 value,
145 SimpleFields {
146 alpha: 1,
147 bravo: 777.into(),
148 charlie: (-2).into(),
149 delta: 9876543210.into(),
150 echo: None,
151 #[cfg(not(feature = "disable-large-integers"))]
152 foxtrot: i128::MAX.into(),
153 golf: Time::from_hms(12, 0, 10).unwrap().into(),
154 hotel: Some("Hello world!".into()),
155 india: Box::new(None),
156 juliet: true.into(),
157 kilo: None,
158 lima: Arc::new(Some(3.14)),
159 mike: None,
160 november: None,
161 oscar: None,
162 papa: Some(Decimal::from_f32(45.2).unwrap().into()),
163 }
164 );
165 }
166 }
167
168 SimpleFields::delete_many(executor, expr!(SimpleFields::alpha == 1))
170 .await
171 .expect("Failed to clear the SimpleNullFields table");
172 let entity = SimpleFields {
173 alpha: 255,
174 bravo: None,
175 charlie: None,
176 delta: None,
177 echo: Some(Uuid::parse_str("5e915574-bb30-4430-98cf-c5854f61fbbd").unwrap()),
178 #[cfg(not(feature = "disable-large-integers"))]
179 foxtrot: None,
180 golf: None,
181 hotel: None,
182 india: Box::new(None),
183 juliet: None,
184 kilo: 4294967295.into(),
185 lima: Arc::new(None),
186 mike: date!(2025 - 09 - 07).into(),
187 november: Cell::new(Decimal::from_f32(1.5).unwrap().into()).into(),
188 oscar: RefCell::new(Decimal::from_f32(5080.6244).unwrap().into()).into(),
189 papa: None,
190 };
191 entity
192 .save(executor)
193 .await
194 .expect("Failed to save simple 2");
195 let entity = SimpleFields::find_one(executor, expr!(SimpleFields::alpha == 255))
196 .await
197 .expect("Failed to query simple 2")
198 .expect("Failed to find simple 2");
199 assert_eq!(entity.alpha, 255);
200 assert_eq!(entity.bravo, None);
201 assert_eq!(entity.charlie, None);
202 assert_eq!(entity.delta, None);
203 assert_eq!(
204 entity.echo,
205 Some(Uuid::parse_str("5e915574-bb30-4430-98cf-c5854f61fbbd").unwrap())
206 );
207 #[cfg(not(feature = "disable-large-integers"))]
208 assert_eq!(entity.foxtrot, None);
209 assert_eq!(entity.golf, None);
210 assert_eq!(entity.hotel, None);
211 assert_eq!(*entity.india, None);
212 assert_eq!(entity.juliet, None);
213 assert_eq!(entity.kilo, Some(4294967295));
214 assert_eq!(*entity.lima, None);
215 assert_eq!(entity.mike, Some(date!(2025 - 09 - 07)));
216 assert_eq!(
217 entity.november,
218 Some(Cell::new(Decimal::from_f32(1.5).unwrap().into()))
219 );
220 assert_eq!(
221 entity.oscar,
222 Some(RefCell::new(Decimal::from_f32(5080.6244).unwrap().into()))
223 );
224 assert_eq!(entity.papa, None);
225
226 #[cfg(not(feature = "disable-multiple-statements"))]
228 {
229 let writer = executor.driver().sql_writer();
230 let mut query = DynQuery::default();
231 writer.write_delete::<SimpleFields>(&mut query, true);
232 writer.write_insert(&mut query, [&entity], false);
233 writer.write_select(
234 &mut query,
235 &QueryBuilder::new()
236 .select(SimpleFields::columns())
237 .from(SimpleFields::table())
238 .where_expr(true),
239 );
240 {
241 let mut stream = pin!(executor.run(query));
242 let Ok(Some(QueryResult::Affected(RowsAffected { rows_affected, .. }))) =
243 stream.try_next().await
244 else {
245 panic!("Could not process the first query correctly")
246 };
247 if let Some(rows_affected) = rows_affected {
248 assert_eq!(rows_affected, 1);
249 }
250 let Ok(Some(QueryResult::Affected(RowsAffected { rows_affected, .. }))) =
251 stream.try_next().await
252 else {
253 panic!("Could not process the first query correctly")
254 };
255 if let Some(rows_affected) = rows_affected {
256 assert_eq!(rows_affected, 1);
257 }
258 let Ok(Some(QueryResult::Row(row))) = stream.try_next().await else {
259 panic!("Could not process the second query correctly")
260 };
261 let value =
262 SimpleFields::from_row(row).expect("Could not decode the row into SimpleFields");
263 assert_eq!(
264 value,
265 SimpleFields {
266 alpha: 255,
267 bravo: None,
268 charlie: None,
269 delta: None,
270 echo: Some(Uuid::parse_str("5e915574-bb30-4430-98cf-c5854f61fbbd").unwrap()),
271 #[cfg(not(feature = "disable-large-integers"))]
272 foxtrot: None,
273 golf: None,
274 hotel: None,
275 india: Box::new(None),
276 juliet: None,
277 kilo: 4294967295.into(),
278 lima: Arc::new(None),
279 mike: date!(2025 - 09 - 07).into(),
280 november: Cell::new(Decimal::from_f32(1.5).unwrap().into()).into(),
281 oscar: RefCell::new(Decimal::from_f32(5080.6244).unwrap().into()).into(),
282 papa: None,
283 }
284 );
285 }
286 }
287}