index.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sqlchemy
  15. import (
  16. "fmt"
  17. "sort"
  18. "strings"
  19. )
  20. type STableIndex struct {
  21. name string
  22. columns []string
  23. isUnique bool
  24. ts ITableSpec
  25. }
  26. func NewTableIndex(ts ITableSpec, name string, cols []string, unique bool) STableIndex {
  27. sort.Sort(TColumnNames(cols))
  28. return STableIndex{
  29. name: name,
  30. columns: cols,
  31. isUnique: unique,
  32. ts: ts,
  33. }
  34. }
  35. type TColumnNames []string
  36. func (cols TColumnNames) Len() int {
  37. return len(cols)
  38. }
  39. func (cols TColumnNames) Swap(i, j int) {
  40. cols[i], cols[j] = cols[j], cols[i]
  41. }
  42. func (cols TColumnNames) Less(i, j int) bool {
  43. if strings.Compare(cols[i], cols[j]) < 0 {
  44. return true
  45. } else {
  46. return false
  47. }
  48. }
  49. const IndexLimit int = 64
  50. func (index *STableIndex) Name() string {
  51. if len(index.name) > 0 {
  52. return index.name
  53. }
  54. name := fmt.Sprintf("ix_%s_%s", index.ts.Name(), strings.Join(index.columns, "_"))
  55. if len(name) > IndexLimit {
  56. name = name[:IndexLimit]
  57. }
  58. return name
  59. }
  60. func (index STableIndex) clone(ts ITableSpec) STableIndex {
  61. return NewTableIndex(ts, "", index.columns, index.isUnique)
  62. }
  63. func (index *STableIndex) IsIdentical(cols ...string) bool {
  64. if len(index.columns) != len(cols) {
  65. return false
  66. }
  67. sort.Sort(TColumnNames(cols))
  68. for i := 0; i < len(index.columns); i++ {
  69. if index.columns[i] != cols[i] {
  70. return false
  71. }
  72. }
  73. return true
  74. }
  75. func (index *STableIndex) QuotedColumns(quoteStr string) []string {
  76. ret := make([]string, len(index.columns))
  77. for i := 0; i < len(ret); i++ {
  78. ret[i] = fmt.Sprintf("%s%s%s", quoteStr, index.columns[i], quoteStr)
  79. }
  80. return ret
  81. }
  82. // AddIndex adds a SQL index over multiple columns for a Table
  83. // param unique: indicates a unique index cols: name of columns
  84. func (ts *STableSpec) addIndexWithName(name string, unique bool, cols ...string) bool {
  85. for i := 0; i < len(ts._indexes); i++ {
  86. if ts._indexes[i].IsIdentical(cols...) {
  87. return false
  88. }
  89. }
  90. idx := NewTableIndex(ts, name, cols, unique)
  91. ts._indexes = append(ts._indexes, idx)
  92. return true
  93. }
  94. func (ts *STableSpec) AddIndex(unique bool, cols ...string) bool {
  95. return ts.addIndexWithName("", unique, cols...)
  96. }