Broke up all commands into individual templates.
* Added bunch of new command names and descriptions. * Changed "all" command to "boil", so we can use "all" for the name of the all database select helper.
This commit is contained in:
parent
241cac7d67
commit
195e8d16e8
12 changed files with 223 additions and 148 deletions
|
@ -1,17 +1,21 @@
|
||||||
package cmds
|
package cmds
|
||||||
|
|
||||||
import "github.com/spf13/cobra"
|
import (
|
||||||
|
"sort"
|
||||||
|
|
||||||
var allCmd = &cobra.Command{
|
"github.com/spf13/cobra"
|
||||||
Use: "all",
|
)
|
||||||
Short: "Generate all templates from table definitions",
|
|
||||||
|
var boilCmd = &cobra.Command{
|
||||||
|
Use: "boil",
|
||||||
|
Short: "Generates ALL templates by running every command alphabetically",
|
||||||
}
|
}
|
||||||
|
|
||||||
// allRun executes every sqlboiler command, starting with structs.
|
// boilRun executes every sqlboiler command, starting with structs.
|
||||||
func allRun(cmd *cobra.Command, args []string) {
|
func boilRun(cmd *cobra.Command, args []string) {
|
||||||
// Exclude these commands from the output
|
// Exclude these commands from the output
|
||||||
skipTemplates := []string{
|
skipTemplates := []string{
|
||||||
"all",
|
"boil",
|
||||||
}
|
}
|
||||||
|
|
||||||
var templateNames []string
|
var templateNames []string
|
||||||
|
@ -34,7 +38,10 @@ func allRun(cmd *cobra.Command, args []string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepend "struct" command to templateNames slice
|
// Sort all names alphabetically
|
||||||
|
sort.Strings(templateNames)
|
||||||
|
|
||||||
|
// Prepend "struct" command to templateNames slice so it sits at top of sort
|
||||||
templateNames = append([]string{"struct"}, templateNames...)
|
templateNames = append([]string{"struct"}, templateNames...)
|
||||||
|
|
||||||
// Loop through and generate every command template (excluding skipTemplates)
|
// Loop through and generate every command template (excluding skipTemplates)
|
|
@ -13,17 +13,29 @@ import (
|
||||||
//
|
//
|
||||||
// Command names should match the template file name (without the file extension).
|
// Command names should match the template file name (without the file extension).
|
||||||
var sqlBoilerCommands = map[string]*cobra.Command{
|
var sqlBoilerCommands = map[string]*cobra.Command{
|
||||||
"all": allCmd,
|
// Command to generate all commands
|
||||||
"insert": insertCmd,
|
"boil": boilCmd,
|
||||||
"delete": deleteCmd,
|
// Struct commands
|
||||||
"select": selectCmd,
|
|
||||||
"struct": structCmd,
|
"struct": structCmd,
|
||||||
|
// Insert commands
|
||||||
|
"insert": insertCmd,
|
||||||
|
// Select commands
|
||||||
|
"all": allCmd,
|
||||||
|
"allby": allByCmd,
|
||||||
|
"fieldsall": fieldsAllCmd,
|
||||||
|
"fieldsallby": fieldsAllByCmd,
|
||||||
|
"find": findCmd,
|
||||||
|
"findby": findByCmd,
|
||||||
|
"fieldsfind": fieldsFindCmd,
|
||||||
|
"fieldsfindby": fieldsFindByCmd,
|
||||||
|
// Delete commands
|
||||||
|
"delete": deleteCmd,
|
||||||
}
|
}
|
||||||
|
|
||||||
// sqlBoilerCommandRuns points each command to its custom run function.
|
// sqlBoilerCommandRuns points each command to its custom run function.
|
||||||
// If a run function is not defined here, it will use the defaultRun() default run function.
|
// If a run function is not defined here, it will use the defaultRun() default run function.
|
||||||
var sqlBoilerCommandRuns = map[string]CobraRunFunc{
|
var sqlBoilerCommandRuns = map[string]CobraRunFunc{
|
||||||
"all": allRun,
|
"boil": boilRun,
|
||||||
}
|
}
|
||||||
|
|
||||||
// sqlBoilerTemplateFuncs is a map of all the functions that get passed into the templates.
|
// sqlBoilerTemplateFuncs is a map of all the functions that get passed into the templates.
|
||||||
|
@ -37,6 +49,46 @@ var sqlBoilerTemplateFuncs = template.FuncMap{
|
||||||
"makeGoInsertParamFlags": makeGoInsertParamFlags,
|
"makeGoInsertParamFlags": makeGoInsertParamFlags,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var allCmd = &cobra.Command{
|
||||||
|
Use: "all",
|
||||||
|
Short: "Generate a helper to select all records",
|
||||||
|
}
|
||||||
|
|
||||||
|
var allByCmd = &cobra.Command{
|
||||||
|
Use: "allby",
|
||||||
|
Short: "Generate a helper to select all records with specific column values",
|
||||||
|
}
|
||||||
|
|
||||||
|
var fieldsAllCmd = &cobra.Command{
|
||||||
|
Use: "fieldsall",
|
||||||
|
Short: "Generate a helper to select specific fields of all records",
|
||||||
|
}
|
||||||
|
|
||||||
|
var fieldsAllByCmd = &cobra.Command{
|
||||||
|
Use: "fieldsallby",
|
||||||
|
Short: "Generate a helper to select specific fields of records with specific column values",
|
||||||
|
}
|
||||||
|
|
||||||
|
var findCmd = &cobra.Command{
|
||||||
|
Use: "find",
|
||||||
|
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 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{
|
var insertCmd = &cobra.Command{
|
||||||
Use: "insert",
|
Use: "insert",
|
||||||
Short: "Generate insert statement helpers from table definitions",
|
Short: "Generate insert statement helpers from table definitions",
|
||||||
|
@ -47,11 +99,6 @@ var deleteCmd = &cobra.Command{
|
||||||
Short: "Generate delete statement helpers from table definitions",
|
Short: "Generate delete statement helpers from table definitions",
|
||||||
}
|
}
|
||||||
|
|
||||||
var selectCmd = &cobra.Command{
|
|
||||||
Use: "select",
|
|
||||||
Short: "Generate select statement helpers from table definitions",
|
|
||||||
}
|
|
||||||
|
|
||||||
var structCmd = &cobra.Command{
|
var structCmd = &cobra.Command{
|
||||||
Use: "struct",
|
Use: "struct",
|
||||||
Short: "Generate structs from table definitions",
|
Short: "Generate structs from table definitions",
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
@ -187,17 +188,37 @@ func initTemplates() ([]*template.Template, error) {
|
||||||
|
|
||||||
// initCommands loads all of the commands in the sqlBoilerCommands and hooks their run functions.
|
// initCommands loads all of the commands in the sqlBoilerCommands and hooks their run functions.
|
||||||
func initCommands(rootCmd *cobra.Command, commands map[string]*cobra.Command, commandRuns map[string]CobraRunFunc) {
|
func initCommands(rootCmd *cobra.Command, commands map[string]*cobra.Command, commandRuns map[string]CobraRunFunc) {
|
||||||
|
var commandNames []string
|
||||||
|
|
||||||
|
// Build a list of command names to alphabetically sort them for ordered loading.
|
||||||
for _, c := range commands {
|
for _, c := range commands {
|
||||||
|
// Skip the boil command load, we do it manually below.
|
||||||
|
if c.Name() == "boil" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
commandNames = append(commandNames, c.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize the "boil" command first, and manually. It should be at the top of the help file.
|
||||||
|
commands["boil"].Run = commandRuns["boil"]
|
||||||
|
rootCmd.AddCommand(commands["boil"])
|
||||||
|
|
||||||
|
// Load commands alphabetically. This ensures proper order of help file.
|
||||||
|
sort.Strings(commandNames)
|
||||||
|
|
||||||
|
// Loop every command name, load it and hook it to its Run handler
|
||||||
|
for _, c := range commandNames {
|
||||||
// If there is a commandRun for the command (matched by name)
|
// If there is a commandRun for the command (matched by name)
|
||||||
// then set the Run hook
|
// then set the Run hook
|
||||||
r, ok := commandRuns[c.Name()]
|
r, ok := commandRuns[c]
|
||||||
if ok {
|
if ok {
|
||||||
c.Run = r
|
commands[c].Run = r
|
||||||
} else {
|
} else {
|
||||||
c.Run = defaultRun // Load default run if no custom run is found
|
commands[c].Run = defaultRun // Load default run if no custom run is found
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the command
|
// Add the command
|
||||||
rootCmd.AddCommand(c)
|
rootCmd.AddCommand(commands[c])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
13
cmds/templates/all.tpl
Normal file
13
cmds/templates/all.tpl
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
{{- $tableName := .TableName -}}
|
||||||
|
// {{makeGoColName $tableName}}All retrieves all records.
|
||||||
|
func {{makeGoColName $tableName}}All(db *sqlx.DB) ([]*{{makeGoColName $tableName}}, error) {
|
||||||
|
{{$varName := makeGoVarName $tableName -}}
|
||||||
|
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||||
|
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {{$varName}}, nil
|
||||||
|
}
|
13
cmds/templates/allby.tpl
Normal file
13
cmds/templates/allby.tpl
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
{{- $tableName := .TableName -}}
|
||||||
|
// {{makeGoColName $tableName}}AllBy retrieves all records with the specified column values.
|
||||||
|
func {{makeGoColName $tableName}}AllBy(db *sqlx.DB, columns map[string]interface{}) ([]*{{makeGoColName $tableName}}, error) {
|
||||||
|
{{$varName := makeGoVarName $tableName -}}
|
||||||
|
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||||
|
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {{$varName}}, nil
|
||||||
|
}
|
15
cmds/templates/fieldsall.tpl
Normal file
15
cmds/templates/fieldsall.tpl
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
{{- $tableName := .TableName -}}
|
||||||
|
// {{makeGoColName $tableName}}FieldsAll 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 {{makeGoColName $tableName}}FieldsAll(db *sqlx.DB, results interface{}) error {
|
||||||
|
{{$varName := makeGoVarName $tableName -}}
|
||||||
|
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||||
|
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {{$varName}}, nil
|
||||||
|
}
|
16
cmds/templates/fieldsallby.tpl
Normal file
16
cmds/templates/fieldsallby.tpl
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
{{- $tableName := .TableName -}}
|
||||||
|
// {{makeGoColName $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 {{makeGoColName $tableName}}FieldsAllBy(db *sqlx.DB, columns map[string]interface{}, results interface{}) error {
|
||||||
|
{{$varName := makeGoVarName $tableName -}}
|
||||||
|
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||||
|
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {{$varName}}, nil
|
||||||
|
}
|
18
cmds/templates/fieldsfind.tpl
Normal file
18
cmds/templates/fieldsfind.tpl
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
{{- $tableName := .TableName -}}
|
||||||
|
// {{makeGoColName $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 {{makeGoColName $tableName}}FieldsFind(db *sqlx.DB, id int, results interface{}) (*{{makeGoColName $tableName}}, error) {
|
||||||
|
if id == 0 {
|
||||||
|
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||||
|
}
|
||||||
|
{{$varName := makeGoVarName $tableName}}
|
||||||
|
var {{$varName}} *{{makeGoColName $tableName}}
|
||||||
|
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {{$varName}}, nil
|
||||||
|
}
|
19
cmds/templates/fieldsfindby.tpl
Normal file
19
cmds/templates/fieldsfindby.tpl
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{{- $tableName := .TableName -}}
|
||||||
|
// {{makeGoColName $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 {{makeGoColName $tableName}}FieldsFindBy(db *sqlx.DB, columns map[string]interface{}, results interface{}) (*{{makeGoColName $tableName}}, error) {
|
||||||
|
if id == 0 {
|
||||||
|
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||||
|
}
|
||||||
|
{{$varName := makeGoVarName $tableName}}
|
||||||
|
var {{$varName}} *{{makeGoColName $tableName}}
|
||||||
|
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {{$varName}}, nil
|
||||||
|
}
|
16
cmds/templates/find.tpl
Normal file
16
cmds/templates/find.tpl
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
{{- $tableName := .TableName -}}
|
||||||
|
// {{makeGoColName $tableName}}Find retrieves a single record by ID.
|
||||||
|
func {{makeGoColName $tableName}}Find(db *sqlx.DB, id int) (*{{makeGoColName $tableName}}, error) {
|
||||||
|
if id == 0 {
|
||||||
|
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||||
|
}
|
||||||
|
{{$varName := makeGoVarName $tableName}}
|
||||||
|
var {{$varName}} *{{makeGoColName $tableName}}
|
||||||
|
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {{$varName}}, nil
|
||||||
|
}
|
16
cmds/templates/findby.tpl
Normal file
16
cmds/templates/findby.tpl
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
{{- $tableName := .TableName -}}
|
||||||
|
// {{makeGoColName $tableName}}FindBy retrieves a single record with the specified column values.
|
||||||
|
func {{makeGoColName $tableName}}FindBy(db *sqlx.DB, columns map[string]interface{}) (*{{makeGoColName $tableName}}, error) {
|
||||||
|
if id == 0 {
|
||||||
|
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||||
|
}
|
||||||
|
{{$varName := makeGoVarName $tableName}}
|
||||||
|
var {{$varName}} *{{makeGoColName $tableName}}
|
||||||
|
err := db.Select(&{{$varName}}, fmt.Sprintf(`SELECT {{makeSelectParamNames $tableName .TableData}} WHERE %s=$1`, column), value)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {{$varName}}, nil
|
||||||
|
}
|
|
@ -1,126 +0,0 @@
|
||||||
{{- $tableName := .TableName -}}
|
|
||||||
// {{makeGoColName $tableName}}All retrieves all records.
|
|
||||||
func {{makeGoColName $tableName}}All(db *sqlx.DB) ([]*{{makeGoColName $tableName}}, error) {
|
|
||||||
{{$varName := makeGoVarName $tableName -}}
|
|
||||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {{$varName}}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{makeGoColName $tableName}}AllBy retrieves all records with the specified column values.
|
|
||||||
func {{makeGoColName $tableName}}AllBy(db *sqlx.DB, columns map[string]interface{}) ([]*{{makeGoColName $tableName}}, error) {
|
|
||||||
{{$varName := makeGoVarName $tableName -}}
|
|
||||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {{$varName}}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{makeGoColName $tableName}}FieldsAll 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 {{makeGoColName $tableName}}FieldsAll(db *sqlx.DB, results interface{}) error {
|
|
||||||
{{$varName := makeGoVarName $tableName -}}
|
|
||||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {{$varName}}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{makeGoColName $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 {{makeGoColName $tableName}}FieldsAllBy(db *sqlx.DB, columns map[string]interface{}, results interface{}) error {
|
|
||||||
{{$varName := makeGoVarName $tableName -}}
|
|
||||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {{$varName}}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{makeGoColName $tableName}}Find retrieves a single record by ID.
|
|
||||||
func {{makeGoColName $tableName}}Find(db *sqlx.DB, id int) (*{{makeGoColName $tableName}}, error) {
|
|
||||||
if id == 0 {
|
|
||||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
|
||||||
}
|
|
||||||
{{$varName := makeGoVarName $tableName}}
|
|
||||||
var {{$varName}} *{{makeGoColName $tableName}}
|
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {{$varName}}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{makeGoColName $tableName}}FindBy retrieves a single record with the specified column values.
|
|
||||||
func {{makeGoColName $tableName}}FindBy(db *sqlx.DB, columns map[string]interface{}) (*{{makeGoColName $tableName}}, error) {
|
|
||||||
if id == 0 {
|
|
||||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
|
||||||
}
|
|
||||||
{{$varName := makeGoVarName $tableName}}
|
|
||||||
var {{$varName}} *{{makeGoColName $tableName}}
|
|
||||||
err := db.Select(&{{$varName}}, fmt.Sprintf(`SELECT {{makeSelectParamNames $tableName .TableData}} WHERE %s=$1`, column), value)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {{$varName}}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{makeGoColName $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 {{makeGoColName $tableName}}FieldsFind(db *sqlx.DB, id int, results interface{}) (*{{makeGoColName $tableName}}, error) {
|
|
||||||
if id == 0 {
|
|
||||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
|
||||||
}
|
|
||||||
{{$varName := makeGoVarName $tableName}}
|
|
||||||
var {{$varName}} *{{makeGoColName $tableName}}
|
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {{$varName}}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// {{makeGoColName $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 {{makeGoColName $tableName}}FieldsFindBy(db *sqlx.DB, columns map[string]interface{}, results interface{}) (*{{makeGoColName $tableName}}, error) {
|
|
||||||
if id == 0 {
|
|
||||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
|
||||||
}
|
|
||||||
{{$varName := makeGoVarName $tableName}}
|
|
||||||
var {{$varName}} *{{makeGoColName $tableName}}
|
|
||||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {{$varName}}, nil
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue