Fixed and renamed templates, added package names
This commit is contained in:
parent
0768a89aa6
commit
58414106a2
15 changed files with 105 additions and 116 deletions
|
@ -22,6 +22,7 @@ func boilRun(cmd *cobra.Command, args []string) {
|
|||
data := tplData{
|
||||
Table: cmdData.Tables[i],
|
||||
Columns: cmdData.Columns[i],
|
||||
PkgName: cmdData.PkgName,
|
||||
}
|
||||
|
||||
var out [][]byte
|
||||
|
|
|
@ -42,14 +42,12 @@ var sqlBoilerCommands = map[string]*cobra.Command{
|
|||
// Insert commands
|
||||
"insert": insertCmd,
|
||||
// Select commands
|
||||
"all": allCmd,
|
||||
"allby": allByCmd,
|
||||
"fieldsall": fieldsAllCmd,
|
||||
"fieldsallby": fieldsAllByCmd,
|
||||
"find": findCmd,
|
||||
"findby": findByCmd,
|
||||
"fieldsfind": fieldsFindCmd,
|
||||
"fieldsfindby": fieldsFindByCmd,
|
||||
"all": allCmd,
|
||||
"where": whereCmd,
|
||||
"select": selectCmd,
|
||||
"selectwhere": selectWhereCmd,
|
||||
"find": findCmd,
|
||||
"findselect": findSelectCmd,
|
||||
// Delete commands
|
||||
"delete": deleteCmd,
|
||||
}
|
||||
|
@ -72,23 +70,39 @@ var sqlBoilerTemplateFuncs = template.FuncMap{
|
|||
"scanParamNames": scanParamNames,
|
||||
}
|
||||
|
||||
/* Struct commands */
|
||||
|
||||
var structCmd = &cobra.Command{
|
||||
Use: "struct",
|
||||
Short: "Generate structs from table definitions",
|
||||
}
|
||||
|
||||
/* Insert commands */
|
||||
|
||||
var insertCmd = &cobra.Command{
|
||||
Use: "insert",
|
||||
Short: "Generate insert statement helpers from table definitions",
|
||||
}
|
||||
|
||||
/* Select commands */
|
||||
|
||||
var allCmd = &cobra.Command{
|
||||
Use: "all",
|
||||
Short: "Generate a helper to select all records",
|
||||
}
|
||||
|
||||
var allByCmd = &cobra.Command{
|
||||
Use: "allby",
|
||||
var whereCmd = &cobra.Command{
|
||||
Use: "where",
|
||||
Short: "Generate a helper to select all records with specific column values",
|
||||
}
|
||||
|
||||
var fieldsAllCmd = &cobra.Command{
|
||||
Use: "fieldsall",
|
||||
var selectCmd = &cobra.Command{
|
||||
Use: "select",
|
||||
Short: "Generate a helper to select specific fields of all records",
|
||||
}
|
||||
|
||||
var fieldsAllByCmd = &cobra.Command{
|
||||
Use: "fieldsallby",
|
||||
var selectWhereCmd = &cobra.Command{
|
||||
Use: "selectwhere",
|
||||
Short: "Generate a helper to select specific fields of records with specific column values",
|
||||
}
|
||||
|
||||
|
@ -97,32 +111,14 @@ var findCmd = &cobra.Command{
|
|||
Short: "Generate a helper to select a single record by ID",
|
||||
}
|
||||
|
||||
var findByCmd = &cobra.Command{
|
||||
Use: "findby",
|
||||
Short: "Generate a helper to select a single record that has specific column values",
|
||||
var findSelectCmd = &cobra.Command{
|
||||
Use: "findselect",
|
||||
Short: "Generate a helper to select specific fields of a record by ID",
|
||||
}
|
||||
|
||||
var fieldsFindCmd = &cobra.Command{
|
||||
Use: "fieldsfind",
|
||||
Short: "Generate a helper to select specific fields of records by ID",
|
||||
}
|
||||
|
||||
var fieldsFindByCmd = &cobra.Command{
|
||||
Use: "fieldsfindby",
|
||||
Short: "Generate a helper to select specific fields of a single record that has specific column values",
|
||||
}
|
||||
|
||||
var insertCmd = &cobra.Command{
|
||||
Use: "insert",
|
||||
Short: "Generate insert statement helpers from table definitions",
|
||||
}
|
||||
/* Delete commands */
|
||||
|
||||
var deleteCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Short: "Generate delete statement helpers from table definitions",
|
||||
}
|
||||
|
||||
var structCmd = &cobra.Command{
|
||||
Use: "struct",
|
||||
Short: "Generate structs from table definitions",
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ type CmdData struct {
|
|||
type tplData struct {
|
||||
Table string
|
||||
Columns []dbdrivers.DBColumn
|
||||
PkgName string
|
||||
}
|
||||
|
||||
// errorQuit displays an error message and then exits the application.
|
||||
|
@ -44,6 +45,7 @@ func defaultRun(cmd *cobra.Command, args []string) {
|
|||
data := tplData{
|
||||
Table: cmdData.Tables[i],
|
||||
Columns: cmdData.Columns[i],
|
||||
PkgName: cmdData.PkgName,
|
||||
}
|
||||
|
||||
// outHandler takes a slice of byte slices, so append the Template
|
||||
|
|
|
@ -1,26 +1,27 @@
|
|||
{{- $tableName := titleCase .Table -}}
|
||||
{{- $varName := camelCase $tableName -}}
|
||||
{{- $varName := camelCase .Table -}}
|
||||
// {{$tableName}}All retrieves all records.
|
||||
func {{$tableName}}All(db boil.DB) ([]*{{$tableName}}, error) {
|
||||
var {{$varName}} []*{{$tableName}}
|
||||
|
||||
rows, err := db.Query(`SELECT {{selectParamNames .Table .Columns}} FROM {{.Table}}`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: failed to query: %v", err)
|
||||
return nil, fmt.Errorf("{{.PkgName}}: failed to query: %v", err)
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
{{- $tmpVarName := (print $varName "Tmp") -}}
|
||||
{{$varName}}Tmp := {{$tableName}}{}
|
||||
|
||||
if err := rows.Scan({{scanParamNames $varName .Columns}}); err != nil {
|
||||
return nil, fmt.Errorf("models: failed to scan row: %v", err)
|
||||
if err := rows.Scan({{scanParamNames $tmpVarName .Columns}}); err != nil {
|
||||
return nil, fmt.Errorf("{{.PkgName}}: failed to scan row: %v", err)
|
||||
}
|
||||
|
||||
{{$varName}} = append({{$varName}}, {{$varName}}Tmp)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("models: failed to read rows: %v", err)
|
||||
return nil, fmt.Errorf("{{.PkgName}}: failed to read rows: %v", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
// {{titleCase $tableName}}Delete deletes a single record.
|
||||
func {{titleCase $tableName}}Delete(db boil.DB, id int) error {
|
||||
if id == nil {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} delete")
|
||||
return nil, errors.New("{{.PkgName}}: no id provided for {{$tableName}} delete")
|
||||
}
|
||||
|
||||
err := db.Exec("DELETE FROM {{$tableName}} WHERE id=$1", id)
|
||||
if err != nil {
|
||||
return errors.New("model: unable to delete from {{$tableName}}: %s", err)
|
||||
return errors.New("{{.PkgName}}: unable to delete from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
{{- $tableName := .Table -}}
|
||||
// {{titleCase $tableName}}FieldsAllBy retrieves the specified columns
|
||||
// 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.
|
||||
// For example: friendName string `db:"friend_name"`
|
||||
func {{titleCase $tableName}}FieldsAllBy(db boil.DB, columns map[string]interface{}, results interface{}) error {
|
||||
{{$varName := camelCase $tableName -}}
|
||||
var {{$varName}} []*{{titleCase $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .Columns}}`)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
{{- $tableName := .Table -}}
|
||||
// {{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.
|
||||
// For example: friendName string `db:"friend_name"`
|
||||
func {{titleCase $tableName}}FieldsFind(db boil.DB, id int, results interface{}) (*{{titleCase $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := camelCase $tableName}}
|
||||
var {{$varName}} *{{titleCase $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .Columns}} WHERE id=$1`, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
{{- $tableName := .Table -}}
|
||||
// {{titleCase $tableName}}FieldsFindBy retrieves the specified columns
|
||||
// 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.
|
||||
// For example: friendName string `db:"friend_name"`
|
||||
func {{titleCase $tableName}}FieldsFindBy(db boil.DB, columns map[string]interface{}, results interface{}) (*{{titleCase $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := camelCase $tableName}}
|
||||
var {{$varName}} *{{titleCase $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .Columns}} WHERE id=$1`, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
|
@ -2,14 +2,14 @@
|
|||
// {{titleCase $tableName}}Find retrieves a single record by ID.
|
||||
func {{titleCase $tableName}}Find(db boil.DB, id int) (*{{titleCase $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
return nil, errors.New("{{.PkgName}}: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := camelCase $tableName}}
|
||||
var {{$varName}} *{{titleCase $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .Columns}} WHERE id=$1`, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
{{- $tableName := .Table -}}
|
||||
// {{titleCase $tableName}}FindBy retrieves a single record with the specified column values.
|
||||
func {{titleCase $tableName}}FindBy(db boil.DB, columns map[string]interface{}) (*{{titleCase $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := camelCase $tableName}}
|
||||
var {{$varName}} *{{titleCase $tableName}}
|
||||
err := db.Select(&{{$varName}}, fmt.Sprintf(`SELECT {{selectParamNames $tableName .Columns}} WHERE %s=$1`, column), value)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
18
cmds/templates/findselect.tpl
Normal file
18
cmds/templates/findselect.tpl
Normal file
|
@ -0,0 +1,18 @@
|
|||
{{- $tableName := .Table -}}
|
||||
// {{titleCase $tableName}}FindSelect 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.
|
||||
// For example: friendName string `db:"friend_name"`
|
||||
func {{titleCase $tableName}}FindSelect(db boil.DB, id int, results interface{}) error {
|
||||
if id == 0 {
|
||||
return nil, errors.New("{{.PkgName}}: no id provided for {{$tableName}} select")
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`SELECT %s FROM {{$tableName}} WHERE id=$1`, boil.SelectNames(results))
|
||||
err := db.Select(results, query, id)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -2,14 +2,14 @@
|
|||
// {{titleCase $tableName}}Insert inserts a single record.
|
||||
func {{titleCase $tableName}}Insert(db boil.DB, o *{{titleCase $tableName}}) (int, error) {
|
||||
if o == nil {
|
||||
return 0, errors.New("model: no {{$tableName}} provided for insertion")
|
||||
return 0, errors.New("{{.PkgName}}: no {{$tableName}} provided for insertion")
|
||||
}
|
||||
|
||||
var rowID int
|
||||
err := db.QueryRow(`INSERT INTO {{$tableName}} ({{insertParamNames .Columns}}) VALUES({{insertParamFlags .Columns}}) RETURNING id`)
|
||||
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("model: unable to insert {{$tableName}}: %s", err)
|
||||
return 0, fmt.Errorf("{{.PkgName}}: unable to insert {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return rowID, nil
|
||||
|
|
14
cmds/templates/select.tpl
Normal file
14
cmds/templates/select.tpl
Normal file
|
@ -0,0 +1,14 @@
|
|||
{{- $tableName := .Table -}}
|
||||
// {{titleCase $tableName}}Select 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.
|
||||
// For example: friendName string `db:"friend_name"`
|
||||
func {{titleCase $tableName}}Select(db boil.DB, results interface{}) error {
|
||||
query := fmt.Sprintf(`SELECT %s FROM {{$tableName}}`, boil.SelectNames(results))
|
||||
err := db.Select(results, query)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
12
cmds/templates/selectwhere.tpl
Normal file
12
cmds/templates/selectwhere.tpl
Normal file
|
@ -0,0 +1,12 @@
|
|||
{{- $tableName := .Table -}}
|
||||
// {{titleCase $tableName}}SelectWhere retrieves all records with the specified column values.
|
||||
func {{titleCase $tableName}}SelectWhere(db boil.DB, results interface{}, columns map[string]interface{}) error {
|
||||
query := fmt.Sprintf(`SELECT %s FROM {{$tableName}} WHERE %s`, boil.SelectNames(results), boil.Where(columns))
|
||||
err := db.Select(results, query, boil.WhereParams(columns)...)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
14
cmds/templates/where.tpl
Normal file
14
cmds/templates/where.tpl
Normal file
|
@ -0,0 +1,14 @@
|
|||
{{- $tableName := .Table -}}
|
||||
{{- $varName := camelCase $tableName -}}
|
||||
// {{titleCase $tableName}}Where retrieves all records with the specified column values.
|
||||
func {{titleCase $tableName}}Where(db boil.DB, columns map[string]interface{}) ([]*{{titleCase $tableName}}, error) {
|
||||
var {{$varName}} []*{{titleCase $tableName}}
|
||||
query := fmt.Sprintf(`SELECT {{selectParamNames $tableName .Columns}} FROM {{$tableName}} WHERE %s`, boil.Where(columns))
|
||||
err := db.Select(&{{$varName}}, query, boil.WhereParams(columns)...)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{$tableName}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varName}}, nil
|
||||
}
|
Loading…
Add table
Reference in a new issue