Skip to content

Commit

Permalink
refactor: Use ptr for QueryNodes.toSQL()
Browse files Browse the repository at this point in the history
Since `SortBy` and `SortDirection` is modified directly from `QueryNodes`
pointer, `sortBy` and `sortDirrection` return value from `QueryNodes.toSQL()`
no longger needed
  • Loading branch information
ditatompel committed Aug 5, 2024
1 parent 37798bd commit 5a6641a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
21 changes: 9 additions & 12 deletions internal/monero/monero.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type QueryNodes struct {
}

// toSQL generates SQL query from query parameters
func (q QueryNodes) toSQL() (args []interface{}, where, sortBy, sortDirection string) {
func (q *QueryNodes) toSQL() (args []interface{}, where string) {
wq := []string{}

if q.Host != "" {
Expand Down Expand Up @@ -128,17 +128,14 @@ func (q QueryNodes) toSQL() (args []interface{}, where, sortBy, sortDirection st
where = "WHERE " + strings.Join(wq, " AND ")
}

as := []string{"last_checked", "uptime"}
sortBy = "last_checked"
if slices.Contains(as, q.SortBy) {
sortBy = q.SortBy
if !slices.Contains([]string{"last_checked", "uptime"}, q.SortBy) {
q.SortBy = "last_checked"
}
sortDirection = "DESC"
if q.SortDirection == "asc" {
sortDirection = "ASC"
if q.SortDirection != "asc" {
q.SortDirection = "DESC"
}

return args, where, sortBy, sortDirection
return args, where
}

// Nodes represents a list of nodes
Expand All @@ -150,7 +147,7 @@ type Nodes struct {

// Get nodes from database
func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) {
args, where, sortBy, sortDirection := q.toSQL()
args, where := q.toSQL()

var nodes Nodes

Expand All @@ -174,12 +171,12 @@ func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) {
*
FROM
tbl_node
%s -- where query if any
%s
ORDER BY
%s
%s
LIMIT ?
OFFSET ?`, where, sortBy, sortDirection)
OFFSET ?`, where, q.SortBy, q.SortDirection)
err = r.db.Select(&nodes.Items, query, args...)

return nodes, err
Expand Down
35 changes: 18 additions & 17 deletions internal/monero/monero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ func TestQueryNodes_toSQL(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotArgs, gotWhere, gotSortBy, gotSortDirection := tt.query.toSQL()
gotArgs, gotWhere := tt.query.toSQL()
if !equalArgs(gotArgs, tt.wantArgs) {
t.Errorf("QueryNodes.toSQL() gotArgs = %v, want %v", gotArgs, tt.wantArgs)
}
if gotWhere != tt.wantWhere {
t.Errorf("QueryNodes.toSQL() gotWhere = %v, want %v", gotWhere, tt.wantWhere)
}
if gotSortBy != tt.wantSortBy {
t.Errorf("QueryNodes.toSQL() gotSortBy = %v, want %v", gotSortBy, tt.wantSortBy)
if tt.query.SortBy != tt.wantSortBy {
t.Errorf("QueryNodes.toSQL() gotSortBy = %v, want %v", tt.query.SortBy, tt.wantSortBy)
}
if gotSortDirection != tt.wantSortDirection {
t.Errorf("QueryNodes.toSQL() gotSortDirection = %v, want %v", gotSortDirection, tt.wantSortDirection)
if tt.query.SortDirection != tt.wantSortDirection {
t.Errorf("QueryNodes.toSQL() gotSortDirection = %v, want %v", tt.query.SortDirection, tt.wantSortDirection)
}
})
}
Expand All @@ -107,19 +107,20 @@ func TestQueryNodes_toSQL(t *testing.T) {
// Single bench test:
// go test ./internal/monero -bench QueryNodes_toSQL -benchmem -run=^$ -v
func Benchmark_QueryNodes_toSQL(b *testing.B) {
q := QueryNodes{
Host: "test",
Nettype: "any",
Protocol: "any",
CC: "any",
Status: -1,
CORS: -1,
RowsPerPage: 10,
Page: 1,
SortBy: "last_checked",
SortDirection: "desc",
}
for i := 0; i < b.N; i++ {
_, _, _, _ = QueryNodes{
Host: "test",
Nettype: "any",
Protocol: "any",
CC: "any",
Status: -1,
CORS: -1,
RowsPerPage: 10,
Page: 1,
SortBy: "last_checked",
SortDirection: "desc",
}.toSQL()
_, _ = q.toSQL()
}
}

Expand Down

0 comments on commit 5a6641a

Please sign in to comment.