Begin query building

* Added golden fixtures
This commit is contained in:
Patrick O'brien 2016-04-19 15:31:07 +10:00
parent dc50c0d42c
commit 2fc2a36306
5 changed files with 334 additions and 209 deletions

1
boil/_fixtures/00.sql Normal file
View file

@ -0,0 +1 @@
SELECT * FROM "t"

View file

@ -4,61 +4,68 @@ import "github.com/pobri19/sqlboiler/boil"
type QueryMod func(q *boil.Query) type QueryMod func(q *boil.Query)
func (q *boil.Query) Apply(mods ...QueryMod) {
for _, mod := range mods {
mod(q)
}
}
func Limit(limit int) QueryMod { func Limit(limit int) QueryMod {
return func(q *boil.Query) { return func(q *boil.Query) {
q.limit = limit boil.SetLimit(q, limit)
} }
} }
func Join(join string) QueryMod { func InnerJoin(on string, args ...interface{}) QueryMod {
return func(q *boil.Query) { return func(q *boil.Query) {
q.joins = append(q.joins, join) boil.SetInnerJoin(q, on, args...)
}
}
func OuterJoin(on string, args ...interface{}) QueryMod {
return func(q *boil.Query) {
boil.SetOuterJoin(q, on, args...)
}
}
func LeftOuterJoin(on string, args ...interface{}) QueryMod {
return func(q *boil.Query) {
boil.SetLeftOuterJoin(q, on, args...)
}
}
func RightOuterJoin(on string, args ...interface{}) QueryMod {
return func(q *boil.Query) {
boil.SetRightOuterJoin(q, on, args...)
} }
} }
func Select(columns ...string) QueryMod { func Select(columns ...string) QueryMod {
return func(q *boil.Query) { return func(q *boil.Query) {
q.selectCols = append(q.selectCols, columns...) boil.SetSelect(q, columns...)
} }
} }
func Where(clause string, args ...interface{}) QueryMod { func Where(clause string, args ...interface{}) QueryMod {
return func(q *boil.Query) { return func(q *boil.Query) {
w := where{ boil.SetWhere(q, clause, args...)
clause: clause,
args: args,
}
q.where = append(q.where, w)
} }
} }
func GroupBy(clause string) QueryMod { func GroupBy(clause string) QueryMod {
return func(q *boil.Query) { return func(q *boil.Query) {
q.groupBy = append(q.groupBy, clause) boil.SetGroupBy(q, clause)
} }
} }
func OrderBy(clause string) QueryMod { func OrderBy(clause string) QueryMod {
return func(q *boil.Query) { return func(q *boil.Query) {
q.orderBy = append(q.orderBy, clause) boil.SetOrderBy(q, clause)
} }
} }
func Having(clause string) QueryMod { func Having(clause string) QueryMod {
return func(q *boil.Query) { return func(q *boil.Query) {
q.having = append(q.having, clause) boil.SetHaving(q, clause)
} }
} }
func From(table string) QueryMod { func From(table string) QueryMod {
return func(q *boil.Query) { return func(q *boil.Query) {
q.from = table boil.SetFrom(q, table)
} }
} }

View file

