2016-02-29 10:39:49 +01:00
|
|
|
package cmds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"go/format"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/pobri19/sqlboiler/dbdrivers"
|
|
|
|
)
|
|
|
|
|
|
|
|
// generateTemplate generates the template associated to the passed in command name.
|
2016-03-01 15:20:13 +01:00
|
|
|
func generateTemplate(commandName string, data *tplData) []byte {
|
|
|
|
template := getTemplate(commandName)
|
2016-02-29 10:39:49 +01:00
|
|
|
|
|
|
|
if template == nil {
|
|
|
|
errorQuit(fmt.Errorf("Unable to find the template: %s", commandName+".tpl"))
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:20:13 +01:00
|
|
|
output, err := processTemplate(template, data)
|
2016-02-29 10:39:49 +01:00
|
|
|
if err != nil {
|
|
|
|
errorQuit(fmt.Errorf("Unable to process the template: %s", err))
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:20:13 +01:00
|
|
|
return output
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 15:20:13 +01:00
|
|
|
// getTemplate returns a pointer to the template matching the passed in name
|
|
|
|
func getTemplate(name string) *template.Template {
|
|
|
|
var tpl *template.Template
|
2016-02-29 10:39:49 +01:00
|
|
|
|
2016-03-01 15:20:13 +01:00
|
|
|
// Find the template that matches the passed in template name
|
|
|
|
for _, t := range templates {
|
|
|
|
if t.Name() == name+".tpl" {
|
|
|
|
tpl = t
|
|
|
|
break
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
2016-03-01 15:20:13 +01:00
|
|
|
}
|
2016-02-29 10:39:49 +01:00
|
|
|
|
2016-03-01 15:20:13 +01:00
|
|
|
return tpl
|
|
|
|
}
|
2016-02-29 10:39:49 +01:00
|
|
|
|
2016-03-01 15:20:13 +01:00
|
|
|
// processTemplate takes a template and returns the output of the template execution.
|
|
|
|
func processTemplate(t *template.Template, data *tplData) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := t.Execute(&buf, data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := format.Source(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 15:20:13 +01:00
|
|
|
return output, nil
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// it into a go styled object variable name of "ColumnName".
|
2016-03-01 16:53:56 +01:00
|
|
|
// makeGoName also fully uppercases "ID" components of names, for example
|
2016-02-29 10:39:49 +01:00
|
|
|
// "column_name_id" to "ColumnNameID".
|
2016-03-01 16:53:56 +01:00
|
|
|
func makeGoName(name string) string {
|
2016-02-29 10:39:49 +01:00
|
|
|
s := strings.Split(name, "_")
|
|
|
|
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if s[i] == "id" {
|
|
|
|
s[i] = "ID"
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s[i] = strings.Title(s[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(s, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeGoVarName 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
|
|
|
|
// "var_name_id" to "varNameID".
|
|
|
|
func makeGoVarName(name string) string {
|
|
|
|
s := strings.Split(name, "_")
|
|
|
|
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
|
|
|
|
if s[i] == "id" && i > 0 {
|
|
|
|
s[i] = "ID"
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s[i] = strings.Title(s[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(s, "")
|
|
|
|
}
|
|
|
|
|
2016-03-01 16:53:56 +01:00
|
|
|
// makeDBName takes a table name in the format of "table_name" and a
|
2016-02-29 10:39:49 +01:00
|
|
|
// column name in the format of "column_name" and returns a name used in the
|
|
|
|
// `db:""` component of an object in the format of "table_name_column_name"
|
2016-03-01 16:53:56 +01:00
|
|
|
func makeDBName(tableName, colName string) string {
|
2016-02-29 10:39:49 +01:00
|
|
|
return tableName + "_" + colName
|
|
|
|
}
|
|
|
|
|
2016-03-01 16:53:56 +01:00
|
|
|
// makeGoInsertParamNames takes a []DBColumn and returns a comma seperated
|
2016-02-29 10:39:49 +01:00
|
|
|
// list of parameter names for the insert statement template.
|
2016-03-01 16:53:56 +01:00
|
|
|
func makeGoInsertParamNames(data []dbdrivers.DBColumn) string {
|
2016-02-29 10:39:49 +01:00
|
|
|
var paramNames string
|
|
|
|
for i := 0; i < len(data); i++ {
|
2016-03-01 16:53:56 +01:00
|
|
|
paramNames = paramNames + data[i].Name
|
2016-02-29 10:39:49 +01:00
|
|
|
if len(data) != i+1 {
|
|
|
|
paramNames = paramNames + ", "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paramNames
|
|
|
|
}
|
|
|
|
|
2016-03-01 16:53:56 +01:00
|
|
|
// makeGoInsertParamFlags takes a []DBColumn and returns a comma seperated
|
2016-02-29 10:39:49 +01:00
|
|
|
// list of parameter flags for the insert statement template.
|
2016-03-01 16:53:56 +01:00
|
|
|
func makeGoInsertParamFlags(data []dbdrivers.DBColumn) string {
|
2016-02-29 10:39:49 +01:00
|
|
|
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 + ", "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paramFlags
|
|
|
|
}
|
|
|
|
|
2016-03-01 16:53:56 +01:00
|
|
|
// makeSelectParamNames takes a []DBColumn and returns a comma seperated
|
2016-02-29 10:39:49 +01:00
|
|
|
// 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, ...
|
2016-03-01 16:53:56 +01:00
|
|
|
func makeSelectParamNames(tableName string, data []dbdrivers.DBColumn) string {
|
2016-02-29 10:39:49 +01:00
|
|
|
var paramNames string
|
|
|
|
for i := 0; i < len(data); i++ {
|
2016-03-01 16:53:56 +01:00
|
|
|
paramNames = fmt.Sprintf("%s%s AS %s", paramNames, data[i].Name,
|
|
|
|
makeDBName(tableName, data[i].Name),
|
2016-02-29 10:39:49 +01:00
|
|
|
)
|
|
|
|
if len(data) != i+1 {
|
|
|
|
paramNames = paramNames + ", "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paramNames
|
|
|
|
}
|