Added nullable struct types
* Added null.v3 import
This commit is contained in:
parent
57e20cbfe3
commit
7539febd22
3 changed files with 42 additions and 23 deletions
|
@ -21,6 +21,7 @@ var sqlBoilerDefaultImports = imports{
|
||||||
"fmt",
|
"fmt",
|
||||||
},
|
},
|
||||||
thirdparty: []string{
|
thirdparty: []string{
|
||||||
|
"gopkg.in/guregu/null.v3",
|
||||||
"github.com/jmoiron/sqlx",
|
"github.com/jmoiron/sqlx",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ type DBDriver interface {
|
||||||
// ParseTableInfo builds a DBTable out of a column name and column type.
|
// ParseTableInfo builds a DBTable out of a column name and column type.
|
||||||
// Its main responsibility is to convert database types to Go types, for example
|
// Its main responsibility is to convert database types to Go types, for example
|
||||||
// "varchar" to "string".
|
// "varchar" to "string".
|
||||||
ParseTableInfo(colName, colType string) DBTable
|
ParseTableInfo(colName, colType, isNullable string) DBTable
|
||||||
|
|
||||||
// Open the database connection
|
// Open the database connection
|
||||||
Open() error
|
Open() error
|
||||||
|
@ -29,6 +29,7 @@ type DBDriver interface {
|
||||||
// DBTable holds a column name, for example "column_name", and a column type,
|
// DBTable holds a column name, for example "column_name", and a column type,
|
||||||
// for example "int64". Column types are Go types, converted by ParseTableInfo.
|
// for example "int64". Column types are Go types, converted by ParseTableInfo.
|
||||||
type DBTable struct {
|
type DBTable struct {
|
||||||
ColName string
|
ColName string
|
||||||
ColType string
|
ColType string
|
||||||
|
IsNullable string
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,7 +78,7 @@ func (d *PostgresDriver) GetAllTableNames() ([]string, error) {
|
||||||
func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBTable, error) {
|
func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBTable, error) {
|
||||||
var tableInfo []DBTable
|
var tableInfo []DBTable
|
||||||
|
|
||||||
rows, err := d.dbConn.Query(`select column_name, data_type from
|
rows, err := d.dbConn.Query(`select column_name, data_type, is_nullable from
|
||||||
information_schema.columns where table_name=$1`, tableName)
|
information_schema.columns where table_name=$1`, tableName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -87,11 +87,11 @@ func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBTable, error) {
|
||||||
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var colName, colType string
|
var colName, colType, isNullable string
|
||||||
if err := rows.Scan(&colName, &colType); err != nil {
|
if err := rows.Scan(&colName, &colType, &isNullable); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tableInfo = append(tableInfo, d.ParseTableInfo(colName, colType))
|
tableInfo = append(tableInfo, d.ParseTableInfo(colName, colType, isNullable))
|
||||||
}
|
}
|
||||||
|
|
||||||
return tableInfo, nil
|
return tableInfo, nil
|
||||||
|
@ -100,25 +100,42 @@ func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBTable, error) {
|
||||||
// ParseTableInfo converts postgres database types to Go types, for example
|
// ParseTableInfo converts postgres database types to Go types, for example
|
||||||
// "varchar" to "string" and "bigint" to "int64". It returns this parsed data
|
// "varchar" to "string" and "bigint" to "int64". It returns this parsed data
|
||||||
// as a DBTable object.
|
// as a DBTable object.
|
||||||
func (d *PostgresDriver) ParseTableInfo(colName, colType string) DBTable {
|
func (d *PostgresDriver) ParseTableInfo(colName, colType, isNullable string) DBTable {
|
||||||
t := DBTable{}
|
t := DBTable{}
|
||||||
|
|
||||||
t.ColName = colName
|
t.ColName = colName
|
||||||
switch colType {
|
if isNullable == "YES" {
|
||||||
case "bigint", "bigserial", "integer", "smallint", "smallserial", "serial":
|
switch colType {
|
||||||
t.ColType = "int64"
|
case "bigint", "bigserial", "integer", "smallint", "smallserial", "serial":
|
||||||
case "bit", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
|
t.ColType = "null.Int"
|
||||||
t.ColType = "string"
|
case "bit", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
|
||||||
case "bytea":
|
t.ColType = "null.String"
|
||||||
t.ColType = "[]byte"
|
case "boolean":
|
||||||
case "boolean":
|
t.ColType = "null.Bool"
|
||||||
t.ColType = "bool"
|
case "date", "interval", "time", "timestamp without time zone", "timestamp with time zone":
|
||||||
case "date", "interval", "time", "timestamp without time zone", "timestamp with time zone":
|
t.ColType = "null.Time"
|
||||||
t.ColType = "time.Time"
|
case "double precision", "money", "numeric", "real":
|
||||||
case "double precision", "money", "numeric", "real":
|
t.ColType = "null.Float"
|
||||||
t.ColType = "float64"
|
default:
|
||||||
default:
|
t.ColType = "null.String"
|
||||||
t.ColType = "string"
|
}
|
||||||
|
} else {
|
||||||
|
switch colType {
|
||||||
|
case "bigint", "bigserial", "integer", "smallint", "smallserial", "serial":
|
||||||
|
t.ColType = "int64"
|
||||||
|
case "bit", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
|
||||||
|
t.ColType = "string"
|
||||||
|
case "bytea":
|
||||||
|
t.ColType = "[]byte"
|
||||||
|
case "boolean":
|
||||||
|
t.ColType = "bool"
|
||||||
|
case "date", "interval", "time", "timestamp without time zone", "timestamp with time zone":
|
||||||
|
t.ColType = "time.Time"
|
||||||
|
case "double precision", "money", "numeric", "real":
|
||||||
|
t.ColType = "float64"
|
||||||
|
default:
|
||||||
|
t.ColType = "string"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return t
|
return t
|
||||||
|
|
Loading…
Add table
Reference in a new issue