-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_table.rs
515 lines (515 loc) · 18.7 KB
/
test_table.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Generated file
#[allow(unused_imports)]
use catalytic::query_transform::{
CountType, DeleteUnique, Insert, MultipleSelectQueryErrorTransform, QueryEntityVec,
QueryEntityVecResult, QueryResultUniqueRow, QueryResultUniqueRowExpect, Qv, ScyllaQueryResult,
SelectMultiple, SelectUnique, SelectUniqueExpect, SingleSelectQueryErrorTransform, Truncate,
TtlType, Update,
};
use catalytic::scylla;
use scylla::frame::value::LegacySerializedValues;
#[allow(unused_imports)]
use scylla::frame::value::SerializeValuesError;
use scylla::transport::errors::QueryError;
use scylla::transport::iterator::TypedRowIterator;
use scylla::CachingSession;
#[doc = r" The query to select all rows in the table"]
pub const SELECT_ALL_QUERY: &str = "select b, c, d, a, e from test_table";
#[doc = r" The query to count all rows in the table"]
pub const SELECT_ALL_COUNT_QUERY: &str = "select count(*) from test_table";
#[doc = r" The query to insert a unique row in the table"]
pub const INSERT_QUERY: &str = "insert into test_table(b, c, d, a, e) values (?, ?, ?, ?, ?)";
#[doc = r" The query to insert a unique row in the table with a TTL"]
pub const INSERT_TTL_QUERY: &str =
"insert into test_table(b, c, d, a, e) values (?, ?, ?, ?, ?) using ttl ?";
#[doc = r" The query truncate the whole table"]
pub const TRUNCATE_QUERY: &str = "truncate test_table";
#[doc = r" The query to retrieve a unique row in this table"]
pub const SELECT_UNIQUE_QUERY: &str =
"select b, c, d, a, e from test_table where b = ? and c = ? and d = ? and a = ?";
#[doc = "The query to update column e"]
pub const UPDATE_E_QUERY: &str =
"update test_table set e = ? where b = ? and c = ? and d = ? and a = ?";
#[doc = r" The query to delete a unique row in the table"]
pub const DELETE_QUERY: &str = "delete from test_table where b = ? and c = ? and d = ? and a = ?";
#[doc = r" This is the struct which is generated from the table"]
#[doc = r" If you want to perform CRUD operations, do the following:"]
#[doc = r" Create -> convert this struct to a borrowed struct"]
#[doc = r" Read, Update, Delete -> convert this struct to a borrowed primary key struct"]
#[doc = r" When you converted this struct to the specified type, you will have methods available"]
#[doc = r" for the things you want"]
#[derive(
scylla :: FromRow, scylla :: SerializeRow, catalytic_macro :: Mirror, Debug, Clone, PartialEq,
)]
# [scylla (crate = scylla , flavor = "enforce_order" , skip_name_checks)]
pub struct TestTable {
#[partition_key]
pub b: i32,
#[partition_key]
pub c: i32,
#[clustering_key]
pub d: i32,
#[clustering_key]
pub a: i32,
pub e: i32,
}
impl TestTable {
#[doc = r" Create an borrowed primary key from the struct values"]
#[doc = r" You can use this primary key struct to perform updates, deletions and selects on"]
#[doc = r" a unique row"]
pub fn primary_key(&self) -> PrimaryKeyRef {
PrimaryKeyRef {
b: &self.b,
c: &self.c,
d: &self.d,
a: &self.a,
}
}
#[doc = r" Create an owned primary key from the struct values, it will actually clone the values if needed"]
pub fn primary_key_owned(&self) -> PrimaryKey {
self.primary_key().into_owned()
}
#[doc = r" Create an owned primary key from the struct values without cloning"]
pub fn into_primary_key_owned(self) -> PrimaryKey {
PrimaryKey {
b: self.b,
c: self.c,
d: self.d,
a: self.a,
}
}
}
#[doc = r" Returns a struct that can perform a query which counts the rows in this table"]
pub fn select_all_count_qv(
) -> SelectUniqueExpect<catalytic::query_transform::Count, &'static str, &'static [u8; 0]> {
SelectUniqueExpect::new(Qv {
query: SELECT_ALL_COUNT_QUERY,
values: &[],
})
}
#[doc = r" Performs the count query"]
pub async fn select_all_count(
session: &CachingSession,
) -> Result<QueryResultUniqueRowExpect<CountType>, SingleSelectQueryErrorTransform> {
select_all_count_qv().select_count(session).await
}
#[doc = r" Returns a struct that can perform a selection of all rows in the database"]
pub fn select_all_qv() -> SelectMultiple<TestTable, &'static str, &'static [u8; 0]> {
SelectMultiple::new(Qv {
query: SELECT_ALL_QUERY,
values: &[],
})
}
#[doc = r" Returns a struct that can perform a selection of all rows in the database"]
#[doc = r" with a specified page size"]
pub async fn select_all(
session: &CachingSession,
page_size: Option<i32>,
) -> Result<TypedRowIterator<TestTable>, QueryError> {
select_all_qv().select(session, page_size).await
}
#[doc = r" Returns a struct that can perform a selection of all rows in the database"]
#[doc = r" It will accumulate all rows in memory by sending paged queries"]
pub async fn select_all_in_memory(
session: &CachingSession,
page_size: i32,
) -> Result<QueryEntityVec<TestTable>, MultipleSelectQueryErrorTransform> {
select_all_qv()
.select_all_in_memory(session, page_size)
.await
}
#[doc = r" A struct that contains borrowed values"]
#[doc = r" This can be used to perform an insertion that is unique identified by the values of this struct"]
#[doc = r" If you want to perform an update, deletion or select or a unique row, convert this"]
#[doc = r" struct to the primary key struct"]
#[derive(Copy, Debug, Clone, PartialEq)]
pub struct TestTableRef<'a> {
pub b: &'a i32,
pub c: &'a i32,
pub d: &'a i32,
pub a: &'a i32,
pub e: &'a i32,
}
impl From<TestTableRef<'_>> for TestTable {
#[doc = r" Conversation method to go from a borrowed struct to an owned struct"]
fn from(f: TestTableRef<'_>) -> TestTable {
TestTable {
b: f.b.clone(),
c: f.c.clone(),
d: f.d.clone(),
a: f.a.clone(),
e: f.e.clone(),
}
}
}
impl TestTable {
#[doc = r" Conversation method to go from an owned struct to a borrowed struct"]
pub fn to_ref(&self) -> TestTableRef {
TestTableRef {
b: &self.b,
c: &self.c,
d: &self.d,
a: &self.a,
e: &self.e,
}
}
}
impl<'a> TestTableRef<'a> {
#[doc = r" Conversation method to go from a borrowed struct to an owned struct"]
pub fn primary_key(&self) -> PrimaryKeyRef {
PrimaryKeyRef {
b: self.b,
c: self.c,
d: self.d,
a: self.a,
}
}
}
#[doc = r" Returns a struct that can perform a truncate operation"]
pub fn truncate_qv() -> Truncate<&'static str, &'static [u8; 0]> {
Truncate::new(Qv {
query: TRUNCATE_QUERY,
values: &[],
})
}
#[doc = r" Performs a truncate"]
#[doc = r" !This will delete all rows in the table!"]
pub async fn truncate(session: &CachingSession) -> ScyllaQueryResult {
truncate_qv().truncate(session).await
}
impl<'a> TestTableRef<'a> {
#[doc = r" Returns a struct that can perform an insert operation"]
pub fn insert_qv(&self) -> Result<Insert, SerializeValuesError> {
let mut serialized = LegacySerializedValues::new();
serialized.add_value(&self.b)?;
serialized.add_value(&self.c)?;
serialized.add_value(&self.d)?;
serialized.add_value(&self.a)?;
serialized.add_value(&self.e)?;
Ok(Insert::new(Qv {
query: INSERT_QUERY,
values: serialized,
}))
}
#[doc = r" Performs an insert"]
pub async fn insert(&self, session: &CachingSession) -> ScyllaQueryResult {
tracing::debug!("Inserting: {:#?}", self);
self.insert_qv()?.insert(session).await
}
#[doc = r" Returns a struct that can perform an insert operation with a TTL"]
pub fn insert_ttl_qv(&self, ttl: TtlType) -> Result<Insert, SerializeValuesError> {
let mut serialized = LegacySerializedValues::new();
serialized.add_value(&self.b)?;
serialized.add_value(&self.c)?;
serialized.add_value(&self.d)?;
serialized.add_value(&self.a)?;
serialized.add_value(&self.e)?;
serialized.add_value(&ttl)?;
Ok(Insert::new(Qv {
query: INSERT_TTL_QUERY,
values: serialized,
}))
}
#[doc = r" Performs an insert with a TTL"]
pub async fn insert_ttl(&self, session: &CachingSession, ttl: TtlType) -> ScyllaQueryResult {
tracing::debug!("Insert with ttl {}, {:#?}", ttl, self);
self.insert_ttl_qv(ttl)?.insert(session).await
}
#[doc = r" Performs either an insertion or deletion, depending on the insert parameter"]
pub async fn insert_or_delete(
&self,
session: &CachingSession,
insert: bool,
) -> ScyllaQueryResult {
if insert {
self.insert(session).await
} else {
self.primary_key().delete(session).await
}
}
}
impl TestTable {
#[doc = r" Performs an update on the current struct based on the update parameter"]
pub fn in_memory_update(&mut self, update: UpdatableColumn) {
match update {
UpdatableColumn::E(val) => {
self.e = val;
}
}
}
#[doc = r" Performs multiple updates on the current struct"]
pub fn in_memory_updates(&mut self, updates: Vec<UpdatableColumn>) {
for updatable_column in updates {
self.in_memory_update(updatable_column)
}
}
}
#[doc = r" The owned primary key struct"]
#[doc = r" If you want to perform a read, delete or update, convert it to the borrowed type"]
#[derive(catalytic_macro :: PrimaryKey, Debug, Clone, PartialEq)]
pub struct PrimaryKey {
#[partition_key]
pub b: i32,
#[partition_key]
pub c: i32,
#[clustering_key]
pub d: i32,
#[clustering_key]
pub a: i32,
}
#[doc = r" The borrowed primary key struct"]
#[doc = r" This struct can be used to perform reads, deletes and updates"]
#[derive(catalytic_macro :: PrimaryKey, Copy, Debug, Clone, PartialEq)]
pub struct PrimaryKeyRef<'a> {
pub b: &'a i32,
pub c: &'a i32,
pub d: &'a i32,
pub a: &'a i32,
}
#[doc = r" Conversation method to go from a borrowed primary key to an owned primary key"]
impl PrimaryKeyRef<'_> {
pub fn into_owned(self) -> PrimaryKey {
self.into()
}
}
#[doc = r" Conversation method to go from an owned primary key to an borrowed primary key"]
impl PrimaryKey {
pub fn to_ref(&self) -> PrimaryKeyRef<'_> {
PrimaryKeyRef {
b: &self.b,
c: &self.c,
d: &self.d,
a: &self.a,
}
}
}
#[doc = r" Conversation method to go from a borrowed primary key to an owned primary key"]
impl From<PrimaryKeyRef<'_>> for PrimaryKey {
fn from(f: PrimaryKeyRef<'_>) -> PrimaryKey {
PrimaryKey {
b: f.b.clone(),
c: f.c.clone(),
d: f.d.clone(),
a: f.a.clone(),
}
}
}
impl PrimaryKeyRef<'_> {
#[doc = r" Returns a struct that can perform a unique row selection"]
pub fn select_unique_qv(&self) -> Result<SelectUnique<TestTable>, SerializeValuesError> {
let mut serialized_values = LegacySerializedValues::new();
serialized_values.add_value(&self.b)?;
serialized_values.add_value(&self.c)?;
serialized_values.add_value(&self.d)?;
serialized_values.add_value(&self.a)?;
Ok(SelectUnique::new(Qv {
query: SELECT_UNIQUE_QUERY,
values: serialized_values,
}))
}
#[doc = r" Performs the unique row selection"]
pub async fn select_unique(
&self,
session: &CachingSession,
) -> Result<QueryResultUniqueRow<TestTable>, SingleSelectQueryErrorTransform> {
tracing::debug!(
"Selecting unique row for table {} with values: {:#?}",
"test_table",
self
);
self.select_unique_qv()?.select(session).await
}
}
impl PrimaryKeyRef<'_> {
#[doc = r" Returns a struct that can perform a unique row selection"]
pub fn select_unique_expect_qv(
&self,
) -> Result<SelectUniqueExpect<TestTable>, SerializeValuesError> {
let mut serialized_values = LegacySerializedValues::new();
serialized_values.add_value(&self.b)?;
serialized_values.add_value(&self.c)?;
serialized_values.add_value(&self.d)?;
serialized_values.add_value(&self.a)?;
Ok(SelectUniqueExpect::new(Qv {
query: SELECT_UNIQUE_QUERY,
values: serialized_values,
}))
}
#[doc = r" Performs the unique row selection"]
pub async fn select_unique_expect(
&self,
session: &CachingSession,
) -> Result<QueryResultUniqueRowExpect<TestTable>, SingleSelectQueryErrorTransform> {
tracing::debug!(
"Selecting unique row for table {} with values: {:#?}",
"test_table",
self
);
self.select_unique_expect_qv()?.select(session).await
}
}
impl PrimaryKeyRef<'_> {
#[doc = "Returns a struct that can perform an update operation for column e"]
pub fn update_e_qv(&self, val: &i32) -> Result<Update, SerializeValuesError> {
let mut serialized_values = LegacySerializedValues::new();
serialized_values.add_value(&val)?;
serialized_values.add_value(&self.b)?;
serialized_values.add_value(&self.c)?;
serialized_values.add_value(&self.d)?;
serialized_values.add_value(&self.a)?;
Ok(Update::new(Qv {
query: UPDATE_E_QUERY,
values: serialized_values,
}))
}
#[doc = "Performs an update operation for column e"]
pub async fn update_e(&self, session: &CachingSession, val: &i32) -> ScyllaQueryResult {
tracing::debug!(
"Updating table {} with val {:#?} for row {:#?}",
"test_table",
val,
self
);
self.update_e_qv(val)?.update(session).await
}
}
impl PrimaryKeyRef<'_> {
#[doc = r" Returns a struct that can perform an update on a dynamic updatable column"]
pub fn update_dyn_qv(
&self,
val: UpdatableColumnRef<'_>,
) -> Result<Update, SerializeValuesError> {
match val {
UpdatableColumnRef::E(val) => self.update_e_qv(val),
}
}
#[doc = r" Performs the dynamic update"]
pub async fn update_dyn(
&self,
session: &CachingSession,
val: UpdatableColumnRef<'_>,
) -> ScyllaQueryResult {
self.update_dyn_qv(val)?.update(session).await
}
}
impl PrimaryKeyRef<'_> {
#[doc = r" Returns a struct that can perform a dynamic amount of column updates"]
pub fn update_dyn_multiple_qv(
&self,
val: &[UpdatableColumnRef<'_>],
) -> Result<Update<String, LegacySerializedValues>, SerializeValuesError> {
if val.is_empty() {
panic!("Empty update array")
}
let mut query = vec![];
let mut serialized_values = LegacySerializedValues::new();
for v in val {
match v {
UpdatableColumnRef::E(v) => {
query.push(concat!(stringify!(e), " = ?"));
serialized_values.add_value(v)?;
}
}
}
let columns_to_update: String = query.join(", ");
let update_statement = format!(
"update {} set {} {}",
"test_table", columns_to_update, "where b = ? and c = ? and d = ? and a = ?"
);
serialized_values.add_value(&self.b)?;
serialized_values.add_value(&self.c)?;
serialized_values.add_value(&self.d)?;
serialized_values.add_value(&self.a)?;
Ok(Update::new(Qv {
query: update_statement,
values: serialized_values,
}))
}
#[doc = r" Performs the dynamic column updates"]
pub async fn update_dyn_multiple(
&self,
session: &CachingSession,
val: &[UpdatableColumnRef<'_>],
) -> ScyllaQueryResult {
tracing::debug!(
"Updating table {} with vals {:#?} for row {:#?}",
"test_table",
val,
self
);
self.update_dyn_multiple_qv(val)?.update(session).await
}
}
impl PrimaryKeyRef<'_> {
#[doc = r" Returns a struct that can perform a single row deletion"]
pub fn delete_qv(&self) -> Result<DeleteUnique, SerializeValuesError> {
let mut serialized_values = LegacySerializedValues::new();
serialized_values.add_value(&self.b)?;
serialized_values.add_value(&self.c)?;
serialized_values.add_value(&self.d)?;
serialized_values.add_value(&self.a)?;
Ok(DeleteUnique::new(Qv {
query: DELETE_QUERY,
values: serialized_values,
}))
}
#[doc = r" Performs a single row deletion"]
pub async fn delete(&self, session: &CachingSession) -> ScyllaQueryResult {
tracing::debug!(
"Deleting a row from table {} with values {:#?}",
"test_table",
self
);
self.delete_qv()?.delete_unique(session).await
}
}
#[doc = r" This struct can be converted to a borrowed struct which can be used to update single rows"]
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq)]
pub enum UpdatableColumn {
E(i32),
}
impl UpdatableColumn {
#[doc = r" Conversation method to go from an owned updatable column struct to a borrowed updatable column struct"]
pub fn to_ref(&self) -> UpdatableColumnRef<'_> {
match &self {
UpdatableColumn::E(v) => UpdatableColumnRef::E(v),
}
}
}
#[doc = r" This struct can be used to update columns"]
#[doc = r" If you have a borrowed primary key and you want to update a column, you can pass in"]
#[doc = r" one of the variants"]
#[derive(Copy, Debug, Clone, PartialEq)]
pub enum UpdatableColumnRef<'a> {
E(&'a i32),
}
pub trait UpdatableColumnVec {
fn to_ref(&self) -> Vec<UpdatableColumnRef<'_>>;
}
impl UpdatableColumnVec for Vec<UpdatableColumn> {
#[doc = r" Conversation method to go from a vec of owned updatable column structs to a vec of borrowed updatable column structs"]
fn to_ref(&self) -> Vec<UpdatableColumnRef<'_>> {
self.iter().map(|v| v.to_ref()).collect()
}
}
impl From<UpdatableColumnRef<'_>> for UpdatableColumn {
#[doc = r" Conversation method to go from a borrowed updatable column struct to an owned updatable column struct"]
fn from(f: UpdatableColumnRef<'_>) -> UpdatableColumn {
match f {
UpdatableColumnRef::E(v) => UpdatableColumn::E(v.clone()),
}
}
}
impl UpdatableColumnRef<'_> {
#[doc = r" Conversation method to go from a borrowed updatable column struct to an owned updatable column struct"]
pub fn into_owned(self) -> UpdatableColumn {
self.into()
}
}
impl TestTable {
#[doc = "Creates the updatable column e which can be used to update it in the database"]
pub fn updatable_column_e(&self) -> UpdatableColumnRef {
UpdatableColumnRef::E(&self.e)
}
}