Fix DBTable
This commit is contained in:
parent
dfef617a95
commit
80e03dc3f7
18 changed files with 95 additions and 82 deletions
13
boil/types.go
Normal file
13
boil/types.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package boil
|
||||
|
||||
import "database/sql"
|
||||
|
||||
// DB implements the functions necessary for the templates to function.
|
||||
type DB interface {
|
||||
Exec(query string, args ...interface{}) (sql.Result, error)
|
||||
Query(query string, args ...interface{}) (*sql.Rows, error)
|
||||
QueryRow(query string, args ...interface{}) *sql.Row
|
||||
}
|
||||
|
||||
// M type is for providing where filters to Where helpers.
|
||||
type M map[string]interface{}
|
|
@ -21,8 +21,8 @@ var sqlBoilerDefaultImports = imports{
|
|||
"fmt",
|
||||
},
|
||||
thirdparty: []string{
|
||||
"github.com/pobri19/sqlboiler/boil",
|
||||
"gopkg.in/guregu/null.v3",
|
||||
"github.com/jmoiron/sqlx",
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -63,9 +63,9 @@ var sqlBoilerCommandRuns = map[string]CobraRunFunc{
|
|||
// 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.
|
||||
var sqlBoilerTemplateFuncs = template.FuncMap{
|
||||
"makeGoColName": makeGoColName,
|
||||
"makeGoName": makeGoName,
|
||||
"makeGoVarName": makeGoVarName,
|
||||
"makeDBColName": makeDBColName,
|
||||
"makeDBName": makeDBName,
|
||||
"makeSelectParamNames": makeSelectParamNames,
|
||||
"makeGoInsertParamNames": makeGoInsertParamNames,
|
||||
"makeGoInsertParamFlags": makeGoInsertParamFlags,
|
||||
|
|
|
@ -16,7 +16,7 @@ type CobraRunFunc func(cmd *cobra.Command, args []string)
|
|||
// the database driver chosen by the driver flag at runtime, and a pointer to the
|
||||
// output file, if one is specified with a flag.
|
||||
type CmdData struct {
|
||||
TablesInfo [][]dbdrivers.DBTable
|
||||
TablesInfo [][]dbdrivers.DBColumn
|
||||
TableNames []string
|
||||
PkgName string
|
||||
OutFolder string
|
||||
|
@ -26,7 +26,7 @@ type CmdData struct {
|
|||
// tplData is used to pass data to the template
|
||||
type tplData struct {
|
||||
TableName string
|
||||
TableData []dbdrivers.DBTable
|
||||
TableData []dbdrivers.DBColumn
|
||||
}
|
||||
|
||||
// errorQuit displays an error message and then exits the application.
|
||||
|
|
|
@ -143,7 +143,7 @@ func initTableNames() {
|
|||
}
|
||||
|
||||
// initTablesInfo builds a description of each table (column name, column type)
|
||||
// and assigns it to cmdData.TablesInfo, the slice of dbdrivers.DBTable slices.
|
||||
// and assigns it to cmdData.TablesInfo, the slice of dbdrivers.DBColumn slices.
|
||||
func initTablesInfo() {
|
||||
// loop over table Names and build TablesInfo
|
||||
for i := 0; i < len(cmdData.TableNames); i++ {
|
||||
|
|
|
@ -57,9 +57,9 @@ func processTemplate(t *template.Template, data *tplData) ([]byte, error) {
|
|||
}
|
||||
|
||||
// it into a go styled object variable name of "ColumnName".
|
||||
// makeGoColName also fully uppercases "ID" components of names, for example
|
||||
// makeGoName also fully uppercases "ID" components of names, for example
|
||||
// "column_name_id" to "ColumnNameID".
|
||||
func makeGoColName(name string) string {
|
||||
func makeGoName(name string) string {
|
||||
s := strings.Split(name, "_")
|
||||
|
||||
for i := 0; i < len(s); i++ {
|
||||
|
@ -97,19 +97,19 @@ func makeGoVarName(name string) string {
|
|||
return strings.Join(s, "")
|
||||
}
|
||||
|
||||
// makeDBColName takes a table name in the format of "table_name" and a
|
||||
// makeDBName takes a table name in the format of "table_name" and a
|
||||
// column name in the format of "column_name" and returns a name used in the
|
||||
// `db:""` component of an object in the format of "table_name_column_name"
|
||||
func makeDBColName(tableName, colName string) string {
|
||||
func makeDBName(tableName, colName string) string {
|
||||
return tableName + "_" + colName
|
||||
}
|
||||
|
||||
// makeGoInsertParamNames takes a []DBTable and returns a comma seperated
|
||||
// makeGoInsertParamNames takes a []DBColumn and returns a comma seperated
|
||||
// list of parameter names for the insert statement template.
|
||||
func makeGoInsertParamNames(data []dbdrivers.DBTable) string {
|
||||
func makeGoInsertParamNames(data []dbdrivers.DBColumn) string {
|
||||
var paramNames string
|
||||
for i := 0; i < len(data); i++ {
|
||||
paramNames = paramNames + data[i].ColName
|
||||
paramNames = paramNames + data[i].Name
|
||||
if len(data) != i+1 {
|
||||
paramNames = paramNames + ", "
|
||||
}
|
||||
|
@ -117,9 +117,9 @@ func makeGoInsertParamNames(data []dbdrivers.DBTable) string {
|
|||
return paramNames
|
||||
}
|
||||
|
||||
// makeGoInsertParamFlags takes a []DBTable and returns a comma seperated
|
||||
// makeGoInsertParamFlags takes a []DBColumn and returns a comma seperated
|
||||
// list of parameter flags for the insert statement template.
|
||||
func makeGoInsertParamFlags(data []dbdrivers.DBTable) string {
|
||||
func makeGoInsertParamFlags(data []dbdrivers.DBColumn) string {
|
||||
var paramFlags string
|
||||
for i := 0; i < len(data); i++ {
|
||||
paramFlags = fmt.Sprintf("%s$%d", paramFlags, i+1)
|
||||
|
@ -130,15 +130,15 @@ func makeGoInsertParamFlags(data []dbdrivers.DBTable) string {
|
|||
return paramFlags
|
||||
}
|
||||
|
||||
// makeSelectParamNames takes a []DBTable and returns a comma seperated
|
||||
// makeSelectParamNames takes a []DBColumn 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 {
|
||||
func makeSelectParamNames(tableName string, data []dbdrivers.DBColumn) 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),
|
||||
paramNames = fmt.Sprintf("%s%s AS %s", paramNames, data[i].Name,
|
||||
makeDBName(tableName, data[i].Name),
|
||||
)
|
||||
if len(data) != i+1 {
|
||||
paramNames = paramNames + ", "
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
// {{makeGoColName $tableName}}All retrieves all records.
|
||||
func {{makeGoColName $tableName}}All(db *sqlx.DB) ([]*{{makeGoColName $tableName}}, error) {
|
||||
// {{makeGoName $tableName}}All retrieves all records.
|
||||
func {{makeGoName $tableName}}All(db boil.DB) ([]*{{makeGoName $tableName}}, error) {
|
||||
{{$varName := makeGoVarName $tableName -}}
|
||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||
var {{$varName}} []*{{makeGoName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
{{- $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) {
|
||||
// {{makeGoName $tableName}}AllBy retrieves all records with the specified column values.
|
||||
func {{makeGoName $tableName}}AllBy(db boil.DB, columns map[string]interface{}) ([]*{{makeGoName $tableName}}, error) {
|
||||
{{$varName := makeGoVarName $tableName -}}
|
||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||
var {{$varName}} []*{{makeGoName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
// {{makeGoColName $tableName}}Delete deletes a single record.
|
||||
func {{makeGoColName $tableName}}Delete(db *sqlx.DB, id int) error {
|
||||
// {{makeGoName $tableName}}Delete deletes a single record.
|
||||
func {{makeGoName $tableName}}Delete(db boil.DB, id int) error {
|
||||
if id == nil {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} delete")
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
// {{makeGoColName $tableName}}FieldsAll retrieves the specified columns for all records.
|
||||
// {{makeGoName $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 {
|
||||
func {{makeGoName $tableName}}FieldsAll(db boil.DB, results interface{}) error {
|
||||
{{$varName := makeGoVarName $tableName -}}
|
||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||
var {{$varName}} []*{{makeGoName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
// {{makeGoColName $tableName}}FieldsAllBy retrieves the specified columns
|
||||
// {{makeGoName $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 {
|
||||
func {{makeGoName $tableName}}FieldsAllBy(db boil.DB, columns map[string]interface{}, results interface{}) error {
|
||||
{{$varName := makeGoVarName $tableName -}}
|
||||
var {{$varName}} []*{{makeGoColName $tableName}}
|
||||
var {{$varName}} []*{{makeGoName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}}`)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
// {{makeGoColName $tableName}}FieldsFind retrieves the specified columns for a single record by ID.
|
||||
// {{makeGoName $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) {
|
||||
func {{makeGoName $tableName}}FieldsFind(db boil.DB, id int, results interface{}) (*{{makeGoName $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := makeGoVarName $tableName}}
|
||||
var {{$varName}} *{{makeGoColName $tableName}}
|
||||
var {{$varName}} *{{makeGoName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
// {{makeGoColName $tableName}}FieldsFindBy retrieves the specified columns
|
||||
// {{makeGoName $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) {
|
||||
func {{makeGoName $tableName}}FieldsFindBy(db boil.DB, columns map[string]interface{}, results interface{}) (*{{makeGoName $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := makeGoVarName $tableName}}
|
||||
var {{$varName}} *{{makeGoColName $tableName}}
|
||||
var {{$varName}} *{{makeGoName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
// {{makeGoColName $tableName}}Find retrieves a single record by ID.
|
||||
func {{makeGoColName $tableName}}Find(db *sqlx.DB, id int) (*{{makeGoColName $tableName}}, error) {
|
||||
// {{makeGoName $tableName}}Find retrieves a single record by ID.
|
||||
func {{makeGoName $tableName}}Find(db boil.DB, id int) (*{{makeGoName $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := makeGoVarName $tableName}}
|
||||
var {{$varName}} *{{makeGoColName $tableName}}
|
||||
var {{$varName}} *{{makeGoName $tableName}}
|
||||
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
{{- $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) {
|
||||
// {{makeGoName $tableName}}FindBy retrieves a single record with the specified column values.
|
||||
func {{makeGoName $tableName}}FindBy(db boil.DB, columns map[string]interface{}) (*{{makeGoName $tableName}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("model: no id provided for {{$tableName}} select")
|
||||
}
|
||||
{{$varName := makeGoVarName $tableName}}
|
||||
var {{$varName}} *{{makeGoColName $tableName}}
|
||||
var {{$varName}} *{{makeGoName $tableName}}
|
||||
err := db.Select(&{{$varName}}, fmt.Sprintf(`SELECT {{makeSelectParamNames $tableName .TableData}} WHERE %s=$1`, column), value)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
// {{makeGoColName $tableName}}Insert inserts a single record.
|
||||
func {{makeGoColName $tableName}}Insert(db *sqlx.DB, o *{{makeGoColName $tableName}}) (int, error) {
|
||||
// {{makeGoName $tableName}}Insert inserts a single record.
|
||||
func {{makeGoName $tableName}}Insert(db boil.DB, o *{{makeGoName $tableName}}) (int, error) {
|
||||
if o == nil {
|
||||
return 0, errors.New("model: no {{$tableName}} provided for insertion")
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{{- $tableName := .TableName -}}
|
||||
// {{makeGoColName $tableName}} is an object representing the database table.
|
||||
type {{makeGoColName $tableName}} struct {
|
||||
// {{makeGoName $tableName}} is an object representing the database table.
|
||||
type {{makeGoName $tableName}} struct {
|
||||
{{range $key, $value := .TableData -}}
|
||||
{{makeGoColName $value.ColName}} {{$value.ColType}} `db:"{{makeDBColName $tableName $value.ColName}}" json:"{{$value.ColName}}"`
|
||||
{{makeGoName $value.Name}} {{$value.Type}} `db:"{{makeDBName $tableName $value.Name}}" json:"{{$value.Name}}"`
|
||||
{{end -}}
|
||||
}
|
||||
|
|
|
@ -11,13 +11,13 @@ type DBDriver interface {
|
|||
// GetAllTableNames connects to the database and retrieves all "public" table names
|
||||
GetAllTableNames() ([]string, error)
|
||||
|
||||
// GetTableInfo builds an object of []DBTable containing the table information
|
||||
GetTableInfo(tableName string) ([]DBTable, error)
|
||||
// GetTableInfo retrieves column information about the table.
|
||||
GetTableInfo(tableName string) ([]DBColumn, error)
|
||||
|
||||
// ParseTableInfo builds a DBTable out of a column name and column type.
|
||||
// ParseTableInfo builds a DBColumn out of a column name and column type.
|
||||
// Its main responsibility is to convert database types to Go types, for example
|
||||
// "varchar" to "string".
|
||||
ParseTableInfo(colName, colType, isNullable string) DBTable
|
||||
ParseTableInfo(name, colType string, isNullable bool) DBColumn
|
||||
|
||||
// Open the database connection
|
||||
Open() error
|
||||
|
@ -26,10 +26,10 @@ type DBDriver interface {
|
|||
Close()
|
||||
}
|
||||
|
||||
// DBTable holds a column name, for example "column_name", and a column type,
|
||||
// for example "int64". Column types are Go types, converted by ParseTableInfo.
|
||||
type DBTable struct {
|
||||
ColName string
|
||||
ColType string
|
||||
IsNullable string
|
||||
// DBColumn holds information about a database column name.
|
||||
// Column types are Go types, converted by ParseTableInfo.
|
||||
type DBColumn struct {
|
||||
Name string
|
||||
Type string
|
||||
IsNullable bool
|
||||
}
|
||||
|
|
|
@ -73,10 +73,10 @@ func (d *PostgresDriver) GetAllTableNames() ([]string, error) {
|
|||
|
||||
// GetTableInfo takes a table name and attempts to retrieve the table information
|
||||
// from the database information_schema.columns. It retrieves the column names
|
||||
// and column types and returns those as a []DBTable after ParseTableInfo()
|
||||
// and column types and returns those as a []DBColumn after ParseTableInfo()
|
||||
// converts the SQL types to Go types, for example: "varchar" to "string"
|
||||
func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBTable, error) {
|
||||
var tableInfo []DBTable
|
||||
func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBColumn, error) {
|
||||
var tableInfo []DBColumn
|
||||
|
||||
rows, err := d.dbConn.Query(`select column_name, data_type, is_nullable from
|
||||
information_schema.columns where table_name=$1`, tableName)
|
||||
|
@ -91,7 +91,7 @@ func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBTable, error) {
|
|||
if err := rows.Scan(&colName, &colType, &isNullable); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tableInfo = append(tableInfo, d.ParseTableInfo(colName, colType, isNullable))
|
||||
tableInfo = append(tableInfo, d.ParseTableInfo(colName, colType, "YES" == isNullable))
|
||||
}
|
||||
|
||||
return tableInfo, nil
|
||||
|
@ -99,42 +99,42 @@ func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBTable, error) {
|
|||
|
||||
// ParseTableInfo converts postgres database types to Go types, for example
|
||||
// "varchar" to "string" and "bigint" to "int64". It returns this parsed data
|
||||
// as a DBTable object.
|
||||
func (d *PostgresDriver) ParseTableInfo(colName, colType, isNullable string) DBTable {
|
||||
t := DBTable{}
|
||||
// as a DBColumn object.
|
||||
func (d *PostgresDriver) ParseTableInfo(colName, colType string, isNullable bool) DBColumn {
|
||||
var t DBColumn
|
||||
|
||||
t.ColName = colName
|
||||
if isNullable == "YES" {
|
||||
t.Name = colName
|
||||
if isNullable {
|
||||
switch colType {
|
||||
case "bigint", "bigserial", "integer", "smallint", "smallserial", "serial":
|
||||
t.ColType = "null.Int"
|
||||
t.Type = "null.Int"
|
||||
case "bit", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
|
||||
t.ColType = "null.String"
|
||||
t.Type = "null.String"
|
||||
case "boolean":
|
||||
t.ColType = "null.Bool"
|
||||
t.Type = "null.Bool"
|
||||
case "date", "interval", "time", "timestamp without time zone", "timestamp with time zone":
|
||||
t.ColType = "null.Time"
|
||||
t.Type = "null.Time"
|
||||
case "double precision", "money", "numeric", "real":
|
||||
t.ColType = "null.Float"
|
||||
t.Type = "null.Float"
|
||||
default:
|
||||
t.ColType = "null.String"
|
||||
t.Type = "null.String"
|
||||
}
|
||||
} else {
|
||||
switch colType {
|
||||
case "bigint", "bigserial", "integer", "smallint", "smallserial", "serial":
|
||||
t.ColType = "int64"
|
||||
t.Type = "int64"
|
||||
case "bit", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
|
||||
t.ColType = "string"
|
||||
t.Type = "string"
|
||||
case "bytea":
|
||||
t.ColType = "[]byte"
|
||||
t.Type = "[]byte"
|
||||
case "boolean":
|
||||
t.ColType = "bool"
|
||||
t.Type = "bool"
|
||||
case "date", "interval", "time", "timestamp without time zone", "timestamp with time zone":
|
||||
t.ColType = "time.Time"
|
||||
t.Type = "time.Time"
|
||||
case "double precision", "money", "numeric", "real":
|
||||
t.ColType = "float64"
|
||||
t.Type = "float64"
|
||||
default:
|
||||
t.ColType = "string"
|
||||
t.Type = "string"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue