mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2024-12-23 03:59: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
|
// 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{}
|
wq := []string{}
|
||||||
|
|
||||||
if q.Host != "" {
|
if q.Host != "" {
|
||||||
|
@ -128,17 +128,14 @@ func (q QueryNodes) toSQL() (args []interface{}, where, sortBy, sortDirection st
|
||||||
where = "WHERE " + strings.Join(wq, " AND ")
|
where = "WHERE " + strings.Join(wq, " AND ")
|
||||||
}
|
}
|
||||||
|
|
||||||
as := []string{"last_checked", "uptime"}
|
if !slices.Contains([]string{"last_checked", "uptime"}, q.SortBy) {
|
||||||
sortBy = "last_checked"
|
q.SortBy = "last_checked"
|
||||||
if slices.Contains(as, q.SortBy) {
|
|
||||||
sortBy = q.SortBy
|
|
||||||
}
|
}
|
||||||
sortDirection = "DESC"
|
if q.SortDirection != "asc" {
|
||||||
if q.SortDirection == "asc" {
|
q.SortDirection = "DESC"
|
||||||
sortDirection = "ASC"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return args, where, sortBy, sortDirection
|
return args, where
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nodes represents a list of nodes
|
// Nodes represents a list of nodes
|
||||||
|
@ -150,7 +147,7 @@ type Nodes struct {
|
||||||
|
|
||||||
// Get nodes from database
|
// Get nodes from database
|
||||||
func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) {
|
func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) {
|
||||||
args, where, sortBy, sortDirection := q.toSQL()
|
args, where := q.toSQL()
|
||||||
|
|
||||||
var nodes Nodes
|
var nodes Nodes
|
||||||
|
|
||||||
|
@ -174,12 +171,12 @@ func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) {
|
||||||
*
|
*
|
||||||
FROM
|
FROM
|
||||||
tbl_node
|
tbl_node
|
||||||
%s -- where query if any
|
%s
|
||||||
ORDER BY
|
ORDER BY
|
||||||
%s
|
%s
|
||||||
%s
|
%s
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
OFFSET ?`, where, sortBy, sortDirection)
|
OFFSET ?`, where, q.SortBy, q.SortDirection)
|
||||||
err = r.db.Select(&nodes.Items, query, args...)
|
err = r.db.Select(&nodes.Items, query, args...)
|
||||||
|
|
||||||
return nodes, err
|
return nodes, err
|
||||||
|
|
|
@ -87,18 +87,18 @@ func TestQueryNodes_toSQL(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
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) {
|
if !equalArgs(gotArgs, tt.wantArgs) {
|
||||||
t.Errorf("QueryNodes.toSQL() gotArgs = %v, want %v", gotArgs, tt.wantArgs)
|
t.Errorf("QueryNodes.toSQL() gotArgs = %v, want %v", gotArgs, tt.wantArgs)
|
||||||
}
|
}
|
||||||
if gotWhere != tt.wantWhere {
|
if gotWhere != tt.wantWhere {
|
||||||
t.Errorf("QueryNodes.toSQL() gotWhere = %v, want %v", gotWhere, tt.wantWhere)
|
t.Errorf("QueryNodes.toSQL() gotWhere = %v, want %v", gotWhere, tt.wantWhere)
|
||||||
}
|
}
|
||||||
if gotSortBy != tt.wantSortBy {
|
if tt.query.SortBy != tt.wantSortBy {
|
||||||
t.Errorf("QueryNodes.toSQL() gotSortBy = %v, want %v", gotSortBy, tt.wantSortBy)
|
t.Errorf("QueryNodes.toSQL() gotSortBy = %v, want %v", tt.query.SortBy, tt.wantSortBy)
|
||||||
}
|
}
|
||||||
if gotSortDirection != tt.wantSortDirection {
|
if tt.query.SortDirection != tt.wantSortDirection {
|
||||||
t.Errorf("QueryNodes.toSQL() gotSortDirection = %v, want %v", gotSortDirection, 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:
|
// Single bench test:
|
||||||
// go test ./internal/monero -bench QueryNodes_toSQL -benchmem -run=^$ -v
|
// go test ./internal/monero -bench QueryNodes_toSQL -benchmem -run=^$ -v
|
||||||
func Benchmark_QueryNodes_toSQL(b *testing.B) {
|
func Benchmark_QueryNodes_toSQL(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
q := QueryNodes{
|
||||||
_, _, _, _ = QueryNodes{
|
|
||||||
Host: "test",
|
Host: "test",
|
||||||
Nettype: "any",
|
Nettype: "any",
|
||||||
Protocol: "any",
|
Protocol: "any",
|
||||||
|
@ -119,7 +118,9 @@ func Benchmark_QueryNodes_toSQL(b *testing.B) {
|
||||||
Page: 1,
|
Page: 1,
|
||||||
SortBy: "last_checked",
|
SortBy: "last_checked",
|
||||||
SortDirection: "desc",
|
SortDirection: "desc",
|
||||||
}.toSQL()
|
}
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, _ = q.toSQL()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue