diff --git a/boil/_fixtures/03.sql b/boil/_fixtures/03.sql index 604e276..653a40a 100644 --- a/boil/_fixtures/03.sql +++ b/boil/_fixtures/03.sql @@ -1 +1 @@ -SELECT "count(*) as ab, thing as bd","stuff" FROM "t"; \ No newline at end of file +SELECT count(*) as ab, thing as bd, "stuff" FROM "t"; \ No newline at end of file diff --git a/boil/query.go b/boil/query.go index 72c42f1..9b8689b 100644 --- a/boil/query.go +++ b/boil/query.go @@ -5,6 +5,8 @@ import ( "database/sql" "fmt" "strings" + + "github.com/nullbio/sqlboiler/strmangle" ) type where struct { @@ -71,7 +73,7 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) { buf.WriteString("COUNT(") } if len(q.selectCols) > 0 { - buf.WriteString(`"` + strings.Join(q.selectCols, `","`) + `"`) + buf.WriteString(strings.Join(q.selectCols, `, `)) } else { buf.WriteByte('*') } @@ -177,6 +179,10 @@ func SetExecutor(q *Query, exec Executor) { // SetSelect on the query. func SetSelect(q *Query, columns ...string) { + for i := 0; i < len(columns); i++ { + columns[i] = strmangle.IdentQuote(columns[i]) + } + q.selectCols = append(q.selectCols, columns...) } diff --git a/boil/query_test.go b/boil/query_test.go index 53e5e99..587bc66 100644 --- a/boil/query_test.go +++ b/boil/query_test.go @@ -29,7 +29,7 @@ func TestBuildQuery(t *testing.T) { {&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: "t", selectCols: []string{"count(*) as ab, thing as bd", `"stuff"`}}, nil}, } for i, test := range tests { @@ -252,7 +252,7 @@ func TestSetSelect(t *testing.T) { t.Errorf("Expected selectCols len 2, got %d", len(q.selectCols)) } - if q.selectCols[0] != "col1" && q.selectCols[1] != "col2" { + if q.selectCols[0] != `"col1"` && q.selectCols[1] != `"col2"` { t.Errorf("select cols value mismatch: %#v", q.selectCols) } } diff --git a/strmangle/strmangle.go b/strmangle/strmangle.go index 55abd65..25bbb25 100644 --- a/strmangle/strmangle.go +++ b/strmangle/strmangle.go @@ -19,8 +19,8 @@ var ( smartQuoteRgx = regexp.MustCompile(`^(?i)"?[a-z_][_a-z0-9]*"?(\."?[_a-z][_a-z0-9]*"?)*$`) ) -// SmartQuote intelligently quotes identifiers in sql statements -func SmartQuote(s string) string { +// IdentQuote attempts to quote simple identifiers in SQL tatements +func IdentQuote(s string) string { if s == "null" { return s } diff --git a/strmangle/strmangle_test.go b/strmangle/strmangle_test.go index a379f2b..fb1e034 100644 --- a/strmangle/strmangle_test.go +++ b/strmangle/strmangle_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -func TestSmartQuote(t *testing.T) { +func TestIdentQuote(t *testing.T) { t.Parallel() tests := []struct { @@ -16,14 +16,16 @@ func TestSmartQuote(t *testing.T) { {In: `null`, Out: `null`}, {In: `"thing"`, Out: `"thing"`}, {In: `*`, Out: `*`}, + {In: ``, Out: ``}, {In: `thing.thing`, Out: `"thing"."thing"`}, {In: `"thing"."thing"`, Out: `"thing"."thing"`}, {In: `thing.thing.thing.thing`, Out: `"thing"."thing"."thing"."thing"`}, {In: `thing."thing".thing."thing"`, Out: `"thing"."thing"."thing"."thing"`}, + {In: `count(*) as ab, thing as bd`, Out: `count(*) as ab, thing as bd`}, } for _, test := range tests { - if got := SmartQuote(test.In); got != test.Out { + if got := IdentQuote(test.In); got != test.Out { t.Errorf("want: %s, got: %s", test.Out, got) } }