table.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. Comment string
  24. }
  25. func (table *Table) Columns() []*Column {
  26. return table.columns
  27. }
  28. func (table *Table) ColumnsSeq() []string {
  29. return table.columnsSeq
  30. }
  31. func NewEmptyTable() *Table {
  32. return NewTable("", nil)
  33. }
  34. func NewTable(name string, t reflect.Type) *Table {
  35. return &Table{Name: name, Type: t,
  36. columnsSeq: make([]string, 0),
  37. columns: make([]*Column, 0),
  38. columnsMap: make(map[string][]*Column),
  39. Indexes: make(map[string]*Index),
  40. Created: make(map[string]bool),
  41. PrimaryKeys: make([]string, 0),
  42. }
  43. }
  44. func (table *Table) columnsByName(name string) []*Column {
  45. n := len(name)
  46. for k := range table.columnsMap {
  47. if len(k) != n {
  48. continue
  49. }
  50. if strings.EqualFold(k, name) {
  51. return table.columnsMap[k]
  52. }
  53. }
  54. return nil
  55. }
  56. func (table *Table) GetColumn(name string) *Column {
  57. cols := table.columnsByName(name)
  58. if cols != nil {
  59. return cols[0]
  60. }
  61. return nil
  62. }
  63. func (table *Table) GetColumnIdx(name string, idx int) *Column {
  64. cols := table.columnsByName(name)
  65. if cols != nil && idx < len(cols) {
  66. return cols[idx]
  67. }
  68. return nil
  69. }
  70. // if has primary key, return column
  71. func (table *Table) PKColumns() []*Column {
  72. columns := make([]*Column, len(table.PrimaryKeys))
  73. for i, name := range table.PrimaryKeys {
  74. columns[i] = table.GetColumn(name)
  75. }
  76. return columns
  77. }
  78. func (table *Table) ColumnType(name string) reflect.Type {
  79. t, _ := table.Type.FieldByName(name)
  80. return t.Type
  81. }
  82. func (table *Table) AutoIncrColumn() *Column {
  83. return table.GetColumn(table.AutoIncrement)
  84. }
  85. func (table *Table) VersionColumn() *Column {
  86. return table.GetColumn(table.Version)
  87. }
  88. func (table *Table) UpdatedColumn() *Column {
  89. return table.GetColumn(table.Updated)
  90. }
  91. func (table *Table) DeletedColumn() *Column {
  92. return table.GetColumn(table.Deleted)
  93. }
  94. // add a column to table
  95. func (table *Table) AddColumn(col *Column) {
  96. table.columnsSeq = append(table.columnsSeq, col.Name)
  97. table.columns = append(table.columns, col)
  98. colName := strings.ToLower(col.Name)
  99. if c, ok := table.columnsMap[colName]; ok {
  100. table.columnsMap[colName] = append(c, col)
  101. } else {
  102. table.columnsMap[colName] = []*Column{col}
  103. }
  104. if col.IsPrimaryKey {
  105. table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
  106. }
  107. if col.IsAutoIncrement {
  108. table.AutoIncrement = col.Name
  109. }
  110. if col.IsCreated {
  111. table.Created[col.Name] = true
  112. }
  113. if col.IsUpdated {
  114. table.Updated = col.Name
  115. }
  116. if col.IsDeleted {
  117. table.Deleted = col.Name
  118. }
  119. if col.IsVersion {
  120. table.Version = col.Name
  121. }
  122. }
  123. // add an index or an unique to table
  124. func (table *Table) AddIndex(index *Index) {
  125. table.Indexes[index.Name] = index
  126. }