github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/core/table.go (about) 1 package core 2 3 import ( 4 "reflect" 5 "strings" 6 ) 7 8 // database table 9 type Table struct { 10 Name string 11 Type reflect.Type 12 columnsSeq []string 13 columnsMap map[string][]*Column 14 columns []*Column 15 Indexes map[string]*Index 16 PrimaryKeys []string 17 AutoIncrement string 18 Created map[string]bool 19 Updated string 20 Deleted string 21 Version string 22 Cacher Cacher 23 StoreEngine string 24 Charset string 25 } 26 27 func (table *Table) Columns() []*Column { 28 return table.columns 29 } 30 31 func (table *Table) ColumnsSeq() []string { 32 return table.columnsSeq 33 } 34 35 func NewEmptyTable() *Table { 36 return NewTable("", nil) 37 } 38 39 func NewTable(name string, t reflect.Type) *Table { 40 return &Table{Name: name, Type: t, 41 columnsSeq: make([]string, 0), 42 columns: make([]*Column, 0), 43 columnsMap: make(map[string][]*Column), 44 Indexes: make(map[string]*Index), 45 Created: make(map[string]bool), 46 PrimaryKeys: make([]string, 0), 47 } 48 } 49 50 func (table *Table) columnsByName(name string) []*Column { 51 52 n := len(name) 53 54 for k := range table.columnsMap { 55 if len(k) != n { 56 continue 57 } 58 if strings.EqualFold(k, name) { 59 return table.columnsMap[k] 60 } 61 } 62 return nil 63 } 64 65 func (table *Table) GetColumn(name string) *Column { 66 67 cols := table.columnsByName(name) 68 69 if cols != nil { 70 return cols[0] 71 } 72 73 return nil 74 } 75 76 func (table *Table) GetColumnIdx(name string, idx int) *Column { 77 78 cols := table.columnsByName(name) 79 80 if cols != nil && idx < len(cols) { 81 return cols[idx] 82 } 83 84 return nil 85 } 86 87 // if has primary key, return column 88 func (table *Table) PKColumns() []*Column { 89 columns := make([]*Column, len(table.PrimaryKeys)) 90 for i, name := range table.PrimaryKeys { 91 columns[i] = table.GetColumn(name) 92 } 93 return columns 94 } 95 96 func (table *Table) ColumnType(name string) reflect.Type { 97 t, _ := table.Type.FieldByName(name) 98 return t.Type 99 } 100 101 func (table *Table) AutoIncrColumn() *Column { 102 return table.GetColumn(table.AutoIncrement) 103 } 104 105 func (table *Table) VersionColumn() *Column { 106 return table.GetColumn(table.Version) 107 } 108 109 func (table *Table) UpdatedColumn() *Column { 110 return table.GetColumn(table.Updated) 111 } 112 113 func (table *Table) DeletedColumn() *Column { 114 return table.GetColumn(table.Deleted) 115 } 116 117 // add a column to table 118 func (table *Table) AddColumn(col *Column) { 119 table.columnsSeq = append(table.columnsSeq, col.Name) 120 table.columns = append(table.columns, col) 121 colName := strings.ToLower(col.Name) 122 if c, ok := table.columnsMap[colName]; ok { 123 table.columnsMap[colName] = append(c, col) 124 } else { 125 table.columnsMap[colName] = []*Column{col} 126 } 127 128 if col.IsPrimaryKey { 129 table.PrimaryKeys = append(table.PrimaryKeys, col.Name) 130 } 131 if col.IsAutoIncrement { 132 table.AutoIncrement = col.Name 133 } 134 if col.IsCreated { 135 table.Created[col.Name] = true 136 } 137 if col.IsUpdated { 138 table.Updated = col.Name 139 } 140 if col.IsDeleted { 141 table.Deleted = col.Name 142 } 143 if col.IsVersion { 144 table.Version = col.Name 145 } 146 } 147 148 // add an index or an unique to table 149 func (table *Table) AddIndex(index *Index) { 150 table.Indexes[index.Name] = index 151 }