From 9184dbce1ecb192f88bb380199d42909eeb8d767 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 11 Jun 2014 14:02:22 +0800 Subject: [PATCH] add drop index sql and modify column sql interface --- dialect.go | 23 +++++++++++++++++++++++ index.go | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/dialect.go b/dialect.go index 59f7a886..64819ec3 100644 --- a/dialect.go +++ b/dialect.go @@ -58,6 +58,9 @@ type Dialect interface { CreateTableSql(table *Table, tableName, storeEngine, charset string) string DropTableSql(tableName string) string CreateIndexSql(tableName string, index *Index) string + DropIndexSql(tableName string, index *Index) string + + ModifyColumnSql(tableName string, col *Column) string GetColumns(tableName string) ([]string, map[string]*Column, error) GetTables() ([]*Table, error) @@ -175,6 +178,26 @@ func (db *Base) CreateIndexSql(tableName string, index *Index) string { quote(strings.Join(index.Cols, quote(",")))) } +func (db *Base) DropIndexSql(tableName string, index *Index) string { + quote := db.Quote + //var unique string + var idxName string = index.Name + if !strings.HasPrefix(idxName, "UQE_") && + !strings.HasPrefix(idxName, "IDX_") { + if index.Type == UniqueType { + 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", + quote(idxName), quote(tableName)) +} + +func (db *Base) ModifyColumnSql(tableName string, col *Column) string { + return fmt.Sprintf("alter table %s MODIFY COLUMN %s", tableName, col.StringNoPk(db.dialect)) +} + func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset string) string { var sql string sql = "CREATE TABLE IF NOT EXISTS " diff --git a/index.go b/index.go index eb088850..e8f447d7 100644 --- a/index.go +++ b/index.go @@ -1,5 +1,9 @@ package core +import ( + "sort" +) + const ( IndexType = iota + 1 UniqueType @@ -19,6 +23,21 @@ func (index *Index) AddColumn(cols ...string) { } } +func (index *Index) Equal(dst *Index) bool { + if len(index.Cols) != len(dst.Cols) { + return false + } + sort.StringSlice(index.Cols).Sort() + sort.StringSlice(dst.Cols).Sort() + + for i := 0; i < len(index.Cols); i++ { + if index.Cols[i] != dst.Cols[i] { + return false + } + } + return true +} + // new an index func NewIndex(name string, indexType int) *Index { return &Index{name, indexType, make([]string, 0)}