Add multiple from statements.

This commit is contained in:
Aaron L 2016-08-03 21:50:26 -07:00
parent 01124d9d0b
commit 48e14f4fd8
3 changed files with 15 additions and 18 deletions

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

@ -0,0 +1 @@
SELECT count(*) as ab, thing as bd, "stuff" FROM "a","b";

View file

@ -33,7 +33,7 @@ type Query struct {
update map[string]interface{}
selectCols []string
count bool
from string
from []string
innerJoins []join
outerJoins []join
leftOuterJoins []join
@ -73,12 +73,7 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
buf.WriteString("COUNT(")
}
if len(q.selectCols) > 0 {
cols := make([]string, len(q.selectCols))
copy(cols, q.selectCols)
for i := 0; i < len(cols); i++ {
cols[i] = strmangle.IdentQuote(cols[i])
}
buf.WriteString(strings.Join(q.selectCols, `, `))
buf.WriteString(strings.Join(strmangle.IdentQuoteSlice(q.selectCols), `, `))
} else {
buf.WriteByte('*')
}
@ -88,7 +83,7 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
}
buf.WriteString(" FROM ")
fmt.Fprintf(buf, `"%s"`, q.from)
buf.WriteString(strings.Join(strmangle.IdentQuoteSlice(q.from), ","))
where, args := whereClause(q)
buf.WriteString(where)
@ -198,8 +193,8 @@ func Select(q *Query) []string {
}
// SetFrom on the query.
func SetFrom(q *Query, from string) {
q.from = from
func SetFrom(q *Query, from ...string) {
q.from = append(q.from, from...)
}
// SetInnerJoin on the query.

View file

@ -26,10 +26,11 @@ func TestBuildQuery(t *testing.T) {
q *Query
args []interface{}
}{
{&Query{from: "t"}, nil},
{&Query{from: "q", limit: 5, offset: 6}, nil},
{&Query{from: "q", orderBy: []string{"a ASC", "b DESC"}}, nil},
{&Query{from: "t", selectCols: []string{"count(*) as ab, thing as bd", `"stuff"`}}, nil},
{&Query{from: []string{"t"}}, nil},
{&Query{from: []string{"q"}, limit: 5, offset: 6}, nil},
{&Query{from: []string{"q"}, orderBy: []string{"a ASC", "b DESC"}}, nil},
{&Query{from: []string{"t"}, selectCols: []string{"count(*) as ab, thing as bd", `"stuff"`}}, nil},
{&Query{from: []string{"a", "b"}, selectCols: []string{"count(*) as ab, thing as bd", `"stuff"`}}, nil},
}
for i, test := range tests {
@ -192,14 +193,14 @@ func TestSetHaving(t *testing.T) {
}
}
func TestSetTable(t *testing.T) {
func TestSetFrom(t *testing.T) {
t.Parallel()
q := &Query{}
SetFrom(q, "videos a, orders b")
SetFrom(q, "videos a", "orders b")
expect := "videos a, orders b"
if q.from != expect {
expect := []string{"videos a", "orders b"}
if !reflect.DeepEqual(q.from, expect) {
t.Errorf("Expected %s, got %s", expect, q.from)
}
}