2016-02-23 22:38:24 +10:00
|
|
|
package cmds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2016-02-24 18:53:34 +10:00
|
|
|
// init the "insert" command
|
2016-02-23 22:38:24 +10:00
|
|
|
func init() {
|
|
|
|
SQLBoiler.AddCommand(insertCmd)
|
|
|
|
insertCmd.Run = insertRun
|
|
|
|
}
|
|
|
|
|
|
|
|
var insertCmd = &cobra.Command{
|
|
|
|
Use: "insert",
|
|
|
|
Short: "Generate insert statement helpers from table definitions",
|
|
|
|
}
|
|
|
|
|
2016-02-24 18:53:34 +10:00
|
|
|
// insertRun executes the insert command, and generates the insert statement
|
|
|
|
// boilerplate from the template file.
|
2016-02-23 22:38:24 +10:00
|
|
|
func insertRun(cmd *cobra.Command, args []string) {
|
2016-02-24 15:40:07 +10:00
|
|
|
err := outHandler(generateInserts())
|
|
|
|
if err != nil {
|
|
|
|
errorQuit(err)
|
2016-02-23 22:38:24 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 18:53:34 +10:00
|
|
|
// generateInserts returns a slice of each template execution result.
|
|
|
|
// Each of these results holds an insert statement generated from the insert template.
|
2016-02-23 22:38:24 +10:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-02-24 15:40:07 +10:00
|
|
|
outputs, err := processTemplate(t)
|
|
|
|
if err != nil {
|
|
|
|
errorQuit(err)
|
2016-02-23 22:38:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return outputs
|
|
|
|
}
|