1#![allow(dead_code)]
2#![allow(unused_imports)]
3use std::{borrow::Cow, pin::pin, sync::LazyLock};
4use tank::{Driver, Entity, Executor, Query, QueryResult, SqlWriter, cols, stream::TryStreamExt};
5use tokio::sync::Mutex;
6
7#[derive(Entity, Debug, PartialEq)]
8struct Arrays1 {
9 #[cfg(not(feature = "disable-intervals"))]
10 aa: [time::Duration; 3],
11 bb: Option<[[f32; 4]; 2]>,
12 cc: [[[[i8; 1]; 1]; 3]; 1],
13 dd: [[[[[i64; 3]; 1]; 1]; 2]; 1],
14}
15
16#[derive(Entity, Debug, PartialEq)]
17struct Arrays2 {
18 alpha: [i32; 5],
19 bravo: [[u8; 2]; 3],
20 charlie: [[[bool; 2]; 1]; 2],
21 delta: [[[[f64; 2]; 2]; 1]; 1],
22 echo: [[[Cow<'static, str>; 2]; 2]; 1],
23}
24static MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
25
26pub async fn arrays1<E: Executor>(executor: &mut E) {
27 let _ = MUTEX.lock().await;
28
29 Arrays1::drop_table(executor, true, false)
31 .await
32 .expect("Failed to drop Arrays1 table");
33 Arrays2::drop_table(executor, true, false)
34 .await
35 .expect("Failed to drop Arrays2 table");
36 Arrays1::create_table(executor, false, true)
37 .await
38 .expect("Failed to create Arrays1 table");
39 Arrays2::create_table(executor, false, true)
40 .await
41 .expect("Failed to create Arrays2 table");
42
43 Arrays1::insert_many(
44 executor,
45 &[
46 Arrays1 {
47 #[cfg(not(feature = "disable-intervals"))]
48 aa: [
49 time::Duration::seconds(1),
50 time::Duration::seconds(2),
51 time::Duration::seconds(3),
52 ],
53 bb: None,
54 cc: [[[[10]], [[20]], [[30]]]],
55 dd: [[[[[100, 200, 300]]], [[[400, 500, 600]]]]],
56 },
57 Arrays1 {
58 #[cfg(not(feature = "disable-intervals"))]
59 aa: [
60 time::Duration::milliseconds(150),
61 time::Duration::milliseconds(250),
62 time::Duration::milliseconds(350),
63 ],
64 bb: [[9.9, 8.8, 7.7, 6.6], [5.5, 4.4, 3.3, 2.2]].into(),
65 cc: [[[[1]], [[2]], [[3]]]],
66 dd: [[[[[7, 8, 9]]], [[[10, 11, 12]]]]],
67 },
68 ],
69 )
70 .await
71 .expect("Could not insert Arrays1 values");
72 {
73 let mut stream = pin!(Arrays1::find_many(executor, &true, None));
74 while let Some(value) = stream
75 .try_next()
76 .await
77 .expect("Failed to retrieve the value")
78 {
79 if value.bb.is_none() {
80 assert_eq!(
81 value,
82 Arrays1 {
83 #[cfg(not(feature = "disable-intervals"))]
84 aa: [
85 time::Duration::seconds(1),
86 time::Duration::seconds(2),
87 time::Duration::seconds(3),
88 ],
89 bb: None,
90 cc: [[[[10]], [[20]], [[30]]]],
91 dd: [[[[[100, 200, 300]]], [[[400, 500, 600]]]]],
92 }
93 );
94 } else {
95 assert_eq!(
96 value,
97 Arrays1 {
98 #[cfg(not(feature = "disable-intervals"))]
99 aa: [
100 time::Duration::milliseconds(150),
101 time::Duration::milliseconds(250),
102 time::Duration::milliseconds(350),
103 ],
104 bb: [[9.9, 8.8, 7.7, 6.6], [5.5, 4.4, 3.3, 2.2]].into(),
105 cc: [[[[1]], [[2]], [[3]]]],
106 dd: [[[[[7, 8, 9]]], [[[10, 11, 12]]]]],
107 }
108 );
109 }
110 }
111 }
112
113 #[cfg(not(feature = "disable-multiple-statements"))]
115 {
116 let mut query = String::new();
117 let writer = executor.driver().sql_writer();
118 writer.write_drop_table::<Arrays1>(&mut query, true);
119 writer.write_create_table::<Arrays1>(&mut query, true);
120 writer.write_drop_table::<Arrays2>(&mut query, true);
121 writer.write_create_table::<Arrays2>(&mut query, true);
122 let value = Arrays1 {
123 #[cfg(not(feature = "disable-intervals"))]
124 aa: [
125 time::Duration::seconds(1),
126 time::Duration::seconds(2),
127 time::Duration::seconds(3),
128 ],
129 bb: [
130 [1.1, -2.2, -3.3, 4.4],
131 [5.5, f32::INFINITY, f32::NEG_INFINITY, 8.8],
132 ]
133 .into(),
134 cc: [[[[1]], [[2]], [[3]]]],
135 dd: [[[[[10, 20, 30]]], [[[40, 50, 60]]]]],
136 };
137 writer.write_insert(&mut query, &[value], false);
138 writer.write_select(&mut query, cols!(*), Arrays1::table(), &true, None);
139 let value = Arrays2 {
140 alpha: [1, 2, 3, 4, 5],
141 bravo: [[10, 11], [12, 13], [14, 15]],
142 charlie: [[[true, false]], [[false, true]]],
143 delta: [[[[1.1, 1.2], [2.1, 2.2]]]],
144 echo: [[
145 [Cow::Borrowed("hello"), Cow::Owned("world".to_string())],
146 [Cow::Owned("foo".to_string()), Cow::Borrowed("bar")],
147 ]],
148 };
149 writer.write_insert(&mut query, &[value], false);
150 writer.write_select(
151 &mut query,
152 [Arrays2::alpha, Arrays2::bravo, Arrays2::charlie],
153 Arrays2::table(),
154 &true,
155 None,
156 );
157 writer.write_select(
158 &mut query,
159 [Arrays2::delta, Arrays2::echo],
160 Arrays2::table(),
161 &true,
162 None,
163 );
164 let rows = pin!(executor.run(query).try_filter_map(|v| async move {
165 Ok(match v {
166 QueryResult::Row(v) => Some(v),
167 QueryResult::Affected(..) => None,
168 })
169 }));
170 let rows = rows
171 .try_collect::<Vec<_>>()
172 .await
173 .expect("Could not collect the rows");
174
175 let value = Arrays1::from_row(rows[0].clone()).expect("First must be Arrays1");
176 assert_eq!(
177 value,
178 Arrays1 {
179 #[cfg(not(feature = "disable-intervals"))]
180 aa: [
181 time::Duration::seconds(1),
182 time::Duration::seconds(2),
183 time::Duration::seconds(3),
184 ],
185 bb: [
186 [1.1, -2.2, -3.3, 4.4],
187 [5.5, f32::INFINITY, f32::NEG_INFINITY, 8.8],
188 ]
189 .into(),
190 cc: [[[[1]], [[2]], [[3]]]],
191 dd: [[[[[10, 20, 30]]], [[[40, 50, 60]]]]],
192 },
193 );
194 }
195}