22
qb/select.go
22
qb/select.go
@@ -22,6 +22,13 @@ const (
|
|||||||
DESC = false
|
DESC = false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (o Order) String() string {
|
||||||
|
if o {
|
||||||
|
return "ASC"
|
||||||
|
}
|
||||||
|
return "DESC"
|
||||||
|
}
|
||||||
|
|
||||||
// SelectBuilder builds CQL SELECT statements.
|
// SelectBuilder builds CQL SELECT statements.
|
||||||
type SelectBuilder struct {
|
type SelectBuilder struct {
|
||||||
table string
|
table string
|
||||||
@@ -29,8 +36,7 @@ type SelectBuilder struct {
|
|||||||
distinct columns
|
distinct columns
|
||||||
where where
|
where where
|
||||||
groupBy columns
|
groupBy columns
|
||||||
orderBy string
|
orderBy columns
|
||||||
order Order
|
|
||||||
limit uint
|
limit uint
|
||||||
limitPerPartition uint
|
limitPerPartition uint
|
||||||
allowFiltering bool
|
allowFiltering bool
|
||||||
@@ -75,14 +81,10 @@ func (b *SelectBuilder) ToCql() (stmt string, names []string) {
|
|||||||
cql.WriteByte(' ')
|
cql.WriteByte(' ')
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.orderBy != "" {
|
if len(b.orderBy) > 0 {
|
||||||
cql.WriteString("ORDER BY ")
|
cql.WriteString("ORDER BY ")
|
||||||
cql.WriteString(b.orderBy)
|
b.orderBy.writeCql(&cql)
|
||||||
if b.order {
|
cql.WriteByte(' ')
|
||||||
cql.WriteString(" ASC ")
|
|
||||||
} else {
|
|
||||||
cql.WriteString(" DESC ")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.limit != 0 {
|
if b.limit != 0 {
|
||||||
@@ -144,7 +146,7 @@ func (b *SelectBuilder) GroupBy(columns ...string) *SelectBuilder {
|
|||||||
|
|
||||||
// OrderBy sets ORDER BY clause on the query.
|
// OrderBy sets ORDER BY clause on the query.
|
||||||
func (b *SelectBuilder) OrderBy(column string, o Order) *SelectBuilder {
|
func (b *SelectBuilder) OrderBy(column string, o Order) *SelectBuilder {
|
||||||
b.orderBy, b.order = column, o
|
b.orderBy = append(b.orderBy, column+" "+o.String())
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,12 +59,23 @@ func TestSelectBuilder(t *testing.T) {
|
|||||||
B: Select("cycling.cyclist_name").GroupBy("id"),
|
B: Select("cycling.cyclist_name").GroupBy("id"),
|
||||||
S: "SELECT id FROM cycling.cyclist_name GROUP BY id ",
|
S: "SELECT id FROM cycling.cyclist_name GROUP BY id ",
|
||||||
},
|
},
|
||||||
|
// Add GROUP BY two columns
|
||||||
|
{
|
||||||
|
B: Select("cycling.cyclist_name").GroupBy("id", "user_uuid"),
|
||||||
|
S: "SELECT id,user_uuid FROM cycling.cyclist_name GROUP BY id,user_uuid ",
|
||||||
|
},
|
||||||
// Add ORDER BY
|
// Add ORDER BY
|
||||||
{
|
{
|
||||||
B: Select("cycling.cyclist_name").Where(w).OrderBy("firstname", ASC),
|
B: Select("cycling.cyclist_name").Where(w).OrderBy("firstname", ASC),
|
||||||
S: "SELECT * FROM cycling.cyclist_name WHERE id=? ORDER BY firstname ASC ",
|
S: "SELECT * FROM cycling.cyclist_name WHERE id=? ORDER BY firstname ASC ",
|
||||||
N: []string{"expr"},
|
N: []string{"expr"},
|
||||||
},
|
},
|
||||||
|
// Add ORDER BY two columns
|
||||||
|
{
|
||||||
|
B: Select("cycling.cyclist_name").Where(w).OrderBy("firstname", ASC).OrderBy("lastname", DESC),
|
||||||
|
S: "SELECT * FROM cycling.cyclist_name WHERE id=? ORDER BY firstname ASC,lastname DESC ",
|
||||||
|
N: []string{"expr"},
|
||||||
|
},
|
||||||
// Add LIMIT
|
// Add LIMIT
|
||||||
{
|
{
|
||||||
B: Select("cycling.cyclist_name").Where(w).Limit(10),
|
B: Select("cycling.cyclist_name").Where(w).Limit(10),
|
||||||
|
|||||||
Reference in New Issue
Block a user