add isregular for index

This commit is contained in:
Lunny Xiao 2015-02-17 15:00:51 +08:00
parent 16cb27928f
commit 833d5b7beb
2 changed files with 28 additions and 21 deletions

View File

@ -52,10 +52,7 @@ type Dialect interface {
IndexCheckSql(tableName, idxName string) (string, []interface{}) IndexCheckSql(tableName, idxName string) (string, []interface{})
TableCheckSql(tableName string) (string, []interface{}) TableCheckSql(tableName string) (string, []interface{})
//ColumnCheckSql(tableName, colName string) (string, []interface{})
//IsTableExist(tableName string) (bool, error)
//IsIndexExist(tableName string, idx *Index) (bool, error)
IsColumnExist(tableName string, col *Column) (bool, error) IsColumnExist(tableName string, col *Column) (bool, error)
CreateTableSql(table *Table, tableName, storeEngine, charset string) string CreateTableSql(table *Table, tableName, storeEngine, charset string) string
@ -176,10 +173,8 @@ func (db *Base) CreateIndexSql(tableName string, index *Index) string {
var idxName string var idxName string
if index.Type == UniqueType { if index.Type == UniqueType {
unique = " UNIQUE" unique = " UNIQUE"
idxName = fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
} else {
idxName = fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
} }
idxName = index.XName(tableName)
return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v);", unique, return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v);", unique,
quote(idxName), quote(tableName), quote(idxName), quote(tableName),
quote(strings.Join(index.Cols, quote(",")))) quote(strings.Join(index.Cols, quote(","))))
@ -187,18 +182,13 @@ func (db *Base) CreateIndexSql(tableName string, index *Index) string {
func (db *Base) DropIndexSql(tableName string, index *Index) string { func (db *Base) DropIndexSql(tableName string, index *Index) string {
quote := db.dialect.Quote quote := db.dialect.Quote
//var unique string var name string
var idxName string = index.Name if index.IsRegular {
if !strings.HasPrefix(idxName, "UQE_") && name = index.XName(tableName)
!strings.HasPrefix(idxName, "IDX_") { } else {
if index.Type == UniqueType { name = index.Name
idxName = fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
} else {
idxName = fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
}
} }
return fmt.Sprintf("DROP INDEX %v ON %s", return fmt.Sprintf("DROP INDEX %v ON %s", quote(name), quote(tableName))
quote(idxName), quote(tableName))
} }
func (db *Base) ModifyColumnSql(tableName string, col *Column) string { func (db *Base) ModifyColumnSql(tableName string, col *Column) string {

View File

@ -1,7 +1,9 @@
package core package core
import ( import (
"fmt"
"sort" "sort"
"strings"
) )
const ( const (
@ -11,9 +13,21 @@ const (
// database index // database index
type Index struct { type Index struct {
Name string IsRegular bool
Type int Name string
Cols []string Type int
Cols []string
}
func (index *Index) XName(tableName string) string {
if !strings.HasPrefix(index.Name, "UQE_") &&
!strings.HasPrefix(index.Name, "IDX_") {
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
} }
// add columns which will be composite index // add columns which will be composite index
@ -24,6 +38,9 @@ func (index *Index) AddColumn(cols ...string) {
} }
func (index *Index) Equal(dst *Index) bool { func (index *Index) Equal(dst *Index) bool {
if index.Type != dst.Type {
return false
}
if len(index.Cols) != len(dst.Cols) { if len(index.Cols) != len(dst.Cols) {
return false return false
} }
@ -40,5 +57,5 @@ func (index *Index) Equal(dst *Index) bool {
// new an index // new an index
func NewIndex(name string, indexType int) *Index { func NewIndex(name string, indexType int) *Index {
return &Index{name, indexType, make([]string, 0)} return &Index{true, name, indexType, make([]string, 0)}
} }