From 69e07b9a05ea9bb42bc7f753bf520420f3fd9762 Mon Sep 17 00:00:00 2001 From: Aaron L Date: Sun, 18 Sep 2016 16:26:30 -0700 Subject: [PATCH] File shuffle --- templates/05_relationship_one_to_one.tpl | 26 +++++ ...o_many.tpl => 06_relationship_to_many.tpl} | 0 ...r.tpl => 07_relationship_to_one_eager.tpl} | 0 .../08_relationship_one_to_one_eager.tpl | 89 +++++++++++++++ ....tpl => 09_relationship_to_many_eager.tpl} | 0 ....tpl => 10_relationship_to_one_setops.tpl} | 0 .../11_relationship_one_to_one_setops.tpl | 103 ++++++++++++++++++ ...tpl => 12_relationship_to_many_setops.tpl} | 0 templates/{10_all.tpl => 13_all.tpl} | 0 templates/{11_find.tpl => 14_find.tpl} | 0 templates/{12_insert.tpl => 15_insert.tpl} | 0 templates/{13_update.tpl => 16_update.tpl} | 0 templates/{14_upsert.tpl => 17_upsert.tpl} | 0 templates/{15_delete.tpl => 18_delete.tpl} | 0 templates/{16_reload.tpl => 19_reload.tpl} | 0 templates/{17_exists.tpl => 20_exists.tpl} | 0 ..._timestamps.tpl => 21_auto_timestamps.tpl} | 0 17 files changed, 218 insertions(+) create mode 100644 templates/05_relationship_one_to_one.tpl rename templates/{05_relationship_to_many.tpl => 06_relationship_to_many.tpl} (100%) rename templates/{06_relationship_to_one_eager.tpl => 07_relationship_to_one_eager.tpl} (100%) create mode 100644 templates/08_relationship_one_to_one_eager.tpl rename templates/{07_relationship_to_many_eager.tpl => 09_relationship_to_many_eager.tpl} (100%) rename templates/{08_relationship_to_one_setops.tpl => 10_relationship_to_one_setops.tpl} (100%) create mode 100644 templates/11_relationship_one_to_one_setops.tpl rename templates/{09_relationship_to_many_setops.tpl => 12_relationship_to_many_setops.tpl} (100%) rename templates/{10_all.tpl => 13_all.tpl} (100%) rename templates/{11_find.tpl => 14_find.tpl} (100%) rename templates/{12_insert.tpl => 15_insert.tpl} (100%) rename templates/{13_update.tpl => 16_update.tpl} (100%) rename templates/{14_upsert.tpl => 17_upsert.tpl} (100%) rename templates/{15_delete.tpl => 18_delete.tpl} (100%) rename templates/{16_reload.tpl => 19_reload.tpl} (100%) rename templates/{17_exists.tpl => 20_exists.tpl} (100%) rename templates/{19_auto_timestamps.tpl => 21_auto_timestamps.tpl} (100%) diff --git a/templates/05_relationship_one_to_one.tpl b/templates/05_relationship_one_to_one.tpl new file mode 100644 index 0000000..2b69a5b --- /dev/null +++ b/templates/05_relationship_one_to_one.tpl @@ -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 -}} diff --git a/templates/05_relationship_to_many.tpl b/templates/06_relationship_to_many.tpl similarity index 100% rename from templates/05_relationship_to_many.tpl rename to templates/06_relationship_to_many.tpl diff --git a/templates/06_relationship_to_one_eager.tpl b/templates/07_relationship_to_one_eager.tpl similarity index 100% rename from templates/06_relationship_to_one_eager.tpl rename to templates/07_relationship_to_one_eager.tpl diff --git a/templates/08_relationship_one_to_one_eager.tpl b/templates/08_relationship_one_to_one_eager.tpl new file mode 100644 index 0000000..55a6268 --- /dev/null +++ b/templates/08_relationship_one_to_one_eager.tpl @@ -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 */}} diff --git a/templates/07_relationship_to_many_eager.tpl b/templates/09_relationship_to_many_eager.tpl similarity index 100% rename from templates/07_relationship_to_many_eager.tpl rename to templates/09_relationship_to_many_eager.tpl diff --git a/templates/08_relationship_to_one_setops.tpl b/templates/10_relationship_to_one_setops.tpl similarity index 100% rename from templates/08_relationship_to_one_setops.tpl rename to templates/10_relationship_to_one_setops.tpl diff --git a/templates/11_relationship_one_to_one_setops.tpl b/templates/11_relationship_one_to_one_setops.tpl new file mode 100644 index 0000000..d2cbeaa --- /dev/null +++ b/templates/11_relationship_one_to_one_setops.tpl @@ -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 */}} diff --git a/templates/09_relationship_to_many_setops.tpl b/templates/12_relationship_to_many_setops.tpl similarity index 100% rename from templates/09_relationship_to_many_setops.tpl rename to templates/12_relationship_to_many_setops.tpl diff --git a/templates/10_all.tpl b/templates/13_all.tpl similarity index 100% rename from templates/10_all.tpl rename to templates/13_all.tpl diff --git a/templates/11_find.tpl b/templates/14_find.tpl similarity index 100% rename from templates/11_find.tpl rename to templates/14_find.tpl diff --git a/templates/12_insert.tpl b/templates/15_insert.tpl similarity index 100% rename from templates/12_insert.tpl rename to templates/15_insert.tpl diff --git a/templates/13_update.tpl b/templates/16_update.tpl similarity index 100% rename from templates/13_update.tpl rename to templates/16_update.tpl diff --git a/templates/14_upsert.tpl b/templates/17_upsert.tpl similarity index 100% rename from templates/14_upsert.tpl rename to templates/17_upsert.tpl diff --git a/templates/15_delete.tpl b/templates/18_delete.tpl similarity index 100% rename from templates/15_delete.tpl rename to templates/18_delete.tpl diff --git a/templates/16_reload.tpl b/templates/19_reload.tpl similarity index 100% rename from templates/16_reload.tpl rename to templates/19_reload.tpl diff --git a/templates/17_exists.tpl b/templates/20_exists.tpl similarity index 100% rename from templates/17_exists.tpl rename to templates/20_exists.tpl diff --git a/templates/19_auto_timestamps.tpl b/templates/21_auto_timestamps.tpl similarity index 100% rename from templates/19_auto_timestamps.tpl rename to templates/21_auto_timestamps.tpl