@ -1,141 +1 @@
package qs package qs
import (
"testing"
"github.com/pobri19/sqlboiler/boil"
)
func TestApply(t *testing.T) {
t.Parallel()
q := &boil.Query{}
qfn1 := Limit(10)
qfn2 := Where("x > $1 AND y > $2", 5, 3)
q.Apply(qfn1, qfn2)
expect1 := 10
if q.limit != expect1 {
t.Errorf("Expected %d, got %d", expect1, q.limit)
}
expect2 := "x > $1 AND y > $2"
if len(q.where) != 1 {
t.Errorf("Expected %d where slices, got %d", len(q.where))
}
expect := "x > $1 AND y > $2"
if q.where[0].clause != expect2 {
t.Errorf("Expected %s, got %s", expect, q.where)
}
if len(q.where[0].args) != 2 {
t.Errorf("Expected %d args, got %d", 2, len(q.where[0].args))
}
if q.where[0].args[0].(int) != 5 || q.where[0].args[1].(int) != 3 {
t.Errorf("Args not set correctly, expected 5 & 3, got: %#v", q.where[0].args)
}
}
func TestDB(t *testing.T) {
t.Parallel()
}
func TestLimit(t *testing.T) {
t.Parallel()
q := &boil.Query{}
qfn := Limit(10)
qfn(q)
expect := 10
if q.limit != expect {
t.Errorf("Expected %d, got %d", expect, q.limit)
}
}
func TestWhere(t *testing.T) {
t.Parallel()
q := &boil.Query{}
qfn := Where("x > $1 AND y > $2", 5, 3)
qfn(q)
if len(q.where) != 1 {
t.Errorf("Expected %d where slices, got %d", len(q.where))
}
expect := "x > $1 AND y > $2"
if q.where[0].clause != expect {
t.Errorf("Expected %s, got %s", expect, q.where)
}
if len(q.where[0].args) != 2 {
t.Errorf("Expected %d args, got %d", 2, len(q.where[0].args))
}
if q.where[0].args[0].(int) != 5 || q.where[0].args[1].(int) != 3 {
t.Errorf("Args not set correctly, expected 5 & 3, got: %#v", q.where[0].args)
}
}
func TestGroupBy(t *testing.T) {
t.Parallel()
q := &boil.Query{}
qfn := GroupBy("col1, col2")
qfn(q)
expect := "col1, col2"
if len(q.groupBy) != 1 && q.groupBy[0] != expect {
t.Errorf("Expected %s, got %s", expect, q.groupBy[0])
}
}
func TestOrderBy(t *testing.T) {
t.Parallel()
q := &boil.Query{}
qfn := OrderBy("col1 desc, col2 asc")
qfn(q)
expect := "col1 desc, col2 asc"
if len(q.orderBy) != 1 && q.orderBy[0] != expect {
t.Errorf("Expected %s, got %s", expect, q.orderBy[0])
}
}
func TestHaving(t *testing.T) {
t.Parallel()
q := &boil.Query{}
qfn := Having("count(orders.order_id) > 10")
qfn(q)
expect := "count(orders.order_id) > 10"
if len(q.having) != 1 && q.having[0] != expect {
t.Errorf("Expected %s, got %s", expect, q.having[0])
}
}
func TestFrom(t *testing.T) {
t.Parallel()
q := &boil.Query{}
qfn := From("videos a, orders b")
qfn(q)
expect := "videos a, orders b"
if q.from != expect {
t.Errorf("Expected %s, got %s", expect, q.from)
}
}

View file

