From 09cdb7a652900eb042ef8e1d2f31acf6eedf065d Mon Sep 17 00:00:00 2001 From: Patrick O'brien Date: Sun, 28 Aug 2016 14:51:03 +1000 Subject: [PATCH] Fix SQL (takes exec now) * Add SQLG * Update templates using SQL --- boil/query.go | 8 +++++++- boil/query_test.go | 14 +++++++++++++- templates/09_find.tpl | 3 +-- templates/14_reload.tpl | 3 +-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/boil/query.go b/boil/query.go index 1b7201c..f179abd 100644 --- a/boil/query.go +++ b/boil/query.go @@ -65,8 +65,9 @@ type join struct { } // 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{ + executor: exec, plainSQL: plainSQL{ sql: query, 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 func ExecQuery(q *Query) (sql.Result, error) { qs, args := buildQuery(q) diff --git a/boil/query_test.go b/boil/query_test.go index 8c75936..f618189 100644 --- a/boil/query_test.go +++ b/boil/query_test.go @@ -362,7 +362,19 @@ func TestAppendSelect(t *testing.T) { func TestSQL(t *testing.T) { 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" { t.Errorf("Expected %q, got %s", "thing", q.plainSQL.sql) } diff --git a/templates/09_find.tpl b/templates/09_find.tpl index 6db4650..a3c7fcf 100644 --- a/templates/09_find.tpl +++ b/templates/09_find.tpl @@ -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, ) - q := boil.SQL(query, {{$pkNames | join ", "}}) - boil.SetExecutor(q, exec) + q := boil.SQL(exec, query, {{$pkNames | join ", "}}) err := q.BindFast({{$varNameSingular}}Obj, {{$varNameSingular}}TitleCases) if err != nil { diff --git a/templates/14_reload.tpl b/templates/14_reload.tpl index ded548d..c38c2aa 100644 --- a/templates/14_reload.tpl +++ b/templates/14_reload.tpl @@ -72,8 +72,7 @@ func (o *{{$tableNameSingular}}Slice) ReloadAll(exec boil.Executor) error { strmangle.Placeholders(len(*o) * len({{$varNameSingular}}PrimaryKeyColumns), 1, len({{$varNameSingular}}PrimaryKeyColumns)), ) - q := boil.SQL(sql, args...) - boil.SetExecutor(q, exec) + q := boil.SQL(exec, sql, args...) err := q.BindFast(&{{$varNamePlural}}, {{$varNameSingular}}TitleCases) if err != nil {