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.

index.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. tableName = strings.Replace(tableName, `"`, "", -1)
  22. tableName = strings.Replace(tableName, `.`, "_", -1)
  23. if index.Type == UniqueType {
  24. return fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
  25. }
  26. return fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
  27. }
  28. return index.Name
  29. }
  30. // add columns which will be composite index
  31. func (index *Index) AddColumn(cols ...string) {
  32. for _, col := range cols {
  33. index.Cols = append(index.Cols, col)
  34. }
  35. }
  36. func (index *Index) Equal(dst *Index) bool {
  37. if index.Type != dst.Type {
  38. return false
  39. }
  40. if len(index.Cols) != len(dst.Cols) {
  41. return false
  42. }
  43. sort.StringSlice(index.Cols).Sort()
  44. sort.StringSlice(dst.Cols).Sort()
  45. for i := 0; i < len(index.Cols); i++ {
  46. if index.Cols[i] != dst.Cols[i] {
  47. return false
  48. }
  49. }
  50. return true
  51. }
  52. // new an index
  53. func NewIndex(name string, indexType int) *Index {
  54. return &Index{true, name, indexType, make([]string, 0)}
  55. }