2016-06-23 08:09:56 +02:00
|
|
|
package bdb
|
|
|
|
|
2016-11-11 10:01:09 +01:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/vattle/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-08-03 06:22:32 +02:00
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
DBType string
|
|
|
|
Default string
|
|
|
|
Nullable bool
|
|
|
|
Unique bool
|
|
|
|
Validated bool
|
2016-09-17 09:02:03 +02:00
|
|
|
|
|
|
|
// Postgres only extension bits
|
|
|
|
// ArrType is the underlying data type of the Postgres
|
|
|
|
// ARRAY type. See here:
|
|
|
|
// https://www.postgresql.org/docs/9.1/static/infoschema-element-types.html
|
|
|
|
ArrType *string
|
|
|
|
UDTName string
|
2017-01-02 06:16:08 +01:00
|
|
|
|
|
|
|
// MySQL only bits
|
|
|
|
// Used to get full type, ex:
|
|
|
|
// tinyint(1) instead of tinyint
|
|
|
|
// Used for "tinyint-as-bool" flag
|
|
|
|
FullDBType string
|
2017-03-15 13:31:09 +01:00
|
|
|
|
|
|
|
// MS SQL only bits
|
|
|
|
// Used to indicate that the value
|
|
|
|
// for this column is auto generated by database on insert (i.e. - timestamp (old) or rowversion (new))
|
|
|
|
AutoGenerated 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 {
|
2017-03-15 13:31:09 +01:00
|
|
|
if (defaults && (len(c.Default) != 0 || c.AutoGenerated)) || (!defaults && len(c.Default) == 0 && !c.AutoGenerated) {
|
2016-06-23 08:09:56 +02:00
|
|
|
cols = append(cols, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cols
|
|
|
|
}
|
2016-11-11 10:01:09 +01:00
|
|
|
|
|
|
|
// FilterColumnsByEnum generates the list of columns that are enum values.
|
|
|
|
func FilterColumnsByEnum(columns []Column) []Column {
|
|
|
|
var cols []Column
|
|
|
|
|
|
|
|
for _, c := range columns {
|
|
|
|
if strings.HasPrefix(c.DBType, "enum") {
|
|
|
|
cols = append(cols, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cols
|
|
|
|
}
|