index.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package core
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. )
  7. const (
  8. IndexType = iota + 1
  9. UniqueType
  10. )
  11. // database index
  12. type Index struct {
  13. IsRegular bool
  14. Name string
  15. Type int
  16. Cols []string
  17. }
  18. func (index *Index) XName(tableName string) string {
  19. if !strings.HasPrefix(index.Name, "UQE_") &&
  20. !strings.HasPrefix(index.Name, "IDX_") {
  21. if index.Type == UniqueType {
  22. return fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
  23. }
  24. return fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
  25. }
  26. return index.Name
  27. }
  28. // add columns which will be composite index
  29. func (index *Index) AddColumn(cols ...string) {
  30. for _, col := range cols {
  31. index.Cols = append(index.Cols, col)
  32. }
  33. }
  34. func (index *Index) Equal(dst *Index) bool {
  35. if index.Type != dst.Type {
  36. return false
  37. }
  38. if len(index.Cols) != len(dst.Cols) {
  39. return false
  40. }
  41. sort.StringSlice(index.Cols).Sort()
  42. sort.StringSlice(dst.Cols).Sort()
  43. for i := 0; i < len(index.Cols); i++ {
  44. if index.Cols[i] != dst.Cols[i] {
  45. return false
  46. }
  47. }
  48. return true
  49. }
  50. // new an index
  51. func NewIndex(name string, indexType int) *Index {
  52. return &Index{true, name, indexType, make([]string, 0)}
  53. }