good morning!!!!

Skip to content
Snippets Groups Projects
Verified Commit d8317b84 authored by a's avatar a
Browse files

done

parent 65d36197
Branches
No related tags found
No related merge requests found
Pipeline #22435 passed
......@@ -2,9 +2,7 @@ package upper
import (
"context"
"database/sql"
"errors"
"strconv"
"strings"
"github.com/upper/db/v4"
......@@ -91,84 +89,70 @@ func (T *Store) GetByID(ctx context.Context, id int) (*sig.Entry, error) {
}
func (T *Store) Search(ctx context.Context, page int, pageSize int, ordering store.Ordering, condition store.SearchCondition) (results []*sig.Entry, count int, err error) {
query := `SELECT *, count(*) OVER() AS "count" FROM "4byte"."entry"`
var queryArgs []any
q := T.db.SQL()
sel := q.Select("*", db.Raw(`count(*) OVER() AS "count"`)).From(`4byte.entry`)
// TODO(garet) these will be slow but they are easy, switch them in the future
const TEXT_SIGNATURE = "signature"
const HASH = "encode(hash, 'hex')"
const EVENT_HASH = "encode(event_hash, 'hex')"
// by default we do exact signature comparison
var left = "signature"
var op = "="
var right = "?"
var arg = condition.Needle
comparator := func() *db.RawExpr {
return db.Raw(left + " " + op + " " + right)
}
switch condition.Type {
case store.SearchConditionTextExact:
if condition.CaseSensitive {
query = query + " WHERE " + TEXT_SIGNATURE + " = ?"
queryArgs = append(queryArgs, condition.Needle)
break
}
query = query + " WHERE LOWER(" + TEXT_SIGNATURE + ") = LOWER(?)"
queryArgs = append(queryArgs, condition.Needle)
case store.SearchConditionTextContains:
if condition.CaseSensitive {
query = query + " WHERE " + TEXT_SIGNATURE + " LIKE '%' || ? || '%'"
queryArgs = append(queryArgs, condition.Needle)
break
}
query = query + " WHERE LOWER(" + TEXT_SIGNATURE + ") LIKE '%' || LOWER(?) || '%'"
queryArgs = append(queryArgs, condition.Needle)
op = "LIKE"
right = `'%' || ? || '%'`
case store.SearchConditionTextStartsWith:
if condition.CaseSensitive {
query = query + " WHERE " + TEXT_SIGNATURE + " LIKE ? || '%'"
queryArgs = append(queryArgs, condition.Needle)
break
}
query = query + " WHERE LOWER(" + TEXT_SIGNATURE + ") LIKE LOWER(?) || '%'"
queryArgs = append(queryArgs, condition.Needle)
op = "LIKE"
right = `? || '%'`
case store.SearchConditionTextEndsWith:
if condition.CaseSensitive {
query = query + " WHERE " + TEXT_SIGNATURE + " LIKE '%' || ?"
queryArgs = append(queryArgs, condition.Needle)
break
}
query = query + " WHERE LOWER(" + TEXT_SIGNATURE + ") LIKE '%' || LOWER(?)"
queryArgs = append(queryArgs, condition.Needle)
op = "LIKE"
right = `'%' || ?`
case store.SearchConditionHashContains:
query = query + " WHERE " + HASH + " LIKE '%' || LOWER(?) || '%'"
queryArgs = append(queryArgs, strings.TrimPrefix(condition.Needle, "0x"))
left = "encode(hash, 'hex')"
op = "LIKE"
right = `'%' || ? || '%'`
condition.CaseSensitive = false
case store.SearchConditionEventHashContains:
query = query + " WHERE " + EVENT_HASH + " LIKE '%' || LOWER(?) || '%'"
queryArgs = append(queryArgs, strings.TrimPrefix(condition.Needle, "0x"))
left = "encode(event_hash, 'hex')"
op = "LIKE"
right = `'%' || ? || '%'`
condition.CaseSensitive = false
}
if !condition.CaseSensitive {
left = "LOWER(" + left + ")"
right = strings.ReplaceAll(right, "?", "LOWER(?)")
}
sel = sel.Where(comparator(), arg)
switch ordering {
case store.OrderingCreatedAtAsc:
query = query + " ORDER BY id ASC"
sel = sel.OrderBy("id")
case store.OrderingTextSignatureAsc:
query = query + " ORDER BY signature ASC"
sel = sel.OrderBy("signature")
case store.OrderingTextSignatureDesc:
query = query + " ORDER BY signature DESC"
sel = sel.OrderBy("-signature")
case store.OrderingBytesSignatureAsc:
query = query + " ORDER BY hash ASC"
sel = sel.OrderBy("hash")
case store.OrderingBytesSignatureDesc:
query = query + " ORDER BY hash DESC"
sel = sel.OrderBy("-hash")
default:
// anything else is -created_at
query = query + " ORDER BY id DESC"
sel = sel.OrderBy("-id")
}
query = query + " LIMIT " + strconv.Itoa(pageSize)
query = query + " OFFSET " + strconv.Itoa((page-1)*pageSize)
var rows *sql.Rows
rows, err = T.db.SQL().QueryContext(ctx, query, queryArgs...)
if err != nil {
return
}
sel = sel.Limit(pageSize).Offset((page - 1) * pageSize)
var res []EntryWithCount
err = T.db.SQL().NewIterator(rows).All(&res)
err = sel.All(&res)
if err != nil {
return
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment