1#![allow(unused_imports)]
2use crate::silent_logs;
3use anyhow::Context;
4use rust_decimal::Decimal;
5use std::{slice, sync::LazyLock};
6use tank::{
7 AsValue, Entity, Executor, FixedDecimal, Result, expr,
8 stream::{StreamExt, TryStreamExt},
9};
10use time::{Date, Month, Time};
11use tokio::sync::Mutex;
12use uuid::Uuid;
13
14static MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
15
16#[derive(Entity, Debug, PartialEq, Clone)]
17#[tank(schema = "testing", name = "bound_types")]
18pub struct BoundTypes {
19 #[tank(primary_key)]
20 pub id: u8,
21 pub boolean: bool,
22 pub int8: i8,
23 pub uint8: u8,
24 pub int16: i16,
25 pub uint16: u16,
26 pub int32: i32,
27 pub uint32: u32,
28 pub int64: i64,
29 pub float32: f32,
30 pub float64: f64,
31 pub text: String,
32 pub uuid: Uuid,
33 pub date: Date,
34 pub time: Time,
35 pub decimal: FixedDecimal<20, 4>,
36}
37
38fn sample_1() -> BoundTypes {
39 BoundTypes {
40 id: 7,
41 boolean: true,
42 int8: -8,
43 uint8: 8,
44 int16: -16,
45 uint16: 16,
46 int32: -32,
47 uint32: 32,
48 int64: -64,
49 float32: 1.5,
50 float64: 2.5,
51 text: "bound text".into(),
52 uuid: Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap(),
53 date: Date::from_calendar_date(2025, Month::June, 15).unwrap(),
54 time: Time::from_hms(10, 30, 0).unwrap(),
55 decimal: Decimal::new(12356, 4).into(),
56 }
57}
58
59fn sample_2() -> BoundTypes {
60 BoundTypes {
61 id: 8,
62 boolean: false,
63 int8: 127,
64 uint8: 255,
65 int16: 32_767,
66 uint16: 65_535,
67 int32: 2_147_483_647,
68 uint32: 4_294_967_295,
69 int64: 9_223_372_036_854_775_807,
70 float32: -3.25,
71 float64: -6.5,
72 text: "other".into(),
73 uuid: Uuid::nil(),
74 date: Date::from_calendar_date(2000, Month::January, 1).unwrap(),
75 time: Time::from_hms(23, 59, 59).unwrap(),
76 decimal: Decimal::new(-1, 0).into(),
77 }
78}
79
80pub async fn bindings(executor: &mut impl Executor) -> Result<()> {
81 let _lock = MUTEX.lock().await;
82
83 silent_logs! {
85 BoundTypes::drop_table(executor, true, false).await?;
86 }
87 BoundTypes::create_table(executor, true, true).await?;
88
89 let first = sample_1();
90 let second = sample_2();
91 BoundTypes::insert_many(executor, [first.clone(), second.clone()]).await?;
92
93 let mut query = BoundTypes::prepare_find(
94 executor,
95 expr!(
96 BoundTypes::boolean == ?
97 && BoundTypes::int8 == ?
98 && BoundTypes::uint8 == ?
99 && BoundTypes::int16 == ?
100 && BoundTypes::uint16 == ?
101 && BoundTypes::int32 == ?
102 && BoundTypes::uint32 == ?
103 && BoundTypes::int64 == ?
104 && BoundTypes::float32 == ?
105 && BoundTypes::float64 == ?
106 && BoundTypes::text == ?
107 && BoundTypes::uuid == ?
108 && BoundTypes::date == ?
109 && BoundTypes::time == ?
110 && BoundTypes::decimal == ?
111 ),
112 Some(1),
113 )
114 .await?;
115 for (name, value) in [
116 ("first.boolean", first.boolean.as_value()),
117 ("first.int8", first.int8.as_value()),
118 ("first.uint8", first.uint8.as_value()),
119 ("first.int16", first.int16.as_value()),
120 ("first.uint16", first.uint16.as_value()),
121 ("first.int32", first.int32.as_value()),
122 ("first.uint32", first.uint32.as_value()),
123 ("first.int64", first.int64.as_value()),
124 ("first.float32", first.float32.as_value()),
125 ("first.float64", first.float64.as_value()),
126 ("first.text", first.text.clone().as_value()),
127 ("first.uuid", first.uuid.as_value()),
128 ("first.date", first.date.as_value()),
129 ("first.time", first.time.as_value()),
130 ("first.decimal", first.decimal.as_value()),
131 ] {
132 query
133 .bind(value.clone())
134 .context(format!("While binding to bind field {name}: {value:?}"))?;
135 }
136 let loaded = executor
137 .fetch(&mut query)
138 .map(|row| row.and_then(BoundTypes::from_row))
139 .try_collect::<Vec<_>>()
140 .await?;
141 assert_eq!(loaded, &[first], "Bound values did not round-trip");
142
143 query.clear_bindings()?;
144 for (name, value) in [
145 ("second.boolean", second.boolean.as_value()),
146 ("second.int8", second.int8.as_value()),
147 ("second.uint8", second.uint8.as_value()),
148 ("second.int16", second.int16.as_value()),
149 ("second.uint16", second.uint16.as_value()),
150 ("second.int32", second.int32.as_value()),
151 ("second.uint32", second.uint32.as_value()),
152 ("second.int64", second.int64.as_value()),
153 ("second.float32", second.float32.as_value()),
154 ("second.float64", second.float64.as_value()),
155 ("second.text", second.text.clone().as_value()),
156 ("second.uuid", second.uuid.as_value()),
157 ("second.date", second.date.as_value()),
158 ("second.time", second.time.as_value()),
159 ("second.decimal", second.decimal.as_value()),
160 ] {
161 query
162 .bind(value.clone())
163 .context(format!("While binding to bind field {name}: {value:?}"))?;
164 }
165 let loaded = executor
166 .fetch(&mut query)
167 .map(|row| row.and_then(BoundTypes::from_row))
168 .try_collect::<Vec<_>>()
169 .await?;
170 assert_eq!(
171 loaded,
172 &[second],
173 "Second time bounded values did not round-trip"
174 );
175
176 Ok(())
177}