add drop index sql and modify column sql interface
This commit is contained in:
parent
267e37572e
commit
9184dbce1e
23
dialect.go
23
dialect.go
|
@ -58,6 +58,9 @@ type Dialect interface {
|
||||||
CreateTableSql(table *Table, tableName, storeEngine, charset string) string
|
CreateTableSql(table *Table, tableName, storeEngine, charset string) string
|
||||||
DropTableSql(tableName string) string
|
DropTableSql(tableName string) string
|
||||||
CreateIndexSql(tableName string, index *Index) 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)
|
GetColumns(tableName string) ([]string, map[string]*Column, error)
|
||||||
GetTables() ([]*Table, error)
|
GetTables() ([]*Table, error)
|
||||||
|
@ -175,6 +178,26 @@ func (db *Base) CreateIndexSql(tableName string, index *Index) string {
|
||||||
quote(strings.Join(index.Cols, quote(","))))
|
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 {
|
func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset string) string {
|
||||||
var sql string
|
var sql string
|
||||||
sql = "CREATE TABLE IF NOT EXISTS "
|
sql = "CREATE TABLE IF NOT EXISTS "
|
||||||
|
|
19
index.go
19
index.go
|
@ -1,5 +1,9 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
IndexType = iota + 1
|
IndexType = iota + 1
|
||||||
UniqueType
|
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
|
// 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{name, indexType, make([]string, 0)}
|
||||||
|
|
Loading…
Reference in New Issue