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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
use fuel_indexer_types::scalar::{BlockHeight, Boolean, UID};
use sqlparser::ast as sql;

/// Represents a filter that returns a single results.
pub struct SingleFilter<T> {
    filter: String,
    phantom: std::marker::PhantomData<T>,
}

impl<T> std::fmt::Display for SingleFilter<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} LIMIT 1", self.filter)?;
        Ok(())
    }
}
/// Represents a filter with a an optional LIMIT clause that returns many
/// results.
pub struct ManyFilter<T> {
    filter: String,
    limit: Option<usize>,
    phantom: std::marker::PhantomData<T>,
}

impl<T> ManyFilter<T> {
    pub fn limit(&self) -> Option<usize> {
        self.limit
    }
}

impl<T> std::fmt::Display for ManyFilter<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.filter)?;
        if let Some(limit) = self.limit {
            write!(f, " LIMIT {limit}")?;
        }
        Ok(())
    }
}

/// Represents `filter` and `order_by` parts of the `SELECT object from {table}
/// WHERE {filter} {order_by}` statement that is assembled by the indexer to
/// fetch an object from the database. The table name is not available to the
/// plugin and thus only a part of the statment is generated there. The indexer
/// maps the TYPE_ID to the tale name and assemles the full statemnt.
pub struct OrderedFilter<T> {
    filter: Filter<T>,
    order_by: Vec<sql::OrderByExpr>,
}

impl<T> OrderedFilter<T> {
    pub fn limit(self, limit: usize) -> ManyFilter<T> {
        ManyFilter {
            filter: self.to_string(),
            limit: Some(limit),
            phantom: std::marker::PhantomData,
        }
    }
}

/// Convert `OrderedFilter` to `String`. `SELECT * from table_name` is later
/// added by the Fuel indexer to generate the entire query.
impl<T> std::fmt::Display for OrderedFilter<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.filter)?;
        if !self.order_by.is_empty() {
            let order = self
                .order_by
                .iter()
                .map(|x| x.to_string())
                .collect::<Vec<String>>()
                .join(", ");
            write!(f, " ORDER BY {}", order)?;
        }
        Ok(())
    }
}

// Conversions between different filter structs.

/// For the use of Filter as an argument to find()
impl<T> From<Filter<T>> for SingleFilter<T> {
    fn from(filter: Filter<T>) -> SingleFilter<T> {
        SingleFilter {
            filter: filter.to_string(),
            phantom: std::marker::PhantomData,
        }
    }
}

/// For the use of OrderedFilter as an argument to find()
impl<T> From<OrderedFilter<T>> for SingleFilter<T> {
    fn from(filter: OrderedFilter<T>) -> SingleFilter<T> {
        SingleFilter {
            filter: filter.to_string(),
            phantom: std::marker::PhantomData,
        }
    }
}

/// For the use of Filter as an argument to find_many()
impl<T> From<Filter<T>> for ManyFilter<T> {
    fn from(filter: Filter<T>) -> ManyFilter<T> {
        ManyFilter {
            filter: filter.to_string(),
            limit: None,
            phantom: std::marker::PhantomData,
        }
    }
}

/// For the use of OrderedFilter as an argument to find_many()
impl<T> From<OrderedFilter<T>> for ManyFilter<T> {
    fn from(filter: OrderedFilter<T>) -> ManyFilter<T> {
        ManyFilter {
            filter: filter.to_string(),
            limit: None,
            phantom: std::marker::PhantomData,
        }
    }
}

/// For implementing find() in terms of find_many()
impl<T> From<SingleFilter<T>> for ManyFilter<T> {
    fn from(filter: SingleFilter<T>) -> ManyFilter<T> {
        ManyFilter {
            filter: filter.filter,
            limit: Some(1),
            phantom: std::marker::PhantomData,
        }
    }
}

/// Represents a WHERE clause of the SQL statement. Multiple `Filter`s can be
/// joined with `and` and `or` and also ordered, at which point they become
/// `OrderedFilter`s.
pub struct Filter<T> {
    filter: sql::Expr,
    phantom: std::marker::PhantomData<T>,
}

impl<T> std::fmt::Display for Filter<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.filter)
    }
}

impl<T> Filter<T> {
    fn new(filter: sql::Expr) -> Self {
        Self {
            filter,
            phantom: std::marker::PhantomData,
        }
    }

