github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/migrate/column_type.go (about) 1 package migrate 2 3 import ( 4 "fmt" 5 "reflect" 6 "strings" 7 ) 8 9 const ( 10 UNIQUE int = iota 11 PRIMARYKEY 12 AUTOINCREMENT 13 NULL 14 NOTNULL 15 ) 16 17 // columnType will be injected to migration script 18 // along with MigrationDriver. `AttrMap` is used to 19 // defines distinct column's attribute between database 20 // implementation. e.g. 'AUTOINCREMENT' in sqlite and 21 // 'AUTO_INCREMENT' in mysql. 22 type columnType struct { 23 Driver string 24 AttrMap map[int]string 25 } 26 27 // defaultMap defines default values for column's attribute 28 // lookup. 29 var defaultMap = map[int]string{ 30 UNIQUE: "UNIQUE", 31 PRIMARYKEY: "PRIMARY KEY", 32 AUTOINCREMENT: "AUTOINCREMENT", 33 NULL: "NULL", 34 NOTNULL: "NOT NULL", 35 } 36 37 // Integer returns column definition for INTEGER typed column. 38 // Additional attributes may be specified as string or predefined key 39 // listed in defaultMap. 40 func (c *columnType) Integer(colName string, spec ...interface{}) string { 41 return fmt.Sprintf("%s INTEGER %s", colName, c.parseAttr(spec)) 42 } 43 44 // String returns column definition for VARCHAR(255) typed column. 45 func (c *columnType) String(colName string, spec ...interface{}) string { 46 return fmt.Sprintf("%s VARCHAR(255) %s", colName, c.parseAttr(spec)) 47 } 48 49 // Text returns column definition for TEXT typed column. 50 func (c *columnType) Text(colName string, spec ...interface{}) string { 51 return fmt.Sprintf("%s TEXT %s", colName, c.parseAttr(spec)) 52 } 53 54 // Blob returns column definition for BLOB typed column 55 func (c *columnType) Blob(colName string, spec ...interface{}) string { 56 return fmt.Sprintf("%s BLOB %s", colName, c.parseAttr(spec)) 57 } 58 59 // Timestamp returns column definition for TIMESTAMP typed column 60 func (c *columnType) Timestamp(colName string, spec ...interface{}) string { 61 return fmt.Sprintf("%s TIMESTAMP %s", colName, c.parseAttr(spec)) 62 } 63 64 // Bool returns column definition for BOOLEAN typed column 65 func (c *columnType) Bool(colName string, spec ...interface{}) string { 66 return fmt.Sprintf("%s BOOLEAN %s", colName, c.parseAttr(spec)) 67 } 68 69 // Varchar returns column definition for VARCHAR typed column. 70 // column's max length is specified as `length`. 71 func (c *columnType) Varchar(colName string, length int, spec ...interface{}) string { 72 return fmt.Sprintf("%s VARCHAR(%d) %s", colName, length, c.parseAttr(spec)) 73 } 74 75 // attr returns string representation of column attribute specified as key for defaultMap. 76 func (c *columnType) attr(flag int) string { 77 if v, ok := c.AttrMap[flag]; ok { 78 return v 79 } 80 return defaultMap[flag] 81 } 82 83 // parseAttr reflects spec value for its type and returns the string 84 // representation returned by `attr` 85 func (c *columnType) parseAttr(spec []interface{}) string { 86 var attrs []string 87 for _, v := range spec { 88 switch reflect.ValueOf(v).Kind() { 89 case reflect.Int: 90 attrs = append(attrs, c.attr(v.(int))) 91 case reflect.String: 92 attrs = append(attrs, v.(string)) 93 } 94 } 95 return strings.Join(attrs, " ") 96 }