Fix quoting throughout templates

- Use "" style strings for all templates
- Attach a Quote and SchemaTable that understand escaped quotes so we
  can use "" style strings without repercussion.
- Make SchemaTable use escaped quotes
- Remove schemaTable from the templates in favor of .SchemaTable
This commit is contained in:
Aaron L 2016-09-11 22:30:25 -07:00
parent d183ec4bb5
commit 8392a4ba2a
17 changed files with 107 additions and 49 deletions

View file

@ -16,6 +16,7 @@ import (
"github.com/vattle/sqlboiler/bdb" "github.com/vattle/sqlboiler/bdb"
"github.com/vattle/sqlboiler/bdb/drivers" "github.com/vattle/sqlboiler/bdb/drivers"
"github.com/vattle/sqlboiler/boil" "github.com/vattle/sqlboiler/boil"
"github.com/vattle/sqlboiler/strmangle"
) )
const ( const (
@ -103,6 +104,8 @@ func (s *State) Run(includeTests bool) error {
NoHooks: s.Config.NoHooks, NoHooks: s.Config.NoHooks,
NoAutoTimestamps: s.Config.NoAutoTimestamps, NoAutoTimestamps: s.Config.NoAutoTimestamps,
Dialect: s.Dialect, Dialect: s.Dialect,
LQ: strmangle.QuoteCharacter(s.Dialect.LQ),
RQ: strmangle.QuoteCharacter(s.Dialect.RQ),
StringFuncs: templateStringMappers, StringFuncs: templateStringMappers,
} }
@ -137,6 +140,8 @@ func (s *State) Run(includeTests bool) error {
NoAutoTimestamps: s.Config.NoAutoTimestamps, NoAutoTimestamps: s.Config.NoAutoTimestamps,
Tags: s.Config.Tags, Tags: s.Config.Tags,
Dialect: s.Dialect, Dialect: s.Dialect,
LQ: strmangle.QuoteCharacter(s.Dialect.LQ),
RQ: strmangle.QuoteCharacter(s.Dialect.RQ),
StringFuncs: templateStringMappers, StringFuncs: templateStringMappers,
} }

View file

@ -38,12 +38,12 @@ func init() {
// using a database that supports real schemas, for example, // using a database that supports real schemas, for example,
// for Postgres: "schema_name"."table_name", versus // for Postgres: "schema_name"."table_name", versus
// simply "table_name" for MySQL (because it does not support real schemas) // simply "table_name" for MySQL (because it does not support real schemas)
func SchemaTable(lq byte, rq byte, driver string, schema string, table string) string { func SchemaTable(lq, rq string, driver string, schema string, table string) string {
if driver == "postgres" && schema != "public" { if driver == "postgres" && schema != "public" {
return fmt.Sprintf(`%c%s%c.%c%s%c`, lq, schema, rq, lq, table, rq) return fmt.Sprintf(`%s%s%s.%s%s%s`, lq, schema, rq, lq, table, rq)
} }
return fmt.Sprintf(`%c%s%c`, lq, table, rq) return fmt.Sprintf(`%s%s%s`, lq, table, rq)
} }
// IdentQuote attempts to quote simple identifiers in SQL tatements // IdentQuote attempts to quote simple identifiers in SQL tatements
@ -118,6 +118,16 @@ func Identifier(in int) string {
return cols.String() return cols.String()
} }
// QuoteCharacter returns a string that allows the quote character
// to be embedded into a Go string that uses double quotes:
func QuoteCharacter(q byte) string {
if q == '"' {
return `\"`
}
return string(q)
}
// Plural converts singular words to plural words (eg: person to people) // Plural converts singular words to plural words (eg: person to people)
func Plural(name string) string { func Plural(name string) string {
buf := GetBuffer() buf := GetBuffer()
@ -433,7 +443,7 @@ func SetParamNames(columns []string) string {
// WhereClause returns the where clause using start as the $ flag index // WhereClause returns the where clause using start as the $ flag index
// For example, if start was 2 output would be: "colthing=$2 AND colstuff=$3" // For example, if start was 2 output would be: "colthing=$2 AND colstuff=$3"
func WhereClause(start int, cols []string) string { func WhereClause(lq, rq string, start int, cols []string) string {
if start == 0 { if start == 0 {
panic("0 is not a valid start number for whereClause") panic("0 is not a valid start number for whereClause")
} }
@ -442,7 +452,7 @@ func WhereClause(start int, cols []string) string {
defer PutBuffer(buf) defer PutBuffer(buf)
for i, c := range cols { for i, c := range cols {
buf.WriteString(fmt.Sprintf(`"%s"=$%d`, c, start+i)) buf.WriteString(fmt.Sprintf(`%s%s%s=$%d`, lq, c, rq, start+i))
if i < len(cols)-1 { if i < len(cols)-1 {
buf.WriteString(" AND ") buf.WriteString(" AND ")
} }

View file

@ -69,6 +69,20 @@ func TestIdentifier(t *testing.T) {
} }
} }
func TestQuoteCharacter(t *testing.T) {
t.Parallel()
if QuoteCharacter('[') != "[" {
t.Error("want just the normal quote character")
}
if QuoteCharacter('`') != "`" {
t.Error("want just the normal quote character")
}
if QuoteCharacter('"') != `\"` {
t.Error("want an escaped character")
}
}
func TestPlaceholders(t *testing.T) { func TestPlaceholders(t *testing.T) {
t.Parallel() t.Parallel()
@ -317,7 +331,7 @@ func TestWhereClause(t *testing.T) {
} }
for i, test := range tests { for i, test := range tests {
r := WhereClause(test.Start, test.Cols) r := WhereClause(`"`, `"`, test.Start, test.Cols)
if r != test.Should { if r != test.Should {
t.Errorf("(%d) want: %s, got: %s\nTest: %#v", i, test.Should, r, test) t.Errorf("(%d) want: %s, got: %s\nTest: %#v", i, test.Should, r, test)
} }

View file

@ -16,15 +16,37 @@ import (
type templateData struct { type templateData struct {
Tables []bdb.Table Tables []bdb.Table
Table bdb.Table Table bdb.Table
// Controls what names are output
PkgName string
Schema string Schema string
// Controls which code is output (mysql vs postgres ...)
DriverName string DriverName string
UseLastInsertID bool UseLastInsertID bool
PkgName string
// Turn off auto timestamps or hook generation
NoHooks bool NoHooks bool
NoAutoTimestamps bool NoAutoTimestamps bool
// Tags control which
Tags []string Tags []string
// StringFuncs are usable in templates with stringMap
StringFuncs map[string]func(string) string StringFuncs map[string]func(string) string
// Dialect controls quoting
Dialect boil.Dialect Dialect boil.Dialect
LQ string
RQ string
}
func (t templateData) Quotes(s string) string {
return fmt.Sprintf("%s%s%s", t.LQ, s, t.RQ)
}
func (t templateData) SchemaTable(table string) string {
return strmangle.SchemaTable(t.LQ, t.RQ, t.DriverName, t.Schema, table)
} }
type templateList struct { type templateList struct {
@ -115,7 +137,7 @@ var templateStringMappers = map[string]func(string) string{
// add a function pointer here. // add a function pointer here.
var templateFunctions = template.FuncMap{ var templateFunctions = template.FuncMap{
// String ops // String ops
"quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) }, "quoteWrap": func(s string) string { return fmt.Sprintf(`"%s"`, s) },
"id": strmangle.Identifier, "id": strmangle.Identifier,
// Pluralization // Pluralization
@ -143,7 +165,6 @@ var templateFunctions = template.FuncMap{
// Database related mangling // Database related mangling
"whereClause": strmangle.WhereClause, "whereClause": strmangle.WhereClause,
"schemaTable": strmangle.SchemaTable,
// Text helpers // Text helpers
"textsFromForeignKey": textsFromForeignKey, "textsFromForeignKey": textsFromForeignKey,

View file

@ -1,5 +1,5 @@
{{- define "relationship_to_one_helper" -}} {{- define "relationship_to_one_helper" -}}
{{- $tmplData := .Dot -}}{{/* .Dot holds the root templateData struct, passed in through preserveDot */}} {{- $dot := .Dot -}}{{/* .Dot holds the root templateData struct, passed in through preserveDot */}}
{{- with .Rel -}}{{/* Rel holds the text helper data, passed in through preserveDot */}} {{- with .Rel -}}{{/* Rel holds the text helper data, passed in through preserveDot */}}
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}} {{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
// {{.Function.Name}}G pointed to by the foreign key. // {{.Function.Name}}G pointed to by the foreign key.
@ -16,7 +16,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}(exec bo
queryMods = append(queryMods, mods...) queryMods = append(queryMods, mods...)
query := {{.ForeignTable.NamePluralGo}}(exec, queryMods...) query := {{.ForeignTable.NamePluralGo}}(exec, queryMods...)
boil.SetFrom(query.Query, `{{schemaTable $tmplData.Dialect.LQ $tmplData.Dialect.RQ $tmplData.DriverName $tmplData.Schema .ForeignTable.Name}}`) boil.SetFrom(query.Query, "{{.ForeignTable.Name | $dot.SchemaTable}}")
return query return query
} }

View file

@ -12,6 +12,7 @@
{{- else -}} {{- else -}}
{{- /* Begin execution of template for many-to-many relationship. */ -}} {{- /* Begin execution of template for many-to-many relationship. */ -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}} {{- $rel := textsFromRelationship $dot.Tables $table . -}}
{{- $schemaForeignTable := .ForeignTable | $dot.SchemaTable -}}
// {{$rel.Function.Name}}G retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} // {{$rel.Function.Name}}G retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}}
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. {{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}G(mods ...qm.QueryMod) {{$varNameSingular}}Query { func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}G(mods ...qm.QueryMod) {{$varNameSingular}}Query {
@ -22,7 +23,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. {{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) {{$varNameSingular}}Query { func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) {{$varNameSingular}}Query {
queryMods := []qm.QueryMod{ queryMods := []qm.QueryMod{
qm.Select(`"{{id 0}}".*`), qm.Select("{{id 0 | $dot.Quotes}}.*"),
} }
if len(mods) != 0 { if len(mods) != 0 {
@ -31,17 +32,17 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
{{if .ToJoinTable -}} {{if .ToJoinTable -}}
queryMods = append(queryMods, queryMods = append(queryMods,
qm.InnerJoin(`{{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .JoinTable}} as "{{id 1}}" on "{{id 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}"`), qm.InnerJoin("{{.JoinTable | $dot.SchemaTable}} as {{id 1 | $dot.Quotes}} on {{id 0 | $dot.Quotes}}.{{.ForeignColumn | $dot.Quotes}} = {{id 1 | $dot.Quotes}}.{{.JoinForeignColumn | $dot.Quotes}}"),
qm.Where(`"{{id 1}}"."{{.JoinLocalColumn}}"=$1`, {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}), qm.Where("{{id 1 | $dot.Quotes}}.{{.JoinLocalColumn | $dot.Quotes}}=$1", {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}),
) )
{{else -}} {{else -}}
queryMods = append(queryMods, queryMods = append(queryMods,
qm.Where(`"{{id 0}}"."{{.ForeignColumn}}"=$1`, {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}), qm.Where("{{id 0 | $dot.Quotes}}.{{.ForeignColumn | $dot.Quotes}}=$1", {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}),
) )
{{end}} {{end}}
query := {{$rel.ForeignTable.NamePluralGo}}(exec, queryMods...) query := {{$rel.ForeignTable.NamePluralGo}}(exec, queryMods...)
boil.SetFrom(query.Query, `{{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .ForeignTable}} as "{{id 0}}"`) boil.SetFrom(query.Query, "{{$schemaForeignTable}} as {{id 0 | $dot.Quotes}}")
return query return query
} }

View file

@ -1,6 +1,6 @@
{{- define "relationship_to_one_eager_helper" -}} {{- define "relationship_to_one_eager_helper" -}}
{{- $tmplData := .Dot -}}{{/* .Dot holds the root templateData struct, passed in through preserveDot */}} {{- $dot := .Dot -}}{{/* .Dot holds the root templateData struct, passed in through preserveDot */}}
{{- $varNameSingular := $tmplData.Table.Name | singular | camelCase -}} {{- $varNameSingular := $dot.Table.Name | singular | camelCase -}}
{{- with .Rel -}} {{- with .Rel -}}
{{- $arg := printf "maybe%s" .LocalTable.NameGo -}} {{- $arg := printf "maybe%s" .LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" .LocalTable.NameGo -}} {{- $slice := printf "%sSlice" .LocalTable.NameGo -}}
@ -28,7 +28,7 @@ func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bo
} }
query := fmt.Sprintf( query := fmt.Sprintf(
`select * from {{schemaTable $tmplData.Dialect.LQ $tmplData.Dialect.RQ $tmplData.DriverName $tmplData.Schema .ForeignKey.ForeignTable}} where "{{.ForeignKey.ForeignColumn}}" in (%s)`, "select * from {{.ForeignKey.ForeignTable | $dot.SchemaTable}} where {{.ForeignKey.ForeignColumn | $dot.Quotes}} in (%s)",
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1), strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
) )
@ -47,7 +47,7 @@ func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bo
return errors.Wrap(err, "failed to bind eager loaded slice {{.ForeignTable.NameGo}}") return errors.Wrap(err, "failed to bind eager loaded slice {{.ForeignTable.NameGo}}")
} }
{{if not $tmplData.NoHooks -}} {{if not $dot.NoHooks -}}
if len({{.ForeignTable.Name | singular | camelCase}}AfterSelectHooks) != 0 { if len({{.ForeignTable.Name | singular | camelCase}}AfterSelectHooks) != 0 {
for _, obj := range resultSlice { for _, obj := range resultSlice {
if err := obj.doAfterSelectHooks(e); err != nil { if err := obj.doAfterSelectHooks(e); err != nil {

View file

@ -13,6 +13,7 @@
{{- $txt := textsFromRelationship $dot.Tables $dot.Table . -}} {{- $txt := textsFromRelationship $dot.Tables $dot.Table . -}}
{{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}} {{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" $txt.LocalTable.NameGo -}} {{- $slice := printf "%sSlice" $txt.LocalTable.NameGo -}}
{{- $schemaForeignTable := .ForeignTable | $dot.SchemaTable -}}
// Load{{$txt.Function.Name}} allows an eager lookup of values, cached into the // Load{{$txt.Function.Name}} allows an eager lookup of values, cached into the
// loaded structs of the objects. // loaded structs of the objects.
func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error { func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
@ -37,13 +38,14 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula
} }
{{if .ToJoinTable -}} {{if .ToJoinTable -}}
{{- $schemaJoinTable := .JoinTable | $dot.SchemaTable -}}
query := fmt.Sprintf( query := fmt.Sprintf(
`select "{{id 0}}".*, "{{id 1}}"."{{.JoinLocalColumn}}" from {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .ForeignTable}} as "{{id 0}}" inner join {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .JoinTable}} as "{{id 1}}" on "{{id 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}" where "{{id 1}}"."{{.JoinLocalColumn}}" in (%s)`, "select {{id 0 | $dot.Quotes}}.*, {{id 1 | $dot.Quotes}}.{{.JoinLocalColumn | $dot.Quotes}} from {{$schemaForeignTable}} as {{id 0 | $dot.Quotes}} inner join {{$schemaJoinTable}} as {{id 1 | $dot.Quotes}} on {{id 0 | $dot.Quotes}}.{{.ForeignColumn | $dot.Quotes}} = {{id 1 | $dot.Quotes}}.{{.JoinForeignColumn | $dot.Quotes}} where {{id 1 | $dot.Quotes}}.{{.JoinLocalColumn | $dot.Quotes}} in (%s)",
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1), strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
) )
{{else -}} {{else -}}
query := fmt.Sprintf( query := fmt.Sprintf(
`select * from {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .ForeignTable}} where "{{.ForeignColumn}}" in (%s)`, "select * from {{$schemaForeignTable}} where {{.ForeignColumn | $dot.Quotes}} in (%s)",
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1), strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
) )
{{end -}} {{end -}}

View file

@ -39,7 +39,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function
{{if .ToJoinTable -}} {{if .ToJoinTable -}}
for _, rel := range related { for _, rel := range related {
query := `insert into {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .JoinTable}} ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)` query := "insert into {{.JoinTable | $dot.SchemaTable}} ({{.JoinLocalColumn | $dot.Quotes}}, {{.JoinForeignColumn | $dot.Quotes}}) values ($1, $2)"
values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}, rel.{{$rel.ForeignTable.ColumnNameGo}}} values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}, rel.{{$rel.ForeignTable.ColumnNameGo}}}
if boil.DebugMode { if boil.DebugMode {
@ -96,10 +96,10 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function
// Sets related.R.{{$rel.Function.ForeignName}}'s {{$rel.Function.Name}} accordingly. // Sets related.R.{{$rel.Function.ForeignName}}'s {{$rel.Function.Name}} accordingly.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Set{{$rel.Function.Name}}(exec boil.Executor, insert bool, related ...*{{$rel.ForeignTable.NameGo}}) error { func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Set{{$rel.Function.Name}}(exec boil.Executor, insert bool, related ...*{{$rel.ForeignTable.NameGo}}) error {
{{if .ToJoinTable -}} {{if .ToJoinTable -}}
query := `delete from {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .JoinTable}} where "{{.JoinLocalColumn}}" = $1` query := "delete from {{.JoinTable | $dot.SchemaTable}} where {{.JoinLocalColumn | $dot.Quotes}} = $1"
values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}} values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}}
{{else -}} {{else -}}
query := `update {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .ForeignTable}} set "{{.ForeignColumn}}" = null where "{{.ForeignColumn}}" = $1` query := "update {{.ForeignTable | $dot.SchemaTable}} set {{.ForeignColumn | $dot.Quotes}} = null where {{.ForeignColumn | $dot.Quotes}} = $1"
values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}} values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}}
{{end -}} {{end -}}
if boil.DebugMode { if boil.DebugMode {
@ -140,7 +140,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Remove{{$rel.Funct
var err error var err error
{{if .ToJoinTable -}} {{if .ToJoinTable -}}
query := fmt.Sprintf( query := fmt.Sprintf(
`delete from {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .JoinTable}} where "{{.JoinLocalColumn}}" = $1 and "{{.JoinForeignColumn}}" in (%s)`, "delete from {{.JoinTable | $dot.SchemaTable}} where {{.JoinLocalColumn | $dot.Quotes}} = $1 and {{.JoinForeignColumn | $dot.Quotes}} in (%s)",
strmangle.Placeholders(dialect.IndexPlaceholders, len(related), 1, 1), strmangle.Placeholders(dialect.IndexPlaceholders, len(related), 1, 1),
) )
values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}} values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}}

View file

@ -8,6 +8,6 @@ func {{$tableNamePlural}}G(mods ...qm.QueryMod) {{$varNameSingular}}Query {
// {{$tableNamePlural}} retrieves all the records using an executor. // {{$tableNamePlural}} retrieves all the records using an executor.
func {{$tableNamePlural}}(exec boil.Executor, mods ...qm.QueryMod) {{$varNameSingular}}Query { func {{$tableNamePlural}}(exec boil.Executor, mods ...qm.QueryMod) {{$varNameSingular}}Query {
mods = append(mods, qm.From(`{{schemaTable .Dialect.LQ .Dialect.RQ .DriverName .Schema .Table.Name}}`)) mods = append(mods, qm.From("{{.Table.Name | .SchemaTable}}"))
return {{$varNameSingular}}Query{NewQuery(exec, mods...)} return {{$varNameSingular}}Query{NewQuery(exec, mods...)}
} }

View file

@ -29,7 +29,7 @@ func Find{{$tableNameSingular}}(exec boil.Executor, {{$pkArgs}}, selectCols ...s
sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",") sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",")
} }
query := fmt.Sprintf( query := fmt.Sprintf(
`select %s from {{schemaTable .Dialect.LQ .Dialect.RQ .DriverName .Schema .Table.Name}} where {{whereClause 1 .Table.PKey.Columns}}`, sel, "select %s from {{.Table.Name | .SchemaTable}} where {{whereClause .LQ .RQ 1 .Table.PKey.Columns}}", sel,
) )
q := boil.SQL(exec, query, {{$pkNames | join ", "}}) q := boil.SQL(exec, query, {{$pkNames | join ", "}})

View file

@ -1,5 +1,6 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}} {{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}} {{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $schemaTable := .Table.Name | .SchemaTable -}}
// InsertG a single record. See Insert for whitelist behavior description. // InsertG a single record. See Insert for whitelist behavior description.
func (o *{{$tableNameSingular}}) InsertG(whitelist ... string) error { func (o *{{$tableNameSingular}}) InsertG(whitelist ... string) error {
return o.Insert(boil.GetDB(), whitelist...) return o.Insert(boil.GetDB(), whitelist...)
@ -64,13 +65,13 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
if err != nil { if err != nil {
return err return err
} }
cache.query = fmt.Sprintf(`INSERT INTO {{schemaTable .Dialect.LQ .Dialect.RQ .DriverName .Schema .Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), strmangle.Placeholders(dialect.IndexPlaceholders, len(wl), 1, 1)) cache.query = fmt.Sprintf("INSERT INTO {{$schemaTable}} ({{.LQ}}%s{{.RQ}}) VALUES (%s)", strings.Join(wl, "{{.LQ}},{{.RQ}}"), strmangle.Placeholders(dialect.IndexPlaceholders, len(wl), 1, 1))
if len(cache.retMapping) != 0 { if len(cache.retMapping) != 0 {
{{if .UseLastInsertID -}} {{if .UseLastInsertID -}}
cache.retQuery = fmt.Sprintf(`SELECT %s FROM {{schemaTable .Dialect.LQ .Dialect.RQ .DriverName .Schema .Table.Name}} WHERE %s`, strings.Join(returnColumns, `","`), strmangle.WhereClause(1, {{$varNameSingular}}PrimaryKeyColumns)) cache.retQuery = fmt.Sprintf("SELECT %s FROM {{$schemaTable}} WHERE %s", strings.Join(returnColumns, "{{.LQ}},{{.RQ}}"), strmangle.WhereClause("{{.LQ}}", "{{.RQ}}", 1, {{$varNameSingular}}PrimaryKeyColumns))
{{else -}} {{else -}}
cache.query += fmt.Sprintf(` RETURNING %s`, strings.Join(returnColumns, ",")) cache.query += fmt.Sprintf(" RETURNING {{.LQ}}%s{{.RQ}}", strings.Join(returnColumns, "{{.LQ}},{{.RQ}}"))
{{end -}} {{end -}}
} }
} }

View file

@ -3,6 +3,7 @@
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}} {{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}} {{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", " -}} {{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", " -}}
{{- $schemaTable := .Table.Name | .SchemaTable -}}
// UpdateG a single {{$tableNameSingular}} record. See Update for // UpdateG a single {{$tableNameSingular}} record. See Update for
// whitelist behavior description. // whitelist behavior description.
func (o *{{$tableNameSingular}}) UpdateG(whitelist ...string) error { func (o *{{$tableNameSingular}}) UpdateG(whitelist ...string) error {
@ -52,7 +53,7 @@ func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string
if !cached { if !cached {
wl := strmangle.UpdateColumnSet({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns, whitelist) wl := strmangle.UpdateColumnSet({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns, whitelist)
cache.query = fmt.Sprintf(`UPDATE {{schemaTable .Dialect.LQ .Dialect.RQ .DriverName .Schema .Table.Name}} SET %s WHERE %s`, strmangle.SetParamNames(wl), strmangle.WhereClause(len(wl)+1, {{$varNameSingular}}PrimaryKeyColumns)) cache.query = fmt.Sprintf("UPDATE {{$schemaTable}} SET %s WHERE %s", strmangle.SetParamNames(wl), strmangle.WhereClause("{{.LQ}}", "{{.RQ}}", len(wl)+1, {{$varNameSingular}}PrimaryKeyColumns))
cache.valueMapping, err = boil.BindMapping({{$varNameSingular}}Type, {{$varNameSingular}}Mapping, append(wl, {{$varNameSingular}}PrimaryKeyColumns...)) cache.valueMapping, err = boil.BindMapping({{$varNameSingular}}Type, {{$varNameSingular}}Mapping, append(wl, {{$varNameSingular}}PrimaryKeyColumns...))
if err != nil { if err != nil {
return err return err
@ -155,8 +156,8 @@ func (o {{$tableNameSingular}}Slice) UpdateAll(exec boil.Executor, cols M) error
args = append(args, o.inPrimaryKeyArgs()...) args = append(args, o.inPrimaryKeyArgs()...)
sql := fmt.Sprintf( sql := fmt.Sprintf(
`UPDATE {{schemaTable .Dialect.LQ .Dialect.RQ .DriverName .Schema .Table.Name}} SET (%s) = (%s) WHERE (%s) IN (%s)`, "UPDATE {{$schemaTable}} SET (%s) = (%s) WHERE (%s) IN (%s)",
strings.Join(colNames, ", "), strings.Join(colNames, ","),
strmangle.Placeholders(dialect.IndexPlaceholders, len(colNames), 1, 1), strmangle.Placeholders(dialect.IndexPlaceholders, len(colNames), 1, 1),
strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, {{$varNameSingular}}PrimaryKeyColumns), ","), strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, {{$varNameSingular}}PrimaryKeyColumns), ","),
strmangle.Placeholders(dialect.IndexPlaceholders, len(o) * len({{$varNameSingular}}PrimaryKeyColumns), len(colNames)+1, len({{$varNameSingular}}PrimaryKeyColumns)), strmangle.Placeholders(dialect.IndexPlaceholders, len(o) * len({{$varNameSingular}}PrimaryKeyColumns), len(colNames)+1, len({{$varNameSingular}}PrimaryKeyColumns)),

View file

@ -1,5 +1,6 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}} {{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}} {{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $schemaTable := .Table.Name | .SchemaTable -}}
// DeleteP deletes a single {{$tableNameSingular}} record with an executor. // DeleteP deletes a single {{$tableNameSingular}} record with an executor.
// DeleteP will match against the primary key column to find the record to delete. // DeleteP will match against the primary key column to find the record to delete.
// Panics on error. // Panics on error.
@ -43,7 +44,7 @@ func (o *{{$tableNameSingular}}) Delete(exec boil.Executor) error {
args := o.inPrimaryKeyArgs() args := o.inPrimaryKeyArgs()
sql := `DELETE FROM {{schemaTable .Dialect.LQ .Dialect.RQ .DriverName .Schema .Table.Name}} WHERE {{whereClause 1 .Table.PKey.Columns}}` sql := "DELETE FROM {{$schemaTable}} WHERE {{whereClause .LQ .RQ 1 .Table.PKey.Columns}}"
if boil.DebugMode { if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, sql) fmt.Fprintln(boil.DebugWriter, sql)
@ -132,8 +133,8 @@ func (o {{$tableNameSingular}}Slice) DeleteAll(exec boil.Executor) error {
args := o.inPrimaryKeyArgs() args := o.inPrimaryKeyArgs()
sql := fmt.Sprintf( sql := fmt.Sprintf(
`DELETE FROM {{schemaTable .Dialect.LQ .Dialect.RQ .DriverName .Schema .Table.Name}} WHERE (%s) IN (%s)`, "DELETE FROM {{$schemaTable}} WHERE (%s) IN (%s)",
strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, {{$varNameSingular}}PrimaryKeyColumns), ","), strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, {{$varNameSingular}}PrimaryKeyColumns), "{{.LQ}},{{.RQ}}"),
strmangle.Placeholders(dialect.IndexPlaceholders, len(o) * len({{$varNameSingular}}PrimaryKeyColumns), 1, len({{$varNameSingular}}PrimaryKeyColumns)), strmangle.Placeholders(dialect.IndexPlaceholders, len(o) * len({{$varNameSingular}}PrimaryKeyColumns), 1, len({{$varNameSingular}}PrimaryKeyColumns)),
) )

View file

@ -1,6 +1,7 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}} {{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}} {{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $varNamePlural := .Table.Name | plural | camelCase -}} {{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $schemaTable := .Table.Name | .SchemaTable -}}
// ReloadGP refetches the object from the database and panics on error. // ReloadGP refetches the object from the database and panics on error.
func (o *{{$tableNameSingular}}) ReloadGP() { func (o *{{$tableNameSingular}}) ReloadGP() {
if err := o.ReloadG(); err != nil { if err := o.ReloadG(); err != nil {
@ -67,7 +68,7 @@ func (o *{{$tableNameSingular}}Slice) ReloadAll(exec boil.Executor) error {
args := o.inPrimaryKeyArgs() args := o.inPrimaryKeyArgs()
sql := fmt.Sprintf( sql := fmt.Sprintf(
`SELECT {{schemaTable .Dialect.LQ .Dialect.RQ .DriverName .Schema .Table.Name}}.* FROM {{schemaTable .Dialect.LQ .Dialect.RQ .DriverName .Schema .Table.Name}} WHERE (%s) IN (%s)`, "SELECT {{$schemaTable}}.* FROM {{$schemaTable}} WHERE (%s) IN (%s)",
strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, {{$varNameSingular}}PrimaryKeyColumns), ","), strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, {{$varNameSingular}}PrimaryKeyColumns), ","),
strmangle.Placeholders(dialect.IndexPlaceholders, len(*o) * len({{$varNameSingular}}PrimaryKeyColumns), 1, len({{$varNameSingular}}PrimaryKeyColumns)), strmangle.Placeholders(dialect.IndexPlaceholders, len(*o) * len({{$varNameSingular}}PrimaryKeyColumns), 1, len({{$varNameSingular}}PrimaryKeyColumns)),
) )

View file

@ -2,11 +2,12 @@
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}} {{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}} {{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", " -}} {{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", " -}}
{{- $schemaTable := .Table.Name | .SchemaTable -}}
// {{$tableNameSingular}}Exists checks if the {{$tableNameSingular}} row exists. // {{$tableNameSingular}}Exists checks if the {{$tableNameSingular}} row exists.
func {{$tableNameSingular}}Exists(exec boil.Executor, {{$pkArgs}}) (bool, error) { func {{$tableNameSingular}}Exists(exec boil.Executor, {{$pkArgs}}) (bool, error) {
var exists bool var exists bool
sql := `select exists(select 1 from {{schemaTable .Dialect.LQ .Dialect.RQ .DriverName .Schema .Table.Name}} where {{whereClause 1 .Table.PKey.Columns}} limit 1)` sql := "select exists(select 1 from {{$schemaTable}} where {{whereClause .LQ .RQ 1 .Table.PKey.Columns}} limit 1)"
if boil.DebugMode { if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, sql) fmt.Fprintln(boil.DebugWriter, sql)

View file

@ -41,11 +41,11 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
} }
{{if .ToJoinTable -}} {{if .ToJoinTable -}}
_, err = tx.Exec(`insert into {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .JoinTable}} ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{$rel.LocalTable.ColumnNameGo}}, b.{{$rel.ForeignTable.ColumnNameGo}}) _, err = tx.Exec("insert into {{.JoinTable | $dot.SchemaTable}} ({{.JoinLocalColumn | $dot.Quotes}}, {{.JoinForeignColumn | $dot.Quotes}}) values ($1, $2)", a.{{$rel.LocalTable.ColumnNameGo}}, b.{{$rel.ForeignTable.ColumnNameGo}})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
_, err = tx.Exec(`insert into {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .JoinTable}} ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{$rel.LocalTable.ColumnNameGo}}, c.{{$rel.ForeignTable.ColumnNameGo}}) _, err = tx.Exec("insert into {{.JoinTable | $dot.SchemaTable}} ({{.JoinLocalColumn | $dot.Quotes}}, {{.JoinForeignColumn | $dot.Quotes}}) values ($1, $2)", a.{{$rel.LocalTable.ColumnNameGo}}, c.{{$rel.ForeignTable.ColumnNameGo}})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }