Skip to content

Commit

Permalink
TWEAK: removed from public API NewBuffer and MapKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
bgaifullin committed Jun 1, 2017
1 parent ba5554a commit 40da11e
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 28 deletions.
9 changes: 5 additions & 4 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbr

import "bytes"

// Buffer is an interface used by Builder to store intermediate results
type Buffer interface {
WriteString(s string) (n int, err error)
String() string
Expand All @@ -10,15 +11,15 @@ type Buffer interface {
Value() []interface{}
}

func newBuffer() Buffer {
return &buffer{}
}

type buffer struct {
bytes.Buffer
v []interface{}
}

func NewBuffer() Buffer {
return &buffer{}
}

func (b *buffer) WriteValue(v ...interface{}) error {
b.v = append(b.v, v...)
return nil
Expand Down
1 change: 1 addition & 0 deletions buffer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package dbr
2 changes: 1 addition & 1 deletion condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestCondition(t *testing.T) {
value: []interface{}{1, 2, 3},
},
} {
buf := NewBuffer()
buf := newBuffer()
err := test.cond.Build(dialect.MySQL, buf)
assert.NoError(t, err)
assert.Equal(t, test.query, buf.String())
Expand Down
4 changes: 2 additions & 2 deletions dbr.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type runner interface {

func exec(runner runner, log EventReceiver, builder Builder, d Dialect) (sql.Result, error) {
i := interpolator{
Buffer: NewBuffer(),
Buffer: newBuffer(),
Dialect: d,
IgnoreBinary: true,
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func exec(runner runner, log EventReceiver, builder Builder, d Dialect) (sql.Res

func query(runner runner, log EventReceiver, builder Builder, d Dialect, dest interface{}) (int, error) {
i := interpolator{
Buffer: NewBuffer(),
Buffer: newBuffer(),
Dialect: d,
IgnoreBinary: true,
}
Expand Down
4 changes: 2 additions & 2 deletions delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestDeleteStmt(t *testing.T) {
buf := NewBuffer()
buf := newBuffer()
builder := DeleteFrom("table").Where(Eq("a", 1))
err := builder.Build(dialect.MySQL, buf)
assert.NoError(t, err)
Expand All @@ -17,7 +17,7 @@ func TestDeleteStmt(t *testing.T) {
}

func BenchmarkDeleteSQL(b *testing.B) {
buf := NewBuffer()
buf := newBuffer()
for i := 0; i < b.N; i++ {
DeleteFrom("table").Where(Eq("a", 1)).Build(dialect.MySQL, buf)
}
Expand Down
10 changes: 5 additions & 5 deletions insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type insertTest struct {
}

func TestInsertStmt(t *testing.T) {
buf := NewBuffer()
buf := newBuffer()
builder := InsertInto("table").Columns("a", "b").Values(1, "one").Record(&insertTest{
A: 2,
C: "two",
Expand All @@ -25,7 +25,7 @@ func TestInsertStmt(t *testing.T) {
}

func TestInsertOnConflictStmt(t *testing.T) {
buf := NewBuffer()
buf := newBuffer()
exp := Expr("a + ?", 1)
builder := InsertInto("table").Columns("a", "b").Values(1, "one")
builder.OnConflict("").Action("a", exp).Action("b", "one")
Expand All @@ -36,7 +36,7 @@ func TestInsertOnConflictStmt(t *testing.T) {
}

func TestInsertOnConflictMapStmt(t *testing.T) {
buf := NewBuffer()
buf := newBuffer()
exp := Expr("a + ?", 1)
builder := InsertInto("table").Columns("a", "b").Values(1, "one")
err := builder.OnConflictMap("", map[string]interface{}{"a": exp, "b": "one"}).Build(dialect.MySQL, buf)
Expand All @@ -46,14 +46,14 @@ func TestInsertOnConflictMapStmt(t *testing.T) {
}

func BenchmarkInsertValuesSQL(b *testing.B) {
buf := NewBuffer()
buf := newBuffer()
for i := 0; i < b.N; i++ {
InsertInto("table").Columns("a", "b").Values(1, "one").Build(dialect.MySQL, buf)
}
}

func BenchmarkInsertRecordSQL(b *testing.B) {
buf := NewBuffer()
buf := newBuffer()
for i := 0; i < b.N; i++ {
InsertInto("table").Columns("a", "b").Record(&insertTest{
A: 2,
Expand Down
15 changes: 7 additions & 8 deletions interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type interpolator struct {
// InterpolateForDialect replaces placeholder in query with corresponding value in dialect
func InterpolateForDialect(query string, value []interface{}, d Dialect) (string, error) {
i := interpolator{
Buffer: NewBuffer(),
Buffer: newBuffer(),
Dialect: d,
}
err := i.interpolate(query, value)
Expand Down Expand Up @@ -66,7 +66,7 @@ func (i *interpolator) interpolate(query string, value []interface{}) error {

func (i *interpolator) encodePlaceholder(value interface{}) error {
if builder, ok := value.(Builder); ok {
pbuf := NewBuffer()
pbuf := newBuffer()
err := builder.Build(i.Dialect, pbuf)
if err != nil {
return err
Expand Down Expand Up @@ -157,7 +157,7 @@ func (i *interpolator) encodePlaceholder(value interface{}) error {
// we need to sort keys, because in this case it is more chance
// for database cache hit because the query will be same for same values
// and this covers extra cost of sorting
keys := MapKeys(v.MapKeys())
keys := mapKeys(v.MapKeys())
sort.Sort(keys)
for n := 0; n < len(keys); n++ {
if n > 0 {
Expand All @@ -180,14 +180,13 @@ func (i *interpolator) encodePlaceholder(value interface{}) error {
return ErrNotSupported
}

// MapKeys uses to sort keys of map
type MapKeys []reflect.Value
type mapKeys []reflect.Value

func (k MapKeys) Len() int {
func (k mapKeys) Len() int {
return len(k)
}

func (k MapKeys) Less(i, j int) bool {
func (k mapKeys) Less(i, j int) bool {
vi, vj := k[i], k[j]
switch vi.Kind() {
case reflect.Bool:
Expand All @@ -205,6 +204,6 @@ func (k MapKeys) Less(i, j int) bool {
}
}

func (k MapKeys) Swap(i, j int) {
func (k mapKeys) Swap(i, j int) {
k[i], k[j] = k[j], k[i]
}
2 changes: 1 addition & 1 deletion interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestInterpolateIgnoreBinary(t *testing.T) {
},
} {
i := interpolator{
Buffer: NewBuffer(),
Buffer: newBuffer(),
Dialect: dialect.MySQL,
IgnoreBinary: true,
}
Expand Down
4 changes: 2 additions & 2 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestSelectStmt(t *testing.T) {
buf := NewBuffer()
buf := newBuffer()
builder := Select("a", "b").
From(Select("a").From("table")).
LeftJoin("table2", "table.a1 = table.a2").
Expand All @@ -27,7 +27,7 @@ func TestSelectStmt(t *testing.T) {
}

func BenchmarkSelectSQL(b *testing.B) {
buf := NewBuffer()
buf := newBuffer()
for i := 0; i < b.N; i++ {
Select("a", "b").From("table").Where(Eq("c", 1)).OrderAsc("d").Build(dialect.MySQL, buf)
}
Expand Down
6 changes: 3 additions & 3 deletions update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestUpdateStmt(t *testing.T) {
buf := NewBuffer()
buf := newBuffer()
builder := Update("table").Set("a", 1).Where(Eq("b", 2))
err := builder.Build(dialect.MySQL, buf)
assert.NoError(t, err)
Expand All @@ -18,14 +18,14 @@ func TestUpdateStmt(t *testing.T) {
}

func BenchmarkUpdateValuesSQL(b *testing.B) {
buf := NewBuffer()
buf := newBuffer()
for i := 0; i < b.N; i++ {
Update("table").Set("a", 1).Build(dialect.MySQL, buf)
}
}

func BenchmarkUpdateMapSQL(b *testing.B) {
buf := NewBuffer()
buf := newBuffer()
for i := 0; i < b.N; i++ {
Update("table").SetMap(map[string]interface{}{"a": 1, "b": 2}).Build(dialect.MySQL, buf)
}
Expand Down

0 comments on commit 40da11e

Please sign in to comment.