1#![allow(unused_imports)]
2use crate::silent_logs;
3use std::sync::{Arc, LazyLock};
4use tank::{AsValue, Entity, Executor, Result, Row, RowOwned, Sql, expr, stream::TryStreamExt};
5use tokio::sync::Mutex;
6
7static MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
8
9#[derive(Entity, Debug, Clone, PartialEq)]
10#[tank(name = "insert_source")]
11pub struct InsertSource {
12 #[tank(primary_key)]
13 pub id: i32,
14 pub name: String,
15 pub score: i32,
16}
17
18#[derive(Entity, Debug, Clone, PartialEq)]
19#[tank(name = "insert_target")]
20pub struct InsertTarget {
21 #[tank(primary_key)]
22 pub id: i32,
23 pub name: String,
24 pub score: i32,
25}
26
27async fn targets(executor: &mut impl Executor) -> Result<Vec<(i32, String, i32)>> {
28 let mut rows = InsertTarget::find_many(executor, true, None)
29 .map_ok(|t| (t.id, t.name, t.score))
30 .try_collect::<Vec<_>>()
31 .await?;
32 rows.sort();
33 Ok(rows)
34}
35
36pub async fn insert_builder(executor: &mut impl Executor) -> Result<()> {
37 let _lock = MUTEX.lock().await;
38
39 silent_logs! {
41 InsertSource::drop_table(executor, true, false).await?;
42 InsertTarget::drop_table(executor, true, false).await?;
43 }
44 InsertSource::create_table(executor, true, false).await?;
45 InsertTarget::create_table(executor, true, false).await?;
46 let sources = [
47 InsertSource {
48 id: 1,
49 name: "one".into(),
50 score: 5,
51 },
52 InsertSource {
53 id: 2,
54 name: "two".into(),
55 score: 15,
56 },
57 InsertSource {
58 id: 3,
59 name: "three".into(),
60 score: 25,
61 },
62 ];
63 InsertSource::insert_many(executor, &sources).await?;
64
65 executor
67 .execute(
68 Sql()
69 .insert_into(InsertTarget::table())
70 .values(&sources[..2])
71 .build(&executor.driver()),
72 )
73 .await?;
74 assert_eq!(
75 targets(executor).await?,
76 [(1, "one".into(), 5), (2, "two".into(), 15)]
77 );
78 executor
79 .execute(
80 Sql()
81 .insert_into(InsertTarget::table())
82 .values(&[InsertSource {
83 id: 2,
84 name: "TWO".into(),
85 score: 16,
86 }])
87 .update()
88 .build(&executor.driver()),
89 )
90 .await?;
91 assert_eq!(
92 targets(executor).await?,
93 [(1, "one".into(), 5), (2, "TWO".into(), 16)]
94 );
95
96 {
98 let labels: Arc<[String]> = ["id", "name", "score"].map(Into::into).into();
99 let row = |id: i32, name: &str, score: i32| {
100 RowOwned::new(
101 labels.clone(),
102 vec![id.as_value(), name.to_string().as_value(), score.as_value()],
103 )
104 };
105 executor
106 .execute(
107 Sql()
108 .insert_into(InsertTarget::table())
109 .columns(labels.iter().cloned())
110 .values(
111 [row(3, "three", 25), row(4, "four", 35)]
112 .iter()
113 .map(Row::values),
114 )
115 .build(&executor.driver()),
116 )
117 .await?;
118 executor
119 .execute(
120 Sql()
121 .insert_into(InsertTarget::table())
122 .columns(labels.iter().cloned())
123 .values([row(1, "ONE", 6)].iter().map(Row::values))
124 .update()
125 .build(&executor.driver()),
126 )
127 .await?;
128 assert_eq!(
129 targets(executor).await?,
130 [
131 (1, "ONE".into(), 6),
132 (2, "TWO".into(), 16),
133 (3, "three".into(), 25),
134 (4, "four".into(), 35),
135 ]
136 );
137 }
138
139 #[cfg(not(feature = "disable-insert-select"))]
141 {
142 silent_logs! {
143 InsertTarget::drop_table(executor, true, false).await?;
144 }
145 InsertTarget::create_table(executor, true, false).await?;
146 executor
147 .execute(
148 Sql()
149 .insert_into(InsertTarget::table())
150 .select(InsertSource::columns())
151 .from(InsertSource::table())
152 .where_expr(expr!(InsertSource::score > 10))
153 .build(&executor.driver()),
154 )
155 .await?;
156 assert_eq!(
157 targets(executor).await?,
158 [(2, "two".into(), 15), (3, "three".into(), 25)]
159 );
160 }
161 Ok(())
162}