table.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package core
  2. import (
  3. "reflect"
  4. "strings"
  5. )
  6. // database table
  7. type Table struct {
  8. Name string
  9. Type reflect.Type
  10. columnsSeq []string
  11. columnsMap map[string][]*Column
  12. columns []*Column
  13. Indexes map[string]*Index
  14. PrimaryKeys []string
  15. AutoIncrement string
  16. Created map[string]bool
  17. Updated string
  18. Deleted string
  19. Version string
  20. Cacher Cacher
  21. StoreEngine string
  22. Charset string
  23. }
  24. func (table *Table) Columns() []*Column {
  25. return table.columns
  26. }
  27. func (table *Table) ColumnsSeq() []string {
  28. return table.columnsSeq
  29. }
  30. func NewEmptyTable() *Table {
  31. return NewTable("", nil)
  32. }
  33. func NewTable(name string, t reflect.Type) *Table {
  34. return &Table{Name: name, Type: t,
  35. columnsSeq: make([]string, 0),
  36. columns: make([]*Column, 0),
  37. columnsMap: make(map[string][]*Column),
  38. Indexes: make(map[string]*Index),
  39. Created: make(map[string]bool),
  40. PrimaryKeys: make([]string, 0),
  41. }
  42. }
  43. func (table *Table) GetColumn(name string) *Column {
  44. if c, ok := table.columnsMap[strings.ToLower(name)]; ok {
  45. return c[0]
  46. }
  47. return nil
  48. }
  49. func (table *Table) GetColumnIdx(name string, idx int) *Column {
  50. if c, ok := table.columnsMap[strings.ToLower(name)]; ok {
  51. if idx < len(c) {
  52. return c[idx]
  53. }
  54. }
  55. return nil
  56. }
  57. // if has primary key, return column
  58. func (table *Table) PKColumns() []*Column {
  59. columns := make([]*Column, len(table.PrimaryKeys))
  60. for i, name := range table.PrimaryKeys {
  61. columns[i] = table.GetColumn(name)
  62. }
  63. return columns
  64. }
  65. func (table *Table) ColumnType(name string) reflect.Type {
  66. t, _ := table.Type.FieldByName(name)
  67. return t.Type
  68. }
  69. func (table *Table) AutoIncrColumn() *Column {
  70. return table.GetColumn(table.AutoIncrement)
  71. }
  72. func (table *Table) VersionColumn() *Column {
  73. return table.GetColumn(table.Version)
  74. }
  75. func (table *Table) UpdatedColumn() *Column {
  76. return table.GetColumn(table.Updated)
  77. }
  78. func (table *Table) DeletedColumn() *Column {
  79. return table.GetColumn(table.Deleted)
  80. }
  81. // add a column to table
  82. func (table *Table) AddColumn(col *Column) {
  83. table.columnsSeq = append(table.columnsSeq, col.Name)
  84. table.columns = append(table.columns, col)
  85. colName := strings.ToLower(col.Name)
  86. if c, ok := table.columnsMap[colName]; ok {
  87. table.columnsMap[colName] = append(c, col)
  88. } else {
  89. table.columnsMap[colName] = []*Column{col}
  90. }
  91. if col.IsPrimaryKey {
  92. table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
  93. }
  94. if col.IsAutoIncrement {
  95. table.AutoIncrement = col.Name
  96. }
  97. if col.IsCreated {
  98. table.Created[col.Name] = true
  99. }
  100. if col.IsUpdated {
  101. table.Updated = col.Name
  102. }
  103. if col.IsDeleted {
  104. table.Deleted = col.Name
  105. }
  106. if col.IsVersion {
  107. table.Version = col.Name
  108. }
  109. }
  110. // add an index or an unique to table
  111. func (table *Table) AddIndex(index *Index) {
  112. table.Indexes[index.Name] = index
  113. }