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

154 lines
3.2 KiB

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