good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
4
4byte
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
4byte
Commits
d8317b84
Verified
Commit
d8317b84
authored
Jun 29, 2023
by
a
Browse files
Options
Downloads
Patches
Plain Diff
done
parent
65d36197
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#22435
passed
Jun 29, 2023
Stage: build
Stage: test
Stage: deploy
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
common/store/stores/upper/store.go
+41
-57
41 additions, 57 deletions
common/store/stores/upper/store.go
with
41 additions
and
57 deletions
common/store/stores/upper/store.go
+
41
−
57
View file @
d8317b84
...
...
@@ -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
)
{
q
uery
:=
`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
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment