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.
73 lines
1.5 KiB
73 lines
1.5 KiB
5 years ago
|
// Copyright 2019 The Xorm Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
5 years ago
|
package schemas
|
||
8 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
5 years ago
|
// enumerate all index types
|
||
8 years ago
|
const (
|
||
|
IndexType = iota + 1
|
||
|
UniqueType
|
||
|
)
|
||
|
|
||
5 years ago
|
// Index represents a database index
|
||
8 years ago
|
type Index struct {
|
||
|
IsRegular bool
|
||
|
Name string
|
||
|
Type int
|
||
|
Cols []string
|
||
|
}
|
||
|
|
||
5 years ago
|
// NewIndex new an index object
|
||
|
func NewIndex(name string, indexType int) *Index {
|
||
|
return &Index{true, name, indexType, make([]string, 0)}
|
||
|
}
|
||
|
|
||
8 years ago
|
func (index *Index) XName(tableName string) string {
|
||
|
if !strings.HasPrefix(index.Name, "UQE_") &&
|
||
|
!strings.HasPrefix(index.Name, "IDX_") {
|
||
5 years ago
|
tableParts := strings.Split(strings.Replace(tableName, `"`, "", -1), ".")
|
||
|
tableName = tableParts[len(tableParts)-1]
|
||
8 years ago
|
if index.Type == UniqueType {
|
||
|
return fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
|
||
|
}
|
||
|
return fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
|
||
|
}
|
||
|
return index.Name
|
||
|
}
|
||
|
|
||
5 years ago
|
// AddColumn add columns which will be composite index
|
||
8 years ago
|
func (index *Index) AddColumn(cols ...string) {
|
||
|
for _, col := range cols {
|
||
|
index.Cols = append(index.Cols, col)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (index *Index) Equal(dst *Index) bool {
|
||
|
if index.Type != dst.Type {
|
||
|
return false
|
||
|
}
|
||
|
if len(index.Cols) != len(dst.Cols) {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
for i := 0; i < len(index.Cols); i++ {
|
||
5 years ago
|
var found bool
|
||
|
for j := 0; j < len(dst.Cols); j++ {
|
||
|
if index.Cols[i] == dst.Cols[j] {
|
||
|
found = true
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if !found {
|
||
8 years ago
|
return false
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|