2016-02-29 06:49:26 +01:00
|
|
|
package cmds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2016-03-23 04:03:35 +01:00
|
|
|
type importList []string
|
|
|
|
|
2016-03-01 12:11:55 +01:00
|
|
|
// imports defines the optional standard imports and
|
|
|
|
// thirdparty imports (from github for example)
|
|
|
|
type imports struct {
|
2016-03-23 04:03:35 +01:00
|
|
|
standard importList
|
|
|
|
thirdparty importList
|
2016-03-01 12:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// sqlBoilerDefaultImports defines the list of default template imports.
|
|
|
|
var sqlBoilerDefaultImports = imports{
|
2016-03-23 04:03:35 +01:00
|
|
|
standard: importList{
|
2016-03-02 17:59:34 +01:00
|
|
|
`"errors"`,
|
|
|
|
`"fmt"`,
|
2016-03-01 12:11:55 +01:00
|
|
|
},
|
2016-03-23 04:03:35 +01:00
|
|
|
thirdparty: importList{
|
2016-03-02 17:59:34 +01:00
|
|
|
`"github.com/pobri19/sqlboiler/boil"`,
|
2016-03-01 12:11:55 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-21 06:15:14 +01:00
|
|
|
// sqlBoilerDefaultTestImports defines the list of default test template imports.
|
|
|
|
var sqlBoilerDefaultTestImports = imports{
|
2016-03-23 04:03:35 +01:00
|
|
|
standard: importList{
|
2016-03-21 06:15:14 +01:00
|
|
|
`"testing"`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:03:35 +01:00
|
|
|
// sqlBoilerConditionalTypeImports imports are only included in the template output
|
2016-03-23 05:25:57 +01:00
|
|
|
// if the database requires one of the following special types. Check TranslateColumn
|
2016-03-23 04:03:35 +01:00
|
|
|
// to see the type assignments.
|
|
|
|
var sqlBoilerConditionalTypeImports = map[string]imports{
|
|
|
|
"null.Int": imports{
|
|
|
|
thirdparty: importList{`"gopkg.in/guregu/null.v3"`},
|
|
|
|
},
|
|
|
|
"null.String": imports{
|
|
|
|
thirdparty: importList{`"gopkg.in/guregu/null.v3"`},
|
|
|
|
},
|
|
|
|
"null.Bool": imports{
|
|
|
|
thirdparty: importList{`"gopkg.in/guregu/null.v3"`},
|
|
|
|
},
|
|
|
|
"null.Float": imports{
|
|
|
|
thirdparty: importList{`"gopkg.in/guregu/null.v3"`},
|
|
|
|
},
|
|
|
|
"null.Time": imports{
|
|
|
|
thirdparty: importList{`"gopkg.in/guregu/null.v3"`},
|
|
|
|
},
|
|
|
|
"time.Time": imports{
|
|
|
|
standard: importList{`"time"`},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-01 12:11:55 +01:00
|
|
|
var sqlBoilerCustomImports map[string]imports
|
2016-03-21 06:15:14 +01:00
|
|
|
var sqlBoilerCustomTestImports map[string]imports
|
2016-03-01 12:11:55 +01:00
|
|
|
|
2016-02-29 10:39:49 +01:00
|
|
|
// 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).
|
2016-02-29 06:49:26 +01:00
|
|
|
var sqlBoilerCommands = map[string]*cobra.Command{
|
2016-02-29 12:45:28 +01:00
|
|
|
// Command to generate all commands
|
|
|
|
"boil": boilCmd,
|
|
|
|
// Struct commands
|
|
|
|
"struct": structCmd,
|
|
|
|
// Insert commands
|
2016-02-29 06:49:26 +01:00
|
|
|
"insert": insertCmd,
|
2016-02-29 12:45:28 +01:00
|
|
|
// Select commands
|
2016-03-16 15:33:58 +01:00
|
|
|
"all": allCmd,
|
|
|
|
"where": whereCmd,
|
|
|
|
"select": selectCmd,
|
|
|
|
"selectwhere": selectWhereCmd,
|
|
|
|
"find": findCmd,
|
|
|
|
"findselect": findSelectCmd,
|
2016-02-29 12:45:28 +01:00
|
|
|
// Delete commands
|
2016-02-29 06:49:26 +01:00
|
|
|
"delete": deleteCmd,
|
2016-03-19 07:22:10 +01:00
|
|
|
// Update commands
|
|
|
|
"update": updateCmd,
|
2016-02-29 06:49:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// sqlBoilerCommandRuns points each command to its custom run function.
|
2016-02-29 10:39:49 +01:00
|
|
|
// If a run function is not defined here, it will use the defaultRun() default run function.
|
2016-02-29 06:49:26 +01:00
|
|
|
var sqlBoilerCommandRuns = map[string]CobraRunFunc{
|
2016-02-29 12:45:28 +01:00
|
|
|
"boil": boilRun,
|
2016-02-29 06:49:26 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 10:39:49 +01:00
|
|
|
// 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.
|
2016-02-29 06:49:26 +01:00
|
|
|
var sqlBoilerTemplateFuncs = template.FuncMap{
|
2016-03-18 12:26:48 +01:00
|
|
|
"singular": singular,
|
|
|
|
"plural": plural,
|
|
|
|
"titleCase": titleCase,
|
|
|
|
"titleCaseSingular": titleCaseSingular,
|
|
|
|
"titleCasePlural": titleCasePlural,
|
|
|
|
"camelCase": camelCase,
|
|
|
|
"camelCaseSingular": camelCaseSingular,
|
|
|
|
"camelCasePlural": camelCasePlural,
|
|
|
|
"makeDBName": makeDBName,
|
|
|
|
"selectParamNames": selectParamNames,
|
|
|
|
"insertParamNames": insertParamNames,
|
|
|
|
"insertParamFlags": insertParamFlags,
|
|
|
|
"insertParamVariables": insertParamVariables,
|
|
|
|
"scanParamNames": scanParamNames,
|
2016-03-18 16:27:55 +01:00
|
|
|
"hasPrimaryKey": hasPrimaryKey,
|
|
|
|
"getPrimaryKey": getPrimaryKey,
|
2016-03-19 07:22:10 +01:00
|
|
|
"updateParamNames": updateParamNames,
|
|
|
|
"updateParamVariables": updateParamVariables,
|
2016-02-29 06:49:26 +01:00
|
|
|
}
|
|
|
|
|
2016-03-16 15:33:58 +01:00
|
|
|
/* 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 */
|
|
|
|
|
2016-02-29 12:45:28 +01:00
|
|
|
var allCmd = &cobra.Command{
|
|
|
|
Use: "all",
|
|
|
|
Short: "Generate a helper to select all records",
|
|
|
|
}
|
|
|
|
|
2016-03-16 15:33:58 +01:00
|
|
|
var whereCmd = &cobra.Command{
|
|
|
|
Use: "where",
|
2016-02-29 12:45:28 +01:00
|
|
|
Short: "Generate a helper to select all records with specific column values",
|
|
|
|
}
|
|
|
|
|
2016-03-16 15:33:58 +01:00
|
|
|
var selectCmd = &cobra.Command{
|
|
|
|
Use: "select",
|
2016-02-29 12:45:28 +01:00
|
|
|
Short: "Generate a helper to select specific fields of all records",
|
|
|
|
}
|
|
|
|
|
2016-03-16 15:33:58 +01:00
|
|
|
var selectWhereCmd = &cobra.Command{
|
|
|
|
Use: "selectwhere",
|
2016-02-29 12:45:28 +01:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
2016-03-16 15:33:58 +01:00
|
|
|
var findSelectCmd = &cobra.Command{
|
|
|
|
Use: "findselect",
|
|
|
|
Short: "Generate a helper to select specific fields of a record by ID",
|
2016-02-29 12:45:28 +01:00
|
|
|
}
|
|
|
|
|
2016-03-16 15:33:58 +01:00
|
|
|
/* Delete commands */
|
2016-02-29 06:49:26 +01:00
|
|
|
|
|
|
|
var deleteCmd = &cobra.Command{
|
|
|
|
Use: "delete",
|
|
|
|
Short: "Generate delete statement helpers from table definitions",
|
|
|
|
}
|
2016-03-19 07:22:10 +01:00
|
|
|
|
|
|
|
/* Update commands */
|
|
|
|
|
|
|
|
var updateCmd = &cobra.Command{
|
|
|
|
Use: "update",
|
|
|
|
Short: "Generate update statement helpers from table definitions",
|
|
|
|
}
|