File shuffle

This commit is contained in:
Aaron L 2016-09-18 16:26:30 -07:00
parent eaa9a92d52
commit 69e07b9a05
17 changed files with 218 additions and 0 deletions

View file

@ -0,0 +1,26 @@
{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $dot := . -}}
{{- range .Table.FKeys -}}
{{- $txt := txtsFromFKey $dot.Tables $dot.Table . -}}
{{- $varNameSingular := .ForeignTable | singular | camelCase -}}
// {{$txt.Function.Name}}G pointed to by the foreign key.
func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) {{$txt.Function.Name}}G(mods ...qm.QueryMod) {{$varNameSingular}}Query {
return {{$txt.Function.Receiver}}.{{$txt.Function.Name}}(boil.GetDB(), mods...)
}
// {{$txt.Function.Name}} pointed to by the foreign key.
func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) {{$txt.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) ({{$varNameSingular}}Query) {
queryMods := []qm.QueryMod{
qm.Where("{{$txt.ForeignTable.ColumnName}}={{if $dot.Dialect.IndexPlaceholders}}$1{{else}}?{{end}}", {{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}),
}
queryMods = append(queryMods, mods...)
query := {{$txt.ForeignTable.NamePluralGo}}(exec, queryMods...)
queries.SetFrom(query.Query, "{{$txt.ForeignTable.Name | $dot.SchemaTable}}")
return query
}
{{- end -}}
{{- end -}}

View file

@ -0,0 +1,89 @@
{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $dot := . -}}
{{- range .Table.FKeys -}}
{{- $txt := txtsFromFKey $dot.Tables $dot.Table . -}}
{{- $varNameSingular := $dot.Table.Name | singular | camelCase -}}
{{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" $txt.LocalTable.NameGo}}
// Load{{$txt.Function.Name}} allows an eager lookup of values, cached into the
// loaded structs of the objects.
func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
var slice []*{{$txt.LocalTable.NameGo}}
var object *{{$txt.LocalTable.NameGo}}
count := 1
if singular {
object = {{$arg}}.(*{{$txt.LocalTable.NameGo}})
} else {
slice = *{{$arg}}.(*{{$slice}})
count = len(slice)
}
args := make([]interface{}, count)
if singular {
args[0] = object.{{$txt.LocalTable.ColumnNameGo}}
} else {
for i, obj := range slice {
args[i] = obj.{{$txt.LocalTable.ColumnNameGo}}
}
}
query := fmt.Sprintf(
"select * from {{.ForeignTable | $dot.SchemaTable}} where {{.ForeignColumn | $dot.Quotes}} in (%s)",
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
)
if boil.DebugMode {
fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args)
}
results, err := e.Query(query, args...)
if err != nil {
return errors.Wrap(err, "failed to eager load {{$txt.ForeignTable.NameGo}}")
}
defer results.Close()
var resultSlice []*{{$txt.ForeignTable.NameGo}}
if err = queries.Bind(results, &resultSlice); err != nil {
return errors.Wrap(err, "failed to bind eager loaded slice {{$txt.ForeignTable.NameGo}}")
}
{{if not $dot.NoHooks -}}
if len({{$txt.ForeignTable.Name | singular | camelCase}}AfterSelectHooks) != 0 {
for _, obj := range resultSlice {
if err := obj.doAfterSelectHooks(e); err != nil {
return err
}
}
}
{{- end}}
if singular && len(resultSlice) != 0 {
if object.R == nil {
object.R = &{{$varNameSingular}}R{}
}
object.R.{{$txt.Function.Name}} = resultSlice[0]
return nil
}
for _, foreign := range resultSlice {
for _, local := range slice {
{{if $txt.Function.UsesBytes -}}
if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, foreign.{{$txt.Function.ForeignAssignment}}) {
{{else -}}
if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.Function.ForeignAssignment}} {
{{end -}}
if local.R == nil {
local.R = &{{$varNameSingular}}R{}
}
local.R.{{$txt.Function.Name}} = foreign
break
}
}
}
return nil
}
{{end -}}{{/* range */}}
{{end}}{{/* join table */}}

View file

@ -0,0 +1,103 @@
{{- /* Begin execution of template for one-to-one setops */ -}}
{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $dot := . -}}
{{- range .Table.FKeys -}}
{{- $txt := txtsFromFKey $dot.Tables $dot.Table . -}}
{{- $varNameSingular := .ForeignTable | singular | camelCase -}}
{{- $localNameSingular := .Table | singular | camelCase}}
// Set{{$txt.Function.Name}} of the {{.Table | singular}} to the related item.
// Sets {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} to related.
// Adds {{$txt.Function.Receiver}} to related.R.{{$txt.Function.ForeignName}}.
func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executor, insert bool, related *{{$txt.ForeignTable.NameGo}}) error {
var err error
if insert {
if err = related.Insert(exec); err != nil {
return errors.Wrap(err, "failed to insert into foreign table")
}
}
oldVal := {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}
related.{{$txt.Function.ForeignAssignment}} = {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}
{{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}}
if err = {{$txt.Function.Receiver}}.Update(exec, "{{.Column}}"); err != nil {
{{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = oldVal
return errors.Wrap(err, "failed to update local table")
}
if {{$txt.Function.Receiver}}.R == nil {
{{$txt.Function.Receiver}}.R = &{{$localNameSingular}}R{
{{$txt.Function.Name}}: related,
}
} else {
{{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} = related
}
{{if .Unique -}}
if related.R == nil {
related.R = &{{$varNameSingular}}R{
{{$txt.Function.ForeignName}}: {{$txt.Function.Receiver}},
}
} else {
related.R.{{$txt.Function.ForeignName}} = {{$txt.Function.Receiver}}
}
{{else -}}
if related.R == nil {
related.R = &{{$varNameSingular}}R{
{{$txt.Function.ForeignName}}: {{$txt.LocalTable.NameGo}}Slice{{"{"}}{{$txt.Function.Receiver}}{{"}"}},
}
} else {
related.R.{{$txt.Function.ForeignName}} = append(related.R.{{$txt.Function.ForeignName}}, {{$txt.Function.Receiver}})
}
{{end -}}
{{if .Nullable}}
{{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true
{{end -}}
return nil
}
{{- if .Nullable}}
// Remove{{$txt.Function.Name}} relationship.
// Sets {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} to nil.
// Removes {{$txt.Function.Receiver}} from all passed in related items' relationships struct (Optional).
func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}(exec boil.Executor, related *{{$txt.ForeignTable.NameGo}}) error {
var err error
{{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = false
if err = {{$txt.Function.Receiver}}.Update(exec, "{{.Column}}"); err != nil {
{{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true
return errors.Wrap(err, "failed to update local table")
}
{{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} = nil
if related == nil || related.R == nil {
return nil
}
{{if .Unique -}}
related.R.{{$txt.Function.ForeignName}} = nil
{{else -}}
for i, ri := range related.R.{{$txt.Function.ForeignName}} {
{{if $txt.Function.UsesBytes -}}
if 0 != bytes.Compare({{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}, ri.{{$txt.Function.LocalAssignment}}) {
{{else -}}
if {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} != ri.{{$txt.Function.LocalAssignment}} {
{{end -}}
continue
}
ln := len(related.R.{{$txt.Function.ForeignName}})
if ln > 1 && i < ln-1 {
related.R.{{$txt.Function.ForeignName}}[i] = related.R.{{$txt.Function.ForeignName}}[ln-1]
}
related.R.{{$txt.Function.ForeignName}} = related.R.{{$txt.Function.ForeignName}}[:ln-1]
break
}
{{end -}}
return nil
}
{{end -}}{{/* if foreignkey nullable */}}
{{- end -}}{{/* range */}}
{{- end -}}{{/* join table */}}