Fix SQL (takes exec now)

* Add SQLG
* Update templates using SQL
This commit is contained in:
Patrick O'brien 2016-08-28 14:51:03 +10:00
parent ed85ea5fec
commit 09cdb7a652
4 changed files with 22 additions and 6 deletions

View file

@ -65,8 +65,9 @@ type join struct {
} }
// SQL makes a plainSQL query, usually for use with bind // SQL makes a plainSQL query, usually for use with bind
func SQL(query string, args ...interface{}) *Query { func SQL(exec Executor, query string, args ...interface{}) *Query {
return &Query{ return &Query{
executor: exec,
plainSQL: plainSQL{ plainSQL: plainSQL{
sql: query, sql: query,
args: args, args: args,
@ -74,6 +75,11 @@ func SQL(query string, args ...interface{}) *Query {
} }
} }
// SQLG makes a plainSQL query using the global Executor, usually for use with bind
func SQLG(query string, args ...interface{}) *Query {
return SQL(GetDB(), query, args...)
}
// ExecQuery executes a query that does not need a row returned // ExecQuery executes a query that does not need a row returned
func ExecQuery(q *Query) (sql.Result, error) { func ExecQuery(q *Query) (sql.Result, error) {
qs, args := buildQuery(q) qs, args := buildQuery(q)

View file

@ -362,7 +362,19 @@ func TestAppendSelect(t *testing.T) {
func TestSQL(t *testing.T) { func TestSQL(t *testing.T) {
t.Parallel() t.Parallel()
q := SQL("thing", 5) q := SQL(&sql.DB{}, "thing", 5)
if q.plainSQL.sql != "thing" {
t.Errorf("Expected %q, got %s", "thing", q.plainSQL.sql)
}
if q.plainSQL.args[0].(int) != 5 {
t.Errorf("Expected 5, got %v", q.plainSQL.args[0])
}
}
func TestSQLG(t *testing.T) {
t.Parallel()
q := SQLG("thing", 5)
if q.plainSQL.sql != "thing" { if q.plainSQL.sql != "thing" {
t.Errorf("Expected %q, got %s", "thing", q.plainSQL.sql) t.Errorf("Expected %q, got %s", "thing", q.plainSQL.sql)
} }

View file

@ -32,8 +32,7 @@ func {{$tableNameSingular}}Find(exec boil.Executor, {{$pkArgs}}, selectCols ...s
`select %s from "{{.Table.Name}}" where {{whereClause 1 .Table.PKey.Columns}}`, sel, `select %s from "{{.Table.Name}}" where {{whereClause 1 .Table.PKey.Columns}}`, sel,
) )
q := boil.SQL(query, {{$pkNames | join ", "}}) q := boil.SQL(exec, query, {{$pkNames | join ", "}})
boil.SetExecutor(q, exec)
err := q.BindFast({{$varNameSingular}}Obj, {{$varNameSingular}}TitleCases) err := q.BindFast({{$varNameSingular}}Obj, {{$varNameSingular}}TitleCases)
if err != nil { if err != nil {

View file

@ -72,8 +72,7 @@ func (o *{{$tableNameSingular}}Slice) ReloadAll(exec boil.Executor) error {
strmangle.Placeholders(len(*o) * len({{$varNameSingular}}PrimaryKeyColumns), 1, len({{$varNameSingular}}PrimaryKeyColumns)), strmangle.Placeholders(len(*o) * len({{$varNameSingular}}PrimaryKeyColumns), 1, len({{$varNameSingular}}PrimaryKeyColumns)),
) )
q := boil.SQL(sql, args...) q := boil.SQL(exec, sql, args...)
boil.SetExecutor(q, exec)
err := q.BindFast(&{{$varNamePlural}}, {{$varNameSingular}}TitleCases) err := q.BindFast(&{{$varNamePlural}}, {{$varNameSingular}}TitleCases)
if err != nil { if err != nil {