From 64ec06b4b4dea57f5c56049dc3cc54d16c0bacfc Mon Sep 17 00:00:00 2001 From: Aaron Date: Tue, 1 Mar 2016 08:34:57 -0800 Subject: [PATCH] Rename some functions. - Use more efficient implementation of the template funcs. --- cmds/commands.go | 12 ++--- cmds/template_funcs.go | 85 +++++++++++++++------------------ cmds/templates/all.tpl | 10 ++-- cmds/templates/allby.tpl | 10 ++-- cmds/templates/delete.tpl | 4 +- cmds/templates/fieldsall.tpl | 10 ++-- cmds/templates/fieldsallby.tpl | 10 ++-- cmds/templates/fieldsfind.tpl | 10 ++-- cmds/templates/fieldsfindby.tpl | 10 ++-- cmds/templates/find.tpl | 10 ++-- cmds/templates/findby.tpl | 10 ++-- cmds/templates/insert.tpl | 6 +-- cmds/templates/struct.tpl | 6 +-- 13 files changed, 92 insertions(+), 101 deletions(-) diff --git a/cmds/commands.go b/cmds/commands.go index bf00774..b4a7062 100644 --- a/cmds/commands.go +++ b/cmds/commands.go @@ -63,12 +63,12 @@ var sqlBoilerCommandRuns = map[string]CobraRunFunc{ // sqlBoilerTemplateFuncs is a map of all the functions that get passed into the templates. // If you wish to pass a new function into your own template, add a pointer to it here. var sqlBoilerTemplateFuncs = template.FuncMap{ - "makeGoName": makeGoName, - "makeGoVarName": makeGoVarName, - "makeDBName": makeDBName, - "makeSelectParamNames": makeSelectParamNames, - "makeGoInsertParamNames": makeGoInsertParamNames, - "makeGoInsertParamFlags": makeGoInsertParamFlags, + "titleCase": titleCase, + "camelCase": camelCase, + "makeDBName": makeDBName, + "selectParamNames": selectParamNames, + "insertParamNames": insertParamNames, + "insertParamFlags": insertParamFlags, } var allCmd = &cobra.Command{ diff --git a/cmds/template_funcs.go b/cmds/template_funcs.go index 4a00ce6..0994ccc 100644 --- a/cmds/template_funcs.go +++ b/cmds/template_funcs.go @@ -57,33 +57,33 @@ func processTemplate(t *template.Template, data *tplData) ([]byte, error) { } // it into a go styled object variable name of "ColumnName". -// makeGoName also fully uppercases "ID" components of names, for example +// titleCase also fully uppercases "ID" components of names, for example // "column_name_id" to "ColumnNameID". -func makeGoName(name string) string { - s := strings.Split(name, "_") +func titleCase(name string) string { + splits := strings.Split(name, "_") - for i := 0; i < len(s); i++ { - if s[i] == "id" { - s[i] = "ID" + for i, split := range splits { + if split == "id" { + splits[i] = "ID" continue } - s[i] = strings.Title(s[i]) + + splits[i] = strings.Title(split) } - return strings.Join(s, "") + return strings.Join(splits, "") } -// makeGoVarName takes a variable name in the format of "var_name" and converts +// camelCase takes a variable name in the format of "var_name" and converts // it into a go styled variable name of "varName". -// makeGoVarName also fully uppercases "ID" components of names, for example +// camelCase also fully uppercases "ID" components of names, for example // "var_name_id" to "varNameID". -func makeGoVarName(name string) string { - s := strings.Split(name, "_") +func camelCase(name string) string { + splits := strings.Split(name, "_") - for i := 0; i < len(s); i++ { - - if s[i] == "id" && i > 0 { - s[i] = "ID" + for i, split := range splits { + if split == "id" && i > 0 { + split = "ID" continue } @@ -91,10 +91,10 @@ func makeGoVarName(name string) string { continue } - s[i] = strings.Title(s[i]) + splits[i] = strings.Title(split) } - return strings.Join(s, "") + return strings.Join(splits, "") } // makeDBName takes a table name in the format of "table_name" and a @@ -104,45 +104,36 @@ func makeDBName(tableName, colName string) string { return tableName + "_" + colName } -// makeGoInsertParamNames takes a []DBColumn and returns a comma seperated +// insertParamNames takes a []DBColumn and returns a comma seperated // list of parameter names for the insert statement template. -func makeGoInsertParamNames(data []dbdrivers.DBColumn) string { - var paramNames string - for i := 0; i < len(data); i++ { - paramNames = paramNames + data[i].Name - if len(data) != i+1 { - paramNames = paramNames + ", " - } +func insertParamNames(columns []dbdrivers.DBColumn) string { + names := make([]string, 0, len(columns)) + for _, c := range columns { + names = append(names, c.Name) } - return paramNames + return strings.Join(names, ", ") } -// makeGoInsertParamFlags takes a []DBColumn and returns a comma seperated +// insertParamFlags takes a []DBColumn and returns a comma seperated // list of parameter flags for the insert statement template. -func makeGoInsertParamFlags(data []dbdrivers.DBColumn) string { - var paramFlags string - for i := 0; i < len(data); i++ { - paramFlags = fmt.Sprintf("%s$%d", paramFlags, i+1) - if len(data) != i+1 { - paramFlags = paramFlags + ", " - } +func insertParamFlags(columns []dbdrivers.DBColumn) string { + params := make([]string, 0, len(columns)) + for i := range columns { + params = append(params, fmt.Sprintf("$%d", i+1)) } - return paramFlags + return strings.Join(params, ", ") } -// makeSelectParamNames takes a []DBColumn and returns a comma seperated +// selectParamNames takes a []DBColumn and returns a comma seperated // list of parameter names with for the select statement template. // It also uses the table name to generate the "AS" part of the statement, for // example: var_name AS table_name_var_name, ... -func makeSelectParamNames(tableName string, data []dbdrivers.DBColumn) string { - var paramNames string - for i := 0; i < len(data); i++ { - paramNames = fmt.Sprintf("%s%s AS %s", paramNames, data[i].Name, - makeDBName(tableName, data[i].Name), - ) - if len(data) != i+1 { - paramNames = paramNames + ", " - } +func selectParamNames(tableName string, columns []string) string { + selects := make([]string, 0, len(columns)) + for _, c := range columns { + statement := fmt.Sprintf("%s AS %s", c, makeDBName(tableName, c)) + selects = append(selects, statement) } - return paramNames + + return strings.Join(selects, ", ") } diff --git a/cmds/templates/all.tpl b/cmds/templates/all.tpl index 38f0772..54f0ffd 100644 --- a/cmds/templates/all.tpl +++ b/cmds/templates/all.tpl @@ -1,9 +1,9 @@ {{- $tableName := .TableName -}} -// {{makeGoName $tableName}}All retrieves all records. -func {{makeGoName $tableName}}All(db boil.DB) ([]*{{makeGoName $tableName}}, error) { - {{$varName := makeGoVarName $tableName -}} - var {{$varName}} []*{{makeGoName $tableName}} - err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`) +// {{titleCase $tableName}}All retrieves all records. +func {{titleCase $tableName}}All(db boil.DB) ([]*{{titleCase $tableName}}, error) { + {{$varName := camelCase $tableName -}} + var {{$varName}} []*{{titleCase $tableName}} + err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}}`) if err != nil { return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err) diff --git a/cmds/templates/allby.tpl b/cmds/templates/allby.tpl index 7a2a903..d3d6340 100644 --- a/cmds/templates/allby.tpl +++ b/cmds/templates/allby.tpl @@ -1,9 +1,9 @@ {{- $tableName := .TableName -}} -// {{makeGoName $tableName}}AllBy retrieves all records with the specified column values. -func {{makeGoName $tableName}}AllBy(db boil.DB, columns map[string]interface{}) ([]*{{makeGoName $tableName}}, error) { - {{$varName := makeGoVarName $tableName -}} - var {{$varName}} []*{{makeGoName $tableName}} - err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`) +// {{titleCase $tableName}}AllBy retrieves all records with the specified column values. +func {{titleCase $tableName}}AllBy(db boil.DB, columns map[string]interface{}) ([]*{{titleCase $tableName}}, error) { + {{$varName := camelCase $tableName -}} + var {{$varName}} []*{{titleCase $tableName}} + err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}}`) if err != nil { return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err) diff --git a/cmds/templates/delete.tpl b/cmds/templates/delete.tpl index 1d35d4d..db0bb6a 100644 --- a/cmds/templates/delete.tpl +++ b/cmds/templates/delete.tpl @@ -1,6 +1,6 @@ {{- $tableName := .TableName -}} -// {{makeGoName $tableName}}Delete deletes a single record. -func {{makeGoName $tableName}}Delete(db boil.DB, id int) error { +// {{titleCase $tableName}}Delete deletes a single record. +func {{titleCase $tableName}}Delete(db boil.DB, id int) error { if id == nil { return nil, errors.New("model: no id provided for {{$tableName}} delete") } diff --git a/cmds/templates/fieldsall.tpl b/cmds/templates/fieldsall.tpl index e6c9fd8..4cd0ac0 100644 --- a/cmds/templates/fieldsall.tpl +++ b/cmds/templates/fieldsall.tpl @@ -1,11 +1,11 @@ {{- $tableName := .TableName -}} -// {{makeGoName $tableName}}FieldsAll retrieves the specified columns for all records. +// {{titleCase $tableName}}FieldsAll retrieves the specified columns for all records. // Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve. // For example: friendName string `db:"friend_name"` -func {{makeGoName $tableName}}FieldsAll(db boil.DB, results interface{}) error { - {{$varName := makeGoVarName $tableName -}} - var {{$varName}} []*{{makeGoName $tableName}} - err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`) +func {{titleCase $tableName}}FieldsAll(db boil.DB, results interface{}) error { + {{$varName := camelCase $tableName -}} + var {{$varName}} []*{{titleCase $tableName}} + err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}}`) if err != nil { return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err) diff --git a/cmds/templates/fieldsallby.tpl b/cmds/templates/fieldsallby.tpl index 3bb096e..0eb24c8 100644 --- a/cmds/templates/fieldsallby.tpl +++ b/cmds/templates/fieldsallby.tpl @@ -1,12 +1,12 @@ {{- $tableName := .TableName -}} -// {{makeGoName $tableName}}FieldsAllBy retrieves the specified columns +// {{titleCase $tableName}}FieldsAllBy retrieves the specified columns // for all records with the specified column values. // Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve. // For example: friendName string `db:"friend_name"` -func {{makeGoName $tableName}}FieldsAllBy(db boil.DB, columns map[string]interface{}, results interface{}) error { - {{$varName := makeGoVarName $tableName -}} - var {{$varName}} []*{{makeGoName $tableName}} - err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`) +func {{titleCase $tableName}}FieldsAllBy(db boil.DB, columns map[string]interface{}, results interface{}) error { + {{$varName := camelCase $tableName -}} + var {{$varName}} []*{{titleCase $tableName}} + err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}}`) if err != nil { return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err) diff --git a/cmds/templates/fieldsfind.tpl b/cmds/templates/fieldsfind.tpl index 68439a9..7b47824 100644 --- a/cmds/templates/fieldsfind.tpl +++ b/cmds/templates/fieldsfind.tpl @@ -1,14 +1,14 @@ {{- $tableName := .TableName -}} -// {{makeGoName $tableName}}FieldsFind retrieves the specified columns for a single record by ID. +// {{titleCase $tableName}}FieldsFind retrieves the specified columns for a single record by ID. // Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve. // For example: friendName string `db:"friend_name"` -func {{makeGoName $tableName}}FieldsFind(db boil.DB, id int, results interface{}) (*{{makeGoName $tableName}}, error) { +func {{titleCase $tableName}}FieldsFind(db boil.DB, id int, results interface{}) (*{{titleCase $tableName}}, error) { if id == 0 { return nil, errors.New("model: no id provided for {{$tableName}} select") } - {{$varName := makeGoVarName $tableName}} - var {{$varName}} *{{makeGoName $tableName}} - err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id) + {{$varName := camelCase $tableName}} + var {{$varName}} *{{titleCase $tableName}} + err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}} WHERE id=$1`, id) if err != nil { return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err) diff --git a/cmds/templates/fieldsfindby.tpl b/cmds/templates/fieldsfindby.tpl index b884279..4e3d114 100644 --- a/cmds/templates/fieldsfindby.tpl +++ b/cmds/templates/fieldsfindby.tpl @@ -1,15 +1,15 @@ {{- $tableName := .TableName -}} -// {{makeGoName $tableName}}FieldsFindBy retrieves the specified columns +// {{titleCase $tableName}}FieldsFindBy retrieves the specified columns // for a single record with the specified column values. // Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve. // For example: friendName string `db:"friend_name"` -func {{makeGoName $tableName}}FieldsFindBy(db boil.DB, columns map[string]interface{}, results interface{}) (*{{makeGoName $tableName}}, error) { +func {{titleCase $tableName}}FieldsFindBy(db boil.DB, columns map[string]interface{}, results interface{}) (*{{titleCase $tableName}}, error) { if id == 0 { return nil, errors.New("model: no id provided for {{$tableName}} select") } - {{$varName := makeGoVarName $tableName}} - var {{$varName}} *{{makeGoName $tableName}} - err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id) + {{$varName := camelCase $tableName}} + var {{$varName}} *{{titleCase $tableName}} + err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}} WHERE id=$1`, id) if err != nil { return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err) diff --git a/cmds/templates/find.tpl b/cmds/templates/find.tpl index 0f39bab..fe42fb7 100644 --- a/cmds/templates/find.tpl +++ b/cmds/templates/find.tpl @@ -1,12 +1,12 @@ {{- $tableName := .TableName -}} -// {{makeGoName $tableName}}Find retrieves a single record by ID. -func {{makeGoName $tableName}}Find(db boil.DB, id int) (*{{makeGoName $tableName}}, error) { +// {{titleCase $tableName}}Find retrieves a single record by ID. +func {{titleCase $tableName}}Find(db boil.DB, id int) (*{{titleCase $tableName}}, error) { if id == 0 { return nil, errors.New("model: no id provided for {{$tableName}} select") } - {{$varName := makeGoVarName $tableName}} - var {{$varName}} *{{makeGoName $tableName}} - err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id) + {{$varName := camelCase $tableName}} + var {{$varName}} *{{titleCase $tableName}} + err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}} WHERE id=$1`, id) if err != nil { return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err) diff --git a/cmds/templates/findby.tpl b/cmds/templates/findby.tpl index 4c7e939..ad856e3 100644 --- a/cmds/templates/findby.tpl +++ b/cmds/templates/findby.tpl @@ -1,12 +1,12 @@ {{- $tableName := .TableName -}} -// {{makeGoName $tableName}}FindBy retrieves a single record with the specified column values. -func {{makeGoName $tableName}}FindBy(db boil.DB, columns map[string]interface{}) (*{{makeGoName $tableName}}, error) { +// {{titleCase $tableName}}FindBy retrieves a single record with the specified column values. +func {{titleCase $tableName}}FindBy(db boil.DB, columns map[string]interface{}) (*{{titleCase $tableName}}, error) { if id == 0 { return nil, errors.New("model: no id provided for {{$tableName}} select") } - {{$varName := makeGoVarName $tableName}} - var {{$varName}} *{{makeGoName $tableName}} - err := db.Select(&{{$varName}}, fmt.Sprintf(`SELECT {{makeSelectParamNames $tableName .TableData}} WHERE %s=$1`, column), value) + {{$varName := camelCase $tableName}} + var {{$varName}} *{{titleCase $tableName}} + err := db.Select(&{{$varName}}, fmt.Sprintf(`SELECT {{selectParamNames $tableName .TableData}} WHERE %s=$1`, column), value) if err != nil { return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err) diff --git a/cmds/templates/insert.tpl b/cmds/templates/insert.tpl index 4a6afdd..16a3b3c 100644 --- a/cmds/templates/insert.tpl +++ b/cmds/templates/insert.tpl @@ -1,12 +1,12 @@ {{- $tableName := .TableName -}} -// {{makeGoName $tableName}}Insert inserts a single record. -func {{makeGoName $tableName}}Insert(db boil.DB, o *{{makeGoName $tableName}}) (int, error) { +// {{titleCase $tableName}}Insert inserts a single record. +func {{titleCase $tableName}}Insert(db boil.DB, o *{{titleCase $tableName}}) (int, error) { if o == nil { return 0, errors.New("model: no {{$tableName}} provided for insertion") } var rowID int - err := db.QueryRow(`INSERT INTO {{$tableName}} ({{makeGoInsertParamNames .TableData}}) VALUES({{makeGoInsertParamFlags .TableData}}) RETURNING id`) + err := db.QueryRow(`INSERT INTO {{$tableName}} ({{insertParamNames .TableData}}) VALUES({{insertParamFlags .TableData}}) RETURNING id`) if err != nil { return 0, fmt.Errorf("model: unable to insert {{$tableName}}: %s", err) diff --git a/cmds/templates/struct.tpl b/cmds/templates/struct.tpl index d3d0c28..11a6bfc 100644 --- a/cmds/templates/struct.tpl +++ b/cmds/templates/struct.tpl @@ -1,7 +1,7 @@ {{- $tableName := .TableName -}} -// {{makeGoName $tableName}} is an object representing the database table. -type {{makeGoName $tableName}} struct { +// {{titleCase $tableName}} is an object representing the database table. +type {{titleCase $tableName}} struct { {{range $key, $value := .TableData -}} - {{makeGoName $value.Name}} {{$value.Type}} `db:"{{makeDBName $tableName $value.Name}}" json:"{{$value.Name}}"` + {{titleCase $value.Name}} {{$value.Type}} `db:"{{makeDBName $tableName $value.Name}}" json:"{{$value.Name}}"` {{end -}} }