Remove a bunch dead code
This commit is contained in:
parent
180fbb0b8c
commit
5f86014847
3 changed files with 7 additions and 204 deletions
138
bdb/column.go
138
bdb/column.go
|
@ -1,12 +1,6 @@
|
|||
package bdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/vattle/sqlboiler/strmangle"
|
||||
)
|
||||
import "github.com/vattle/sqlboiler/strmangle"
|
||||
|
||||
// Column holds information about a database column.
|
||||
// Types are Go types, converted by TranslateColumnType.
|
||||
|
@ -63,133 +57,3 @@ func FilterColumnsByDefault(defaults bool, columns []Column) []Column {
|
|||
|
||||
return cols
|
||||
}
|
||||
|
||||
// FilterColumnsBySimpleDefault generates a list of columns that have simple default values
|
||||
// A simple default value is one without a function call and a non-validated type
|
||||
func FilterColumnsBySimpleDefault(columns []Column) []Column {
|
||||
var cols []Column
|
||||
|
||||
for _, c := range columns {
|
||||
if len(c.Default) != 0 && !strings.ContainsAny(c.Default, "()") && !c.Validated {
|
||||
cols = append(cols, c)
|
||||
}
|
||||
}
|
||||
|
||||
return cols
|
||||
}
|
||||
|
||||
// FilterColumnsByAutoIncrement generates the list of auto increment columns
|
||||
func FilterColumnsByAutoIncrement(autos bool, columns []Column) []Column {
|
||||
var cols []Column
|
||||
|
||||
for _, c := range columns {
|
||||
if (autos && rgxAutoIncColumn.MatchString(c.Default)) ||
|
||||
(!autos && !rgxAutoIncColumn.MatchString(c.Default)) {
|
||||
cols = append(cols, c)
|
||||
}
|
||||
}
|
||||
|
||||
return cols
|
||||
}
|
||||
|
||||
// FilterColumnsByValidated generates the list of validated columns
|
||||
func FilterColumnsByValidated(columns []Column) []Column {
|
||||
var cols []Column
|
||||
|
||||
for _, c := range columns {
|
||||
if c.Validated {
|
||||
cols = append(cols, c)
|
||||
}
|
||||
}
|
||||
|
||||
return cols
|
||||
}
|
||||
|
||||
// FilterColumnsByUnique generates the list of unique columns
|
||||
func FilterColumnsByUnique(columns []Column) []Column {
|
||||
var cols []Column
|
||||
|
||||
for _, c := range columns {
|
||||
if c.Unique {
|
||||
cols = append(cols, c)
|
||||
}
|
||||
}
|
||||
|
||||
return cols
|
||||
}
|
||||
|
||||
var (
|
||||
rgxRawDefaultValue = regexp.MustCompile(`'(.*)'::`)
|
||||
rgxBoolDefaultValue = regexp.MustCompile(`(?i)true|false`)
|
||||
rgxByteaDefaultValue = regexp.MustCompile(`(?i)\\x([0-9A-F]*)`)
|
||||
)
|
||||
|
||||
// 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.
|
||||
func DefaultValues(columns []Column) []string {
|
||||
var dVals []string
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
switch c.Type {
|
||||
case "null.Uint", "null.Uint8", "null.Uint16", "null.Uint32", "null.Uint64",
|
||||
"null.Int", "null.Int8", "null.Int16", "null.Int32", "null.Int64",
|
||||
"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":
|
||||
m = rgxBoolDefaultValue.FindStringSubmatch(dVal)
|
||||
if len(m) == 0 {
|
||||
dVals = append(dVals, "false")
|
||||
}
|
||||
dVals = append(dVals, strings.ToLower(m[0]))
|
||||
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":
|
||||
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, "")
|
||||
}
|
||||
}
|
||||
|
||||
return dVals
|
||||
}
|
||||
|
|
|
@ -66,59 +66,3 @@ func TestFilterColumnsByDefault(t *testing.T) {
|
|||
t.Errorf("Invalid result: %#v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultValues(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
c := Column{}
|
||||
|
||||
c.Default = `\x12345678`
|
||||
c.Type = "[]byte"
|
||||
|
||||
res := DefaultValues([]Column{c})
|
||||
if len(res) != 1 {
|
||||
t.Errorf("Expected res len 1, got %d", len(res))
|
||||
}
|
||||
if res[0] != `[]byte{0x12, 0x34, 0x56, 0x78}` {
|
||||
t.Errorf("Invalid result: %#v", res)
|
||||
}
|
||||
|
||||
c.Default = `\x`
|
||||
|
||||
res = DefaultValues([]Column{c})
|
||||
if res[0] != `[]byte{}` {
|
||||
t.Errorf("Invalid result: %#v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterColumnsByAutoIncrement(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cols := []Column{
|
||||
{Name: "col1", Default: `nextval("thing"::thing)`},
|
||||
{Name: "col2", Default: "things"},
|
||||
{Name: "col3", Default: ""},
|
||||
{Name: "col4", Default: `nextval("thing"::thing)`},
|
||||
}
|
||||
|
||||
res := FilterColumnsByAutoIncrement(true, cols)
|
||||
if res[0].Name != `col1` {
|
||||
t.Errorf("Invalid result: %#v", res)
|
||||
}
|
||||
if res[1].Name != `col4` {
|
||||
t.Errorf("Invalid result: %#v", res)
|
||||
}
|
||||
|
||||
res = FilterColumnsByAutoIncrement(false, cols)
|
||||
if res[0].Name != `col2` {
|
||||
t.Errorf("Invalid result: %#v", res)
|
||||
}
|
||||
if res[1].Name != `col3` {
|
||||
t.Errorf("Invalid result: %#v", res)
|
||||
}
|
||||
|
||||
res = FilterColumnsByAutoIncrement(true, []Column{})
|
||||
if res != nil {
|
||||
t.Errorf("Invalid result: %#v", res)
|
||||
}
|
||||
}
|
||||
|
|
17
templates.go
17
templates.go
|
@ -169,15 +169,10 @@ var templateFunctions = template.FuncMap{
|
|||
"textsFromRelationship": textsFromRelationship,
|
||||
|
||||
// dbdrivers ops
|
||||
"filterColumnsByDefault": bdb.FilterColumnsByDefault,
|
||||
"filterColumnsBySimpleDefault": bdb.FilterColumnsBySimpleDefault,
|
||||
"filterColumnsByAutoIncrement": bdb.FilterColumnsByAutoIncrement,
|
||||
"filterColumnsByValidated": bdb.FilterColumnsByValidated,
|
||||
"filterColumnsByUnique": bdb.FilterColumnsByUnique,
|
||||
"sqlColDefinitions": bdb.SQLColDefinitions,
|
||||
"sqlColDefStrings": bdb.SQLColDefStrings,
|
||||
"columnNames": bdb.ColumnNames,
|
||||
"columnTypes": bdb.ColumnTypes,
|
||||
"columnDBTypes": bdb.ColumnDBTypes,
|
||||
"defaultValues": bdb.DefaultValues,
|
||||
"filterColumnsByDefault": bdb.FilterColumnsByDefault,
|
||||
"sqlColDefinitions": bdb.SQLColDefinitions,
|
||||
"sqlColDefStrings": bdb.SQLColDefStrings,
|
||||
"columnNames": bdb.ColumnNames,
|
||||
"columnTypes": bdb.ColumnTypes,
|
||||
"columnDBTypes": bdb.ColumnDBTypes,
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue