Fixed assign defaultRun bug, fixed "all" order
* Organized files better * More comments
This commit is contained in:
parent
20720b2cc0
commit
b5409c7332
14 changed files with 221 additions and 384 deletions
60
cmds/all.go
60
cmds/all.go
|
@ -2,60 +2,44 @@ package cmds
|
|||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
// // init the "all" command
|
||||
// func init() {
|
||||
// SQLBoiler.AddCommand(allCmd)
|
||||
// allCmd.Run = allRun
|
||||
// }
|
||||
//
|
||||
// // var allCmd = &cobra.Command{
|
||||
// // Use: "all",
|
||||
// // Short: "Generate all templates from table definitions",
|
||||
// // }
|
||||
//
|
||||
// // allRun executes every sqlboiler command, starting with structs
|
||||
// func allRun(cmd *cobra.Command, args []string) {
|
||||
// err := outHandler(generateStructs())
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
//
|
||||
// err = outHandler(generateDeletes())
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
//
|
||||
// err = outHandler(generateInserts())
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
//
|
||||
// err = outHandler(generateSelects())
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
// }
|
||||
var allCmd = &cobra.Command{
|
||||
Use: "all",
|
||||
Short: "Generate all templates from table definitions",
|
||||
}
|
||||
|
||||
// allRun executes every sqlboiler command, starting with structs
|
||||
// allRun executes every sqlboiler command, starting with structs.
|
||||
func allRun(cmd *cobra.Command, args []string) {
|
||||
// Exclude these commands from the output
|
||||
skipTemplates := []string{
|
||||
"all",
|
||||
}
|
||||
|
||||
var templateNames []string
|
||||
|
||||
// Build a list of template names
|
||||
for _, c := range sqlBoilerCommands {
|
||||
skip := false
|
||||
for _, s := range skipTemplates {
|
||||
if s == c.Name() {
|
||||
// Skip name if it's in the exclude list.
|
||||
// Also skip "struct" so that it can be added manually at the beginning
|
||||
// of the slice. Structs should always go to the top of the file.
|
||||
if s == c.Name() || c.Name() == "struct" {
|
||||
skip = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if skip {
|
||||
continue
|
||||
if !skip {
|
||||
templateNames = append(templateNames, c.Name())
|
||||
}
|
||||
}
|
||||
|
||||
err := outHandler(generateTemplate(c.Name()))
|
||||
// Prepend "struct" command to templateNames slice
|
||||
templateNames = append([]string{"struct"}, templateNames...)
|
||||
|
||||
// Loop through and generate every command template (excluding skipTemplates)
|
||||
for _, n := range templateNames {
|
||||
err := outHandler(generateTemplate(n))
|
||||
if err != nil {
|
||||
errorQuit(err)
|
||||
}
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
package cmds
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type CobraRunFunc func(cmd *cobra.Command, args []string)
|
||||
|
||||
// sqlBoilerCommands points each command to its cobra.Command variable.
|
||||
//
|
||||
// If you would like to add your own custom command, add it to this
|
||||
// map, and point it to your <commandName>Cmd variable.
|
||||
//
|
||||
// Command names should match the template file name (without the file extension).
|
||||
var sqlBoilerCommands = map[string]*cobra.Command{
|
||||
"all": allCmd,
|
||||
"insert": insertCmd,
|
||||
|
@ -18,12 +21,13 @@ var sqlBoilerCommands = map[string]*cobra.Command{
|
|||
}
|
||||
|
||||
// sqlBoilerCommandRuns points each command to its custom run function.
|
||||
// If a run function is not defined here, it will use the
|
||||
// defaultRun run function.
|
||||
// If a run function is not defined here, it will use the defaultRun() default run function.
|
||||
var sqlBoilerCommandRuns = map[string]CobraRunFunc{
|
||||
"all": allRun,
|
||||
}
|
||||
|
||||
// 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{
|
||||
"makeGoColName": makeGoColName,
|
||||
"makeGoVarName": makeGoVarName,
|
||||
|
@ -33,11 +37,6 @@ var sqlBoilerTemplateFuncs = template.FuncMap{
|
|||
"makeGoInsertParamFlags": makeGoInsertParamFlags,
|
||||
}
|
||||
|
||||
var allCmd = &cobra.Command{
|
||||
Use: "all",
|
||||
Short: "Generate all templates from table definitions",
|
||||
}
|
||||
|
||||
var insertCmd = &cobra.Command{
|
||||
Use: "insert",
|
||||
Short: "Generate insert statement helpers from table definitions",
|
||||
|
@ -57,28 +56,3 @@ var structCmd = &cobra.Command{
|
|||
Use: "struct",
|
||||
Short: "Generate structs from table definitions",
|
||||
}
|
||||
|
||||
func defaultRun(cmd *cobra.Command, args []string) {
|
||||
err := outHandler(generateTemplate(cmd.Name()))
|
||||
if err != nil {
|
||||
errorQuit(fmt.Errorf("Unable to generate the template for command %s: %s", cmd.Name(), err))
|
||||
}
|
||||
}
|
||||
|
||||
func generateTemplate(name string) [][]byte {
|
||||
var template *template.Template
|
||||
for _, t := range templates {
|
||||
fmt.Printf("File name: %s", t.Name())
|
||||
if t.Name() == name+".tpl" {
|
||||
template = t
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
outputs, err := processTemplate(template)
|
||||
if err != nil {
|
||||
errorQuit(fmt.Errorf("Unable to process the template: %s", err))
|
||||
}
|
||||
|
||||
return outputs
|
||||
}
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
package cmds
|
||||
|
||||
//
|
||||
// // init the "delete" command
|
||||
// func init() {
|
||||
// SQLBoiler.AddCommand(deleteCmd)
|
||||
// deleteCmd.Run = deleteRun
|
||||
// }
|
||||
//
|
||||
// var deleteCmd = &cobra.Command{
|
||||
// Use: "delete",
|
||||
// Short: "Generate delete statement helpers from table definitions",
|
||||
// }
|
||||
//
|
||||
// // deleteRun executes the delete command, and generates the delete statement
|
||||
// // boilerplate from the template file.
|
||||
// func deleteRun(cmd *cobra.Command, args []string) {
|
||||
// err := outHandler(generateDeletes())
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // generateDeletes returns a slice of each template execution result.
|
||||
// // Each of these results holds a delete statement generated from the delete template.
|
||||
// func generateDeletes() [][]byte {
|
||||
// t, err := template.New("delete.tpl").Funcs(template.FuncMap{
|
||||
// "makeGoColName": makeGoColName,
|
||||
// }).ParseFiles("templates/delete.tpl")
|
||||
//
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
//
|
||||
// outputs, err := processTemplate(t)
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
//
|
||||
// return outputs
|
||||
// }
|
|
@ -1,43 +0,0 @@
|
|||
package cmds
|
||||
|
||||
// // init the "insert" command
|
||||
// func init() {
|
||||
// SQLBoiler.AddCommand(insertCmd)
|
||||
// insertCmd.Run = insertRun
|
||||
// }
|
||||
//
|
||||
// var insertCmd = &cobra.Command{
|
||||
// Use: "insert",
|
||||
// Short: "Generate insert statement helpers from table definitions",
|
||||
// }
|
||||
//
|
||||
// // insertRun executes the insert command, and generates the insert statement
|
||||
// // boilerplate from the template file.
|
||||
// func insertRun(cmd *cobra.Command, args []string) {
|
||||
// err := outHandler(generateInserts())
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // generateInserts returns a slice of each template execution result.
|
||||
// // Each of these results holds an insert statement generated from the insert template.
|
||||
// func generateInserts() [][]byte {
|
||||
// t, err := template.New("insert.tpl").Funcs(template.FuncMap{
|
||||
// "makeGoColName": makeGoColName,
|
||||
// "makeDBColName": makeDBColName,
|
||||
// "makeGoInsertParamNames": makeGoInsertParamNames,
|
||||
// "makeGoInsertParamFlags": makeGoInsertParamFlags,
|
||||
// }).ParseFiles("templates/insert.tpl")
|
||||
//
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
//
|
||||
// outputs, err := processTemplate(t)
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
//
|
||||
// return outputs
|
||||
// }
|
|
@ -1,42 +0,0 @@
|
|||
package cmds
|
||||
|
||||
// // init the "select" command
|
||||
// func init() {
|
||||
// SQLBoiler.AddCommand(selectCmd)
|
||||
// selectCmd.Run = selectRun
|
||||
// }
|
||||
//
|
||||
// var selectCmd = &cobra.Command{
|
||||
// Use: "select",
|
||||
// Short: "Generate select statement helpers from table definitions",
|
||||
// }
|
||||
//
|
||||
// // selectRun executes the select command, and generates the select statement
|
||||
// // boilerplate from the select file.
|
||||
// func selectRun(cmd *cobra.Command, args []string) {
|
||||
// err := outHandler(generateSelects())
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // generateSelects returns a slice of each template execution result.
|
||||
// // Each of these results holds a select statement generated from the select template.
|
||||
// func generateSelects() [][]byte {
|
||||
// t, err := template.New("select.tpl").Funcs(template.FuncMap{
|
||||
// "makeGoColName": makeGoColName,
|
||||
// "makeGoVarName": makeGoVarName,
|
||||
// "makeSelectParamNames": makeSelectParamNames,
|
||||
// }).ParseFiles("templates/select.tpl")
|
||||
//
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
//
|
||||
// outputs, err := processTemplate(t)
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
//
|
||||
// return outputs
|
||||
// }
|
142
cmds/shared.go
142
cmds/shared.go
|
@ -1,16 +1,27 @@
|
|||
package cmds
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/pobri19/sqlboiler/dbdrivers"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// CobraRunFunc declares the cobra.Command.Run function definition
|
||||
type CobraRunFunc func(cmd *cobra.Command, args []string)
|
||||
|
||||
// CmdData holds the table schema a slice of (column name, column type) slices.
|
||||
// It also holds a slice of all of the table names sqlboiler is generating against,
|
||||
// the database driver chosen by the driver flag at runtime, and a pointer to the
|
||||
// output file, if one is specified with a flag.
|
||||
type CmdData struct {
|
||||
TablesInfo [][]dbdrivers.DBTable
|
||||
TableNames []string
|
||||
DBDriver dbdrivers.DBDriver
|
||||
OutFile *os.File
|
||||
}
|
||||
|
||||
// tplData is used to pass data to the template
|
||||
type tplData struct {
|
||||
TableName string
|
||||
|
@ -23,31 +34,13 @@ func errorQuit(err error) {
|
|||
os.Exit(-1)
|
||||
}
|
||||
|
||||
// processTemplate takes a template and returns a slice of byte slices.
|
||||
// Each byte slice in the slice of bytes is the output of the template execution.
|
||||
func processTemplate(t *template.Template) ([][]byte, error) {
|
||||
var outputs [][]byte
|
||||
for i := 0; i < len(cmdData.TablesInfo); i++ {
|
||||
data := tplData{
|
||||
TableName: cmdData.TableNames[i],
|
||||
TableData: cmdData.TablesInfo[i],
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := t.Execute(&buf, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out, err := format.Source(buf.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
outputs = append(outputs, out)
|
||||
// defaultRun is the default function passed to the commands cobra.Command.Run.
|
||||
// It will generate the specific commands template and send it to outHandler for output.
|
||||
func defaultRun(cmd *cobra.Command, args []string) {
|
||||
err := outHandler(generateTemplate(cmd.Name()))
|
||||
if err != nil {
|
||||
errorQuit(fmt.Errorf("Unable to generate the template for command %s: %s", cmd.Name(), err))
|
||||
}
|
||||
|
||||
return outputs, nil
|
||||
|
||||
}
|
||||
|
||||
// outHandler loops over the slice of byte slices, outputting them to either
|
||||
|
@ -74,96 +67,3 @@ func outHandler(data [][]byte) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// makeGoColName takes a column name in the format of "column_name" and converts
|
||||
// it into a go styled object variable name of "ColumnName".
|
||||
// makeGoColName also fully uppercases "ID" components of names, for example
|
||||
// "column_name_id" to "ColumnNameID".
|
||||
func makeGoColName(name string) string {
|
||||
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++ {
|
||||
// Only uppercase if not a single word variable
|
||||
if s[i] == "id" && i > 0 {
|
||||
s[i] = "ID"
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip first word Title for variable names
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
s[i] = strings.Title(s[i])
|
||||
}
|
||||
|
||||
return strings.Join(s, "")
|
||||
}
|
||||
|
||||
// makeDBColName takes a table name in the format of "table_name" and a
|
||||
// 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"
|
||||
func makeDBColName(tableName, colName string) string {
|
||||
return tableName + "_" + colName
|
||||
}
|
||||
|
||||
// makeGoInsertParamNames takes a []DBTable and returns a comma seperated
|
||||
// list of parameter names for the insert statement template.
|
||||
func makeGoInsertParamNames(data []dbdrivers.DBTable) string {
|
||||
var paramNames string
|
||||
for i := 0; i < len(data); i++ {
|
||||
paramNames = paramNames + data[i].ColName
|
||||
if len(data) != i+1 {
|
||||
paramNames = paramNames + ", "
|
||||
}
|
||||
}
|
||||
return paramNames
|
||||
}
|
||||
|
||||
// makeGoInsertParamFlags takes a []DBTable and returns a comma seperated
|
||||
// list of parameter flags for the insert statement template.
|
||||
func makeGoInsertParamFlags(data []dbdrivers.DBTable) 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 + ", "
|
||||
}
|
||||
}
|
||||
return paramFlags
|
||||
}
|
||||
|
||||
// makeSelectParamNames takes a []DBTable 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.DBTable) string {
|
||||
var paramNames string
|
||||
for i := 0; i < len(data); i++ {
|
||||
paramNames = fmt.Sprintf("%s%s AS %s", paramNames, data[i].ColName,
|
||||
makeDBColName(tableName, data[i].ColName),
|
||||
)
|
||||
if len(data) != i+1 {
|
||||
paramNames = paramNames + ", "
|
||||
}
|
||||
}
|
||||
return paramNames
|
||||
}
|
||||
|
|
|
@ -12,17 +12,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// CmdData holds the table schema a slice of (column name, column type) slices.
|
||||
// It also holds a slice of all of the table names sqlboiler is generating against,
|
||||
// the database driver chosen by the driver flag at runtime, and a pointer to the
|
||||
// output file, if one is specified with a flag.
|
||||
type CmdData struct {
|
||||
TablesInfo [][]dbdrivers.DBTable
|
||||
TableNames []string
|
||||
DBDriver dbdrivers.DBDriver
|
||||
OutFile *os.File
|
||||
}
|
||||
|
||||
// cmdData is used globally by all commands to access the table schema data,
|
||||
// the database driver and the output file. cmdData is initialized by
|
||||
// the root SQLBoiler cobra command at run time, before other commands execute.
|
||||
|
@ -31,6 +20,14 @@ var cmdData *CmdData
|
|||
// templates holds a slice of pointers to all templates in the templates directory.
|
||||
var templates []*template.Template
|
||||
|
||||
// SQLBoiler is the root app command
|
||||
var SQLBoiler = &cobra.Command{
|
||||
Use: "sqlboiler",
|
||||
Short: "SQL Boiler generates boilerplate structs and statements",
|
||||
Long: "SQL Boiler generates boilerplate structs and statements.\n" +
|
||||
`Complete documentation is available at http://github.com/pobri19/sqlboiler`,
|
||||
}
|
||||
|
||||
// init initializes the sqlboiler flags, such as driver, table, and output file.
|
||||
// It also sets the global preRun hook and postRun hook. Every command will execute
|
||||
// these hooks before and after running to initialize the shared state.
|
||||
|
@ -40,14 +37,9 @@ func init() {
|
|||
SQLBoiler.PersistentFlags().StringP("out", "o", "", "The name of the output file")
|
||||
SQLBoiler.PersistentPreRun = sqlBoilerPreRun
|
||||
SQLBoiler.PersistentPostRun = sqlBoilerPostRun
|
||||
}
|
||||
|
||||
// SQLBoiler is the root app command
|
||||
var SQLBoiler = &cobra.Command{
|
||||
Use: "sqlboiler",
|
||||
Short: "SQL Boiler generates boilerplate structs and statements",
|
||||
Long: "SQL Boiler generates boilerplate structs and statements.\n" +
|
||||
`Complete documentation is available at http://github.com/pobri19/sqlboiler`,
|
||||
// Initialize the SQLBoiler commands and hook the custom Run functions
|
||||
initCommands(SQLBoiler, sqlBoilerCommands, sqlBoilerCommandRuns)
|
||||
}
|
||||
|
||||
// sqlBoilerPostRun cleans up the output file and database connection once
|
||||
|
@ -89,8 +81,6 @@ func sqlBoilerPreRun(cmd *cobra.Command, args []string) {
|
|||
if err != nil {
|
||||
errorQuit(fmt.Errorf("Unable to initialize templates: %s", err))
|
||||
}
|
||||
|
||||
initCommands(sqlBoilerCommands, sqlBoilerCommandRuns)
|
||||
}
|
||||
|
||||
// initDBDriver attempts to set the cmdData DBDriver based off the passed in
|
||||
|
@ -183,7 +173,7 @@ func initTemplates() ([]*template.Template, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
pattern := filepath.Join(wd, "../templates", "*.tpl")
|
||||
pattern := filepath.Join(wd, "/cmds/templates", "*.tpl")
|
||||
tpl, err := template.New("").Funcs(sqlBoilerTemplateFuncs).ParseGlob(pattern)
|
||||
|
||||
if err != nil {
|
||||
|
@ -193,16 +183,18 @@ func initTemplates() ([]*template.Template, error) {
|
|||
return tpl.Templates(), err
|
||||
}
|
||||
|
||||
func initCommands(commands map[string]*cobra.Command, commandRuns map[string]CobraRunFunc) {
|
||||
func initCommands(rootCmd *cobra.Command, commands map[string]*cobra.Command, commandRuns map[string]CobraRunFunc) {
|
||||
for _, c := range commands {
|
||||
// If there is a commandRun for the command (matched by name)
|
||||
// then set the Run hook
|
||||
r, ok := commandRuns[c.Name()]
|
||||
if ok {
|
||||
c.Run = r
|
||||
} else {
|
||||
c.Run = defaultRun // Load default run if no custom run is found
|
||||
}
|
||||
|
||||
// Add the command
|
||||
SQLBoiler.AddCommand(c)
|
||||
rootCmd.AddCommand(c)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
package cmds
|
||||
|
||||
// init the "struct" command
|
||||
// func init() {
|
||||
// SQLBoiler.AddCommand(structCmd)
|
||||
// structCmd.Run = structRun
|
||||
// }
|
||||
|
||||
// var structCmd = &cobra.Command{
|
||||
// Use: "struct",
|
||||
// Short: "Generate structs from table definitions",
|
||||
// }
|
||||
//
|
||||
// // deleteRun executes the struct command, and generates the struct definitions
|
||||
// // boilerplate from the template file.
|
||||
// func structRun(cmd *cobra.Command, args []string) {
|
||||
// err := outHandler(generateStructs())
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
// }
|
||||
|
||||
// generateStructs returns a slice of each template execution result.
|
||||
// Each of these results holds a struct definition generated from the struct template.
|
||||
// func generateStructs() [][]byte {
|
||||
// t, err := template.New("struct.tpl").Funcs(template.FuncMap{
|
||||
// "makeGoColName": makeGoColName,
|
||||
// "makeDBColName": makeDBColName,
|
||||
// }).ParseFiles("templates/struct.tpl")
|
||||
//
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
//
|
||||
// outputs, err := processTemplate(t)
|
||||
// if err != nil {
|
||||
// errorQuit(err)
|
||||
// }
|
||||
//
|
||||
// return outputs
|
||||
// }
|
152
cmds/template_funcs.go
Normal file
152
cmds/template_funcs.go
Normal file
|
@ -0,0 +1,152 @@
|
|||
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.
|
||||
func generateTemplate(commandName string) [][]byte {
|
||||
var template *template.Template
|
||||
|
||||
// Find the template that matches the passed in command name
|
||||
for _, t := range templates {
|
||||
if t.Name() == commandName+".tpl" {
|
||||
template = t
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if template == nil {
|
||||
errorQuit(fmt.Errorf("Unable to find the template: %s", commandName+".tpl"))
|
||||
}
|
||||
|
||||
outputs, err := processTemplate(template)
|
||||
if err != nil {
|
||||
errorQuit(fmt.Errorf("Unable to process the template: %s", err))
|
||||
}
|
||||
|
||||
return outputs
|
||||
}
|
||||
|
||||
// processTemplate takes a template and returns a slice of byte slices.
|
||||
// Each byte slice in the slice of bytes is the output of the template execution.
|
||||
func processTemplate(t *template.Template) ([][]byte, error) {
|
||||
var outputs [][]byte
|
||||
for i := 0; i < len(cmdData.TablesInfo); i++ {
|
||||
data := tplData{
|
||||
TableName: cmdData.TableNames[i],
|
||||
TableData: cmdData.TablesInfo[i],
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := t.Execute(&buf, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out, err := format.Source(buf.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
outputs = append(outputs, out)
|
||||
}
|
||||
|
||||
return outputs, nil
|
||||
}
|
||||
|
||||
// it into a go styled object variable name of "ColumnName".
|
||||
// makeGoColName also fully uppercases "ID" components of names, for example
|
||||
// "column_name_id" to "ColumnNameID".
|
||||
func makeGoColName(name string) string {
|
||||
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, "")
|
||||
}
|
||||
|
||||
// makeDBColName takes a table name in the format of "table_name" and a
|
||||
// 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"
|
||||
func makeDBColName(tableName, colName string) string {
|
||||
return tableName + "_" + colName
|
||||
}
|
||||
|
||||
// makeGoInsertParamNames takes a []DBTable and returns a comma seperated
|
||||
// list of parameter names for the insert statement template.
|
||||
func makeGoInsertParamNames(data []dbdrivers.DBTable) string {
|
||||
var paramNames string
|
||||
for i := 0; i < len(data); i++ {
|
||||
paramNames = paramNames + data[i].ColName
|
||||
if len(data) != i+1 {
|
||||
paramNames = paramNames + ", "
|
||||
}
|
||||
}
|
||||
return paramNames
|
||||
}
|
||||
|
||||
// makeGoInsertParamFlags takes a []DBTable and returns a comma seperated
|
||||
// list of parameter flags for the insert statement template.
|
||||
func makeGoInsertParamFlags(data []dbdrivers.DBTable) 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 + ", "
|
||||
}
|
||||
}
|
||||
return paramFlags
|
||||
}
|
||||
|
||||
// makeSelectParamNames takes a []DBTable 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.DBTable) string {
|
||||
var paramNames string
|
||||
for i := 0; i < len(data); i++ {
|
||||
paramNames = fmt.Sprintf("%s%s AS %s", paramNames, data[i].ColName,
|
||||
makeDBColName(tableName, data[i].ColName),
|
||||
)
|
||||
if len(data) != i+1 {
|
||||
paramNames = paramNames + ", "
|
||||
}
|
||||
}
|
||||
return paramNames
|
||||
}
|
2
main.go
2
main.go
|
@ -6,6 +6,7 @@ So far this includes struct definitions and database statement helper functions.
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/pobri19/sqlboiler/cmds"
|
||||
|
@ -14,6 +15,7 @@ import (
|
|||
func main() {
|
||||
// Execute SQLBoiler
|
||||
if err := cmds.SQLBoiler.Execute(); err != nil {
|
||||
fmt.Printf("Failed to execute SQLBoiler: %s", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue