forked from prisma/quaint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.rs
212 lines (193 loc) · 5.79 KB
/
insert.rs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use crate::ast::*;
/// A builder for an `INSERT` statement.
#[derive(Clone, Debug, PartialEq)]
pub struct Insert<'a> {
pub(crate) table: Table<'a>,
pub(crate) columns: Vec<Column<'a>>,
pub(crate) values: Vec<Row<'a>>,
pub(crate) on_conflict: Option<OnConflict>,
pub(crate) returning: Option<Vec<Column<'a>>>,
}
/// A builder for an `INSERT` statement for a single row.
pub struct SingleRowInsert<'a> {
pub(crate) table: Table<'a>,
pub(crate) columns: Vec<Column<'a>>,
pub(crate) values: Row<'a>,
}
/// A builder for an `INSERT` statement for multiple rows.
pub struct MultiRowInsert<'a> {
pub(crate) table: Table<'a>,
pub(crate) columns: Vec<Column<'a>>,
pub(crate) values: Vec<Row<'a>>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
/// `INSERT` conflict resolution strategies.
pub enum OnConflict {
/// When a row already exists, do nothing.
///
/// ```rust
/// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
/// let query: Insert = Insert::single_into("users").into();
///
/// let (sql, _) = Sqlite::build(query.on_conflict(OnConflict::DoNothing));
///
/// assert_eq!("INSERT OR IGNORE INTO `users` DEFAULT VALUES", sql);
/// ```
DoNothing,
}
impl<'a> From<Insert<'a>> for Query<'a> {
fn from(insert: Insert<'a>) -> Self {
Query::Insert(Box::new(insert))
}
}
impl<'a> From<SingleRowInsert<'a>> for Insert<'a> {
fn from(insert: SingleRowInsert<'a>) -> Self {
let values = if insert.values.is_empty() {
Vec::new()
} else {
vec![insert.values]
};
Insert {
table: insert.table,
columns: insert.columns,
values,
on_conflict: None,
returning: None,
}
}
}
impl<'a> From<MultiRowInsert<'a>> for Insert<'a> {
fn from(insert: MultiRowInsert<'a>) -> Self {
Insert {
table: insert.table,
columns: insert.columns,
values: insert.values,
on_conflict: None,
returning: None,
}
}
}
impl<'a> From<SingleRowInsert<'a>> for Query<'a> {
fn from(insert: SingleRowInsert<'a>) -> Query<'a> {
Query::from(Insert::from(insert))
}
}
impl<'a> From<MultiRowInsert<'a>> for Query<'a> {
fn from(insert: MultiRowInsert<'a>) -> Query<'a> {
Query::from(Insert::from(insert))
}
}
impl<'a> Insert<'a> {
/// Creates a new single row `INSERT` statement for the given table.
///
/// ```rust
/// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
/// let query = Insert::single_into("users");
/// let (sql, _) = Sqlite::build(query);
///
/// assert_eq!("INSERT INTO `users` DEFAULT VALUES", sql);
/// ```
pub fn single_into<T>(table: T) -> SingleRowInsert<'a>
where
T: Into<Table<'a>>,
{
SingleRowInsert {
table: table.into(),
columns: Vec::new(),
values: Row::new(),
}
}
/// Creates a new multi row `INSERT` statement for the given table.
pub fn multi_into<T, K, I>(table: T, columns: I) -> MultiRowInsert<'a>
where
T: Into<Table<'a>>,
K: Into<Column<'a>>,
I: IntoIterator<Item = K>,
{
MultiRowInsert {
table: table.into(),
columns: columns.into_iter().map(|c| c.into()).collect(),
values: Vec::new(),
}
}
/// Sets the conflict resolution strategy.
pub fn on_conflict(mut self, on_conflict: OnConflict) -> Self {
self.on_conflict = Some(on_conflict);
self
}
/// Sets the returned columns.
///
/// ```rust
/// # use quaint::{ast::*, visitor::{Visitor, Postgres}};
/// let query = Insert::single_into("users");
/// let insert = Insert::from(query).returning(vec!["id"]);
/// let (sql, _) = Postgres::build(insert);
///
/// assert_eq!("INSERT INTO \"users\" DEFAULT VALUES RETURNING \"id\"", sql);
/// ```
#[cfg(feature = "postgresql")]
pub fn returning<K, I>(mut self, columns: I) -> Self
where
K: Into<Column<'a>>,
I: IntoIterator<Item = K>,
{
self.returning = Some(columns.into_iter().map(|k| k.into()).collect());
self
}
}
impl<'a> SingleRowInsert<'a> {
/// Adds a new value to the `INSERT` statement
///
/// ```rust
/// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
/// let query = Insert::single_into("users").value("foo", 10);
/// let (sql, params) = Sqlite::build(query);
///
/// assert_eq!("INSERT INTO `users` (`foo`) VALUES (?)", sql);
/// assert_eq!(vec![Value::Integer(10)], params);
/// ```
pub fn value<K, V>(mut self, key: K, val: V) -> SingleRowInsert<'a>
where
K: Into<Column<'a>>,
V: Into<Expression<'a>>,
{
self.columns.push(key.into());
self.values.push(val.into());
self
}
/// Convert into a common `Insert` statement.
pub fn build(self) -> Insert<'a> {
Insert::from(self)
}
}
impl<'a> MultiRowInsert<'a> {
/// Adds a new row to be inserted.
///
/// ```rust
/// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
/// let query = Insert::multi_into("users", vec!["foo"])
/// .values(vec![1])
/// .values(vec![2]);
///
/// let (sql, params) = Sqlite::build(query);
///
/// assert_eq!("INSERT INTO `users` (`foo`) VALUES (?), (?)", sql);
///
/// assert_eq!(
/// vec![
/// Value::Integer(1),
/// Value::Integer(2),
/// ], params);
/// ```
pub fn values<V>(mut self, values: V) -> Self
where
V: Into<Row<'a>>,
{
self.values.push(values.into());
self
}
/// Convert into a common `Insert` statement.
pub fn build(self) -> Insert<'a> {
Insert::from(self)
}
}