zephyr_sdk/
database.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
use crate::{
    env::EnvClient,
    external::{env_push_stack, read_as_id, read_raw, update_raw, write_raw},
    symbol, to_fixed, SdkError,
};
use rs_zephyr_common::ZephyrVal;
use serde::{Deserialize, Serialize};
use soroban_sdk::xdr::{Limits, WriteXdr};

#[derive(Clone, Deserialize, Serialize)]
pub struct TypeWrap(pub Vec<u8>);

impl TypeWrap {
    pub fn to_i128(&self) -> i128 {
        let bytes = to_fixed::<u8, 16>(self.0.clone());
        i128::from_be_bytes(bytes)
    }

    pub fn to_u64(&self) -> u64 {
        let bytes = to_fixed::<u8, 8>(self.0.clone());
        u64::from_be_bytes(bytes)
    }
}

/// Object returned by database reads.
/// It's a wrapper for table rows.
#[derive(Clone, Deserialize, Serialize)]
pub struct TableRows {
    /// Rows within the table
    pub rows: Vec<TableRow>,
}

/// Condition clauses that can be applied when reading the
/// database.
pub enum Condition {
    /// A given column is equal to a certain object.
    ColumnEqualTo(String, Vec<u8>),
    ColumnGt(String, Vec<u8>),
    ColumnLt(String, Vec<u8>),
}

/// Wraps a single row.
#[derive(Clone, Deserialize, Serialize)]
pub struct TableRow {
    /// Vector of wrapped columns.
    pub row: Vec<TypeWrap>,
}

mod unsafe_helpers {
    use crate::external::env_push_stack;

    pub(crate) unsafe fn push_head(table_name: i64, columns: Vec<i64>) {
        env_push_stack(table_name as i64);
        env_push_stack(columns.len() as i64);

        for col in columns {
            env_push_stack(col)
        }
    }

    pub(crate) unsafe fn push_data_segments(segments: Vec<(i64, i64)>) {
        env_push_stack(segments.len() as i64);

        for segment in segments {
            env_push_stack(segment.0);
            env_push_stack(segment.1);
        }
    }
}

#[derive(Clone, Default)]
pub struct Database {}

impl Database {
    pub fn read_table(
        table_name: &str,
        columns: &[&str],
        external_id: Option<i64>,
        conditions: Option<&[Condition]>,
    ) -> Result<TableRows, SdkError> {
        let table_name = symbol::Symbol::try_from_bytes(table_name.as_bytes()).unwrap();
        let cols = columns
            .into_iter()
            .map(|col| symbol::Symbol::try_from_bytes(col.as_bytes()).unwrap().0 as i64)
            .collect::<Vec<i64>>();

        unsafe { unsafe_helpers::push_head(table_name.0 as i64, cols) }

        if let Some(conditions) = conditions {
            unsafe {
                env_push_stack(conditions.len() as i64);

                let mut args = Vec::new();
                for cond in conditions {
                    let (colname, operator, value) = match cond {
                        Condition::ColumnEqualTo(colname, value) => (colname, 0, value),
                        Condition::ColumnGt(colname, value) => (colname, 1, value),
                        Condition::ColumnLt(colname, value) => (colname, 2, value),
                    };

                    env_push_stack(
                        symbol::Symbol::try_from_bytes(colname.as_bytes())
                            .unwrap()
                            .0 as i64,
                    );
                    env_push_stack(operator as i64);

                    args.push((value.as_ptr() as i64, value.len() as i64))
                }

                env_push_stack(args.len() as i64);

                for segment in args {
                    env_push_stack(segment.0);
                    env_push_stack(segment.1);
                }
            }
        };

        let (status, offset, size) = if let Some(external) = external_id {
            unsafe { read_as_id(external) }
        } else {
            unsafe { read_raw() }
        };
        SdkError::express_from_status(status)?;

        let table = {
            let memory: *const u8 = offset as *const u8;

            let slice = unsafe { core::slice::from_raw_parts(memory, size as usize) };

            if let Ok(table) = bincode::deserialize::<TableRows>(slice) {
                table
            } else {
                return Err(SdkError::Conversion);
            }
        };

        Ok(table)
    }

