Skip to content

Commit

Permalink
add UseColums function to avoid extra allocation performed by Colums …
Browse files Browse the repository at this point in the history
…function

fix name

tidy

adding SetWhere

adding SetWhere

fix go mod

fix comment style

revert go mod

Avoid to append if no needed

fix git ignore

revert change in go.sum/go.mod

adding specific benchmark
  • Loading branch information
meox authored and mmatczuk committed Dec 16, 2019
1 parent a57d58f commit 9239d9c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@

# Go vendor dir
vendor

# GoLand IDE
.idea/
24 changes: 20 additions & 4 deletions qb/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ func (b *SelectBuilder) Json() *SelectBuilder {

// Columns adds result columns to the query.
func (b *SelectBuilder) Columns(columns ...string) *SelectBuilder {
b.columns = append(b.columns, columns...)
if len(b.columns) == 0 {
b.columns = columns
} else {
b.columns = append(b.columns, columns...)
}
return b
}

Expand All @@ -143,21 +147,33 @@ func As(column, name string) string {

// Distinct sets DISTINCT clause on the query.
func (b *SelectBuilder) Distinct(columns ...string) *SelectBuilder {
b.distinct = append(b.distinct, columns...)
if len(b.where) == 0 {
b.distinct = columns
} else {
b.distinct = append(b.distinct, columns...)
}
return b
}

// Where adds an expression to the WHERE clause of the query. Expressions are
// ANDed together in the generated CQL.
func (b *SelectBuilder) Where(w ...Cmp) *SelectBuilder {
b.where = append(b.where, w...)
if len(b.where) == 0 {
b.where = w
} else {
b.where = append(b.where, w...)
}
return b
}

// GroupBy sets GROUP BY clause on the query. Columns must be a primary key,
// this will automatically add the the columns as first selectors.
func (b *SelectBuilder) GroupBy(columns ...string) *SelectBuilder {
b.groupBy = append(b.groupBy, columns...)
if len(b.groupBy) == 0 {
b.groupBy = columns
} else {
b.groupBy = append(b.groupBy, columns...)
}
return b
}

Expand Down
19 changes: 18 additions & 1 deletion qb/select_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ import "testing"
func BenchmarkSelectBuilder(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
Select("cycling.cyclist_name").Columns("id", "user_uuid", "firstname", "stars").Where(Eq("id")).ToCql()
Select("cycling.cyclist_name").
Columns("id", "user_uuid", "firstname", "surname", "stars").
Where(Eq("id")).
ToCql()
}
}

func BenchmarkSelectBuildAssign(b *testing.B) {
b.ResetTimer()
cols := []string{
"id", "user_uuid", "firstname",
"surname", "stars",
}
for i := 0; i < b.N; i++ {
Select("cycling.cyclist_name").
Columns(cols...).
Where(Eq("id")).
ToCql()
}
}
2 changes: 1 addition & 1 deletion qb/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type TokenBuilder []string

// Token creates a new TokenBuilder.
func Token(columns ...string) TokenBuilder {
return TokenBuilder(columns)
return columns
}

// Eq produces token(column)=token(?).
Expand Down
12 changes: 10 additions & 2 deletions qb/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,22 @@ func (b *UpdateBuilder) removeValue(column string, value value) *UpdateBuilder {
// Where adds an expression to the WHERE clause of the query. Expressions are
// ANDed together in the generated CQL.
func (b *UpdateBuilder) Where(w ...Cmp) *UpdateBuilder {
b.where = append(b.where, w...)
if len(b.where) == 0 {
b.where = w
} else {
b.where = append(b.where, w...)
}
return b
}

// If adds an expression to the IF clause of the query. Expressions are ANDed
// together in the generated CQL.
func (b *UpdateBuilder) If(w ...Cmp) *UpdateBuilder {
b._if = append(b._if, w...)
if len(b._if) == 0 {
b._if = w
} else {
b._if = append(b._if, w...)
}
return b
}

Expand Down

0 comments on commit 9239d9c

Please sign in to comment.