    pub fn and(self, right: Filter<T>) -> Filter<T> {
        let filter = sql::Expr::BinaryOp {
            left: Box::new(self.filter),
            op: sql::BinaryOperator::And,
            right: Box::new(right.filter),
        };
        Filter {
            filter,
            phantom: std::marker::PhantomData,
        }
    }

    pub fn or(self, right: Filter<T>) -> Filter<T> {
        let filter = sql::Expr::BinaryOp {
            left: Box::new(self.filter),
            op: sql::BinaryOperator::Or,
            right: Box::new(right.filter),
        };
        Filter {
            filter,
            phantom: std::marker::PhantomData,
        }
    }

    pub fn order_by(self, f: impl Into<Order<T>>) -> OrderedFilter<T> {
        let fields: Order<T> = f.into();
        let mut order_by = vec![];
        for (f, asc) in fields.fields {
            let e = sql::OrderByExpr {
                expr: sql::Expr::Identifier(sql::Ident::new(f)),
                asc,
                nulls_first: None,
            };
            order_by.push(e);
        }
        OrderedFilter {
            filter: self,
            order_by,
        }
    }

    pub fn limit(self, limit: usize) -> ManyFilter<T> {
        ManyFilter {
            filter: self.to_string(),
            limit: Some(limit),
            phantom: std::marker::PhantomData,
        }
    }
}

pub struct Order<T> {
    fields: Vec<(String, Option<bool>)>,
    phantom: std::marker::PhantomData<T>,
}

impl<T> Order<T> {
    /// Combine multiple Order<T> into a single value
    fn combine(orders: Vec<Order<T>>) -> Order<T> {
        let mut result = Order {
            fields: vec![],
            phantom: std::marker::PhantomData,
        };
        for o in orders {
            result.fields.extend(o.fields)
        }
        result
    }
}

impl<T, F> From<Field<T, F>> for Order<T> {
    fn from(f1: Field<T, F>) -> Order<T> {
        Order {
            fields: vec![(f1.field, None)],
            phantom: std::marker::PhantomData,
        }
    }
}

impl<T, F1, F2> From<(F1, F2)> for Order<T>
where
    F1: Into<Order<T>>,
    F2: Into<Order<T>>,
{
    fn from((f1, f2): (F1, F2)) -> Order<T> {
        Order::combine(vec![f1.into(), f2.into()])
    }
}

impl<T, F1, F2, F3> From<(F1, F2, F3)> for Order<T>
where
    F1: Into<Order<T>>,
    F2: Into<Order<T>>,
    F3: Into<Order<T>>,
{
    fn from((f1, f2, f3): (F1, F2, F3)) -> Order<T> {
        Order::combine(vec![f1.into(), f2.into(), f3.into()])
    }
}

impl<T, F1, F2, F3, F4> From<(F1, F2, F3, F4)> for Order<T>
where
    F1: Into<Order<T>>,
    F2: Into<Order<T>>,
    F3: Into<Order<T>>,
    F4: Into<Order<T>>,
{
    fn from((f1, f2, f3, f4): (F1, F2, F3, F4)) -> Order<T> {
        Order::combine(vec![f1.into(), f2.into(), f3.into(), f4.into()])
    }
}

impl<T, F1, F2, F3, F4, F5> From<(F1, F2, F3, F4, F5)> for Order<T>
where
    F1: Into<Order<T>>,
    F2: Into<Order<T>>,
    F3: Into<Order<T>>,
    F4: Into<Order<T>>,
    F5: Into<Order<T>>,
{
    fn from((f1, f2, f3, f4, f5): (F1, F2, F3, F4, F5)) -> Order<T> {
        Order::combine(vec![f1.into(), f2.into(), f3.into(), f4.into(), f5.into()])
    }
}

/// A trait used to convert a value of scalar type into `sqlparser::ast::Value`.
/// That is, for injecting a value into the `sqlparser`'s representation which
/// we then use to generate a `OrderedFilter`.
pub trait ToSQLValue
where
    Self: Sized,
{
    fn to_sql_value(self) -> sql::Value;
}

impl ToSQLValue for String {
    fn to_sql_value(self) -> sql::Value {
        sql::Value::SingleQuotedString(self)
    }
}

impl ToSQLValue for Boolean {
    fn to_sql_value(self) -> sql::Value {
        sql::Value::Boolean(self)
    }
}

impl ToSQLValue for UID {
    fn to_sql_value(self) -> sql::Value {
        sql::Value::SingleQuotedString(self.to_string())
    }
}

