/
write.rs
240 lines (211 loc) Β· 7.64 KB
/
write.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! Write query AST
use super::FilteredQuery;
use connector::{filter::Filter, DatasourceFieldName, RecordFilter, WriteArgs};
use prisma_models::prelude::*;
use std::{collections::HashMap, sync::Arc};
#[derive(Debug, Clone)]
pub enum WriteQuery {
CreateRecord(CreateRecord),
CreateManyRecords(CreateManyRecords),
UpdateRecord(UpdateRecord),
DeleteRecord(DeleteRecord),
UpdateManyRecords(UpdateManyRecords),
DeleteManyRecords(DeleteManyRecords),
ConnectRecords(ConnectRecords),
DisconnectRecords(DisconnectRecords),
ExecuteRaw(RawQuery),
QueryRaw(RawQuery),
}
impl WriteQuery {
/// Takes a SelectionResult and writes its contents into the write arguments of the underlying query.
pub fn inject_result_into_args(&mut self, result: SelectionResult) {
let model = self.model();
let args = match self {
Self::CreateRecord(ref mut x) => &mut x.args,
Self::UpdateRecord(x) => &mut x.args,
Self::UpdateManyRecords(x) => &mut x.args,
_ => return,
};
for (selected_field, value) in result {
args.insert(
DatasourceFieldName(selected_field.db_name().to_owned()),
(&selected_field, value),
)
}
args.update_datetimes(&model);
}
pub fn returns(&self, field_selection: &FieldSelection) -> bool {
let returns_id = &self.model().primary_identifier() == field_selection;
// Write operations only return IDs at the moment, so anything different
// from the primary ID is automatically not returned.
// DeleteMany, Connect and Disconnect do not return anything.
match self {
Self::CreateRecord(_) => returns_id,
Self::CreateManyRecords(_) => false,
Self::UpdateRecord(_) => returns_id,
Self::DeleteRecord(_) => returns_id,
Self::UpdateManyRecords(_) => returns_id,
Self::DeleteManyRecords(_) => false,
Self::ConnectRecords(_) => false,
Self::DisconnectRecords(_) => false,
Self::ExecuteRaw(_) => false,
Self::QueryRaw(_) => false,
}
}
pub fn model(&self) -> ModelRef {
match self {
Self::CreateRecord(q) => Arc::clone(&q.model),
Self::CreateManyRecords(q) => Arc::clone(&q.model),
Self::UpdateRecord(q) => Arc::clone(&q.model),
Self::DeleteRecord(q) => Arc::clone(&q.model),
Self::UpdateManyRecords(q) => Arc::clone(&q.model),
Self::DeleteManyRecords(q) => Arc::clone(&q.model),
Self::ConnectRecords(q) => q.relation_field.model(),
Self::DisconnectRecords(q) => q.relation_field.model(),
Self::ExecuteRaw(_) => unimplemented!(),
Self::QueryRaw(_) => unimplemented!(),
}
}
}
impl FilteredQuery for WriteQuery {
fn get_filter(&mut self) -> Option<&mut Filter> {
match self {
Self::UpdateRecord(q) => q.get_filter(),
Self::DeleteManyRecords(q) => q.get_filter(),
Self::DeleteRecord(q) => q.get_filter(),
Self::UpdateManyRecords(q) => q.get_filter(),
_ => unimplemented!(),
}
}
fn set_filter(&mut self, filter: Filter) {
match self {
Self::UpdateRecord(q) => q.set_filter(filter),
Self::DeleteManyRecords(q) => q.set_filter(filter),
Self::DeleteRecord(q) => q.set_filter(filter),
Self::UpdateManyRecords(q) => q.set_filter(filter),
_ => unimplemented!(),
}
}
}
impl std::fmt::Display for WriteQuery {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::CreateRecord(q) => write!(f, "CreateRecord(model: {}, args: {:?})", q.model.name, q.args),
Self::CreateManyRecords(q) => write!(f, "CreateManyRecord(model: {})", q.model.name),
Self::UpdateRecord(q) => write!(
f,
"UpdateRecord(model: {}, filter: {:?}, args: {:?})",
q.model.name, q.record_filter, q.args,
),
Self::DeleteRecord(q) => write!(f, "DeleteRecord: {}, {:?}", q.model.name, q.record_filter),
Self::UpdateManyRecords(q) => write!(f, "UpdateManyRecords(model: {}, args: {:?})", q.model.name, q.args),
Self::DeleteManyRecords(q) => write!(f, "DeleteManyRecords: {}", q.model.name),
Self::ConnectRecords(_) => write!(f, "ConnectRecords"),
Self::DisconnectRecords(_) => write!(f, "DisconnectRecords"),
Self::ExecuteRaw(r) => write!(f, "ExecuteRaw: {:?}", r.inputs),
Self::QueryRaw(r) => write!(f, "QueryRaw: {:?}", r.inputs),
}
}
}
#[derive(Debug, Clone)]
pub struct CreateRecord {
pub model: ModelRef,
pub args: WriteArgs,
}
#[derive(Debug, Clone)]
pub struct CreateManyRecords {
pub model: ModelRef,
pub args: Vec<WriteArgs>,
pub skip_duplicates: bool,
}
impl CreateManyRecords {
pub fn inject_result_into_all(&mut self, result: SelectionResult) {
for (selected_field, value) in result {
for args in self.args.iter_mut() {
args.insert(
DatasourceFieldName(selected_field.db_name().to_owned()),
(&selected_field, value.clone()),
)
}
}
}
}
#[derive(Debug, Clone)]
pub struct UpdateRecord {
pub model: ModelRef,
pub record_filter: RecordFilter,
pub args: WriteArgs,
}
#[derive(Debug, Clone)]
pub struct UpdateManyRecords {
pub model: ModelRef,
pub record_filter: RecordFilter,
pub args: WriteArgs,
}
#[derive(Debug, Clone)]
pub struct DeleteRecord {
pub model: ModelRef,
pub record_filter: Option<RecordFilter>,
}
#[derive(Debug, Clone)]
pub struct DeleteManyRecords {
pub model: ModelRef,
pub record_filter: RecordFilter,
}
#[derive(Debug, Clone)]
pub struct ConnectRecords {
pub parent_id: Option<SelectionResult>,
pub child_ids: Vec<SelectionResult>,
pub relation_field: RelationFieldRef,
}
#[derive(Debug, Clone)]
pub struct DisconnectRecords {
pub parent_id: Option<SelectionResult>,
pub child_ids: Vec<SelectionResult>,
pub relation_field: RelationFieldRef,
}
#[derive(Debug, Clone)]
pub struct RawQuery {
/// Model associated with the raw query, if one is necessary
pub model: Option<ModelRef>,
/// Map of query arguments and their values
pub inputs: HashMap<String, PrismaValue>,
/// Hint as to what kind of query is being executed
pub query_type: Option<String>,
}
impl FilteredQuery for UpdateRecord {
fn get_filter(&mut self) -> Option<&mut Filter> {
Some(&mut self.record_filter.filter)
}
fn set_filter(&mut self, filter: Filter) {
self.record_filter.filter = filter
}
}
impl FilteredQuery for UpdateManyRecords {
fn get_filter(&mut self) -> Option<&mut Filter> {
Some(&mut self.record_filter.filter)
}
fn set_filter(&mut self, filter: Filter) {
self.record_filter.filter = filter
}
}
impl FilteredQuery for DeleteManyRecords {
fn get_filter(&mut self) -> Option<&mut Filter> {
Some(&mut self.record_filter.filter)
}
fn set_filter(&mut self, filter: Filter) {
self.record_filter.filter = filter
}
}
impl FilteredQuery for DeleteRecord {
fn get_filter(&mut self) -> Option<&mut Filter> {
self.record_filter.as_mut().map(|f| &mut f.filter)
}
fn set_filter(&mut self, filter: Filter) {
match self.record_filter {
Some(ref mut rf) => rf.filter = filter,
None => self.record_filter = Some(filter.into()),
}
//.filter = Some(filter)
}
}