add drop index sql and modify column sql interface

This commit is contained in:
Lunny Xiao 2014-06-11 14:02:22 +08:00
parent 267e37572e
commit 9184dbce1e
2 changed files with 42 additions and 0 deletions

View File

@ -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 "

View File

@ -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)}