impl ToSQLValue for BlockHeight {
    fn to_sql_value(self) -> sql::Value {
        sqlparser::test_utils::number(&self.as_usize().to_string())
    }
}

macro_rules! impl_bytes_to_sql_value {
    ($T:ident) => {
        impl ToSQLValue for fuel_indexer_types::scalar::$T {
            fn to_sql_value(self) -> sql::Value {
                sql::Value::SingleQuotedString(hex::encode(self))
            }
        }
    };
}

impl_bytes_to_sql_value!(B256);
impl_bytes_to_sql_value!(Bytes32);
impl_bytes_to_sql_value!(Bytes8);
impl_bytes_to_sql_value!(Bytes4);
impl_bytes_to_sql_value!(Bytes);
impl_bytes_to_sql_value!(AssetId);
impl_bytes_to_sql_value!(Address);
impl_bytes_to_sql_value!(ContractId);
impl_bytes_to_sql_value!(MessageId);
impl_bytes_to_sql_value!(Nonce);
impl_bytes_to_sql_value!(Salt);

macro_rules! impl_number_to_sql_value {
    ($T:ident) => {
        impl ToSQLValue for fuel_indexer_types::scalar::$T {
            fn to_sql_value(self) -> sql::Value {
                sqlparser::test_utils::number(&self.to_string())
            }
        }
    };
}

impl_number_to_sql_value!(I128);
impl_number_to_sql_value!(U128);

impl_number_to_sql_value!(I64);
impl_number_to_sql_value!(U64);

impl_number_to_sql_value!(I32);
impl_number_to_sql_value!(U32);

impl_number_to_sql_value!(I8);
impl_number_to_sql_value!(U8);

/// Captures the information necessary to represent `struct T { field: F }`.
/// It is then used to build a type-safe `Filter<T>`, e.g., `Filter<OrderId>`.
pub struct Field<T, F> {
    field: String,
    phantom: std::marker::PhantomData<(T, F)>,
}

impl<T, F> Field<T, F> {
    pub fn new(field: String) -> Self {
        Field {
            field,
            phantom: std::marker::PhantomData,
        }
    }

    pub fn asc(self) -> Order<T> {
        Order {
            fields: vec![(self.field, Some(true))],
            phantom: std::marker::PhantomData,
        }
    }

    pub fn desc(self) -> Order<T> {
        Order {
            fields: vec![(self.field, Some(false))],
            phantom: std::marker::PhantomData,
        }
    }
}

impl<T, F: ToSQLValue> Field<T, F> {
    pub fn eq(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::Eq, val)
    }

    pub fn ne(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::NotEq, val)
    }

    pub fn gt(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::Gt, val)
    }

    pub fn ge(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::GtEq, val)
    }

    pub fn lt(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::Lt, val)
    }

    pub fn le(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::LtEq, val)
    }

    fn filter(self, op: sql::BinaryOperator, val: F) -> Filter<T> {
        let expr = sql::Expr::BinaryOp {
            left: Box::new(sql::Expr::Identifier(sql::Ident::new(self.field.clone()))),
            op,
            right: Box::new(sql::Expr::Value(val.to_sql_value())),
        };
        Filter::new(expr)
    }
}

/// Captures the information necessary to represent `struct T { field: Option<F> }`
/// which requires additional logic for dealing with NULL values. Like `Field<T, F>`,
/// it is used to build a type-safe `Filter<T>`.
pub struct OptionField<T, F> {
    field: String,
    phantom: std::marker::PhantomData<(T, F)>,
}

impl<T, F> OptionField<T, F> {
    pub fn new(field: String) -> Self {
        OptionField {
            field,
            phantom: std::marker::PhantomData,
        }
    }
}

impl<T, F: ToSQLValue> OptionField<T, F> {
    pub fn eq(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::Eq, val)
    }

    pub fn ne(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::NotEq, val)
    }

    pub fn gt(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::Gt, val)
    }

    pub fn ge(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::GtEq, val)
    }

    pub fn lt(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::Lt, val)
    }

    pub fn le(self, val: F) -> Filter<T> {
        self.filter(sql::BinaryOperator::LtEq, val)
    }

    pub fn is_null(self) -> Filter<T> {
        Filter::new(sql::Expr::IsNull(Box::new(sql::Expr::Identifier(
            sql::Ident::new(self.field),
        ))))
    }

    pub fn is_not_null(self) -> Filter<T> {
        Filter::new(sql::Expr::IsNotNull(Box::new(sql::Expr::Identifier(
            sql::Ident::new(self.field),
        ))))
    }

    // Helper function that unwraps the Option converting None to NULL.
    fn filter(self, op: sql::BinaryOperator, val: F) -> Filter<T> {
        let expr = sql::Expr::BinaryOp {
            left: Box::new(sql::Expr::Identifier(sql::Ident::new(self.field))),
            op,
            right: Box::new(sql::Expr::Value(val.to_sql_value())),
        };
        Filter::new(expr)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_find_query_generation() {
        use fuel_indexer_types::scalar::{Address, BlockHeight, Bytes8, I32};

        struct MyStruct {}

        fn my_field() -> Field<MyStruct, I32> {
            Field::new("my_field".to_string())
        }

        fn my_address_field() -> Field<MyStruct, Address> {
            Field::new("my_address_field".to_string())
        }

        fn my_bytes8_field() -> Field<MyStruct, Bytes8> {
            Field::new("my_bytes8_field".to_string())
        }

        fn my_blockheight_field() -> Field<MyStruct, BlockHeight> {
            Field::new("my_blockheight_field".to_string())
        }

        let f: Filter<MyStruct> = my_field().gt(7);
        assert_eq!(&f.to_string(), "my_field > 7");

        let f: OrderedFilter<MyStruct> = my_field().gt(7).order_by(my_field().asc());
        assert_eq!(&f.to_string(), "my_field > 7 ORDER BY my_field ASC");

        // Ordering by multiple fields
        let o1: ManyFilter<MyStruct> = my_field()
            .gt(7)
            .order_by((my_field().asc(), my_bytes8_field()))
            .into();
        assert_eq!(
            &o1.to_string(),
            "my_field > 7 ORDER BY my_field ASC, my_bytes8_field"
        );

        let o2: ManyFilter<MyStruct> = my_field()
            .gt(7)
            .order_by((my_field().asc(), my_bytes8_field().desc()))
            .into();
        assert_eq!(
            &o2.to_string(),
            "my_field > 7 ORDER BY my_field ASC, my_bytes8_field DESC"
        );

        let o2: ManyFilter<MyStruct> = my_field()
            .gt(7)
            .order_by((
                my_field().asc(),
                my_bytes8_field().desc(),
                my_blockheight_field(),
            ))
            .into();
        assert_eq!(&o2.to_string(), "my_field > 7 ORDER BY my_field ASC, my_bytes8_field DESC, my_blockheight_field");

        // Converting to SingleFilter imposes a LIMIT 1
        let sf: SingleFilter<MyStruct> =
            my_field().gt(7).order_by(my_field().asc()).into();
        assert_eq!(
            &sf.to_string(),
            "my_field > 7 ORDER BY my_field ASC LIMIT 1"
        );

        // SingleFilter converted to ManyFilter retains the LIMIT 1
        let mf: ManyFilter<MyStruct> = sf.into();
        assert_eq!(
            &mf.to_string(),
            "my_field > 7 ORDER BY my_field ASC LIMIT 1"
        );

        // Converting to ManyFilter does not impose a LIMIT
        let mf: ManyFilter<MyStruct> =
            my_field().gt(7).order_by(my_field().desc()).into();
        assert_eq!(&mf.to_string(), "my_field > 7 ORDER BY my_field DESC");

        let addr: Filter<MyStruct> = my_address_field().eq(Address::zeroed());
        assert_eq!(&addr.to_string(), "my_address_field = '0000000000000000000000000000000000000000000000000000000000000000'");

        let addr: Filter<MyStruct> = my_address_field().eq(Address::from([238; 32]));
        assert_eq!(&addr.to_string(), "my_address_field = 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'");

        let bytes: Filter<MyStruct> = my_bytes8_field().eq(Bytes8::zeroed());
        assert_eq!(&bytes.to_string(), "my_bytes8_field = '0000000000000000'");

        let bytes: Filter<MyStruct> =
            my_bytes8_field().eq(Bytes8::from([1, 2, 3, 4, 5, 6, 7, 15]));
        assert_eq!(&bytes.to_string(), "my_bytes8_field = '010203040506070f'");

        let word: Filter<MyStruct> = my_blockheight_field().eq(BlockHeight::new(123));
        assert_eq!(&word.to_string(), "my_blockheight_field = 123");
    }
}