1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use super::ValuesClause;
use crate::backend::{sql_dialect, Backend, SqlDialect};
use crate::insertable::CanInsertInSingleQuery;
use crate::query_builder::{AstPass, QueryFragment, QueryId};
use crate::QueryResult;
use std::marker::PhantomData;
#[cfg_attr(
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes",
cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")
)]
#[derive(Debug)]
pub struct BatchInsert<V, Tab, QId, const STABLE_QUERY_ID: bool> {
pub values: V,
_marker: PhantomData<(QId, Tab)>,
}
impl<V, Tab, QId, const STABLE_QUERY_ID: bool> BatchInsert<V, Tab, QId, STABLE_QUERY_ID> {
pub(crate) fn new(values: V) -> Self {
Self {
values,
_marker: PhantomData,
}
}
}
impl<V, QId: 'static, Tab: 'static, const STABLE_QUERY_ID: bool> QueryId
for BatchInsert<V, Tab, QId, STABLE_QUERY_ID>
{
type QueryId = QId;
const HAS_STATIC_QUERY_ID: bool = STABLE_QUERY_ID;
}
impl<T, Table, QId, DB, const HAS_STATIC_QUERY_ID: bool> CanInsertInSingleQuery<DB>
for BatchInsert<T, Table, QId, HAS_STATIC_QUERY_ID>
where
T: CanInsertInSingleQuery<DB>,
DB: Backend+ SqlDialect<InsertWithDefaultKeyword = sql_dialect::default_keyword_for_insert::IsoSqlDefaultKeyword>
{
fn rows_to_insert(&self) -> Option<usize> {
self.values.rows_to_insert()
}
}
impl<T, DB, const N: usize> CanInsertInSingleQuery<DB> for [T; N]
where
DB: Backend+ SqlDialect<InsertWithDefaultKeyword = sql_dialect::default_keyword_for_insert::IsoSqlDefaultKeyword>
{
fn rows_to_insert(&self) -> Option<usize> {
Some(N)
}
}
impl<T, DB, const N: usize> CanInsertInSingleQuery<DB> for Box<[T; N]>
where
DB: Backend+ SqlDialect<InsertWithDefaultKeyword = sql_dialect::default_keyword_for_insert::IsoSqlDefaultKeyword>
{
fn rows_to_insert(&self) -> Option<usize> {
Some(N)
}
}
impl<T, DB> CanInsertInSingleQuery<DB> for [T]
where
DB: Backend+ SqlDialect<InsertWithDefaultKeyword = sql_dialect::default_keyword_for_insert::IsoSqlDefaultKeyword>
{
fn rows_to_insert(&self) -> Option<usize> {
Some(self.len())
}
}
impl<T, DB> CanInsertInSingleQuery<DB> for Vec<T>
where
DB: Backend+ SqlDialect<InsertWithDefaultKeyword = sql_dialect::default_keyword_for_insert::IsoSqlDefaultKeyword>,
{
fn rows_to_insert(&self) -> Option<usize> {
Some(self.len())
}
}
impl<Tab, DB, V, QId, const HAS_STATIC_QUERY_ID: bool> QueryFragment<DB>
for BatchInsert<V, Tab, QId, HAS_STATIC_QUERY_ID>
where
DB: Backend,
Self: QueryFragment<DB, DB::BatchInsertSupport>,
{
fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
<Self as QueryFragment<DB, DB::BatchInsertSupport>>::walk_ast(self, pass)
}
}
impl<Tab, DB, V, QId, const HAS_STATIC_QUERY_ID: bool>
QueryFragment<DB, sql_dialect::batch_insert_support::PostgresLikeBatchInsertSupport>
for BatchInsert<Vec<ValuesClause<V, Tab>>, Tab, QId, HAS_STATIC_QUERY_ID>
where
DB: Backend
+ SqlDialect<
BatchInsertSupport = sql_dialect::batch_insert_support::PostgresLikeBatchInsertSupport,
>,
DB::InsertWithDefaultKeyword: sql_dialect::default_keyword_for_insert::SupportsDefaultKeyword,
ValuesClause<V, Tab>: QueryFragment<DB>,
V: QueryFragment<DB>,
{
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
if !HAS_STATIC_QUERY_ID {
out.unsafe_to_cache_prepared();
}
let mut values = self.values.iter();
if let Some(value) = values.next() {
value.walk_ast(out.reborrow())?;
}
for value in values {
out.push_sql(", (");
value.values.walk_ast(out.reborrow())?;
out.push_sql(")");
}
Ok(())
}
}