@ -1,19 +1,32 @@
package boil package boil
import "database/sql" import (
"bytes"
"database/sql"
"fmt"
"strings"
)
type where struct { type where struct {
clause string clause string
args []interface{} args []interface{}
} }
type join struct {
on string
args []interface{}
}
type Query struct { type Query struct {
executor Executor executor Executor
delete bool delete bool
update map[string]interface{} update map[string]interface{}
selectCols []string selectCols []string
from string from string
joins []string innerJoins []join
outerJoins []join
leftOuterJoins []join
rightOuterJoins []join
where []where where []where
groupBy []string groupBy []string
orderBy []string orderBy []string
@ -21,48 +34,49 @@ type Query struct {
limit int limit int
} }
func SetDelete(q *Query, flag bool) { func buildQuery(q *Query) (string, []interface{}) {
q.delete = flag var buf *bytes.Buffer
var args []interface{}
switch {
case q.delete:
buf, args = buildDeleteQuery(q)
case len(q.update) > 0:
buf, args = buildUpdateQuery(q)
default:
buf, args = buildSelectQuery(q)
}
return buf.String(), args
} }
func SetUpdate(q *Query, cols map[string]interface{}) { func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
q.update = cols buf := &bytes.Buffer{}
buf.WriteString("SELECT ")
if len(q.selectCols) > 0 {
buf.WriteString(strings.Join(q.selectCols, ","))
} else {
buf.WriteByte('*')
}
buf.WriteString(" FROM ")
fmt.Fprintf(buf, `"%s"`, q.from)
return buf, []interface{}{}
} }
func SetExecutor(q *Query, exec Executor) { func buildDeleteQuery(q *Query) (*bytes.Buffer, []interface{}) {
q.executor = exec buf := &bytes.Buffer{}
return buf, nil
} }
func SetSelect() { func buildUpdateQuery(q *Query) (*bytes.Buffer, []interface{}) {
buf := &bytes.Buffer{}
}
func SetFrom() {
}
func SetJoins() {
}
func SetWhere() {
}
func SetGroupBy() {
}
func SetOrderBy() {
}
func SetHaving() {
}
func SetLimit() {
return buf, nil
} }
func ExecQuery(q *Query) error { func ExecQuery(q *Query) error {
@ -77,6 +91,64 @@ func ExecQueryAll(q *Query) (*sql.Rows, error) {
return nil, nil return nil, nil
} }
func buildQuery(q *Query) string { func Apply(q *Query, mods ...func(q *Query)) {
return "" for _, mod := range mods {
mod(q)
}
}
func SetDelete(q *Query, flag bool) {
q.delete = flag
}
func SetUpdate(q *Query, cols map[string]interface{}) {
q.update = cols
}
func SetExecutor(q *Query, exec Executor) {
q.executor = exec
}
func SetSelect(q *Query, columns ...string) {
q.selectCols = append(q.selectCols, columns...)
}
func SetFrom(q *Query, table string) {
q.from = table
}
func SetInnerJoin(q *Query, on string, args ...interface{}) {
q.innerJoins = append(q.innerJoins, join{on: on, args: args})
}
func SetOuterJoin(q *Query, on string, args ...interface{}) {
q.outerJoins = append(q.outerJoins, join{on: on, args: args})
}
func SetLeftOuterJoin(q *Query, on string, args ...interface{}) {
q.leftOuterJoins = append(q.leftOuterJoins, join{on: on, args: args})
}
func SetRightOuterJoin(q *Query, on string, args ...interface{}) {
q.rightOuterJoins = append(q.rightOuterJoins, join{on: on, args: args})
}
func SetWhere(q *Query, clause string, args ...interface{}) {
q.where = append(q.where, where{clause: clause, args: args})
}
func SetGroupBy(q *Query, clause string) {
q.groupBy = append(q.groupBy, clause)
}
func SetOrderBy(q *Query, clause string) {
q.orderBy = append(q.orderBy, clause)
}
func SetHaving(q *Query, clause string) {
q.having = append(q.having, clause)
}
func SetLimit(q *Query, limit int) {
q.limit = limit
} }

View file

@ -1 +1,186 @@
package boil package boil
import (
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
)
var writeGoldenFiles = flag.Bool(
"golden",
false,
"Write golden files.",
)
func TestBuildQuery(t *testing.T) {
t.Parallel()
tests := []struct {
q *Query
args []interface{}
}{
{&Query{from: "t"}, []interface{}{}},
}
for i, test := range tests {
filename := filepath.Join("_fixtures", fmt.Sprintf("%02d.sql", i))
out, args := buildQuery(test.q)
if *writeGoldenFiles {
err := ioutil.WriteFile(filename, []byte(out), 0664)
if err != nil {
t.Fatalf("Failed to write golden file %s: %s\n", filename, err)
}
t.Logf("wrote golden file: %s\n", filename)
continue
}
byt, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("Failed to read golden file %q: %v", filename, err)
}
if string(byt) != out {
t.Errorf("[%02d] Test failed:\nWant:\n%s\nGot:\n%s", i, byt, out)
}
if !reflect.DeepEqual(args, test.args) {
t.Errorf("[%02d] Test failed:\nWant:\n%s\nGot:\n%s", i, spew.Sdump(test.args), spew.Sdump(args))
}
}
}
// func TestApply(t *testing.T) {
// t.Parallel()
//
// q := &boil.Query{}
// qfn1 := Limit(10)
// qfn2 := Where("x > $1 AND y > $2", 5, 3)
//
// q.Apply(qfn1, qfn2)
//
// expect1 := 10
// if q.limit != expect1 {
// t.Errorf("Expected %d, got %d", expect1, q.limit)
// }
//
// expect2 := "x > $1 AND y > $2"
// if len(q.where) != 1 {
// t.Errorf("Expected %d where slices, got %d", len(q.where))
// }
//
// expect := "x > $1 AND y > $2"
// if q.where[0].clause != expect2 {
// t.Errorf("Expected %s, got %s", expect, q.where)
// }
//
// if len(q.where[0].args) != 2 {
// t.Errorf("Expected %d args, got %d", 2, len(q.where[0].args))
// }
//
// if q.where[0].args[0].(int) != 5 || q.where[0].args[1].(int) != 3 {
// t.Errorf("Args not set correctly, expected 5 & 3, got: %#v", q.where[0].args)
// }
// }
//
// func TestLimit(t *testing.T) {
// t.Parallel()
//
// q := &boil.Query{}
// qfn := Limit(10)
//
// qfn(q)
//
// expect := 10
// if q.limit != expect {
// t.Errorf("Expected %d, got %d", expect, q.limit)
// }
// }
//
// func TestWhere(t *testing.T) {
// t.Parallel()
//
// q := &boil.Query{}
// qfn := Where("x > $1 AND y > $2", 5, 3)
//
// qfn(q)
//
// if len(q.where) != 1 {
// t.Errorf("Expected %d where slices, got %d", len(q.where))
// }
//
// expect := "x > $1 AND y > $2"
// if q.where[0].clause != expect {
// t.Errorf("Expected %s, got %s", expect, q.where)
// }
//
// if len(q.where[0].args) != 2 {
// t.Errorf("Expected %d args, got %d", 2, len(q.where[0].args))
// }
//
// if q.where[0].args[0].(int) != 5 || q.where[0].args[1].(int) != 3 {
// t.Errorf("Args not set correctly, expected 5 & 3, got: %#v", q.where[0].args)
// }
// }
//
// func TestGroupBy(t *testing.T) {
// t.Parallel()
//
// q := &boil.Query{}
// qfn := GroupBy("col1, col2")
//
// qfn(q)
//
// expect := "col1, col2"
// if len(q.groupBy) != 1 && q.groupBy[0] != expect {
// t.Errorf("Expected %s, got %s", expect, q.groupBy[0])
// }
// }
//
// func TestOrderBy(t *testing.T) {
// t.Parallel()
//
// q := &boil.Query{}
// qfn := OrderBy("col1 desc, col2 asc")
//
// qfn(q)
//
// expect := "col1 desc, col2 asc"
// if len(q.orderBy) != 1 && q.orderBy[0] != expect {
// t.Errorf("Expected %s, got %s", expect, q.orderBy[0])
// }
// }
//
// func TestHaving(t *testing.T) {
// t.Parallel()
//
// q := &boil.Query{}
// qfn := Having("count(orders.order_id) > 10")
//
// qfn(q)
//
// expect := "count(orders.order_id) > 10"
// if len(q.having) != 1 && q.having[0] != expect {
// t.Errorf("Expected %s, got %s", expect, q.having[0])
// }
// }
//
// func TestFrom(t *testing.T) {
// t.Parallel()
//
// q := &boil.Query{}
// qfn := From("videos a, orders b")
//
// qfn(q)
//
// expect := "videos a, orders b"
// if q.from != expect {
// t.Errorf("Expected %s, got %s", expect, q.from)
// }
// }
// }