2016-06-23 08:09:56 +02:00
|
|
|
package bdb
|
|
|
|
|
2016-07-06 08:02:35 +02:00
|
|
|
import (
|
2016-07-13 18:51:40 +02:00
|
|
|
"fmt"
|
2016-07-06 08:02:35 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2016-07-13 18:51:40 +02:00
|
|
|
|
|
|
|
"github.com/nullbio/sqlboiler/strmangle"
|
2016-07-06 08:02:35 +02:00
|
|
|
)
|
|
|
|
|
2016-06-23 08:09:56 +02:00
|
|
|
// Column holds information about a database column.
|
|
|
|
// Types are Go types, converted by TranslateColumnType.
|
|
|
|
type Column struct {
|
2016-07-09 19:13:35 +02:00
|
|
|
Name string
|
|
|
|
Type string
|
2016-07-13 18:51:40 +02:00
|
|
|
DBType string
|
2016-07-09 19:13:35 +02:00
|
|
|
Default string
|
|
|
|
Nullable bool
|
2016-06-23 08:09:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ColumnNames of the columns.
|
|
|
|
func ColumnNames(cols []Column) []string {
|
|
|
|
names := make([]string, len(cols))
|
|
|
|
for i, c := range cols {
|
|
|
|
names[i] = c.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2016-07-13 18:51:40 +02:00
|
|
|
// ColumnDBTypes of the columns.
|
|
|
|
func ColumnDBTypes(cols []Column) map[string]string {
|
|
|
|
types := map[string]string{}
|
|
|
|
|
|
|
|
for _, c := range cols {
|
|
|
|
types[strmangle.TitleCase(c.Name)] = c.DBType
|
|
|
|
}
|
|
|
|
|
|
|
|
return types
|
|
|
|
}
|
|
|
|
|
2016-06-23 08:09:56 +02:00
|
|
|
// FilterColumnsByDefault generates the list of columns that have default values
|
2016-06-23 08:48:49 +02:00
|
|
|
func FilterColumnsByDefault(defaults bool, columns []Column) []Column {
|
2016-06-23 08:09:56 +02:00
|
|
|
var cols []Column
|
|
|
|
|
|
|
|
for _, c := range columns {
|
|
|
|
if (defaults && len(c.Default) != 0) || (!defaults && len(c.Default) == 0) {
|
|
|
|
cols = append(cols, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cols
|
|
|
|
}
|
|
|
|
|
2016-07-06 08:02:35 +02:00
|
|
|
// FilterColumnsBySimpleDefault generates a list of columns that have simple default values
|
|
|
|
// A simple default value is one without a function call
|
|
|
|
func FilterColumnsBySimpleDefault(columns []Column) []Column {
|
|
|
|
var cols []Column
|
|
|
|
|
|
|
|
for _, c := range columns {
|
|
|
|
if len(c.Default) != 0 && !strings.ContainsAny(c.Default, "()") {
|
|
|
|
cols = append(cols, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cols
|
|
|
|
}
|
|
|
|
|
2016-06-23 08:09:56 +02:00
|
|
|
// FilterColumnsByAutoIncrement generates the list of auto increment columns
|
2016-07-06 08:02:35 +02:00
|
|
|
func FilterColumnsByAutoIncrement(autos bool, columns []Column) []Column {
|
2016-06-23 08:09:56 +02:00
|
|
|
var cols []Column
|
|
|
|
|
|
|
|
for _, c := range columns {
|
2016-07-06 08:02:35 +02:00
|
|
|
if (autos && rgxAutoIncColumn.MatchString(c.Default)) ||
|
|
|
|
(!autos && !rgxAutoIncColumn.MatchString(c.Default)) {
|
2016-06-23 08:09:56 +02:00
|
|
|
cols = append(cols, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cols
|
|
|
|
}
|
2016-07-06 08:02:35 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
rgxRawDefaultValue = regexp.MustCompile(`'(.*)'::`)
|
|
|
|
rgxBoolDefaultValue = regexp.MustCompile(`(?i)true|false`)
|
|
|
|
rgxByteaDefaultValue = regexp.MustCompile(`(?i)\\x([0-9A-F]*)`)
|
|
|
|
)
|
|
|
|
|
2016-07-13 18:51:40 +02:00
|
|
|
// DefaultValues returns the Go converted values of the default value columns.
|
|
|
|
// For the time columns it will return time.Now() since we cannot extract
|
|
|
|
// the true time from the default value string.
|
2016-07-08 18:39:36 +02:00
|
|
|
func DefaultValues(columns []Column) []string {
|
|
|
|
var dVals []string
|
2016-07-06 08:02:35 +02:00
|
|
|
|
2016-07-08 18:39:36 +02:00
|
|
|
for _, c := range columns {
|
|
|
|
var dVal string
|
|
|
|
// Attempt to strip out the raw default value if its contained
|
|
|
|
// within a Postgres type cast statement
|
|
|
|
m := rgxRawDefaultValue.FindStringSubmatch(c.Default)
|
|
|
|
if len(m) > 1 {
|
|
|
|
dVal = m[len(m)-1]
|
|
|
|
} else {
|
|
|
|
dVal = c.Default
|
2016-07-06 08:02:35 +02:00
|
|
|
}
|
2016-07-08 18:39:36 +02:00
|
|
|
|
|
|
|
switch c.Type {
|
|
|
|
case "null.Uint", "null.Uint8", "null.Uint16", "null.Uint32", "null.Uint64",
|
|
|
|
"null.Int", "null.Int8", "null.Int16", "null.Int32", "null.Int64",
|
2016-07-13 18:51:40 +02:00
|
|
|
"null.Float32", "null.Float64":
|
|
|
|
dVals = append(dVals,
|
|
|
|
fmt.Sprintf(`null.New%s(%s, true)`,
|
|
|
|
strings.TrimPrefix(c.Type, "null."),
|
|
|
|
dVal),
|
|
|
|
)
|
|
|
|
case "uint", "uint8", "uint16", "uint32", "uint64",
|
|
|
|
"int", "int8", "int16", "int32", "int64", "float32", "float64":
|
|
|
|
dVals = append(dVals, fmt.Sprintf(`%s(%s)`, c.Type, dVal))
|
|
|
|
case "null.Bool":
|
|
|
|
m = rgxBoolDefaultValue.FindStringSubmatch(dVal)
|
|
|
|
if len(m) == 0 {
|
|
|
|
dVals = append(dVals, `null.NewBool(false, true)`)
|
|
|
|
}
|
|
|
|
dVals = append(dVals, fmt.Sprintf(`null.NewBool(%s, true)`, strings.ToLower(dVal)))
|
|
|
|
case "bool":
|
2016-07-08 18:39:36 +02:00
|
|
|
m = rgxBoolDefaultValue.FindStringSubmatch(dVal)
|
|
|
|
if len(m) == 0 {
|
|
|
|
dVals = append(dVals, "false")
|
|
|
|
}
|
|
|
|
dVals = append(dVals, strings.ToLower(m[0]))
|
2016-07-13 18:51:40 +02:00
|
|
|
case "null.Time":
|
|
|
|
dVals = append(dVals, fmt.Sprintf(`null.NewTime(time.Now(), true)`))
|
|
|
|
case "time.Time":
|
|
|
|
dVals = append(dVals, `time.Now()`)
|
|
|
|
case "null.String":
|
|
|
|
dVals = append(dVals, fmt.Sprintf(`null.NewString("%s", true)`, dVal))
|
|
|
|
case "string":
|
2016-07-08 18:39:36 +02:00
|
|
|
dVals = append(dVals, `"`+dVal+`"`)
|
|
|
|
case "[]byte":
|
|
|
|
m := rgxByteaDefaultValue.FindStringSubmatch(dVal)
|
|
|
|
if len(m) != 2 {
|
|
|
|
dVals = append(dVals, `[]byte{}`)
|
|
|
|
}
|
|
|
|
hexstr := m[1]
|
|
|
|
bs := make([]string, len(hexstr)/2)
|
|
|
|
count := 0
|
|
|
|
for i := 0; i < len(hexstr); i += 2 {
|
|
|
|
bs[count] = "0x" + hexstr[i:i+2]
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
dVals = append(dVals, `[]byte{`+strings.Join(bs, ", ")+`}`)
|
|
|
|
default:
|
|
|
|
dVals = append(dVals, "")
|
2016-07-06 08:02:35 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-08 18:39:36 +02:00
|
|
|
|
|
|
|
return dVals
|
2016-07-06 08:02:35 +02:00
|
|
|
}
|