Merge branch 'master' of github.com:nullbio/sqlboiler
This commit is contained in:
commit
fc1eff4d83
17 changed files with 224 additions and 108 deletions
|
@ -54,7 +54,7 @@ func (p *PostgresDriver) TableNames() ([]string, error) {
|
|||
|
||||
rows, err := p.dbConn.Query(`
|
||||
select table_name from information_schema.tables
|
||||
where table_schema='public' and table_name not like '%migrations%'
|
||||
where table_schema = 'public' and table_name not like '%migrations%'
|
||||
`)
|
||||
|
||||
if err != nil {
|
||||
|
@ -82,7 +82,8 @@ func (p *PostgresDriver) Columns(tableName string) ([]bdb.Column, error) {
|
|||
|
||||
rows, err := p.dbConn.Query(`
|
||||
select column_name, data_type, column_default, is_nullable
|
||||
from information_schema.columns where table_name=$1
|
||||
from information_schema.columns
|
||||
where table_name=$1 and table_schema = 'public'
|
||||
`, tableName)
|
||||
|
||||
if err != nil {
|
||||
|
@ -123,7 +124,7 @@ func (p *PostgresDriver) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, erro
|
|||
query := `
|
||||
select tc.constraint_name
|
||||
from information_schema.table_constraints as tc
|
||||
where tc.table_name = $1 and tc.constraint_type = 'PRIMARY KEY';`
|
||||
where tc.table_name = $1 and tc.constraint_type = 'PRIMARY KEY' and tc.table_schema = 'public';`
|
||||
|
||||
row := p.dbConn.QueryRow(query, tableName)
|
||||
if err = row.Scan(&pkey.Name); err != nil {
|
||||
|
@ -136,7 +137,7 @@ func (p *PostgresDriver) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, erro
|
|||
queryColumns := `
|
||||
select kcu.column_name
|
||||
from information_schema.key_column_usage as kcu
|
||||
where constraint_name = $1;`
|
||||
where constraint_name = $1 and table_schema = 'public';`
|
||||
|
||||
var rows *sql.Rows
|
||||
if rows, err = p.dbConn.Query(queryColumns, pkey.Name); err != nil {
|
||||
|
@ -178,7 +179,7 @@ func (p *PostgresDriver) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, err
|
|||
from information_schema.table_constraints as tc
|
||||
inner join information_schema.key_column_usage as kcu ON tc.constraint_name = kcu.constraint_name
|
||||
inner join information_schema.constraint_column_usage as ccu ON tc.constraint_name = ccu.constraint_name
|
||||
where tc.table_name = $1 and tc.constraint_type = 'FOREIGN KEY';`
|
||||
where tc.table_name = $1 and tc.constraint_type = 'FOREIGN KEY' and tc.table_schema = 'information_schema';`
|
||||
|
||||
var rows *sql.Rows
|
||||
var err error
|
||||
|
|
28
bdb/keys.go
28
bdb/keys.go
|
@ -34,9 +34,33 @@ func (s SQLColumnDef) String() string {
|
|||
return fmt.Sprintf("%s %s", s.Name, s.Type)
|
||||
}
|
||||
|
||||
// SQLColumnDefs has small helper functions
|
||||
type SQLColumnDefs []SQLColumnDef
|
||||
|
||||
// Names returns all the names
|
||||
func (s SQLColumnDefs) Names() []string {
|
||||
names := make([]string, len(s))
|
||||
|
||||
for i, sqlDef := range s {
|
||||
names[i] = sqlDef.Name
|
||||
}
|
||||
|
||||
return names
|
||||
}
|
||||
|
||||
// Types returns all the types
|
||||
func (s SQLColumnDefs) Types() []string {
|
||||
types := make([]string, len(s))
|
||||
|
||||
for i, sqlDef := range s {
|
||||
types[i] = sqlDef.Type
|
||||
}
|
||||
|
||||
return types
|
||||
}
|
||||
|
||||
// SQLColDefinitions creates a definition in sql format for a column
|
||||
// example: id int64, thingName string
|
||||
func SQLColDefinitions(cols []Column, names []string) []SQLColumnDef {
|
||||
func SQLColDefinitions(cols []Column, names []string) SQLColumnDefs {
|
||||
ret := make([]SQLColumnDef, len(names))
|
||||
|
||||
for i, n := range names {
|
||||
|
|
|
@ -4,9 +4,10 @@ package bdb
|
|||
// local table has no id, and the foreign table has an id that matches a column
|
||||
// in the local table.
|
||||
type ToManyRelationship struct {
|
||||
Name string
|
||||
Column string
|
||||
ForeignTable string
|
||||
ForeignColumn string
|
||||
ToJoinTable bool
|
||||
}
|
||||
|
||||
// ToManyRelationships relationship lookups
|
||||
|
@ -24,20 +25,13 @@ func ToManyRelationships(table string, tables []Table) []ToManyRelationship {
|
|||
continue
|
||||
}
|
||||
|
||||
// singularName := strmangle.Singular(table)
|
||||
// standardColName := fmt.Sprintf("%s_id", singularName)
|
||||
|
||||
relationship := ToManyRelationship{
|
||||
Column: f.ForeignColumn,
|
||||
ForeignTable: t.Name,
|
||||
ForeignColumn: f.Column,
|
||||
ToJoinTable: t.IsJoinTable,
|
||||
}
|
||||
|
||||
// if standardColName == f.ForeignColumn {
|
||||
// relationship.Name = table
|
||||
// } else {
|
||||
// relationship.Name = table
|
||||
// }
|
||||
|
||||
relationships = append(relationships, relationship)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,8 @@ func TestToManyRelationships(t *testing.T) {
|
|||
|
||||
tables := []Table{
|
||||
Table{
|
||||
Name: "videos",
|
||||
Name: "videos",
|
||||
IsJoinTable: true,
|
||||
FKeys: []ForeignKey{
|
||||
{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
|
||||
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id"},
|
||||
|
@ -24,8 +25,8 @@ func TestToManyRelationships(t *testing.T) {
|
|||
|
||||
relationships := ToManyRelationships("users", tables)
|
||||
r := relationships[0]
|
||||
if r.Name != "Videos" {
|
||||
t.Error("wrong name:", r.Name)
|
||||
if r.Column != "id" {
|
||||
t.Error("wrong local column:", r.Column)
|
||||
}
|
||||
if r.ForeignTable != "videos" {
|
||||
t.Error("wrong foreign table:", r.ForeignTable)
|
||||
|
@ -33,4 +34,7 @@ func TestToManyRelationships(t *testing.T) {
|
|||
if r.ForeignColumn != "user_id" {
|
||||
t.Error("wrong foreign column:", r.ForeignColumn)
|
||||
}
|
||||
if !r.ToJoinTable {
|
||||
t.Error("expected a join table - kind of - not really but we're faking it")
|
||||
}
|
||||
}
|
||||
|
|
37
output.go
37
output.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/format"
|
||||
|
@ -8,6 +9,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
@ -82,6 +84,10 @@ type executeTemplateData struct {
|
|||
}
|
||||
|
||||
func executeTemplates(e executeTemplateData) error {
|
||||
if e.data.Table.IsJoinTable {
|
||||
return nil
|
||||
}
|
||||
|
||||
var out [][]byte
|
||||
var imps imports
|
||||
|
||||
|
@ -110,6 +116,10 @@ func executeTemplates(e executeTemplateData) error {
|
|||
}
|
||||
|
||||
func executeSingletonTemplates(e executeTemplateData) error {
|
||||
if e.data.Table.IsJoinTable {
|
||||
return nil
|
||||
}
|
||||
|
||||
rgxRemove := regexp.MustCompile(`[0-9]+_`)
|
||||
|
||||
for _, template := range e.templates {
|
||||
|
@ -199,6 +209,8 @@ func outHandler(outFolder string, fileName string, pkgName string, imps imports,
|
|||
return nil
|
||||
}
|
||||
|
||||
var rgxSyntaxError = regexp.MustCompile(`(\d+):\d+: `)
|
||||
|
||||
// executeTemplate takes a template and returns the output of the template
|
||||
// execution.
|
||||
func executeTemplate(t *template.Template, data *templateData) ([]byte, error) {
|
||||
|
@ -209,7 +221,30 @@ func executeTemplate(t *template.Template, data *templateData) ([]byte, error) {
|
|||
|
||||
output, err := format.Source(buf.Bytes())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to format template")
|
||||
matches := rgxSyntaxError.FindStringSubmatch(err.Error())
|
||||
if matches == nil {
|
||||
return nil, errors.Wrap(err, "failed to format template")
|
||||
}
|
||||
|
||||
lineNum, _ := strconv.Atoi(matches[1])
|
||||
scanner := bufio.NewScanner(&buf)
|
||||
errBuf := &bytes.Buffer{}
|
||||
line := 0
|
||||
for ; scanner.Scan(); line++ {
|
||||
if delta := line - lineNum; delta < -5 || delta > 5 {
|
||||
continue
|
||||
}
|
||||
|
||||
if line == lineNum {
|
||||
errBuf.WriteString(">>> ")
|
||||
} else {
|
||||
fmt.Fprintf(errBuf, "% 3d ", line)
|
||||
}
|
||||
errBuf.Write(scanner.Bytes())
|
||||
errBuf.WriteByte('\n')
|
||||
}
|
||||
|
||||
return nil, errors.Wrapf(err, "failed to format template\n\n%s\n", errBuf.Bytes())
|
||||
}
|
||||
|
||||
return output, nil
|
||||
|
|
|
@ -121,17 +121,17 @@ func GenerateParamFlags(colCount int, startAt int) string {
|
|||
|
||||
// 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"
|
||||
func WhereClause(pkeyCols []string, start int) string {
|
||||
func WhereClause(cols []string, start int) string {
|
||||
if start == 0 {
|
||||
panic("0 is not a valid start number for whereClause")
|
||||
}
|
||||
|
||||
cols := make([]string, len(pkeyCols))
|
||||
for i, c := range pkeyCols {
|
||||
cols[i] = fmt.Sprintf("%s=$%d", c, start+i)
|
||||
ret := make([]string, len(cols))
|
||||
for i, c := range cols {
|
||||
ret[i] = fmt.Sprintf(`"%s"=$%d`, c, start+i)
|
||||
}
|
||||
|
||||
return strings.Join(cols, " AND ")
|
||||
return strings.Join(ret, " AND ")
|
||||
}
|
||||
|
||||
// DriverUsesLastInsertID returns whether the database driver supports the
|
||||
|
@ -150,3 +150,20 @@ func DriverUsesLastInsertID(driverName string) bool {
|
|||
func Substring(start, end int, str string) string {
|
||||
return str[start:end]
|
||||
}
|
||||
|
||||
// JoinSlices merges two string slices of equal length
|
||||
func JoinSlices(sep string, a, b []string) []string {
|
||||
lna, lnb := len(a), len(b)
|
||||
if lna != lnb {
|
||||
panic("joinSlices: can only merge slices of same length")
|
||||
} else if lna == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
ret := make([]string, len(a))
|
||||
for i, elem := range a {
|
||||
ret[i] = fmt.Sprintf("%s%s%s", elem, sep, b[i])
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
|
|
@ -148,9 +148,9 @@ func TestWhereClause(t *testing.T) {
|
|||
Start int
|
||||
Should string
|
||||
}{
|
||||
{Cols: []string{"col1"}, Start: 2, Should: "col1=$2"},
|
||||
{Cols: []string{"col1", "col2"}, Start: 4, Should: "col1=$4 AND col2=$5"},
|
||||
{Cols: []string{"col1", "col2", "col3"}, Start: 4, Should: "col1=$4 AND col2=$5 AND col3=$6"},
|
||||
{Cols: []string{"col1"}, Start: 2, Should: `"col1"=$2`},
|
||||
{Cols: []string{"col1", "col2"}, Start: 4, Should: `"col1"=$4 AND "col2"=$5`},
|
||||
{Cols: []string{"col1", "col2", "col3"}, Start: 4, Should: `"col1"=$4 AND "col2"=$5 AND "col3"=$6`},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
|
@ -191,3 +191,32 @@ func TestSubstring(t *testing.T) {
|
|||
t.Errorf("substring was wrong: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJoinSlices(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ret := JoinSlices("", nil, nil)
|
||||
if ret != nil {
|
||||
t.Error("want nil, got:", ret)
|
||||
}
|
||||
|
||||
ret = JoinSlices(" ", []string{"one", "two"}, []string{"three", "four"})
|
||||
if got := ret[0]; got != "one three" {
|
||||
t.Error("ret element was wrong:", got)
|
||||
}
|
||||
if got := ret[1]; got != "two four" {
|
||||
t.Error("ret element was wrong:", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJoinSlicesFail(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Error("did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
JoinSlices("", nil, []string{"hello"})
|
||||
}
|
||||
|
|
|
@ -106,8 +106,9 @@ var templateStringMappers = map[string]func(string) string{
|
|||
var templateFunctions = template.FuncMap{
|
||||
// String ops
|
||||
"substring": strmangle.Substring,
|
||||
"remove": func(rem string, str string) string { return strings.Replace(str, rem, "", -1) },
|
||||
"prefix": func(add string, str string) string { return fmt.Sprintf("%s%s", add, str) },
|
||||
"remove": func(rem, str string) string { return strings.Replace(str, rem, "", -1) },
|
||||
"replace": func(rep, with, str string) string { return strings.Replace(str, rep, with, -1) },
|
||||
"prefix": func(add, str string) string { return fmt.Sprintf("%s%s", add, str) },
|
||||
"quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) },
|
||||
|
||||
// Pluralization
|
||||
|
@ -122,6 +123,7 @@ var templateFunctions = template.FuncMap{
|
|||
|
||||
// String Slice ops
|
||||
"join": func(sep string, slice []string) string { return strings.Join(slice, sep) },
|
||||
"joinSlices": strmangle.JoinSlices,
|
||||
"stringMap": strmangle.StringMap,
|
||||
"hasElement": strmangle.HasElement,
|
||||
"prefixStringSlice": strmangle.PrefixStringSlice,
|
||||
|
@ -138,4 +140,5 @@ var templateFunctions = template.FuncMap{
|
|||
"sqlColDefinitions": bdb.SQLColDefinitions,
|
||||
"sqlColDefStrings": bdb.SQLColDefStrings,
|
||||
"columnNames": bdb.ColumnNames,
|
||||
"toManyRelationships": bdb.ToManyRelationships,
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
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, `,`))
|
||||
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)
|
||||
|
|
|
@ -1,56 +1,58 @@
|
|||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $pkg := .PkgName -}}
|
||||
{{- $localTable := .Table.Name -}}
|
||||
{{- $ltable := .Table.Name | singular | titleCase -}}
|
||||
{{- range $table := .Tables -}}
|
||||
{{- if eq $table.Name $localTable -}}
|
||||
{{- $dot := . }}
|
||||
{{- $table := .Table }}
|
||||
{{- $localTableSing := .Table.Name | singular -}}
|
||||
{{- $localTable := $localTableSing | titleCase -}}
|
||||
{{- $colName := $localTableSing | printf "%s_id" -}}
|
||||
{{- $receiver := .Table.Name | toLower | substring 0 1 -}}
|
||||
{{- range toManyRelationships .Table.Name .Tables -}}
|
||||
{{- if .ToJoinTable -}}
|
||||
{{- else -}}
|
||||
{{ range $fkey := .FKeys -}}
|
||||
{{- if eq $localTable $fkey.ForeignTable -}}
|
||||
{{- $ftable := $table.Name | plural | titleCase -}}
|
||||
{{- $recv := $localTable | substring 0 1 | toLower -}}
|
||||
{{- $fn := $ftable -}}
|
||||
{{- $col := $localTable | singular | printf "%s_id" -}}
|
||||
{{- if eq $col $fkey.Column -}}
|
||||
{{- $col := $localTable -}}
|
||||
{{- end -}}
|
||||
//func ({{$recv}} *{{$ltable}}) {{$fn}}
|
||||
{{ end -}}
|
||||
{{- $foreignTableSing := .ForeignTable | singular}}
|
||||
{{- $foreignTable := $foreignTableSing | titleCase}}
|
||||
{{- $foreignSlice := $foreignTableSing | camelCase | printf "%sSlice"}}
|
||||
{{- $foreignTableHumanReadable := .ForeignTable | replace "_" " " -}}
|
||||
{{- $foreignPluralNoun := .ForeignTable | plural | titleCase -}}
|
||||
{{- $isNormal := eq $colName .ForeignColumn -}}
|
||||
|
||||
{{- if $isNormal -}}
|
||||
// {{$foreignPluralNoun}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}}.
|
||||
func ({{$receiver}} *{{$localTable}}) {{$foreignPluralNoun}}(
|
||||
|
||||
{{- else -}}
|
||||
{{- $fnName := .ForeignColumn | remove "_id" | titleCase | printf "%[2]s%[1]s" $foreignPluralNoun -}}
|
||||
// {{$fnName}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}} via {{.ForeignColumn}} column.
|
||||
func ({{$receiver}} *{{$localTable}}) {{$fnName}}(
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{/*
|
||||
// Challengee fetches the Video pointed to by the foreign key.
|
||||
func (b *Battle) Challengee(exec boil.Executor, selectCols ...string) (*Video, error) {
|
||||
video := &Video{}
|
||||
|
||||
query := fmt.Sprintf(`select %s from videos where id = $1`, strings.Join(selectCols, `,`))
|
||||
err := exec.QueryRow(query, b.ChallengeeID).Scan(boil.GetStructPointers(video, selectCols...)...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`models: unable to select from videos: %v`, err)
|
||||
}
|
||||
exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) {
|
||||
var ret {{$foreignSlice}}
|
||||
|
||||
return video, nil
|
||||
}
|
||||
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $localColumn := .Column | remove "_id" | singular | titleCase -}}
|
||||
{{- $foreignColumn := .Column | remove "_id" | singular | titleCase -}}
|
||||
{{- $foreignTable := .ForeignTable | singular | titleCase -}}
|
||||
{{- $varname := .ForeignTable | singular | camelCase -}}
|
||||
{{- $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...)...)
|
||||
query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} where "{{.ForeignColumn}}"=$1`, strings.Join(selectCols, `","`))
|
||||
rows, err := exec.Query(query, {{.Column | titleCase | printf "%s.%s" $receiver }})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`{{$pkg}}: unable to select from {{.ForeignTable}}: %v`, err)
|
||||
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{.ForeignTable}}: %v`, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
next := new({{$foreignTable}})
|
||||
|
||||
err = rows.Scan(boil.GetStructPointers(next, selectCols...)...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to scan into {{$foreignTable}}: %v`, err)
|
||||
}
|
||||
|
||||
ret = append(ret, next)
|
||||
}
|
||||
|
||||
return {{$varname}}, nil
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{.ForeignTable}}: %v`, err)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
*/}}
|
||||
{{end -}}{{/* if join table */}}
|
||||
{{- end -}}{{/* range relationships */}}
|
||||
{{- end -}}{{/* outer if join table */}}
|
||||
|
|
|
@ -1,18 +1,21 @@
|
|||
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
||||
{{- $dbName := singular .Table.Name -}}
|
||||
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
||||
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
|
||||
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}}
|
||||
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", "}}
|
||||
// {{$tableNameSingular}}Find retrieves a single record by ID.
|
||||
func {{$tableNameSingular}}Find({{sqlColDefinitions .Table.Columns .Table.PKey.Columns | sqlColDefStrings | join " " }}, selectCols ...string) (*{{$tableNameSingular}}, error) {
|
||||
return {{$tableNameSingular}}FindX(boil.GetDB(), {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | join ", "}}, selectCols...)
|
||||
func {{$tableNameSingular}}Find({{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
|
||||
return {{$tableNameSingular}}FindX(boil.GetDB(), {{$pkNames | join ", "}}, selectCols...)
|
||||
}
|
||||
|
||||
func {{$tableNameSingular}}FindX(exec boil.Executor, {{sqlColDefinitions .Table.Columns .Table.PKey.Columns | sqlColDefStrings | join " "}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
|
||||
func {{$tableNameSingular}}FindX(exec boil.Executor, {{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
|
||||
{{$varNameSingular}} := &{{$tableNameSingular}}{}
|
||||
|
||||
mods := []qm.QueryMod{
|
||||
qm.Select(selectCols...),
|
||||
qm.Table("{{.Table.Name}}"),
|
||||
qm.Where("{{whereClause .Table.PKey.Columns 1}}", {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | join ", "}}),
|
||||
qm.Where(`{{whereClause .Table.PKey.Columns 1}}`, {{$pkNames | join ", "}}),
|
||||
}
|
||||
|
||||
q := NewQueryX(exec, mods...)
|
||||
|
|
|
@ -29,7 +29,7 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
|
|||
return err
|
||||
}
|
||||
|
||||
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} (%s) VALUES (%s)`, strings.Join(wl, ","), boil.GenerateParamFlags(len(wl), 1))
|
||||
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), boil.GenerateParamFlags(len(wl), 1))
|
||||
|
||||
{{if driverUsesLastInsertID .DriverName}}
|
||||
if len(returnColumns) != 0 {
|
||||
|
@ -40,7 +40,7 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
|
|||
|
||||
lastId, err := result.lastInsertId()
|
||||
if err != nil || lastId == 0 {
|
||||
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, ","), boil.WhereClause(wl))
|
||||
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, `","`), boil.WhereClause(wl))
|
||||
rows, err := exec.Query(sel, boil.GetStructValues(o, wl...)...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to insert into {{.Table.Name}}: %s", err)
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
||||
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
||||
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
|
||||
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}}
|
||||
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", "}}
|
||||
// Update a single {{$tableNameSingular}} record. It takes a whitelist of
|
||||
// column_name's that should be updated. The primary key will be used to find
|
||||
// the record to update.
|
||||
|
@ -15,12 +18,12 @@ func (o *{{$tableNameSingular}}) UpdateX(exec boil.Executor, whitelist ... strin
|
|||
}
|
||||
|
||||
// UpdateAt updates the {{$tableNameSingular}} using the primary key to find the row to update.
|
||||
func (o *{{$tableNameSingular}}) UpdateAt({{sqlColDefinitions .Table.Columns .Table.PKey.Columns | sqlColDefStrings | join " "}}, whitelist ...string) error {
|
||||
return o.UpdateAtX(boil.GetDB(), {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | join ", "}}, whitelist...)
|
||||
func (o *{{$tableNameSingular}}) UpdateAt({{$pkArgs}}, whitelist ...string) error {
|
||||
return o.UpdateAtX(boil.GetDB(), {{$pkNames | join ", "}}, whitelist...)
|
||||
}
|
||||
|
||||
// UpdateAtX uses an executor to update the {{$tableNameSingular}} using the primary key to find the row to update.
|
||||
func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{sqlColDefinitions .Table.Columns .Table.PKey.Columns | sqlColDefStrings | join " "}}, whitelist ...string) error {
|
||||
func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{$pkArgs}}, whitelist ...string) error {
|
||||
if err := o.doBeforeUpdateHooks(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ func (o *{{$tableNameSingular}}) DeleteX(exec boil.Executor) error {
|
|||
|
||||
mods = append(mods,
|
||||
qm.Table("{{.Table.Name}}"),
|
||||
qm.Where("{{whereClause .Table.PKey.Columns 1}}", {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}),
|
||||
qm.Where(`{{whereClause .Table.PKey.Columns 1}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}),
|
||||
)
|
||||
|
||||
query := NewQueryX(exec, mods...)
|
||||
|
|
|
@ -19,7 +19,7 @@ func Test{{$tableNamePlural}}Bind(t *testing.T) {
|
|||
|
||||
j := {{$tableNameSingular}}{}
|
||||
|
||||
err = {{$tableNamePlural}}(qm.Where("{{whereClause .Table.PKey.Columns 1}}", {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})).Bind(&j)
|
||||
err = {{$tableNamePlural}}(qm.Where(`{{whereClause .Table.PKey.Columns 1}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})).Bind(&j)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to call Bind on {{$tableNameSingular}} single object: %s", err)
|
||||
}
|
||||
|
|
|
@ -11,29 +11,28 @@ type Config struct {
|
|||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// Set the DebugMode to true so we can see generated sql statements
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// Set DebugMode so we can see generated sql statements
|
||||
boil.DebugMode = true
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
var err error
|
||||
|
||||
err = setup()
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to execute setup: %s", err)
|
||||
os.Exit(-1)
|
||||
if err = setup(); err != nil {
|
||||
fmt.Println("Unable to execute setup:", err)
|
||||
os.Exit(-2)
|
||||
}
|
||||
|
||||
err = disableTriggers()
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to disable triggers: %s", err)
|
||||
var code int
|
||||
if err = disableTriggers(); err != nil {
|
||||
fmt.Println("Unable to disable triggers:", err)
|
||||
} else {
|
||||
boil.SetDB(dbConn)
|
||||
code = m.Run()
|
||||
}
|
||||
boil.SetDB(dbConn)
|
||||
code := m.Run()
|
||||
|
||||
err = teardown()
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to execute teardown: %s", err)
|
||||
os.Exit(-1)
|
||||
if err = teardown(); err != nil {
|
||||
fmt.Println("Unable to execute teardown:", err)
|
||||
os.Exit(-3)
|
||||
}
|
||||
|
||||
os.Exit(code)
|
||||
|
@ -184,6 +183,7 @@ func setup() error {
|
|||
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Printf("pg_dump exec failed: %s\n\n%s\n", err, errBuf.String())
|
||||
return err
|
||||
}
|
||||
|
||||
dbConn, err = DBConnect(
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
var testCfg *Config
|
||||
|
||||
var dbConn *sql.DB
|
||||
var (
|
||||
testCfg *Config
|
||||
dbConn *sql.DB
|
||||
)
|
||||
|
||||
func InitViper() error {
|
||||
var err error
|
||||
|
|
Loading…
Add table
Reference in a new issue