You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

table.go 3.3 kB

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