From 19dedfc6a08383d6464af0fdcb852076447e7cda Mon Sep 17 00:00:00 2001 From: sagarafr Date: Wed, 24 Oct 2018 14:27:03 +0200 Subject: [PATCH] fix(qb): Fixed group by generation * In qb/select.go, if there are no colums and the query have a GroupBy statement then the query will be malformated. * In qb/utils.go, refactor writeCql with a strings.Join call. --- qb/select.go | 5 ++--- qb/utils.go | 8 ++------ 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/qb/select.go b/qb/select.go index 0287dec..bc6ed74 100644 --- a/qb/select.go +++ b/qb/select.go @@ -10,6 +10,7 @@ package qb import ( "bytes" "fmt" + "strings" ) // Order specifies sorting order. @@ -53,9 +54,7 @@ func (b *SelectBuilder) ToCql() (stmt string, names []string) { cql.WriteString("DISTINCT ") b.distinct.writeCql(&cql) case len(b.groupBy) > 0: - b.groupBy.writeCql(&cql) - cql.WriteByte(',') - b.columns.writeCql(&cql) + cql.WriteString(strings.Join(append(b.groupBy, b.columns...), ",")) case len(b.columns) == 0: cql.WriteByte('*') default: diff --git a/qb/utils.go b/qb/utils.go index ffa4ab4..8b78b2b 100644 --- a/qb/utils.go +++ b/qb/utils.go @@ -6,6 +6,7 @@ package qb import ( "bytes" + "strings" ) // placeholders returns a string with count ? placeholders joined with commas. @@ -24,10 +25,5 @@ func placeholders(cql *bytes.Buffer, count int) { type columns []string func (cols columns) writeCql(cql *bytes.Buffer) { - for i, c := range cols { - cql.WriteString(c) - if i < len(cols)-1 { - cql.WriteByte(',') - } - } + cql.WriteString(strings.Join(cols, ",")) }