Skip to content

Commit

Permalink
Merge pull request #551 from upper/hotfix/linter-warnings
Browse files Browse the repository at this point in the history
Fix linter warnings
  • Loading branch information
xiam authored Feb 26, 2020
2 parents 8c74a80 + 554b6bb commit 5b94830
Show file tree
Hide file tree
Showing 58 changed files with 211 additions and 311 deletions.
13 changes: 0 additions & 13 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

package db

import (
"reflect"
)

// Function interface defines methods for representing database functions.
// This is an exported interface but it's rarely used directly, you may want to
// use the `db.Func()` function instead.
Expand Down Expand Up @@ -52,15 +48,6 @@ type Function interface {
// // RTRIM("Hello ")
// db.Func("RTRIM", "Hello ")
func Func(name string, args ...interface{}) Function {
if len(args) == 1 {
if reflect.TypeOf(args[0]).Kind() == reflect.Slice {
iargs := make([]interface{}, len(args))
for i := range args {
iargs[i] = args[i]
}
args = iargs
}
}
return &dbFunc{name: name, args: args}
}

Expand Down
6 changes: 4 additions & 2 deletions internal/cache/hashstructure/hashstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ func (w *walker) visit(v reflect.Value, opts *visitOpts) (uint64, error) {
default:
return 0, fmt.Errorf("unknown kind to hash: %s", k)
}

return 0, nil
}

func hashUpdateOrdered(h hash.Hash64, a, b uint64) uint64 {
Expand Down Expand Up @@ -321,3 +319,7 @@ const (
visitFlagInvalid visitFlag = iota
visitFlagSet = iota << 1
)

var (
_ = visitFlagInvalid
)
4 changes: 2 additions & 2 deletions internal/sqladapter/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ cancel:
if !inTx {
// This is only executed if t.Database() was **not** a transaction and if
// sess was created with sess.NewTransaction().
tx.Rollback()
_ = tx.Rollback()
}
return err
}
Expand Down Expand Up @@ -310,7 +310,7 @@ cancel:
if !inTx {
// This is only executed if t.Database() was **not** a transaction and if
// sess was created with sess.NewTransaction().
tx.Rollback()
_ = tx.Rollback()
}
return err
}
Expand Down
32 changes: 17 additions & 15 deletions internal/sqladapter/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,24 +350,26 @@ func (d *database) Close() error {
d.baseTx = nil
d.sessMu.Unlock()
}()
if d.sess != nil {
if cleaner, ok := d.PartialDatabase.(hasCleanUp); ok {
cleaner.CleanUp()
}
if d.sess == nil {
return nil
}

d.cachedCollections.Clear()
d.cachedStatements.Clear() // Closes prepared statements as well.
d.cachedCollections.Clear()
d.cachedStatements.Clear() // Closes prepared statements as well.

tx := d.Transaction()
if tx == nil {
// Not within a transaction.
return d.sess.Close()
tx := d.Transaction()
if tx == nil {
if cleaner, ok := d.PartialDatabase.(hasCleanUp); ok {
if err := cleaner.CleanUp(); err != nil {
return err
}
}
// Not within a transaction.
return d.sess.Close()
}

if !tx.Committed() {
tx.Rollback()
return nil
}
if !tx.Committed() {
_ = tx.Rollback()
}
return nil
}
Expand Down Expand Up @@ -649,7 +651,7 @@ func (d *database) WaitForConnection(connectFn func() error) error {
waitTime := time.Millisecond * 10

// Waitig 5 seconds for a successful connection.
for timeStart := time.Now(); time.Now().Sub(timeStart) < time.Second*5; {
for timeStart := time.Now(); time.Since(timeStart) < time.Second*5; {
err := connectFn()
if err == nil {
return nil // Connected!
Expand Down
10 changes: 5 additions & 5 deletions internal/sqladapter/exql/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,34 @@ func BenchmarkColumnHash(b *testing.B) {
func BenchmarkColumnCompile(b *testing.B) {
c := Column{Name: "name"}
for i := 0; i < b.N; i++ {
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}

func BenchmarkColumnCompileNoCache(b *testing.B) {
for i := 0; i < b.N; i++ {
c := Column{Name: "name"}
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}

func BenchmarkColumnWithDotCompile(b *testing.B) {
c := Column{Name: "role.name"}
for i := 0; i < b.N; i++ {
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}

func BenchmarkColumnWithImplicitAsKeywordCompile(b *testing.B) {
c := Column{Name: "role.name foo"}
for i := 0; i < b.N; i++ {
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}

func BenchmarkColumnWithAsKeywordCompile(b *testing.B) {
c := Column{Name: "role.name AS foo"}
for i := 0; i < b.N; i++ {
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}
4 changes: 1 addition & 3 deletions internal/sqladapter/exql/column_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ func JoinColumnValues(values ...Fragment) *ColumnValues {

// Insert adds a column to the columns array.
func (c *ColumnValues) Insert(values ...Fragment) *ColumnValues {
for _, f := range values {
c.ColumnValues = append(c.ColumnValues, f)
}
c.ColumnValues = append(c.ColumnValues, values...)
c.hash.Reset()
return c
}
Expand Down
8 changes: 4 additions & 4 deletions internal/sqladapter/exql/column_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ func BenchmarkColumnValueHash(b *testing.B) {
func BenchmarkColumnValueCompile(b *testing.B) {
cv := &ColumnValue{Column: ColumnWithName("id"), Operator: "=", Value: NewValue(1)}
for i := 0; i < b.N; i++ {
cv.Compile(defaultTemplate)
_, _ = cv.Compile(defaultTemplate)
}
}

func BenchmarkColumnValueCompileNoCache(b *testing.B) {
for i := 0; i < b.N; i++ {
cv := &ColumnValue{Column: ColumnWithName("id"), Operator: "=", Value: NewValue(1)}
cv.Compile(defaultTemplate)
_, _ = cv.Compile(defaultTemplate)
}
}

Expand Down Expand Up @@ -140,7 +140,7 @@ func BenchmarkColumnValuesCompile(b *testing.B) {
&ColumnValue{Column: ColumnWithName("modified"), Operator: "<=", Value: NewValue(Raw{Value: "NOW()"})},
)
for i := 0; i < b.N; i++ {
cvs.Compile(defaultTemplate)
_, _ = cvs.Compile(defaultTemplate)
}
}

Expand All @@ -153,6 +153,6 @@ func BenchmarkColumnValuesCompileNoCache(b *testing.B) {
&ColumnValue{Column: ColumnWithName("created"), Operator: ">=", Value: NewValue(Raw{Value: "NOW()"})},
&ColumnValue{Column: ColumnWithName("modified"), Operator: "<=", Value: NewValue(Raw{Value: "NOW()"})},
)
cvs.Compile(defaultTemplate)
_, _ = cvs.Compile(defaultTemplate)
}
}
4 changes: 2 additions & 2 deletions internal/sqladapter/exql/columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func BenchmarkColumnsCompile(b *testing.B) {
&Column{Name: "role.id"},
)
for i := 0; i < b.N; i++ {
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}

Expand All @@ -69,6 +69,6 @@ func BenchmarkColumnsCompileNoCache(b *testing.B) {
&Column{Name: "role.name"},
&Column{Name: "role.id"},
)
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}
6 changes: 3 additions & 3 deletions internal/sqladapter/exql/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ func BenchmarkDatabaseHash(b *testing.B) {
func BenchmarkDatabaseCompile(b *testing.B) {
c := Database{Name: "name"}
for i := 0; i < b.N; i++ {
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}

func BenchmarkDatabaseCompileNoCache(b *testing.B) {
for i := 0; i < b.N; i++ {
c := Database{Name: "name"}
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}

func BenchmarkDatabaseCompileNoCache2(b *testing.B) {
for i := 0; i < b.N; i++ {
c := Database{Name: fmt.Sprintf("name: %v", i)}
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}
2 changes: 0 additions & 2 deletions internal/sqladapter/exql/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ const (
DROP TABLE {{.Table | compile}}
`

defaultGroupByColumnLayout = `{{.Column}}`

defaultGroupByLayout = `
{{if .GroupColumns}}
GROUP BY {{.GroupColumns}}
Expand Down
4 changes: 2 additions & 2 deletions internal/sqladapter/exql/group_by_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func BenchmarkGroupByCompile(b *testing.B) {
&Column{Name: "role.id"},
)
for i := 0; i < b.N; i++ {
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}

Expand All @@ -65,6 +65,6 @@ func BenchmarkGroupByCompileNoCache(b *testing.B) {
&Column{Name: "role.name"},
&Column{Name: "role.id"},
)
c.Compile(defaultTemplate)
_, _ = c.Compile(defaultTemplate)
}
}
6 changes: 3 additions & 3 deletions internal/sqladapter/exql/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func BenchmarkCompileJoin(b *testing.B) {
),
})
for i := 0; i < b.N; i++ {
j.Compile(defaultTemplate)
_, _ = j.Compile(defaultTemplate)
}
}

Expand All @@ -246,7 +246,7 @@ func BenchmarkCompileJoinNoCache(b *testing.B) {
},
),
})
j.Compile(defaultTemplate)
_, _ = j.Compile(defaultTemplate)
}
}

Expand All @@ -267,6 +267,6 @@ func BenchmarkCompileJoinNoCache2(b *testing.B) {
},
),
})
j.Compile(defaultTemplate)
_, _ = j.Compile(defaultTemplate)
}
}
16 changes: 8 additions & 8 deletions internal/sqladapter/exql/order_by_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func BenchmarkCompileOrderByCompile(b *testing.B) {
),
}
for i := 0; i < b.N; i++ {
o.Compile(defaultTemplate)
_, _ = o.Compile(defaultTemplate)
}
}

Expand All @@ -85,21 +85,21 @@ func BenchmarkCompileOrderByCompileNoCache(b *testing.B) {
&SortColumn{Column: &Column{Name: "foo"}},
),
)
o.Compile(defaultTemplate)
_, _ = o.Compile(defaultTemplate)
}
}

func BenchmarkCompileOrderCompile(b *testing.B) {
o := Descendent
for i := 0; i < b.N; i++ {
o.Compile(defaultTemplate)
_, _ = o.Compile(defaultTemplate)
}
}

func BenchmarkCompileOrderCompileNoCache(b *testing.B) {
for i := 0; i < b.N; i++ {
o := Descendent
o.Compile(defaultTemplate)
_, _ = o.Compile(defaultTemplate)
}
}

Expand All @@ -113,14 +113,14 @@ func BenchmarkSortColumnHash(b *testing.B) {
func BenchmarkSortColumnCompile(b *testing.B) {
s := &SortColumn{Column: &Column{Name: "foo"}}
for i := 0; i < b.N; i++ {
s.Compile(defaultTemplate)
_, _ = s.Compile(defaultTemplate)
}
}

func BenchmarkSortColumnCompileNoCache(b *testing.B) {
for i := 0; i < b.N; i++ {
s := &SortColumn{Column: &Column{Name: "foo"}}
s.Compile(defaultTemplate)
_, _ = s.Compile(defaultTemplate)
}
}

Expand All @@ -140,7 +140,7 @@ func BenchmarkSortColumnsCompile(b *testing.B) {
&SortColumn{Column: &Column{Name: "bar"}},
)
for i := 0; i < b.N; i++ {
s.Compile(defaultTemplate)
_, _ = s.Compile(defaultTemplate)
}
}

Expand All @@ -150,6 +150,6 @@ func BenchmarkSortColumnsCompileNoCache(b *testing.B) {
&SortColumn{Column: &Column{Name: "foo"}},
&SortColumn{Column: &Column{Name: "bar"}},
)
s.Compile(defaultTemplate)
_, _ = s.Compile(defaultTemplate)
}
}
4 changes: 2 additions & 2 deletions internal/sqladapter/exql/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ func BenchmarkRawCreate(b *testing.B) {
func BenchmarkRawString(b *testing.B) {
raw := &Raw{Value: "foo"}
for i := 0; i < b.N; i++ {
raw.String()
_ = raw.String()
}
}

func BenchmarkRawCompile(b *testing.B) {
raw := &Raw{Value: "foo"}
for i := 0; i < b.N; i++ {
raw.Compile(defaultTemplate)
_, _ = raw.Compile(defaultTemplate)
}
}

Expand Down
9 changes: 0 additions & 9 deletions internal/sqladapter/exql/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"errors"
"reflect"
"strings"

"upper.io/db.v3/internal/cache"
)

var errUnknownTemplateType = errors.New("Unknown template type")
Expand Down Expand Up @@ -41,13 +39,6 @@ func (layout *Template) doCompile(c Fragment) (string, error) {
return "", nil
}

func getHash(h cache.Hashable) string {
if h != nil && !reflect.ValueOf(h).IsNil() {
return h.Hash()
}
return ""
}

// Hash returns a unique identifier for the struct.
func (s *Statement) Hash() string {
return s.hash.Hash(s)
Expand Down
Loading

0 comments on commit 5b94830

Please sign in to comment.