    pub fn write_table(
        table_name: &str,
        columns: &[&str],
        segments: &[&[u8]],
    ) -> Result<(), SdkError> {
        let table_name = symbol::Symbol::try_from_bytes(table_name.as_bytes()).unwrap();
        let cols = columns
            .into_iter()
            .map(|col| symbol::Symbol::try_from_bytes(col.as_bytes()).unwrap().0 as i64)
            .collect::<Vec<i64>>();

        let segments = segments
            .into_iter()
            .map(|segment| (segment.as_ptr() as i64, segment.len() as i64))
            .collect::<Vec<(i64, i64)>>();

        unsafe {
            unsafe_helpers::push_head(table_name.0 as i64, cols);
            unsafe_helpers::push_data_segments(segments);
        }

        let status = unsafe { write_raw() };
        SdkError::express_from_status(status)
    }

    pub fn update_table(
        table_name: &str,
        columns: &[&str],
        segments: &[&[u8]],
        conditions: &[Condition],
    ) -> Result<(), SdkError> {
        let table_name = symbol::Symbol::try_from_bytes(table_name.as_bytes()).unwrap();
        let cols = columns
            .into_iter()
            .map(|col| symbol::Symbol::try_from_bytes(col.as_bytes()).unwrap().0 as i64)
            .collect::<Vec<i64>>();

        let segments = segments
            .into_iter()
            .map(|segment| (segment.as_ptr() as i64, segment.len() as i64))
            .collect::<Vec<(i64, i64)>>();

        unsafe {
            unsafe_helpers::push_head(table_name.0 as i64, cols);
            unsafe_helpers::push_data_segments(segments);

            env_push_stack(conditions.len() as i64);

            let mut args = Vec::new();
            for cond in conditions {
                let (colname, operator, value) = match cond {
                    Condition::ColumnEqualTo(colname, value) => (colname, 0, value),
                    Condition::ColumnGt(colname, value) => (colname, 1, value),
                    Condition::ColumnLt(colname, value) => (colname, 2, value),
                };

                env_push_stack(
                    symbol::Symbol::try_from_bytes(colname.as_bytes())
                        .unwrap()
                        .0 as i64,
                );
                env_push_stack(operator as i64);

                args.push((value.as_ptr() as i64, value.len() as i64))
            }

            env_push_stack(args.len() as i64);

            for segment in args {
                env_push_stack(segment.0);
                env_push_stack(segment.1);
            }
        }

        let status = unsafe { update_raw() };
        SdkError::express_from_status(status)
    }
}

#[derive(PartialEq)]
pub(crate) enum Action {
    Read,
    Update,
}

/// Simple wrapper for building conditions.
pub struct TableQueryWrapper {
    conditions: Vec<Condition>,
    action: Action,
}

impl TableQueryWrapper {
    /// Creates a new table update object.
    pub(crate) fn new(action: Action) -> Self {
        Self {
            conditions: vec![],
            action,
        }
    }

    /// Adds a new condition in the update according to which a given column
    /// should be equal to an XDR object.
    pub fn column_equal_to_xdr(&mut self, column: impl ToString, xdr: &impl WriteXdr) -> &mut Self {
        let bytes = xdr.to_xdr(Limits::none()).unwrap();
        let condition = Condition::ColumnEqualTo(column.to_string(), bytes);
        self.conditions.push(condition);

        self
    }

    /// Adds a new condition in the update according to which a given column
    /// should be equal to the matching bytes array.
    ///
    /// This filter should be used when dealing with non-XDR types. Serialization
    /// must be carried by the implementor.
    pub fn column_equal_to_bytes(&mut self, column: impl ToString, bytes: &[u8]) -> &mut Self {
        let condition = Condition::ColumnEqualTo(column.to_string(), bytes.to_vec());
        self.conditions.push(condition);

        self
    }

    /// Adds a new condition in the update according to which a given column
    /// should be equal to the matching object.
    ///
    /// Under the hood, the object is converted to a ZephyrVal and is later
    /// serialized.
    pub fn column_equal_to<T: Serialize + TryInto<ZephyrVal>>(
        &mut self,
        column: impl ToString,
        argument: T,
    ) -> &mut Self {
        let argument = bincode::serialize(
            &TryInto::<ZephyrVal>::try_into(argument)
                .map_err(|_| ())
                .unwrap(),
        )
        .unwrap();
        let condition = Condition::ColumnEqualTo(column.to_string(), argument);
        self.conditions.push(condition);

        self
    }

