table.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2019 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package core
  5. import (
  6. "reflect"
  7. "strings"
  8. )
  9. // Table represents a database table
  10. type Table struct {
  11. Name string
  12. Type reflect.Type
  13. columnsSeq []string
  14. columnsMap map[string][]*Column
  15. columns []*Column
  16. Indexes map[string]*Index
  17. PrimaryKeys []string
  18. AutoIncrement string
  19. Created map[string]bool
  20. Updated string
  21. Deleted string
  22. Version string
  23. Cacher Cacher
  24. StoreEngine string
  25. Charset string
  26. Comment string
  27. }
  28. func (table *Table) Columns() []*Column {
  29. return table.columns
  30. }
  31. func (table *Table) ColumnsSeq() []string {
  32. return table.columnsSeq
  33. }
  34. func NewEmptyTable() *Table {
  35. return NewTable("", nil)
  36. }
  37. // NewTable creates a new Table object
  38. func NewTable(name string, t reflect.Type) *Table {
  39. return &Table{Name: name, Type: t,
  40. columnsSeq: make([]string, 0),
  41. columns: make([]*Column, 0),
  42. columnsMap: make(map[string][]*Column),
  43. Indexes: make(map[string]*Index),
  44. Created: make(map[string]bool),
  45. PrimaryKeys: make([]string, 0),
  46. }
  47. }
  48. func (table *Table) columnsByName(name string) []*Column {
  49. n := len(name)
  50. for k := range table.columnsMap {
  51. if len(k) != n {
  52. continue
  53. }
  54. if strings.EqualFold(k, name) {
  55. return table.columnsMap[k]
  56. }
  57. }
  58. return nil
  59. }
  60. func (table *Table) GetColumn(name string) *Column {
  61. cols := table.columnsByName(name)
  62. if cols != nil {
  63. return cols[0]
  64. }
  65. return nil
  66. }
  67. func (table *Table) GetColumnIdx(name string, idx int) *Column {
  68. cols := table.columnsByName(name)
  69. if cols != nil && idx < len(cols) {
  70. return cols[idx]
  71. }
  72. return nil
  73. }
  74. // PKColumns reprents all primary key columns
  75. func (table *Table) PKColumns() []*Column {
  76. columns := make([]*Column, len(table.PrimaryKeys))
  77. for i, name := range table.PrimaryKeys {
  78. columns[i] = table.GetColumn(name)
  79. }
  80. return columns
  81. }
  82. func (table *Table) ColumnType(name string) reflect.Type {
  83. t, _ := table.Type.FieldByName(name)
  84. return t.Type
  85. }
  86. func (table *Table) AutoIncrColumn() *Column {
  87. return table.GetColumn(table.AutoIncrement)
  88. }
  89. func (table *Table) VersionColumn() *Column {
  90. return table.GetColumn(table.Version)
  91. }
  92. func (table *Table) UpdatedColumn() *Column {
  93. return table.GetColumn(table.Updated)
  94. }
  95. func (table *Table) DeletedColumn() *Column {
  96. return table.GetColumn(table.Deleted)
  97. }
  98. // AddColumn adds a column to table
  99. func (table *Table) AddColumn(col *Column) {
  100. table.columnsSeq = append(table.columnsSeq, col.Name)
  101. table.columns = append(table.columns, col)
  102. colName := strings.ToLower(col.Name)
  103. if c, ok := table.columnsMap[colName]; ok {
  104. table.columnsMap[colName] = append(c, col)
  105. } else {
  106. table.columnsMap[colName] = []*Column{col}
  107. }
  108. if col.IsPrimaryKey {
  109. table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
  110. }
  111. if col.IsAutoIncrement {
  112. table.AutoIncrement = col.Name
  113. }
  114. if col.IsCreated {
  115. table.Created[col.Name] = true
  116. }
  117. if col.IsUpdated {
  118. table.Updated = col.Name
  119. }
  120. if col.IsDeleted {
  121. table.Deleted = col.Name
  122. }
  123. if col.IsVersion {
  124. table.Version = col.Name
  125. }
  126. }
  127. // AddIndex adds an index or an unique to table
  128. func (table *Table) AddIndex(index *Index) {
  129. table.Indexes[index.Name] = index
  130. }