diff --git a/boilingcore/imports.go b/boilingcore/imports.go index 528a946..13ed119 100644 --- a/boilingcore/imports.go +++ b/boilingcore/imports.go @@ -183,10 +183,6 @@ func newImporter() importer { "boil_queries": imports{ standard: importList{ `"fmt"`, - `"reflect"`, - `"strconv"`, - `"strings"`, - `"time"`, }, thirdParty: importList{ `"github.com/lbryio/sqlboiler/boil"`, @@ -194,7 +190,6 @@ func newImporter() importer { `"github.com/lbryio/sqlboiler/queries/qm"`, `"github.com/lbryio/sqlboiler/strmangle"`, `"github.com/pkg/errors"`, - `"github.com/lbryio/null.go"`, }, }, "boil_types": { diff --git a/queries/query.go b/queries/query.go index b3ff830..be539e5 100644 --- a/queries/query.go +++ b/queries/query.go @@ -3,15 +3,8 @@ package queries import ( "database/sql" "fmt" - "reflect" - "strconv" - "strings" - "time" - "github.com/lbryio/null.go" "github.com/lbryio/sqlboiler/boil" - - "github.com/go-errors/errors" ) // joinKind is the type of join @@ -112,11 +105,8 @@ func RawG(query string, args ...interface{}) *Query { func (q *Query) Exec() (sql.Result, error) { qs, args := buildQuery(q) if boil.DebugMode { - qStr, err := interpolateParams(qs, args...) - if err != nil { - return nil, err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, qs) + fmt.Fprintln(boil.DebugWriter, args) } return q.executor.Exec(qs, args...) } @@ -125,11 +115,8 @@ func (q *Query) Exec() (sql.Result, error) { func (q *Query) QueryRow() *sql.Row { qs, args := buildQuery(q) if boil.DebugMode { - qStr, err := interpolateParams(qs, args...) - if err != nil { - panic(err) - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, qs) + fmt.Fprintln(boil.DebugWriter, args) } return q.executor.QueryRow(qs, args...) } @@ -138,11 +125,8 @@ func (q *Query) QueryRow() *sql.Row { func (q *Query) Query() (*sql.Rows, error) { qs, args := buildQuery(q) if boil.DebugMode { - qStr, err := interpolateParams(qs, args...) - if err != nil { - return nil, err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, qs) + fmt.Fprintln(boil.DebugWriter, args) } return q.executor.Query(qs, args...) } @@ -296,69 +280,3 @@ func AppendGroupBy(q *Query, clause string) { func AppendOrderBy(q *Query, clause string) { q.orderBy = append(q.orderBy, clause) } - -// duplicated in boil_queries.tpl -func interpolateParams(query string, args ...interface{}) (string, error) { - for i := 0; i < len(args); i++ { - field := reflect.ValueOf(args[i]) - - if value, ok := field.Interface().(time.Time); ok { - query = strings.Replace(query, "?", `"`+value.Format("2006-01-02 15:04:05")+`"`, 1) - } else if nullable, ok := field.Interface().(null.Nullable); ok { - if nullable.IsNull() { - query = strings.Replace(query, "?", "NULL", 1) - } else { - switch field.Type() { - case reflect.TypeOf(null.Time{}): - query = strings.Replace(query, "?", `"`+field.Interface().(null.Time).Time.Format("2006-01-02 15:04:05")+`"`, 1) - case reflect.TypeOf(null.Int{}): - query = strings.Replace(query, "?", strconv.FormatInt(int64(field.Interface().(null.Int).Int), 10), 1) - case reflect.TypeOf(null.Int8{}): - query = strings.Replace(query, "?", strconv.FormatInt(int64(field.Interface().(null.Int8).Int8), 10), 1) - case reflect.TypeOf(null.Int16{}): - query = strings.Replace(query, "?", strconv.FormatInt(int64(field.Interface().(null.Int16).Int16), 10), 1) - case reflect.TypeOf(null.Int32{}): - query = strings.Replace(query, "?", strconv.FormatInt(int64(field.Interface().(null.Int32).Int32), 10), 1) - case reflect.TypeOf(null.Int64{}): - query = strings.Replace(query, "?", strconv.FormatInt(field.Interface().(null.Int64).Int64, 10), 1) - case reflect.TypeOf(null.Uint{}): - query = strings.Replace(query, "?", strconv.FormatUint(uint64(field.Interface().(null.Uint).Uint), 10), 1) - case reflect.TypeOf(null.Uint8{}): - query = strings.Replace(query, "?", strconv.FormatUint(uint64(field.Interface().(null.Uint8).Uint8), 10), 1) - case reflect.TypeOf(null.Uint16{}): - query = strings.Replace(query, "?", strconv.FormatUint(uint64(field.Interface().(null.Uint16).Uint16), 10), 1) - case reflect.TypeOf(null.Uint32{}): - query = strings.Replace(query, "?", strconv.FormatUint(uint64(field.Interface().(null.Uint32).Uint32), 10), 1) - case reflect.TypeOf(null.Uint64{}): - query = strings.Replace(query, "?", strconv.FormatUint(field.Interface().(null.Uint64).Uint64, 10), 1) - case reflect.TypeOf(null.String{}): - query = strings.Replace(query, "?", `"`+field.Interface().(null.String).String+`"`, 1) - case reflect.TypeOf(null.Bool{}): - if field.Interface().(null.Bool).Bool { - query = strings.Replace(query, "?", "1", 1) - } else { - query = strings.Replace(query, "?", "0", 1) - } - } - } - } else { - switch field.Kind() { - case reflect.Bool: - boolString := "0" - if field.Bool() { - boolString = "1" - } - query = strings.Replace(query, "?", boolString, 1) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - query = strings.Replace(query, "?", strconv.FormatInt(field.Int(), 10), 1) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - query = strings.Replace(query, "?", strconv.FormatUint(field.Uint(), 10), 1) - case reflect.String: - query = strings.Replace(query, "?", `"`+field.String()+`"`, 1) - default: - return "", errors.New("Dont know how to interpolate type " + field.Type().String()) - } - } - } - return query, nil -} diff --git a/templates/07_relationship_to_one_eager.tpl b/templates/07_relationship_to_one_eager.tpl index 28010fd..3b24f1a 100644 --- a/templates/07_relationship_to_one_eager.tpl +++ b/templates/07_relationship_to_one_eager.tpl @@ -40,11 +40,7 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula ) if boil.DebugMode { - qStr, err := InterpolateParams(query, args...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args) } results, err := e.Query(query, args...) diff --git a/templates/08_relationship_one_to_one_eager.tpl b/templates/08_relationship_one_to_one_eager.tpl index 261b30c..37d1af0 100644 --- a/templates/08_relationship_one_to_one_eager.tpl +++ b/templates/08_relationship_one_to_one_eager.tpl @@ -40,11 +40,7 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula ) if boil.DebugMode { - qStr, err := InterpolateParams(query, args...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args) } results, err := e.Query(query, args...) diff --git a/templates/09_relationship_to_many_eager.tpl b/templates/09_relationship_to_many_eager.tpl index 8c4159b..1603188 100644 --- a/templates/09_relationship_to_many_eager.tpl +++ b/templates/09_relationship_to_many_eager.tpl @@ -49,11 +49,7 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula {{end -}} if boil.DebugMode { - qStr, err := InterpolateParams(query, args...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args) } results, err := e.Query(query, args...) diff --git a/templates/10_relationship_to_one_setops.tpl b/templates/10_relationship_to_one_setops.tpl index 8362420..14f726e 100644 --- a/templates/10_relationship_to_one_setops.tpl +++ b/templates/10_relationship_to_one_setops.tpl @@ -53,11 +53,8 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo values := []interface{}{related.{{$txt.ForeignTable.ColumnNameGo}}, o.{{$dot.Table.PKey.Columns | stringMap $dot.StringFuncs.titleCase | join ", o."}}{{"}"}} if boil.DebugMode { - qStr, err := InterpolateParams(updateQuery, values...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, updateQuery) + fmt.Fprintln(boil.DebugWriter, values) } if _, err = exec.Exec(updateQuery, values...); err != nil { diff --git a/templates/11_relationship_one_to_one_setops.tpl b/templates/11_relationship_one_to_one_setops.tpl index 7648e23..826eec2 100644 --- a/templates/11_relationship_one_to_one_setops.tpl +++ b/templates/11_relationship_one_to_one_setops.tpl @@ -59,11 +59,8 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo values := []interface{}{o.{{$txt.LocalTable.ColumnNameGo}}, related.{{$foreignPKeyCols | stringMap $dot.StringFuncs.titleCase | join ", related."}}{{"}"}} if boil.DebugMode { - qStr, err := InterpolateParams(updateQuery, values...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, updateQuery) + fmt.Fprintln(boil.DebugWriter, values) } if _, err = exec.Exec(updateQuery, values...); err != nil { diff --git a/templates/12_relationship_to_many_setops.tpl b/templates/12_relationship_to_many_setops.tpl index 6794889..06159ee 100644 --- a/templates/12_relationship_to_many_setops.tpl +++ b/templates/12_relationship_to_many_setops.tpl @@ -66,11 +66,8 @@ func (o *{{$txt.LocalTable.NameGo}}) Add{{$txt.Function.Name}}(exec boil.Executo values := []interface{}{o.{{$txt.LocalTable.ColumnNameGo}}, rel.{{$foreignPKeyCols | stringMap $dot.StringFuncs.titleCase | join ", rel."}}{{"}"}} if boil.DebugMode { - qStr, err := InterpolateParams(updateQuery, values...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, updateQuery) + fmt.Fprintln(boil.DebugWriter, values) } if _, err = exec.Exec(updateQuery, values...); err != nil { @@ -90,11 +87,8 @@ func (o *{{$txt.LocalTable.NameGo}}) Add{{$txt.Function.Name}}(exec boil.Executo values := []interface{}{{"{"}}o.{{$txt.LocalTable.ColumnNameGo}}, rel.{{$txt.ForeignTable.ColumnNameGo}}} if boil.DebugMode { - qStr, err := InterpolateParams(query, values...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, query) + fmt.Fprintln(boil.DebugWriter, values) } _, err = exec.Exec(query, values...) @@ -190,11 +184,8 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo values := []interface{}{{"{"}}o.{{$txt.LocalTable.ColumnNameGo}}} {{end -}} if boil.DebugMode { - qStr, err := InterpolateParams(query, values...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, query) + fmt.Fprintln(boil.DebugWriter, values) } _, err := exec.Exec(query, values...) @@ -269,11 +260,8 @@ func (o *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}(exec boil.Exec } if boil.DebugMode { - qStr, err := InterpolateParams(query, values...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, query) + fmt.Fprintln(boil.DebugWriter, values) } _, err = exec.Exec(query, values...) diff --git a/templates/15_insert.tpl b/templates/15_insert.tpl index 00b6f5b..870e20d 100644 --- a/templates/15_insert.tpl +++ b/templates/15_insert.tpl @@ -98,11 +98,8 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string vals := queries.ValuesFromMapping(value, cache.valueMapping) if boil.DebugMode { - qStr, err := InterpolateParams(cache.query, vals...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, cache.query) + fmt.Fprintln(boil.DebugWriter, vals) } {{if .UseLastInsertID -}} @@ -147,11 +144,8 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string } if boil.DebugMode { - qStr, err := InterpolateParams(cache.retQuery, identifierCols...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, cache.retQuery) + fmt.Fprintln(boil.DebugWriter, identifierCols...) } err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...) diff --git a/templates/16_update.tpl b/templates/16_update.tpl index 1f67234..bc4a6f5 100644 --- a/templates/16_update.tpl +++ b/templates/16_update.tpl @@ -78,11 +78,8 @@ func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping) if boil.DebugMode { - qStr, err := InterpolateParams(cache.query, values...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, cache.query) + fmt.Fprintln(boil.DebugWriter, values) } _, err = exec.Exec(cache.query, values...) @@ -173,11 +170,8 @@ func (o {{$tableNameSingular}}Slice) UpdateAll(exec boil.Executor, cols M) error strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), {{if .Dialect.IndexPlaceholders}}len(colNames)+1{{else}}0{{end}}, {{$varNameSingular}}PrimaryKeyColumns, len(o))) if boil.DebugMode { - qStr, err := InterpolateParams(sql, args...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, sql) + fmt.Fprintln(boil.DebugWriter, args...) } _, err := exec.Exec(sql, args...) diff --git a/templates/17_upsert.tpl b/templates/17_upsert.tpl index 6eef9fe..dbce58c 100644 --- a/templates/17_upsert.tpl +++ b/templates/17_upsert.tpl @@ -147,11 +147,8 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if eq .DriverName } if boil.DebugMode { - qStr, err := InterpolateParams(cache.query, vals...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, cache.query) + fmt.Fprintln(boil.DebugWriter, vals) } {{if .UseLastInsertID -}} @@ -196,11 +193,8 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if eq .DriverName } if boil.DebugMode { - qStr, err := InterpolateParams(cache.retQuery, identifierCols...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, cache.retQuery) + fmt.Fprintln(boil.DebugWriter, identifierCols...) } err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(returns...) diff --git a/templates/18_delete.tpl b/templates/18_delete.tpl index 8c63e43..aea163e 100644 --- a/templates/18_delete.tpl +++ b/templates/18_delete.tpl @@ -6,7 +6,7 @@ // Panics on error. func (o *{{$tableNameSingular}}) DeleteP(exec boil.Executor) { if err := o.Delete(exec); err != nil { - panic(boil.WrapErr(err)) + panic(boil.WrapErr(err)) } } @@ -46,11 +46,8 @@ func (o *{{$tableNameSingular}}) Delete(exec boil.Executor) error { sql := "DELETE FROM {{$schemaTable}} WHERE {{if .Dialect.IndexPlaceholders}}{{whereClause .LQ .RQ 1 .Table.PKey.Columns}}{{else}}{{whereClause .LQ .RQ 0 .Table.PKey.Columns}}{{end}}" if boil.DebugMode { - qStr, err := InterpolateParams(sql, args...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, sql) + fmt.Fprintln(boil.DebugWriter, args...) } _, err := exec.Exec(sql, args...) @@ -142,11 +139,8 @@ func (o {{$tableNameSingular}}Slice) DeleteAll(exec boil.Executor) error { strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), {{if .Dialect.IndexPlaceholders}}1{{else}}0{{end}}, {{$varNameSingular}}PrimaryKeyColumns, len(o)) if boil.DebugMode { - qStr, err := InterpolateParams(sql, args...) - if err != nil { - return err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, sql) + fmt.Fprintln(boil.DebugWriter, args) } _, err := exec.Exec(sql, args...) diff --git a/templates/20_exists.tpl b/templates/20_exists.tpl index 7d3dc3d..2e20fa6 100644 --- a/templates/20_exists.tpl +++ b/templates/20_exists.tpl @@ -14,11 +14,8 @@ func {{$tableNameSingular}}Exists(exec boil.Executor, {{$pkArgs}}) (bool, error) {{- end}} if boil.DebugMode { - qStr, err := InterpolateParams(sql, {{$pkNames | join ", "}}) - if err != nil { - return false, err - } - fmt.Fprintln(boil.DebugWriter, qStr) + fmt.Fprintln(boil.DebugWriter, sql) + fmt.Fprintln(boil.DebugWriter, {{$pkNames | join ", "}}) } row := exec.QueryRow(sql, {{$pkNames | join ", "}}) diff --git a/templates/singleton/boil_queries.tpl b/templates/singleton/boil_queries.tpl index 89b0e74..1260dde 100644 --- a/templates/singleton/boil_queries.tpl +++ b/templates/singleton/boil_queries.tpl @@ -136,69 +136,3 @@ func checkMerge(tx boil.Executor, foreignKeys []foreignKey) error { return nil } - -// duplicated in queries/query.go -func InterpolateParams(query string, args ...interface{}) (string, error) { - for i := 0; i < len(args); i++ { - field := reflect.ValueOf(args[i]) - - if value, ok := field.Interface().(time.Time); ok { - query = strings.Replace(query, "?", `"`+value.Format("2006-01-02 15:04:05")+`"`, 1) - } else if nullable, ok := field.Interface().(null.Nullable); ok { - if nullable.IsNull() { - query = strings.Replace(query, "?", "NULL", 1) - } else { - switch field.Type() { - case reflect.TypeOf(null.Time{}): - query = strings.Replace(query, "?", `"`+field.Interface().(null.Time).Time.Format("2006-01-02 15:04:05")+`"`, 1) - case reflect.TypeOf(null.Int{}): - query = strings.Replace(query, "?", strconv.FormatInt(int64(field.Interface().(null.Int).Int), 10), 1) - case reflect.TypeOf(null.Int8{}): - query = strings.Replace(query, "?", strconv.FormatInt(int64(field.Interface().(null.Int8).Int8), 10), 1) - case reflect.TypeOf(null.Int16{}): - query = strings.Replace(query, "?", strconv.FormatInt(int64(field.Interface().(null.Int16).Int16), 10), 1) - case reflect.TypeOf(null.Int32{}): - query = strings.Replace(query, "?", strconv.FormatInt(int64(field.Interface().(null.Int32).Int32), 10), 1) - case reflect.TypeOf(null.Int64{}): - query = strings.Replace(query, "?", strconv.FormatInt(field.Interface().(null.Int64).Int64, 10), 1) - case reflect.TypeOf(null.Uint{}): - query = strings.Replace(query, "?", strconv.FormatUint(uint64(field.Interface().(null.Uint).Uint), 10), 1) - case reflect.TypeOf(null.Uint8{}): - query = strings.Replace(query, "?", strconv.FormatUint(uint64(field.Interface().(null.Uint8).Uint8), 10), 1) - case reflect.TypeOf(null.Uint16{}): - query = strings.Replace(query, "?", strconv.FormatUint(uint64(field.Interface().(null.Uint16).Uint16), 10), 1) - case reflect.TypeOf(null.Uint32{}): - query = strings.Replace(query, "?", strconv.FormatUint(uint64(field.Interface().(null.Uint32).Uint32), 10), 1) - case reflect.TypeOf(null.Uint64{}): - query = strings.Replace(query, "?", strconv.FormatUint(field.Interface().(null.Uint64).Uint64, 10), 1) - case reflect.TypeOf(null.String{}): - query = strings.Replace(query, "?", `"`+field.Interface().(null.String).String+`"`, 1) - case reflect.TypeOf(null.Bool{}): - if field.Interface().(null.Bool).Bool { - query = strings.Replace(query, "?", "1", 1) - } else { - query = strings.Replace(query, "?", "0", 1) - } - } - } - } else { - switch field.Kind() { - case reflect.Bool: - boolString := "0" - if field.Bool() { - boolString = "1" - } - query = strings.Replace(query, "?", boolString, 1) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - query = strings.Replace(query, "?", strconv.FormatInt(field.Int(), 10), 1) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - query = strings.Replace(query, "?", strconv.FormatUint(field.Uint(), 10), 1) - case reflect.String: - query = strings.Replace(query, "?", `"`+field.String()+`"`, 1) - default: - return "", errors.New("Dont know how to interpolate type " + field.Type().String()) - } - } - } - return query, nil -}