Fixed quotating bug for primary keys
This commit is contained in:
parent
9c493810ec
commit
ba8793ec1a
6 changed files with 39 additions and 26 deletions
|
@ -9,6 +9,11 @@ func (q *Query) Bind(obj interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func getStructPointers(obj interface{}, columns ...string) []interface{} {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkType(obj interface{}) (reflect.Type, bool, error) {
|
||||
val := reflect.ValueOf(obj)
|
||||
typ := val.Type()
|
||||
|
|
|
@ -22,7 +22,6 @@ func generateOutput(cmdData *CmdData, data *tplData) error {
|
|||
if len(cmdData.Templates) == 0 {
|
||||
return errors.New("No template files located for generation")
|
||||
}
|
||||
|
||||
var out [][]byte
|
||||
var imps imports
|
||||
|
||||
|
|
|
@ -222,11 +222,15 @@ func hasPrimaryKey(pKey *dbdrivers.PrimaryKey) bool {
|
|||
// For example, if start was 2 output would be: "colthing=$2 AND colstuff=$3"
|
||||
func wherePrimaryKey(pkeyCols []string, start int) string {
|
||||
var output string
|
||||
for i, c := range pkeyCols {
|
||||
|
||||
cols := make([]string, len(pkeyCols))
|
||||
copy(cols, pkeyCols)
|
||||
|
||||
for i, c := range cols {
|
||||
output = fmt.Sprintf("%s%s=$%d", output, c, start)
|
||||
start++
|
||||
|
||||
if i < len(pkeyCols)-1 {
|
||||
if i < len(cols)-1 {
|
||||
output = fmt.Sprintf("%s AND ", output)
|
||||
}
|
||||
}
|
||||
|
@ -237,11 +241,14 @@ func wherePrimaryKey(pkeyCols []string, start int) string {
|
|||
// primaryKeyStrList returns a list of primary key column names in strings
|
||||
// For example: "col1", "col2", "col3"
|
||||
func primaryKeyStrList(pkeyCols []string) string {
|
||||
for i, c := range pkeyCols {
|
||||
pkeyCols[i] = fmt.Sprintf(`"%s"`, c)
|
||||
cols := make([]string, len(pkeyCols))
|
||||
copy(cols, pkeyCols)
|
||||
|
||||
for i, c := range cols {
|
||||
cols[i] = fmt.Sprintf(`"%s"`, c)
|
||||
}
|
||||
|
||||
return strings.Join(pkeyCols, ", ")
|
||||
return strings.Join(cols, ", ")
|
||||
}
|
||||
|
||||
// commaList returns a comma seperated list: "col1, col2, col3"
|
||||
|
|
|
@ -2,27 +2,28 @@
|
|||
{{- $dbName := singular .Table.Name -}}
|
||||
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
|
||||
// {{$tableNameSingular}}Find retrieves a single record by ID.
|
||||
func {{$tableNameSingular}}Find(id int64, selectList ...string) (*{{$tableNameSingular}}, error) {
|
||||
func {{$tableNameSingular}}Find(id int64, columns ...string) (*{{$tableNameSingular}}, error) {
|
||||
return {{$tableNameSingular}}FindX(boil.GetDB(), id, columns...)
|
||||
}
|
||||
|
||||
func {{$tableNameSingular}}FindX(exec boil.Executor, id int64, columns ...string) (*{{$tableNameSingular}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("{{.PkgName}}: no id provided for {{.Table.Name}} select")
|
||||
}
|
||||
var {{$varNameSingular}} *{{$tableNameSingular}}
|
||||
err := boil.GetDB().Select(&{{$varNameSingular}}, `SELECT {{selectParamNames $dbName .Table.Columns}} WHERE id=$1`, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varNameSingular}}, nil
|
||||
}
|
||||
|
||||
func {{$tableNameSingular}}FindX(exec boil.Executor, id int64, selectList ...string) (*{{$tableNameSingular}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("{{.PkgName}}: no id provided for {{.Table.Name}} select")
|
||||
}
|
||||
var {{$varNameSingular}} *{{$tableNameSingular}}
|
||||
//err := boil.GetDB().Select(&{{$varNameSingular}}, `SELECT {{selectParamNames $dbName .Table.Columns}} WHERE id=$1`, id)
|
||||
|
||||
var {{$varNameSingular}} *{{$tableNameSingular}}
|
||||
mods := []qs.QueryMod{
|
||||
qs.Select(columns...),
|
||||
qs.From("{{.Table.Name}}"),
|
||||
qs.Where("id=$1", id),
|
||||
}
|
||||
|
||||
q := NewQueryX(exec, mods...)
|
||||
|
||||
err := boil.ExecQueryOne(q).Scan(
|
||||
)
|
||||
|
||||
//GetStructPointers({{$varNameSingular}}, columnsthings)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
||||
}
|
||||
|
|
|
@ -3,13 +3,13 @@
|
|||
type {{$tableNameSingular}}Slice []*{{$tableNameSingular}}
|
||||
|
||||
func ({{$varNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func ({{$varNameSingular}}Query) All() ({{$tableNameSingular}}Slice, error) {
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func ({{$varNameSingular}}Query) Count() (int64, error) {
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
|
||||
// Import the postgres driver
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue