Skip to content

Commit

Permalink
optimize SQLQuery.AllQuery() logic return sorted *Query list
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Apr 8, 2023
1 parent e758e5a commit 1c556ce
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 36 deletions.
21 changes: 18 additions & 3 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ type Query struct {
Tags map[string]string
}

// QueryList query list
type QueryList []*Query

// QueryMap is a map associating a Tag to its Query
type QueryMap map[string]*Query

func (q *Query) PrepareStyle() string {
prepareStyle := PrepareStyleStmt
if style, exist := q.Tags["prepare"]; exist {
Expand All @@ -51,8 +57,17 @@ func (q *Query) PrepareStyle() string {
return prepareStyle
}

// QueryMap is a map associating a Tag to its Query
type QueryMap map[string]*Query
func (q QueryList) Len() int {
return len(q)
}

func (q QueryList) Less(i, j int) bool {
return q[i].Scope+"_"+q[i].Name < q[j].Scope+"_"+q[j].Name
}

func (q QueryList) Swap(i, j int) {
q[i], q[j] = q[j], q[i]
}

func (q QueryMap) FilterByStyle(style string) QueryMap {
qm := make(QueryMap, len(q))
Expand Down Expand Up @@ -94,7 +109,7 @@ type SQLQuery interface {
ListScope() ScopeQuery

// AllQuery get all *Query list
AllQuery() []*Query
AllQuery() QueryList
}

// SQLParser sql file parser interface
Expand Down
5 changes: 0 additions & 5 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ default: run
run:
go run gen.go

.PHONY: mod-tidy
mod-tidy:
@go mod download
@go mod tidy

.PHONY: generate
generate:
@go generate gen.go
Expand Down
48 changes: 24 additions & 24 deletions examples/auto/yesql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/alimy/yesql/examples

go 1.18

require github.com/alimy/yesql v1.1.2
require github.com/alimy/yesql v1.1.6

require github.com/jmoiron/sqlx v1.3.5

Expand Down
6 changes: 4 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"regexp"
"sort"
"strings"
)

Expand Down Expand Up @@ -71,8 +72,8 @@ func (s *sqlParser) ListScope() ScopeQuery {
return s.scopeQuery
}

func (s *sqlParser) AllQuery() []*Query {
allQuery := make([]*Query, 0, len(s.queryMap))
func (s *sqlParser) AllQuery() QueryList {
allQuery := make(QueryList, 0, len(s.queryMap))
for _, query := range s.queryMap {
allQuery = append(allQuery, query)
}
Expand All @@ -81,6 +82,7 @@ func (s *sqlParser) AllQuery() []*Query {
allQuery = append(allQuery, query)
}
}
sort.Sort(allQuery)
return allQuery
}

Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package yesql

var Version = "v1.1.5"
var Version = "v1.1.6"

0 comments on commit 1c556ce

Please sign in to comment.