Rename some functions.

- Use more efficient implementation of the template funcs.
This commit is contained in:
Aaron 2016-03-01 08:34:57 -08:00
parent 80e03dc3f7
commit 64ec06b4b4
13 changed files with 92 additions and 101 deletions

View file

@ -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{

View file

@ -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, ", ")
}

View file

@ -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)

View file

@ -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)

View file

@ -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")
}

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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 -}}
}