Added initTemplates, moved tpls folder

* Moved all template functions to shared
This commit is contained in:
Patrick O'brien 2016-02-29 13:30:54 +10:00
parent 403951940f
commit 25fa09cf93
9 changed files with 93 additions and 47 deletions

View file

@ -1,10 +1,8 @@
package cmds
import (
"fmt"
"text/template"
"github.com/pobri19/sqlboiler/dbdrivers"
"github.com/spf13/cobra"
)
@ -49,29 +47,3 @@ func generateInserts() [][]byte {
return outputs
}
// 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
}

View file

@ -1,10 +1,8 @@
package cmds
import (
"fmt"
"text/template"
"github.com/pobri19/sqlboiler/dbdrivers"
"github.com/spf13/cobra"
)
@ -48,20 +46,3 @@ func generateSelects() [][]byte {
return outputs
}
// 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
}

View file

@ -124,3 +124,46 @@ func makeGoVarName(name string) string {
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
}

View file

@ -2,8 +2,11 @@ package cmds
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/pobri19/sqlboiler/dbdrivers"
"github.com/spf13/cobra"
@ -25,6 +28,9 @@ type CmdData struct {
// the root SQLBoiler cobra command at run time, before other commands execute.
var cmdData *CmdData
// templates holds a slice of pointers to all templates in the templates directory.
var templates []*template.Template
// 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.
@ -77,6 +83,12 @@ func sqlBoilerPreRun(cmd *cobra.Command, args []string) {
// Initialize the cmdData.OutFile
initOutFile()
// Initialize the templates
templates, err = initTemplates()
if err != nil {
errorQuit(fmt.Errorf("Unable to initialize templates: %s", err))
}
}
// initDBDriver attempts to set the cmdData DBDriver based off the passed in
@ -162,3 +174,27 @@ func initOutFile() {
}
}
}
func initTemplates() ([]*template.Template, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
pattern := filepath.Join(wd, "templates", "*.tpl")
tpl, err := template.New("").Funcs(template.FuncMap{
"makeGoColName": makeGoColName,
"makeGoVarName": makeGoVarName,
"makeDBColName": makeDBColName,
"makeSelectParamNames": makeSelectParamNames,
"makeGoInsertParamNames": makeGoInsertParamNames,
"makeGoInsertParamFlags": makeGoInsertParamFlags,
}).ParseGlob(pattern)
if err != nil {
return nil, err
}
return tpl.Templates(), err
}

14
cmds/sqlboiler_test.go Normal file
View file

@ -0,0 +1,14 @@
package cmds
import "testing"
func TestInitTemplates(t *testing.T) {
templates, err := initTemplates()
if err != nil {
t.Errorf("Unable to init templates: %s", err)
}
if len(templates) < 2 {
t.Errorf("Expected > 2 templates to be loaded from templates folder, only loaded: %d\n\n%#v", len(templates), templates)
}
}