switch to more complete null package, implement nicer sql logging
This commit is contained in:
parent
ed423a3606
commit
05c7f7d06a
21 changed files with 291 additions and 85 deletions
|
@ -170,11 +170,12 @@ func newImporter() importer {
|
|||
`"time"`,
|
||||
},
|
||||
thirdParty: importList{
|
||||
`"github.com/pkg/errors"`,
|
||||
`"github.com/lbryio/null.go"`,
|
||||
`"github.com/lbryio/sqlboiler/boil"`,
|
||||
`"github.com/lbryio/sqlboiler/queries"`,
|
||||
`"github.com/lbryio/sqlboiler/queries/qm"`,
|
||||
`"github.com/lbryio/sqlboiler/strmangle"`,
|
||||
`"github.com/pkg/errors"`,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -183,6 +184,10 @@ func newImporter() importer {
|
|||
standard: importList{
|
||||
`"database/sql"`,
|
||||
`"fmt"`,
|
||||
`"reflect"`,
|
||||
`"strconv"`,
|
||||
`"strings"`,
|
||||
`"time"`,
|
||||
},
|
||||
thirdParty: importList{
|
||||
`"github.com/lbryio/sqlboiler/boil"`,
|
||||
|
@ -190,6 +195,7 @@ 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": {
|
||||
|
@ -315,55 +321,55 @@ func newImporter() importer {
|
|||
// TranslateColumnType to see the type assignments.
|
||||
imp.BasedOnType = mapImports{
|
||||
"null.Float32": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Float64": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Int": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Int8": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Int16": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Int32": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Int64": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Uint": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Uint8": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Uint16": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Uint32": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Uint64": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.String": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Bool": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Time": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.JSON": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"null.Bytes": {
|
||||
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
||||
thirdParty: importList{`"github.com/lbryio/null.go"`},
|
||||
},
|
||||
"time.Time": {
|
||||
standard: importList{`"time"`},
|
||||
|
|
|
@ -246,7 +246,7 @@ func TestCombineTypeImports(t *testing.T) {
|
|||
},
|
||||
thirdParty: importList{
|
||||
`"github.com/lbryio/sqlboiler/boil"`,
|
||||
`"gopkg.in/nullbio/null.v6"`,
|
||||
`"github.com/lbryio/null.go"`,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -281,7 +281,7 @@ func TestCombineTypeImports(t *testing.T) {
|
|||
},
|
||||
thirdParty: importList{
|
||||
`"github.com/lbryio/sqlboiler/boil"`,
|
||||
`"gopkg.in/nullbio/null.v6"`,
|
||||
`"github.com/lbryio/null.go"`,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -297,7 +297,7 @@ func TestCombineImports(t *testing.T) {
|
|||
|
||||
a := imports{
|
||||
standard: importList{"fmt"},
|
||||
thirdParty: importList{"github.com/lbryio/sqlboiler", "gopkg.in/nullbio/null.v6"},
|
||||
thirdParty: importList{"github.com/lbryio/sqlboiler", "github.com/lbryio/null.go"},
|
||||
}
|
||||
b := imports{
|
||||
standard: importList{"os"},
|
||||
|
@ -309,8 +309,8 @@ func TestCombineImports(t *testing.T) {
|
|||
if c.standard[0] != "fmt" && c.standard[1] != "os" {
|
||||
t.Errorf("Wanted: fmt, os got: %#v", c.standard)
|
||||
}
|
||||
if c.thirdParty[0] != "github.com/lbryio/sqlboiler" && c.thirdParty[1] != "gopkg.in/nullbio/null.v6" {
|
||||
t.Errorf("Wanted: github.com/lbryio/sqlboiler, gopkg.in/nullbio/null.v6 got: %#v", c.thirdParty)
|
||||
if c.thirdParty[0] != "github.com/lbryio/sqlboiler" && c.thirdParty[1] != "github.com/lbryio/null.go" {
|
||||
t.Errorf("Wanted: github.com/lbryio/sqlboiler, github.com/lbryio/null.go got: %#v", c.thirdParty)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
null "gopkg.in/nullbio/null.v6"
|
||||
null "github.com/lbryio/null.go"
|
||||
)
|
||||
|
||||
type testObj struct {
|
||||
|
|
|
@ -3,8 +3,15 @@ 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
|
||||
|
@ -105,8 +112,11 @@ func RawG(query string, args ...interface{}) *Query {
|
|||
func (q *Query) Exec() (sql.Result, error) {
|
||||
qs, args := buildQuery(q)
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, qs)
|
||||
fmt.Fprintln(boil.DebugWriter, args)
|
||||
qStr, err := interpolateParams(qs, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
return q.executor.Exec(qs, args...)
|
||||
}
|
||||
|
@ -115,8 +125,11 @@ func (q *Query) Exec() (sql.Result, error) {
|
|||
func (q *Query) QueryRow() *sql.Row {
|
||||
qs, args := buildQuery(q)
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, qs)
|
||||
fmt.Fprintln(boil.DebugWriter, args)
|
||||
qStr, err := interpolateParams(qs, args...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
return q.executor.QueryRow(qs, args...)
|
||||
}
|
||||
|
@ -125,8 +138,11 @@ func (q *Query) QueryRow() *sql.Row {
|
|||
func (q *Query) Query() (*sql.Rows, error) {
|
||||
qs, args := buildQuery(q)
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, qs)
|
||||
fmt.Fprintln(boil.DebugWriter, args)
|
||||
qStr, err := interpolateParams(qs, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
return q.executor.Query(qs, args...)
|
||||
}
|
||||
|
@ -280,3 +296,69 @@ 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
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
null "gopkg.in/nullbio/null.v6"
|
||||
null "github.com/lbryio/null.go"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/satori/go.uuid"
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
null "gopkg.in/nullbio/null.v6"
|
||||
null "github.com/lbryio/null.go"
|
||||
)
|
||||
|
||||
func TestRandomizeStruct(t *testing.T) {
|
||||
|
|
|
@ -40,7 +40,11 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula
|
|||
)
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args)
|
||||
qStr, err := interpolateParams(query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
results, err := e.Query(query, args...)
|
||||
|
|
|
@ -40,7 +40,11 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula
|
|||
)
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args)
|
||||
qStr, err := interpolateParams(query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
results, err := e.Query(query, args...)
|
||||
|
|
|
@ -49,7 +49,11 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula
|
|||
{{end -}}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args)
|
||||
qStr, err := interpolateParams(query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
results, err := e.Query(query, args...)
|
||||
|
|
|
@ -53,8 +53,11 @@ 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 {
|
||||
fmt.Fprintln(boil.DebugWriter, updateQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
qStr, err := interpolateParams(updateQuery, values...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
if _, err = exec.Exec(updateQuery, values...); err != nil {
|
||||
|
|
|
@ -59,8 +59,11 @@ 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 {
|
||||
fmt.Fprintln(boil.DebugWriter, updateQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
qStr, err := interpolateParams(updateQuery, values...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
if _, err = exec.Exec(updateQuery, values...); err != nil {
|
||||
|
|
|
@ -66,8 +66,11 @@ 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 {
|
||||
fmt.Fprintln(boil.DebugWriter, updateQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
qStr, err := interpolateParams(updateQuery, values...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
if _, err = exec.Exec(updateQuery, values...); err != nil {
|
||||
|
@ -87,8 +90,11 @@ 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 {
|
||||
fmt.Fprintln(boil.DebugWriter, query)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
qStr, err := interpolateParams(query, values...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
_, err = exec.Exec(query, values...)
|
||||
|
@ -184,8 +190,11 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo
|
|||
values := []interface{}{{"{"}}o.{{$txt.LocalTable.ColumnNameGo}}}
|
||||
{{end -}}
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, query)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
qStr, err := interpolateParams(query, values...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
_, err := exec.Exec(query, values...)
|
||||
|
@ -260,8 +269,11 @@ func (o *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}(exec boil.Exec
|
|||
}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, query)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
qStr, err := interpolateParams(query, values...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
_, err = exec.Exec(query, values...)
|
||||
|
|
|
@ -98,8 +98,11 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
|||
vals := queries.ValuesFromMapping(value, cache.valueMapping)
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, cache.query)
|
||||
fmt.Fprintln(boil.DebugWriter, vals)
|
||||
qStr, err := interpolateParams(cache.query, vals...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
{{if .UseLastInsertID -}}
|
||||
|
@ -144,8 +147,11 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
|||
}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, identifierCols...)
|
||||
qStr, err := interpolateParams(cache.retQuery, identifierCols...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
|
||||
|
|
|
@ -78,8 +78,11 @@ func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string
|
|||
values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping)
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, cache.query)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
qStr, err := interpolateParams(cache.query, values...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
_, err = exec.Exec(cache.query, values...)
|
||||
|
@ -170,8 +173,11 @@ 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 {
|
||||
fmt.Fprintln(boil.DebugWriter, sql)
|
||||
fmt.Fprintln(boil.DebugWriter, args...)
|
||||
qStr, err := interpolateParams(sql, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
_, err := exec.Exec(sql, args...)
|
||||
|
|
|
@ -147,8 +147,11 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if eq .DriverName
|
|||
}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, cache.query)
|
||||
fmt.Fprintln(boil.DebugWriter, vals)
|
||||
qStr, err := interpolateParams(cache.query, vals...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
{{if .UseLastInsertID -}}
|
||||
|
@ -193,8 +196,11 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if eq .DriverName
|
|||
}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, identifierCols...)
|
||||
qStr, err := interpolateParams(cache.retQuery, identifierCols...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(returns...)
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ func (o *{{$tableNameSingular}}) DeleteP(exec boil.Executor) {
|
|||
// DeleteG will match against the primary key column to find the record to delete.
|
||||
func (o *{{$tableNameSingular}}) DeleteG() error {
|
||||
if o == nil {
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} provided for deletion")
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} provided for deletion")
|
||||
}
|
||||
|
||||
return o.Delete(boil.GetDB())
|
||||
|
@ -25,7 +25,7 @@ func (o *{{$tableNameSingular}}) DeleteG() error {
|
|||
// Panics on error.
|
||||
func (o *{{$tableNameSingular}}) DeleteGP() {
|
||||
if err := o.DeleteG(); err != nil {
|
||||
panic(boil.WrapErr(err))
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,12 +33,12 @@ func (o *{{$tableNameSingular}}) DeleteGP() {
|
|||
// Delete will match against the primary key column to find the record to delete.
|
||||
func (o *{{$tableNameSingular}}) Delete(exec boil.Executor) error {
|
||||
if o == nil {
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} provided for delete")
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} provided for delete")
|
||||
}
|
||||
|
||||
{{if not .NoHooks -}}
|
||||
if err := o.doBeforeDeleteHooks(exec); err != nil {
|
||||
return err
|
||||
return err
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
|
@ -46,18 +46,21 @@ 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 {
|
||||
fmt.Fprintln(boil.DebugWriter, sql)
|
||||
fmt.Fprintln(boil.DebugWriter, args...)
|
||||
qStr, err := interpolateParams(sql, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
_, err := exec.Exec(sql, args...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "{{.PkgName}}: unable to delete from {{.Table.Name}}")
|
||||
return errors.Wrap(err, "{{.PkgName}}: unable to delete from {{.Table.Name}}")
|
||||
}
|
||||
|
||||
{{if not .NoHooks -}}
|
||||
if err := o.doAfterDeleteHooks(exec); err != nil {
|
||||
return err
|
||||
return err
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
|
@ -67,21 +70,21 @@ func (o *{{$tableNameSingular}}) Delete(exec boil.Executor) error {
|
|||
// DeleteAllP deletes all rows, and panics on error.
|
||||
func (q {{$tableNameSingular}}Query) DeleteAllP() {
|
||||
if err := q.DeleteAll(); err != nil {
|
||||
panic(boil.WrapErr(err))
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteAll deletes all matching rows.
|
||||
func (q {{$tableNameSingular}}Query) DeleteAll() error {
|
||||
if q.Query == nil {
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}}Query provided for delete all")
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}}Query provided for delete all")
|
||||
}
|
||||
|
||||
queries.SetDelete(q.Query)
|
||||
|
||||
_, err := q.Query.Exec()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "{{.PkgName}}: unable to delete all from {{.Table.Name}}")
|
||||
return errors.Wrap(err, "{{.PkgName}}: unable to delete all from {{.Table.Name}}")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -90,14 +93,14 @@ func (q {{$tableNameSingular}}Query) DeleteAll() error {
|
|||
// DeleteAllGP deletes all rows in the slice, and panics on error.
|
||||
func (o {{$tableNameSingular}}Slice) DeleteAllGP() {
|
||||
if err := o.DeleteAllG(); err != nil {
|
||||
panic(boil.WrapErr(err))
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteAllG deletes all rows in the slice.
|
||||
func (o {{$tableNameSingular}}Slice) DeleteAllG() error {
|
||||
if o == nil {
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} slice provided for delete all")
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} slice provided for delete all")
|
||||
}
|
||||
return o.DeleteAll(boil.GetDB())
|
||||
}
|
||||
|
@ -105,7 +108,7 @@ func (o {{$tableNameSingular}}Slice) DeleteAllG() error {
|
|||
// DeleteAllP deletes all rows in the slice, using an executor, and panics on error.
|
||||
func (o {{$tableNameSingular}}Slice) DeleteAllP(exec boil.Executor) {
|
||||
if err := o.DeleteAll(exec); err != nil {
|
||||
panic(boil.WrapErr(err))
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -139,8 +142,11 @@ 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 {
|
||||
fmt.Fprintln(boil.DebugWriter, sql)
|
||||
fmt.Fprintln(boil.DebugWriter, args)
|
||||
qStr, err := interpolateParams(sql, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
_, err := exec.Exec(sql, args...)
|
||||
|
|
|
@ -14,8 +14,11 @@ func {{$tableNameSingular}}Exists(exec boil.Executor, {{$pkArgs}}) (bool, error)
|
|||
{{- end}}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, sql)
|
||||
fmt.Fprintln(boil.DebugWriter, {{$pkNames | join ", "}})
|
||||
qStr, err := interpolateParams(sql, {{$pkNames | join ", "}})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
fmt.Fprintln(boil.DebugWriter, qStr)
|
||||
}
|
||||
|
||||
row := exec.QueryRow(sql, {{$pkNames | join ", "}})
|
||||
|
|
|
@ -16,7 +16,7 @@ func (q *{{$tableNameSingular}}Query) Where(filters {{$tableNameSingular}}Filter
|
|||
for i := 0; i < r.NumField(); i++ {
|
||||
f := r.Field(i)
|
||||
if f.Elem().IsValid() {
|
||||
if nullable, ok := f.Elem().Interface().(Nullable); ok && nullable.IsZero() {
|
||||
if nullable, ok := f.Elem().Interface().(null.Nullable); ok && nullable.IsNull() {
|
||||
queries.AppendWhere(q.Query, r.Type().Field(i).Tag.Get("boil")+" IS NULL")
|
||||
} else {
|
||||
queries.AppendWhere(q.Query, r.Type().Field(i).Tag.Get("boil")+" = ?", f.Elem().Interface())
|
||||
|
|
|
@ -72,7 +72,7 @@ func Merge{{$tableNamePlural}}(exec boil.Executor, primaryID uint64, secondaryID
|
|||
pf := pr.Elem().Field(i)
|
||||
sf := sr.Elem().Field(i)
|
||||
if sf.IsValid() {
|
||||
if nullable, ok := sf.Interface().(Nullable); ok && !nullable.IsZero() && pf.Interface().(Nullable).IsZero() {
|
||||
if nullable, ok := sf.Interface().(null.Nullable); ok && !nullable.IsNull() && pf.Interface().(null.Nullable).IsNull() {
|
||||
pf.Set(sf)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -136,3 +136,69 @@ func checkMerge(tx *sql.Tx, 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
|
||||
}
|
||||
|
|
|
@ -1,11 +1,6 @@
|
|||
// M type is for providing columns and column values to UpdateAll.
|
||||
type M map[string]interface{}
|
||||
|
||||
// Nullable means the value may represent an sql NULL. It is implemented by null.* types.
|
||||
type Nullable interface {
|
||||
IsZero() bool
|
||||
}
|
||||
|
||||
// foreignKey connects two tables. When merging records, foreign keys from secondary record must
|
||||
// be reassigned to primary record.
|
||||
type foreignKey struct {
|
||||
|
|
Loading…
Reference in a new issue