    /// Adds a new condition in the update according to which a given column
    /// should be greater than an XDR object.
    pub fn column_gt_xdr(&mut self, column: impl ToString, xdr: &impl WriteXdr) -> &mut Self {
        let bytes = xdr.to_xdr(Limits::none()).unwrap();
        let condition = Condition::ColumnGt(column.to_string(), bytes);
        self.conditions.push(condition);

        self
    }

    /// Adds a new condition in the update according to which a given column
    /// should be greater than the matching bytes array.
    ///
    /// This filter should be used when dealing with non-XDR types. Serialization
    /// must be carried by the implementor.
    pub fn column_gt_bytes(&mut self, column: impl ToString, bytes: &[u8]) -> &mut Self {
        let condition = Condition::ColumnGt(column.to_string(), bytes.to_vec());
        self.conditions.push(condition);

        self
    }

    /// Adds a new condition in the update according to which a given column
    /// should be greater than the matching object.
    ///
    /// Under the hood, the object is converted to a ZephyrVal and is later
    /// serialized.
    pub fn column_gt<T: Serialize + TryInto<ZephyrVal>>(
        &mut self,
        column: impl ToString,
        argument: T,
    ) -> &mut Self {
        let argument = bincode::serialize(
            &TryInto::<ZephyrVal>::try_into(argument)
                .map_err(|_| ())
                .unwrap(),
        )
        .unwrap();
        let condition = Condition::ColumnGt(column.to_string(), argument);
        self.conditions.push(condition);

        self
    }

    /// Adds a new condition in the update according to which a given column
    /// should be less than an XDR object.
    pub fn column_lt_xdr(&mut self, column: impl ToString, xdr: &impl WriteXdr) -> &mut Self {
        let bytes = xdr.to_xdr(Limits::none()).unwrap();
        let condition = Condition::ColumnLt(column.to_string(), bytes);
        self.conditions.push(condition);

        self
    }

    /// Adds a new condition in the update according to which a given column
    /// should be less than the matching bytes array.
    ///
    /// This filter should be used when dealing with non-XDR types. Serialization
    /// must be carried by the implementor.
    pub fn column_lt_bytes(&mut self, column: impl ToString, bytes: &[u8]) -> &mut Self {
        let condition = Condition::ColumnLt(column.to_string(), bytes.to_vec());
        self.conditions.push(condition);

        self
    }

    /// Adds a new condition in the update according to which a given column
    /// should be less than the matching object.
    ///
    /// Under the hood, the object is converted to a ZephyrVal and is later
    /// serialized.
    pub fn column_lt<T: Serialize + TryInto<ZephyrVal>>(
        &mut self,
        column: impl ToString,
        argument: T,
    ) -> &mut Self {
        let argument = bincode::serialize(
            &TryInto::<ZephyrVal>::try_into(argument)
                .map_err(|_| ())
                .unwrap(),
        )
        .unwrap();
        let condition = Condition::ColumnLt(column.to_string(), argument);
        self.conditions.push(condition);

        self
    }

    /// Executes the update.
    /// Note: should only be used when updating a table.
    pub fn execute(&mut self, interact: &impl DatabaseInteract) -> Result<(), SdkError> {
        if self.action != Action::Update {
            return Err(SdkError::ReadOnUpdateAction);
        }

        Ok(interact.update(&EnvClient::empty(), &self.conditions))
    }

    /// Executes the query and returns the results.
    pub fn read<T: DatabaseInteract>(&self) -> Result<Vec<T>, SdkError> {
        let env = EnvClient::empty();

        if self.action != Action::Read {
            return Err(SdkError::UpdateOnReadAction);
        }

        Ok(T::read_to_rows(&env, Some(&self.conditions)))
    }
}

/// Trait that DatabaseDerive structures implement
pub trait DatabaseInteract {
    /// Reads from the database into a vector of `Self`.
    fn read_to_rows(env: &EnvClient, conditions: Option<&[Condition]>) -> Vec<Self>
    where
        Self: Sized;

    /// Inserts a row `Self` into the database table.
    fn put(&self, env: &EnvClient);

    /// Updates an existing row with `Self` into the database table
    /// using the provided conditions as update filter.
    fn update(&self, env: &EnvClient, conditions: &[Condition]);
}