Add relationship "to_one" template.
- Temporarily add some new helper functions, going to clean these all up in favor of pipes - Annotate a couple of super weird methods - Fix a printf verb for err
This commit is contained in:
parent
2819c1c889
commit
f46c2c5337
5 changed files with 56 additions and 5 deletions
|
@ -295,7 +295,8 @@ func WherePrimaryKey(pkeyCols []string, start int) string {
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
// AutoIncPrimKey returns the auto-increment primary key column name or an empty string
|
// AutoIncPrimaryKey returns the auto-increment primary key column name or an
|
||||||
|
// empty string.
|
||||||
func AutoIncPrimaryKey(cols []dbdrivers.Column, pkey *dbdrivers.PrimaryKey) string {
|
func AutoIncPrimaryKey(cols []dbdrivers.Column, pkey *dbdrivers.PrimaryKey) string {
|
||||||
if pkey == nil {
|
if pkey == nil {
|
||||||
return ""
|
return ""
|
||||||
|
@ -315,7 +316,7 @@ func AutoIncPrimaryKey(cols []dbdrivers.Column, pkey *dbdrivers.PrimaryKey) stri
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// ColumnsToString changes the columns into a list of column names
|
// ColumnsToStrings changes the columns into a list of column names
|
||||||
func ColumnsToStrings(cols []dbdrivers.Column) []string {
|
func ColumnsToStrings(cols []dbdrivers.Column) []string {
|
||||||
names := make([]string, len(cols))
|
names := make([]string, len(cols))
|
||||||
for i, c := range cols {
|
for i, c := range cols {
|
||||||
|
@ -353,8 +354,8 @@ func PrimaryKeyFlagIndex(regularCols []dbdrivers.Column, pkeyCols []string) int
|
||||||
return len(regularCols) - len(pkeyCols) + 1
|
return len(regularCols) - len(pkeyCols) + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportsResult returns whether the database driver supports the sql.Results
|
// SupportsResultObject returns whether the database driver supports the
|
||||||
// interface, i.e. LastReturnId and RowsAffected
|
// sql.Results interface, i.e. LastReturnId and RowsAffected
|
||||||
func SupportsResultObject(driverName string) bool {
|
func SupportsResultObject(driverName string) bool {
|
||||||
switch driverName {
|
switch driverName {
|
||||||
case "postgres":
|
case "postgres":
|
||||||
|
@ -389,3 +390,19 @@ func FilterColumnsByAutoIncrement(columns []dbdrivers.Column) string {
|
||||||
|
|
||||||
return strings.Join(cols, `,`)
|
return strings.Join(cols, `,`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddID to the end of the string
|
||||||
|
func AddID(str string) string {
|
||||||
|
return str + "_id"
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveID from the end of the string
|
||||||
|
func RemoveID(str string) string {
|
||||||
|
return strings.TrimSuffix(str, "_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Substring returns a substring of str starting at index start and going
|
||||||
|
// to end-1.
|
||||||
|
func Substring(start, end int, str string) string {
|
||||||
|
return str[start:end]
|
||||||
|
}
|
||||||
|
|
|
@ -7,10 +7,14 @@ import (
|
||||||
"github.com/nullbio/sqlboiler/dbdrivers"
|
"github.com/nullbio/sqlboiler/dbdrivers"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RandDBStruct does nothing yet
|
||||||
|
// TODO(nullbio): What is this?
|
||||||
func RandDBStruct(varName string, table dbdrivers.Table) string {
|
func RandDBStruct(varName string, table dbdrivers.Table) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RandDBStructSlice randomizes a struct?
|
||||||
|
// TODO(nullbio): What is this?
|
||||||
func RandDBStructSlice(varName string, num int, table dbdrivers.Table) string {
|
func RandDBStructSlice(varName string, num int, table dbdrivers.Table) string {
|
||||||
var structs []string
|
var structs []string
|
||||||
for i := 0; i < num; i++ {
|
for i := 0; i < num; i++ {
|
||||||
|
|
|
@ -84,6 +84,9 @@ func loadTemplate(dir string, filename string) (*template.Template, error) {
|
||||||
// templates. If you wish to pass a new function into your own template,
|
// templates. If you wish to pass a new function into your own template,
|
||||||
// add a function pointer here.
|
// add a function pointer here.
|
||||||
var templateFunctions = template.FuncMap{
|
var templateFunctions = template.FuncMap{
|
||||||
|
"tolower": strings.ToLower,
|
||||||
|
"toupper": strings.ToUpper,
|
||||||
|
"substring": strmangle.Substring,
|
||||||
"singular": strmangle.Singular,
|
"singular": strmangle.Singular,
|
||||||
"plural": strmangle.Plural,
|
"plural": strmangle.Plural,
|
||||||
"titleCase": strmangle.TitleCase,
|
"titleCase": strmangle.TitleCase,
|
||||||
|
@ -113,6 +116,8 @@ var templateFunctions = template.FuncMap{
|
||||||
"filterColumnsByDefault": strmangle.FilterColumnsByDefault,
|
"filterColumnsByDefault": strmangle.FilterColumnsByDefault,
|
||||||
"filterColumnsByAutoIncrement": strmangle.FilterColumnsByAutoIncrement,
|
"filterColumnsByAutoIncrement": strmangle.FilterColumnsByAutoIncrement,
|
||||||
"autoIncPrimaryKey": strmangle.AutoIncPrimaryKey,
|
"autoIncPrimaryKey": strmangle.AutoIncPrimaryKey,
|
||||||
|
"addID": strmangle.AddID,
|
||||||
|
"removeID": strmangle.RemoveID,
|
||||||
|
|
||||||
"randDBStruct": strmangle.RandDBStruct,
|
"randDBStruct": strmangle.RandDBStruct,
|
||||||
"randDBStructSlice": strmangle.RandDBStructSlice,
|
"randDBStructSlice": strmangle.RandDBStructSlice,
|
||||||
|
|
|
@ -21,7 +21,7 @@ func {{$tableNameSingular}}FindX(exec boil.Executor, {{primaryKeyFuncSig .Table.
|
||||||
err := boil.ExecQueryOne(q).Scan(boil.GetStructPointers({{$varNameSingular}}, selectCols...)...)
|
err := boil.ExecQueryOne(q).Scan(boil.GetStructPointers({{$varNameSingular}}, selectCols...)...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {{$varNameSingular}}, nil
|
return {{$varNameSingular}}, nil
|
||||||
|
|
25
templates/relationship_to_one.tpl
Normal file
25
templates/relationship_to_one.tpl
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
{{- if .Table.IsJoinTable -}}
|
||||||
|
{{- else -}}
|
||||||
|
{{- $pkg := .PkgName -}}
|
||||||
|
{{- $localTable := titleCaseSingular .Table.Name -}}
|
||||||
|
{{- range .Table.FKeys -}}
|
||||||
|
{{- $localColumn := .Column | removeID | titleCaseSingular -}}
|
||||||
|
{{- $foreignColumn := .Column | removeID | titleCaseSingular -}}
|
||||||
|
{{- $foreignTable := titleCaseSingular .ForeignTable -}}
|
||||||
|
{{- $varname := camelCaseSingular .ForeignTable -}}
|
||||||
|
{{- $receiver := $localTable | tolower | substring 0 1 -}}
|
||||||
|
// {{$foreignColumn}} fetches the {{$foreignTable}} pointed to by the foreign key.
|
||||||
|
func ({{$receiver}} *{{$localTable}}) {{$foreignColumn}}(exec boil.Executor, selectCols ...string) (*{{$foreignTable}}, error) {
|
||||||
|
{{$varname}} := &{{$foreignTable}}{}
|
||||||
|
|
||||||
|
query := fmt.Sprintf(`select %s from {{.ForeignTable}} where id = $1`, strings.Join(selectCols, `,`))
|
||||||
|
err := exec.QueryRow(query, {{$receiver}}.{{titleCase .Column}}).Scan(boil.GetStructPointers({{$varname}}, selectCols...)...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf(`{{$pkg}}: unable to select from {{.ForeignTable}}: %v`, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {{$varname}}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
{{end -}}
|
||||||
|
{{- end -}}
|
Loading…
Add table
Reference in a new issue