github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/drivers/column.go (about) 1 package drivers 2 3 import ( 4 "regexp" 5 6 "github.com/volatiletech/strmangle" 7 ) 8 9 var rgxEnum = regexp.MustCompile(`^enum(\.\w+)?\([^)]+\)$`) 10 11 // Column holds information about a database column. 12 // Types are Go types, converted by TranslateColumnType. 13 type Column struct { 14 Name string `json:"name" toml:"name"` 15 Type string `json:"type" toml:"type"` 16 DBType string `json:"db_type" toml:"db_type"` 17 Default string `json:"default" toml:"default"` 18 Comment string `json:"comment" toml:"comment"` 19 Nullable bool `json:"nullable" toml:"nullable"` 20 Unique bool `json:"unique" toml:"unique"` 21 Validated bool `json:"validated" toml:"validated"` 22 AutoGenerated bool `json:"auto_generated" toml:"auto_generated"` 23 24 // Postgres only extension bits 25 // ArrType is the underlying data type of the Postgres 26 // ARRAY type. See here: 27 // https://www.postgresql.org/docs/9.1/static/infoschema-element-types.html 28 ArrType *string `json:"arr_type" toml:"arr_type"` 29 UDTName string `json:"udt_name" toml:"udt_name"` 30 // DomainName is the domain type name associated to the column. See here: 31 // https://www.postgresql.org/docs/10/extend-type-system.html#EXTEND-TYPE-SYSTEM-DOMAINS 32 DomainName *string `json:"domain_name" toml:"domain_name"` 33 34 // MySQL only bits 35 // Used to get full type, ex: 36 // tinyint(1) instead of tinyint 37 // Used for "tinyint-as-bool" flag 38 FullDBType string `json:"full_db_type" toml:"full_db_type"` 39 } 40 41 // ColumnNames of the columns. 42 func ColumnNames(cols []Column) []string { 43 names := make([]string, len(cols)) 44 for i, c := range cols { 45 names[i] = c.Name 46 } 47 48 return names 49 } 50 51 // ColumnDBTypes of the columns. 52 func ColumnDBTypes(cols []Column) map[string]string { 53 types := map[string]string{} 54 55 for _, c := range cols { 56 types[strmangle.TitleCase(c.Name)] = c.DBType 57 } 58 59 return types 60 } 61 62 // FilterColumnsByAuto generates the list of columns that have autogenerated values 63 func FilterColumnsByAuto(auto bool, columns []Column) []Column { 64 var cols []Column 65 66 for _, c := range columns { 67 if (auto && c.AutoGenerated) || (!auto && !c.AutoGenerated) { 68 cols = append(cols, c) 69 } 70 } 71 72 return cols 73 } 74 75 // FilterColumnsByDefault generates the list of columns that have default values 76 func FilterColumnsByDefault(defaults bool, columns []Column) []Column { 77 var cols []Column 78 79 for _, c := range columns { 80 if (defaults && len(c.Default) != 0) || (!defaults && len(c.Default) == 0) { 81 cols = append(cols, c) 82 } 83 } 84 85 return cols 86 } 87 88 // FilterColumnsByEnum generates the list of columns that are enum values. 89 func FilterColumnsByEnum(columns []Column) []Column { 90 var cols []Column 91 92 for _, c := range columns { 93 if rgxEnum.MatchString(c.DBType) { 94 cols = append(cols, c) 95 } 96 } 97 98 return cols 99 } 100 101 // IsEnumDBType reports whether the column type is Enum 102 func IsEnumDBType(dbType string) bool { 103 return rgxEnum.MatchString(dbType) 104 }