Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
add order_by
Browse files Browse the repository at this point in the history
  • Loading branch information
lostman committed Nov 8, 2023
1 parent e78dd0a commit 20b071a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
32 changes: 31 additions & 1 deletion packages/fuel-indexer-plugin/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,35 @@ pub trait Entity<'a>: Sized + PartialEq + Eq + std::fmt::Debug {
}
}

// Some(BinaryOp { left: BinaryOp { left: Identifier("a"), op: Gt, right: Identifier("b") }, op: And, right: BinaryOp { left: Identifier("b"), op: Lt, right: Value(Long(100))

pub struct Constraint<T> {
constraint: String,
order_by: Option<String>,
order_by_asc: Option<bool>,
phantom: std::marker::PhantomData<T>,
}

impl<T> std::fmt::Display for Constraint<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.constraint)
write!(f, "{}", self.constraint)?;
if let Some(ref field) = self.order_by {
let order = self
.order_by_asc
.map(|b| if b { " ASC" } else { " DESC" })
.unwrap_or("");
write!(f, " ORDER BY {field}{order}")?;
}
Ok(())
}
}

impl<T> Constraint<T> {
fn new(constraint: String) -> Self {
Self {
constraint,
order_by: None,
order_by_asc: None,
phantom: std::marker::PhantomData,
}
}
Expand All @@ -187,6 +201,8 @@ impl<T> Constraint<T> {
&self.constraint,
&c.constraint.to_string()
),
order_by: None,
order_by_asc: None,
phantom: std::marker::PhantomData,
}
}
Expand All @@ -198,9 +214,23 @@ impl<T> Constraint<T> {
&self.constraint,
&c.constraint.to_string()
),
order_by: None,
order_by_asc: None,
phantom: std::marker::PhantomData,
}
}

pub fn order_by_asc<F>(mut self, f: Field<T, F>) -> Constraint<T> {
self.order_by = Some(f.field);
self.order_by_asc = Some(true);
self
}

pub fn order_by_desc<F>(mut self, f: Field<T, F>) -> Constraint<T> {
self.order_by = Some(f.field);
self.order_by_asc = Some(false);
self
}
}

pub struct Field<T, F> {
Expand Down
18 changes: 18 additions & 0 deletions packages/fuel-indexer-tests/indexers/fuel-indexer-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,24 @@ mod fuel_indexer_test {
assert_eq!(i.block_height, 1);
FindEntity::new(i.block_height as u64).save();
} else if block_data.height == 5 {
// Test >= 2 order_by_asc. We have 4 indexed blocks, so the result should be 2.
let i = IndexMetadataEntity::find(
IndexMetadataEntity::block_height()
.ge(2)
.order_by_asc(IndexMetadataEntity::block_height()),
)
.unwrap();
assert_eq!(i.block_height, 1);

// Test >= 2 order_by_desc. We have 4 indexed blocks, so the result should be 4.
let i = IndexMetadataEntity::find(
IndexMetadataEntity::block_height()
.ge(2)
.order_by_desc(IndexMetadataEntity::block_height()),
)
.unwrap();
assert_eq!(i.block_height, 4);
} else if block_data.height == 6 {
// There is no such block. The lookup will fail.
let i =
IndexMetadataEntity::find(IndexMetadataEntity::block_height().eq(777))
Expand Down
1 change: 1 addition & 0 deletions packages/fuel-indexer-tests/tests/indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ async fn test_find() {

assert_eq!(values, vec![2, 1]);

mock_request("/find").await;
mock_request("/find").await;

node.abort();
Expand Down

0 comments on commit 20b071a

Please sign in to comment.