Rename some functions.
- Use more efficient implementation of the template funcs.
This commit is contained in:
parent
80e03dc3f7
commit
64ec06b4b4
13 changed files with 92 additions and 101 deletions
|
@ -63,12 +63,12 @@ var sqlBoilerCommandRuns = map[string]CobraRunFunc{
|
||||||
// sqlBoilerTemplateFuncs is a map of all the functions that get passed into the templates.
|
// 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.
|
// If you wish to pass a new function into your own template, add a pointer to it here.
|
||||||
var sqlBoilerTemplateFuncs = template.FuncMap{
|
var sqlBoilerTemplateFuncs = template.FuncMap{
|
||||||
"makeGoName": makeGoName,
|
"titleCase": titleCase,
|
||||||
"makeGoVarName": makeGoVarName,
|
"camelCase": camelCase,
|
||||||
"makeDBName": makeDBName,
|
"makeDBName": makeDBName,
|
||||||
"makeSelectParamNames": makeSelectParamNames,
|
"selectParamNames": selectParamNames,
|
||||||
"makeGoInsertParamNames": makeGoInsertParamNames,
|
"insertParamNames": insertParamNames,
|
||||||
"makeGoInsertParamFlags": makeGoInsertParamFlags,
|
"insertParamFlags": insertParamFlags,
|
||||||
}
|
}
|
||||||
|
|
||||||
var allCmd = &cobra.Command{
|
var allCmd = &cobra.Command{
|
||||||
|
|
|
@ -57,33 +57,33 @@ func processTemplate(t *template.Template, data *tplData) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// it into a go styled object variable name of "ColumnName".
|
// 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".
|
// "column_name_id" to "ColumnNameID".
|
||||||
func makeGoName(name string) string {
|
func titleCase(name string) string {
|
||||||
s := strings.Split(name, "_")
|
splits := strings.Split(name, "_")
|
||||||
|
|
||||||
for i := 0; i < len(s); i++ {
|
for i, split := range splits {
|
||||||
if s[i] == "id" {
|
if split == "id" {
|
||||||
s[i] = "ID"
|
splits[i] = "ID"
|
||||||
continue
|
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".
|
// 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".
|
// "var_name_id" to "varNameID".
|
||||||
func makeGoVarName(name string) string {
|
func camelCase(name string) string {
|
||||||
s := strings.Split(name, "_")
|
splits := strings.Split(name, "_")
|
||||||
|
|
||||||
for i := 0; i < len(s); i++ {
|
for i, split := range splits {
|
||||||
|
if split == "id" && i > 0 {
|
||||||
if s[i] == "id" && i > 0 {
|
split = "ID"
|
||||||
s[i] = "ID"
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,10 +91,10 @@ func makeGoVarName(name string) string {
|
||||||
continue
|
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
|
// 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
|
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.
|
// list of parameter names for the insert statement template.
|
||||||
func makeGoInsertParamNames(data []dbdrivers.DBColumn) string {
|
func insertParamNames(columns []dbdrivers.DBColumn) string {
|
||||||
var paramNames string
|
names := make([]string, 0, len(columns))
|
||||||
for i := 0; i < len(data); i++ {
|
for _, c := range columns {
|
||||||
paramNames = paramNames + data[i].Name
|
names = append(names, c.Name)
|
||||||
if len(data) != i+1 {
|
|
||||||
paramNames = paramNames + ", "
|
|
||||||
}
|
}
|
||||||
}
|
return strings.Join(names, ", ")
|
||||||
return paramNames
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// list of parameter flags for the insert statement template.
|
||||||
func makeGoInsertParamFlags(data []dbdrivers.DBColumn) string {
|
func insertParamFlags(columns []dbdrivers.DBColumn) string {
|
||||||
var paramFlags string
|
params := make([]string, 0, len(columns))
|
||||||
for i := 0; i < len(data); i++ {
|
for i := range columns {
|
||||||
paramFlags = fmt.Sprintf("%s$%d", paramFlags, i+1)
|
params = append(params, fmt.Sprintf("$%d", i+1))
|
||||||
if len(data) != i+1 {
|
|
||||||
paramFlags = paramFlags + ", "
|
|
||||||
}
|
}
|
||||||
}
|
return strings.Join(params, ", ")
|
||||||
return paramFlags
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// 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
|
// It also uses the table name to generate the "AS" part of the statement, for
|
||||||
// example: var_name AS table_name_var_name, ...
|
// example: var_name AS table_name_var_name, ...
|
||||||
func makeSelectParamNames(tableName string, data []dbdrivers.DBColumn) string {
|
func selectParamNames(tableName string, columns []string) string {
|
||||||
var paramNames string
|
selects := make([]string, 0, len(columns))
|
||||||
for i := 0; i < len(data); i++ {
|
for _, c := range columns {
|
||||||
paramNames = fmt.Sprintf("%s%s AS %s", paramNames, data[i].Name,
|
statement := fmt.Sprintf("%s AS %s", c, makeDBName(tableName, c))
|
||||||
makeDBName(tableName, data[i].Name),
|
selects = append(selects, statement)
|
||||||
)
|
|
||||||
if len(data) != i+1 {
|
|
||||||
paramNames = paramNames + ", "
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return paramNames
|
return strings.Join(selects, ", ")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
{{- $tableName := .TableName -}}
|
{{- $tableName := .TableName -}}
|
||||||
// {{makeGoName $tableName}}All retrieves all records.
|
// {{titleCase $tableName}}All retrieves all records.
|
||||||
func {{makeGoName $tableName}}All(db boil.DB) ([]*{{makeGoName $tableName}}, error) {
|
func {{titleCase $tableName}}All(db boil.DB) ([]*{{titleCase $tableName}}, error) {
|
||||||
{{$varName := makeGoVarName $tableName -}}
|
{{$varName := camelCase $tableName -}}
|
||||||
var {{$varName}} []*{{makeGoName $tableName}}
|
var {{$varName}} []*{{titleCase $tableName}}
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}}`)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
{{- $tableName := .TableName -}}
|
{{- $tableName := .TableName -}}
|
||||||
// {{makeGoName $tableName}}AllBy retrieves all records with the specified column values.
|
// {{titleCase $tableName}}AllBy retrieves all records with the specified column values.
|
||||||
func {{makeGoName $tableName}}AllBy(db boil.DB, columns map[string]interface{}) ([]*{{makeGoName $tableName}}, error) {
|
func {{titleCase $tableName}}AllBy(db boil.DB, columns map[string]interface{}) ([]*{{titleCase $tableName}}, error) {
|
||||||
{{$varName := makeGoVarName $tableName -}}
|
{{$varName := camelCase $tableName -}}
|
||||||
var {{$varName}} []*{{makeGoName $tableName}}
|
var {{$varName}} []*{{titleCase $tableName}}
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}}`)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{{- $tableName := .TableName -}}
|
{{- $tableName := .TableName -}}
|
||||||
// {{makeGoName $tableName}}Delete deletes a single record.
|
// {{titleCase $tableName}}Delete deletes a single record.
|
||||||
func {{makeGoName $tableName}}Delete(db boil.DB, id int) error {
|
func {{titleCase $tableName}}Delete(db boil.DB, id int) error {
|
||||||
if id == nil {
|
if id == nil {
|
||||||
return nil, errors.New("model: no id provided for {{$tableName}} delete")
|
return nil, errors.New("model: no id provided for {{$tableName}} delete")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
{{- $tableName := .TableName -}}
|
{{- $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.
|
// 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"`
|
// For example: friendName string `db:"friend_name"`
|
||||||
func {{makeGoName $tableName}}FieldsAll(db boil.DB, results interface{}) error {
|
func {{titleCase $tableName}}FieldsAll(db boil.DB, results interface{}) error {
|
||||||
{{$varName := makeGoVarName $tableName -}}
|
{{$varName := camelCase $tableName -}}
|
||||||
var {{$varName}} []*{{makeGoName $tableName}}
|
var {{$varName}} []*{{titleCase $tableName}}
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}}`)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
{{- $tableName := .TableName -}}
|
{{- $tableName := .TableName -}}
|
||||||
// {{makeGoName $tableName}}FieldsAllBy retrieves the specified columns
|
// {{titleCase $tableName}}FieldsAllBy retrieves the specified columns
|
||||||
// for all records with the specified column values.
|
// 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.
|
// 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"`
|
// For example: friendName string `db:"friend_name"`
|
||||||
func {{makeGoName $tableName}}FieldsAllBy(db boil.DB, columns map[string]interface{}, results interface{}) error {
|
func {{titleCase $tableName}}FieldsAllBy(db boil.DB, columns map[string]interface{}, results interface{}) error {
|
||||||
{{$varName := makeGoVarName $tableName -}}
|
{{$varName := camelCase $tableName -}}
|
||||||
var {{$varName}} []*{{makeGoName $tableName}}
|
var {{$varName}} []*{{titleCase $tableName}}
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}}`)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
{{- $tableName := .TableName -}}
|
{{- $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.
|
// 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"`
|
// 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 {
|
if id == 0 {
|
||||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||||
}
|
}
|
||||||
{{$varName := makeGoVarName $tableName}}
|
{{$varName := camelCase $tableName}}
|
||||||
var {{$varName}} *{{makeGoName $tableName}}
|
var {{$varName}} *{{titleCase $tableName}}
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
{{- $tableName := .TableName -}}
|
{{- $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.
|
// 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.
|
// 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"`
|
// 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 {
|
if id == 0 {
|
||||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||||
}
|
}
|
||||||
{{$varName := makeGoVarName $tableName}}
|
{{$varName := camelCase $tableName}}
|
||||||
var {{$varName}} *{{makeGoName $tableName}}
|
var {{$varName}} *{{titleCase $tableName}}
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
{{- $tableName := .TableName -}}
|
{{- $tableName := .TableName -}}
|
||||||
// {{makeGoName $tableName}}Find retrieves a single record by ID.
|
// {{titleCase $tableName}}Find retrieves a single record by ID.
|
||||||
func {{makeGoName $tableName}}Find(db boil.DB, id int) (*{{makeGoName $tableName}}, error) {
|
func {{titleCase $tableName}}Find(db boil.DB, id int) (*{{titleCase $tableName}}, error) {
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||||
}
|
}
|
||||||
{{$varName := makeGoVarName $tableName}}
|
{{$varName := camelCase $tableName}}
|
||||||
var {{$varName}} *{{makeGoName $tableName}}
|
var {{$varName}} *{{titleCase $tableName}}
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
{{- $tableName := .TableName -}}
|
{{- $tableName := .TableName -}}
|
||||||
// {{makeGoName $tableName}}FindBy retrieves a single record with the specified column values.
|
// {{titleCase $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) {
|
func {{titleCase $tableName}}FindBy(db boil.DB, columns map[string]interface{}) (*{{titleCase $tableName}}, error) {
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||||
}
|
}
|
||||||
{{$varName := makeGoVarName $tableName}}
|
{{$varName := camelCase $tableName}}
|
||||||
var {{$varName}} *{{makeGoName $tableName}}
|
var {{$varName}} *{{titleCase $tableName}}
|
||||||
err := db.Select(&{{$varName}}, fmt.Sprintf(`SELECT {{makeSelectParamNames $tableName .TableData}} WHERE %s=$1`, column), value)
|
err := db.Select(&{{$varName}}, fmt.Sprintf(`SELECT {{selectParamNames $tableName .TableData}} WHERE %s=$1`, column), value)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
{{- $tableName := .TableName -}}
|
{{- $tableName := .TableName -}}
|
||||||
// {{makeGoName $tableName}}Insert inserts a single record.
|
// {{titleCase $tableName}}Insert inserts a single record.
|
||||||
func {{makeGoName $tableName}}Insert(db boil.DB, o *{{makeGoName $tableName}}) (int, error) {
|
func {{titleCase $tableName}}Insert(db boil.DB, o *{{titleCase $tableName}}) (int, error) {
|
||||||
if o == nil {
|
if o == nil {
|
||||||
return 0, errors.New("model: no {{$tableName}} provided for insertion")
|
return 0, errors.New("model: no {{$tableName}} provided for insertion")
|
||||||
}
|
}
|
||||||
|
|
||||||
var rowID int
|
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 {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("model: unable to insert {{$tableName}}: %s", err)
|
return 0, fmt.Errorf("model: unable to insert {{$tableName}}: %s", err)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{{- $tableName := .TableName -}}
|
{{- $tableName := .TableName -}}
|
||||||
// {{makeGoName $tableName}} is an object representing the database table.
|
// {{titleCase $tableName}} is an object representing the database table.
|
||||||
type {{makeGoName $tableName}} struct {
|
type {{titleCase $tableName}} struct {
|
||||||
{{range $key, $value := .TableData -}}
|
{{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 -}}
|
{{end -}}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue