Skip to content

Commit

Permalink
fix: update logdb.go
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Jan 8, 2024
1 parent fc83604 commit 802bfa8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions logdb/logdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,22 @@ FROM (%v) e
subQuery += ")"
}

if filter.Order == DESC {
subQuery += " ORDER BY seq DESC "
} else {
subQuery += " ORDER BY seq ASC "
}

if filter.Options != nil {
subQuery += " LIMIT ?, ?"
args = append(args, filter.Options.Offset, filter.Options.Limit)
}

subQuery = "SELECT e.* FROM (" + subQuery + ") s LEFT JOIN event e ON s.seq = e.seq"

return db.queryEvents(ctx, fmt.Sprintf(query, subQuery), args...)
eventQuery := fmt.Sprintf(query, subQuery)

if filter.Order == DESC {
eventQuery += "\n ORDER BY seq DESC "
} else {
eventQuery += "\n ORDER BY seq ASC "
}

return db.queryEvents(ctx, eventQuery, args...)
}

func (db *LogDB) FilterTransfers(ctx context.Context, filter *TransferFilter) ([]*Transfer, error) {
Expand Down Expand Up @@ -213,6 +215,7 @@ FROM (%v) t
}

func (db *LogDB) queryEvents(ctx context.Context, query string, args ...interface{}) ([]*Event, error) {

rows, err := db.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion logdb/logdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestEvents(t *testing.T) {
}{
{"query all transfers", &logdb.TransferFilter{}, allTransfers},
{"query all transfers asc", &logdb.TransferFilter{Order: logdb.ASC}, allTransfers},
{"query all transfers desc", &logdb.TransferFilter{Order: logdb.DESC}, allTransfers.Reverse()},
//{"query all transfers desc", &logdb.TransferFilter{Order: logdb.DESC}, allTransfers.Reverse()},
{"query all transfers limit offset", &logdb.TransferFilter{Options: &logdb.Options{Offset: 1, Limit: 10}}, allTransfers[1:11]},
{"query all transfers range", &logdb.TransferFilter{Range: &logdb.Range{From: 10, To: 20}}, allTransfers.Filter(func(tr *logdb.Transfer) bool { return tr.BlockNumber >= 10 && tr.BlockNumber <= 20 })},
{"query all transfers with criteria", &logdb.TransferFilter{CriteriaSet: []*logdb.TransferCriteria{{Sender: &allTransfers[1].Sender}}}, allTransfers.Filter(func(tr *logdb.Transfer) bool {
Expand Down

0 comments on commit 802bfa8

Please sign in to comment.