Add offset and limit generation
This commit is contained in:
parent
40df1b7c86
commit
b8c28c7cf8
5 changed files with 37 additions and 3 deletions
|
@ -1 +1 @@
|
|||
SELECT * FROM "t";
|
||||
SELECT * FROM "t";
|
1
boil/_fixtures/01.sql
Normal file
1
boil/_fixtures/01.sql
Normal file
|
@ -0,0 +1 @@
|
|||
SELECT * FROM "q" LIMIT 5 OFFSET 6;
|
|
@ -40,6 +40,13 @@ func Limit(limit int) QueryMod {
|
|||
}
|
||||
}
|
||||
|
||||
// Offset into the results
|
||||
func Offset(offset int) QueryMod {
|
||||
return func(q *boil.Query) {
|
||||
boil.SetOffset(q, offset)
|
||||
}
|
||||
}
|
||||
|
||||
// InnerJoin on another table
|
||||
func InnerJoin(stmt string, args ...interface{}) QueryMod {
|
||||
return func(q *boil.Query) {
|
||||
|
|
|
@ -41,6 +41,7 @@ type Query struct {
|
|||
orderBy []string
|
||||
having []string
|
||||
limit int
|
||||
offset int
|
||||
}
|
||||
|
||||
func buildQuery(q *Query) (string, []interface{}) {
|
||||
|
@ -85,6 +86,13 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
|
|||
where, args := whereClause(q)
|
||||
buf.WriteString(where)
|
||||
|
||||
if q.limit != 0 {
|
||||
fmt.Fprintf(buf, " LIMIT %d", q.limit)
|
||||
}
|
||||
if q.offset != 0 {
|
||||
fmt.Fprintf(buf, " OFFSET %d", q.offset)
|
||||
}
|
||||
|
||||
buf.WriteByte(';')
|
||||
return buf, args
|
||||
}
|
||||
|
@ -229,6 +237,11 @@ func SetLimit(q *Query, limit int) {
|
|||
q.limit = limit
|
||||
}
|
||||
|
||||
// SetOffset on the query.
|
||||
func SetOffset(q *Query, offset int) {
|
||||
q.offset = offset
|
||||
}
|
||||
|
||||
func whereClause(q *Query) (string, []interface{}) {
|
||||
if len(q.where) == 0 {
|
||||
return "", nil
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
)
|
||||
|
||||
var writeGoldenFiles = flag.Bool(
|
||||
"golden",
|
||||
"test.golden",
|
||||
false,
|
||||
"Write golden files.",
|
||||
)
|
||||
|
@ -26,7 +26,8 @@ func TestBuildQuery(t *testing.T) {
|
|||
q *Query
|
||||
args []interface{}
|
||||
}{
|
||||
{&Query{from: "t"}, []interface{}{}},
|
||||
{&Query{from: "t"}, nil},
|
||||
{&Query{from: "q", limit: 5, offset: 6}, nil},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
|
@ -102,6 +103,18 @@ func TestSetLimit(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSetOffset(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
q := &Query{}
|
||||
SetOffset(q, 10)
|
||||
|
||||
expect := 10
|
||||
if q.offset != expect {
|
||||
t.Errorf("Expected %d, got %d", expect, q.offset)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetSQL(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
Loading…
Reference in a new issue