Added SortByKeys
* Fixed slice allocations in strmangle helpers * Removed duplicate commaList function
This commit is contained in:
parent
4ad069f64d
commit
37a333f6ff
7 changed files with 74 additions and 36 deletions
|
@ -74,6 +74,28 @@ func NonZeroDefaultSet(defaults []string, obj interface{}) []string {
|
|||
return c
|
||||
}
|
||||
|
||||
func SortByKeys(keys []string, strs []string) []string {
|
||||
c := make([]string, len(strs))
|
||||
|
||||
index := 0
|
||||
Outer:
|
||||
for _, v := range keys {
|
||||
for _, k := range strs {
|
||||
if v == k {
|
||||
c[index] = v
|
||||
index++
|
||||
|
||||
if index > len(strs)-1 {
|
||||
break Outer
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// GenerateParamFlags generates the SQL statement parameter flags
|
||||
// For example, $1,$2,$3 etc. It will start counting at startAt.
|
||||
func GenerateParamFlags(colCount int, startAt int) string {
|
||||
|
|
|
@ -142,6 +142,39 @@ func TestNonZeroDefaultSet(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSortByKeys(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
Keys []string
|
||||
Strs []string
|
||||
Ret []string
|
||||
}{
|
||||
{
|
||||
[]string{"id", "name", "thing", "stuff"},
|
||||
[]string{"thing", "stuff", "name", "id"},
|
||||
[]string{"id", "name", "thing", "stuff"},
|
||||
},
|
||||
{
|
||||
[]string{"id", "name", "thing", "stuff"},
|
||||
[]string{"id", "name", "thing", "stuff"},
|
||||
[]string{"id", "name", "thing", "stuff"},
|
||||
},
|
||||
{
|
||||
[]string{"id", "name", "thing", "stuff"},
|
||||
[]string{"stuff", "thing"},
|
||||
[]string{"thing", "stuff"},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
z := SortByKeys(test.Keys, test.Strs)
|
||||
if !reflect.DeepEqual(test.Ret, z) {
|
||||
t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.Ret, z)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWherePrimaryKeyIn(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
|
@ -156,7 +156,6 @@ var sqlBoilerTemplateFuncs = template.FuncMap{
|
|||
"primaryKeyFlagIndex": strmangle.PrimaryKeyFlagIndex,
|
||||
"updateParamNames": strmangle.UpdateParamNames,
|
||||
"updateParamVariables": strmangle.UpdateParamVariables,
|
||||
"primaryKeyStrList": strmangle.PrimaryKeyStrList,
|
||||
"supportsResultObject": strmangle.SupportsResultObject,
|
||||
"filterColumnsByDefault": strmangle.FilterColumnsByDefault,
|
||||
"filterColumnsByAutoIncrement": strmangle.FilterColumnsByAutoIncrement,
|
||||
|
|
|
@ -65,7 +65,7 @@ func (o {{$varNameSingular}}Slice) DeleteAllX(exec boil.Executor) error {
|
|||
var mods []qs.QueryMod
|
||||
|
||||
args := o.inPrimaryKeyArgs()
|
||||
in := boil.WherePrimaryKeyIn(len(o), {{primaryKeyStrList .Table.PKey.Columns}})
|
||||
in := boil.WherePrimaryKeyIn(len(o), {{commaList .Table.PKey.Columns}})
|
||||
|
||||
mods = append(mods,
|
||||
qs.Table("{{.Table.Name}}"),
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
|
||||
var {{$varNameSingular}}Columns = []string{{"{"}}{{commaList .Table.Columns}}{{"}"}}
|
||||
var {{$varNameSingular}}ColumnsWithoutDefault = []string{{"{"}}{{filterColumnsByDefault .Table.Columns false}}{{"}"}}
|
||||
var {{$varNameSingular}}ColumnsWithDefault = []string{{"{"}}{{filterColumnsByDefault .Table.Columns true}}{{"}"}}
|
||||
var {{$varNameSingular}}PrimaryKeyColumns = []string{{"{"}}{{primaryKeyStrList .Table.PKey.Columns}}{{"}"}}
|
||||
var {{$varNameSingular}}PrimaryKeyColumns = []string{{"{"}}{{commaList .Table.PKey.Columns}}{{"}"}}
|
||||
var {{$varNameSingular}}AutoIncrementColumns = []string{{"{"}}{{filterColumnsByAutoIncrement .Table.Columns}}{{"}"}}
|
||||
var {{$varNameSingular}}AutoIncPrimaryKey = "{{autoIncPrimaryKey .Table.Columns .Table.PKey}}"
|
||||
|
||||
|
|
|
@ -70,7 +70,7 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
|
|||
{{end}}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, ins)
|
||||
fmt.Fprintln(boil.DebugWriter, ins, boil.GetStructValues(o, wl...))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -158,8 +158,7 @@ func UpdateParamVariables(prefix string, columns []dbdrivers.Column, pkeyColumns
|
|||
if IsPrimaryKey(c.Name, pkeyColumns) {
|
||||
continue
|
||||
}
|
||||
n := prefix + TitleCase(c.Name)
|
||||
names = append(names, n)
|
||||
names = append(names, fmt.Sprintf("%s%s", prefix, TitleCase(c.Name)))
|
||||
}
|
||||
|
||||
return strings.Join(names, ", ")
|
||||
|
@ -179,9 +178,9 @@ func IsPrimaryKey(col string, pkeyCols []string) bool {
|
|||
// InsertParamNames takes a []Column and returns a comma seperated
|
||||
// list of parameter names for the insert statement template.
|
||||
func InsertParamNames(columns []dbdrivers.Column) string {
|
||||
names := make([]string, 0, len(columns))
|
||||
for _, c := range columns {
|
||||
names = append(names, c.Name)
|
||||
names := make([]string, len(columns))
|
||||
for i, c := range columns {
|
||||
names[i] = c.Name
|
||||
}
|
||||
return strings.Join(names, ", ")
|
||||
}
|
||||
|
@ -189,9 +188,9 @@ func InsertParamNames(columns []dbdrivers.Column) string {
|
|||
// InsertParamFlags takes a []Column and returns a comma seperated
|
||||
// list of parameter flags for the insert statement template.
|
||||
func InsertParamFlags(columns []dbdrivers.Column) string {
|
||||
params := make([]string, 0, len(columns))
|
||||
params := make([]string, len(columns))
|
||||
for i := range columns {
|
||||
params = append(params, fmt.Sprintf("$%d", i+1))
|
||||
params[i] = fmt.Sprintf("$%d", i+1)
|
||||
}
|
||||
return strings.Join(params, ", ")
|
||||
}
|
||||
|
@ -200,11 +199,10 @@ func InsertParamFlags(columns []dbdrivers.Column) string {
|
|||
// comma seperated list of parameter variable names for the insert statement.
|
||||
// For example: prefix("o."), column("name_id") -> "o.NameID, ..."
|
||||
func InsertParamVariables(prefix string, columns []dbdrivers.Column) string {
|
||||
names := make([]string, 0, len(columns))
|
||||
names := make([]string, len(columns))
|
||||
|
||||
for _, c := range columns {
|
||||
n := prefix + TitleCase(c.Name)
|
||||
names = append(names, n)
|
||||
for i, c := range columns {
|
||||
names[i] = prefix + TitleCase(c.Name)
|
||||
}
|
||||
|
||||
return strings.Join(names, ", ")
|
||||
|
@ -215,10 +213,9 @@ func InsertParamVariables(prefix string, columns []dbdrivers.Column) string {
|
|||
// It also uses the table name to generate the "AS" part of the statement, for
|
||||
// example: var_name AS table_name_var_name, ...
|
||||
func SelectParamNames(tableName string, columns []dbdrivers.Column) string {
|
||||
selects := make([]string, 0, len(columns))
|
||||
for _, c := range columns {
|
||||
statement := fmt.Sprintf("%s AS %s", c.Name, MakeDBName(tableName, c.Name))
|
||||
selects = append(selects, statement)
|
||||
selects := make([]string, len(columns))
|
||||
for i, c := range columns {
|
||||
selects[i] = fmt.Sprintf("%s AS %s", c.Name, MakeDBName(tableName, c.Name))
|
||||
}
|
||||
|
||||
return strings.Join(selects, ", ")
|
||||
|
@ -227,10 +224,9 @@ func SelectParamNames(tableName string, columns []dbdrivers.Column) string {
|
|||
// ScanParamNames takes a []Column and returns a comma seperated
|
||||
// list of parameter names for use in a db.Scan() call.
|
||||
func ScanParamNames(object string, columns []dbdrivers.Column) string {
|
||||
scans := make([]string, 0, len(columns))
|
||||
for _, c := range columns {
|
||||
statement := fmt.Sprintf("&%s.%s", object, TitleCase(c.Name))
|
||||
scans = append(scans, statement)
|
||||
scans := make([]string, len(columns))
|
||||
for i, c := range columns {
|
||||
scans[i] = fmt.Sprintf("&%s.%s", object, TitleCase(c.Name))
|
||||
}
|
||||
|
||||
return strings.Join(scans, ", ")
|
||||
|
@ -294,19 +290,6 @@ func WherePrimaryKey(pkeyCols []string, start int) string {
|
|||
return output
|
||||
}
|
||||
|
||||
// PrimaryKeyStrList returns a list of primary key column names in strings
|
||||
// For example: "col1", "col2", "col3"
|
||||
func PrimaryKeyStrList(pkeyCols []string) string {
|
||||
cols := make([]string, len(pkeyCols))
|
||||
copy(cols, pkeyCols)
|
||||
|
||||
for i, c := range cols {
|
||||
cols[i] = fmt.Sprintf(`"%s"`, c)
|
||||
}
|
||||
|
||||
return strings.Join(cols, ", ")
|
||||
}
|
||||
|
||||
// AutoIncPrimKey returns the auto-increment primary key column name or an empty string
|
||||
func AutoIncPrimaryKey(cols []dbdrivers.Column, pkey *dbdrivers.PrimaryKey) string {
|
||||
if pkey == nil {
|
||||
|
|
Loading…
Add table
Reference in a new issue