mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2024-12-22 19:49:25 +00:00
refactor: Use ptr for QueryNodes.toSQL()
Since `SortBy` and `SortDirection` is modified directly from `QueryNodes` pointer, `sortBy` and `sortDirrection` return value from `QueryNodes.toSQL()` no longger needed
This commit is contained in:
parent
37798bd3fa
commit
5a6641a199
2 changed files with 27 additions and 29 deletions
|
@ -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 != "" {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -107,8 +107,7 @@ 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) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _, _, _ = QueryNodes{
|
||||
q := QueryNodes{
|
||||
Host: "test",
|
||||
Nettype: "any",
|
||||
Protocol: "any",
|
||||
|
@ -119,7 +118,9 @@ func Benchmark_QueryNodes_toSQL(b *testing.B) {
|
|||
Page: 1,
|
||||
SortBy: "last_checked",
|
||||
SortDirection: "desc",
|
||||
}.toSQL()
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = q.toSQL()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue