github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/core/index.go (about)

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