improved sync indexes, now check columns;xorm tool bug fiexed

This commit is contained in:
Lunny Xiao 2013-10-28 11:16:22 +08:00
parent bab16dc763
commit be22a978d8
6 changed files with 59 additions and 10 deletions

View File

@ -650,7 +650,8 @@ func (engine *Engine) Sync(beans ...interface{}) error {
session.Statement.RefTable = table
defer session.Close()
if index.Type == UniqueType {
isExist, err := session.isIndexExist(table.Name, name, true)
//isExist, err := session.isIndexExist(table.Name, name, true)
isExist, err := session.isIndexExist2(table.Name, index.Cols, true)
if err != nil {
return err
}
@ -664,7 +665,7 @@ func (engine *Engine) Sync(beans ...interface{}) error {
}
}
} else if index.Type == IndexType {
isExist, err := session.isIndexExist(table.Name, name, false)
isExist, err := session.isIndexExist2(table.Name, index.Cols, false)
if err != nil {
return err
}

View File

@ -8,7 +8,7 @@ import (
"xorm"
)
type SyncUser struct {
type SyncUser2 struct {
Id int64
Name string `xorm:"unique"`
Age int `xorm:"index"`
@ -19,7 +19,7 @@ type SyncUser struct {
Date int
}
type SyncLoginInfo struct {
type SyncLoginInfo2 struct {
Id int64
IP string `xorm:"index"`
UserId int64
@ -32,7 +32,7 @@ type SyncLoginInfo struct {
}
func sync(engine *xorm.Engine) error {
return engine.Sync(&SyncLoginInfo{}, &SyncUser{})
return engine.Sync(&SyncLoginInfo2{}, &SyncUser2{})
}
func sqliteEngine() (*xorm.Engine, error) {
@ -53,7 +53,10 @@ func postgresEngine() (*xorm.Engine, error) {
type engineFunc func() (*xorm.Engine, error)
func main() {
engines := []engineFunc{sqliteEngine, mysqlEngine, postgresEngine}
//engines := []engineFunc{sqliteEngine, mysqlEngine, postgresEngine}
//engines := []engineFunc{sqliteEngine}
//engines := []engineFunc{mysqlEngine}
engines := []engineFunc{postgresEngine}
for _, enginefunc := range engines {
Orm, err := enginefunc()
fmt.Println("--------", Orm.DriverName, "----------")
@ -67,7 +70,12 @@ func main() {
fmt.Println(err)
}
user := &SyncUser{
_, err = Orm.Where("id > 0").Delete(&SyncUser2{})
if err != nil {
fmt.Println(err)
}
user := &SyncUser2{
Name: "testsdf",
Age: 15,
Title: "newsfds",

View File

@ -276,7 +276,7 @@ func (db *mysql) GetIndexes(tableName string) (map[string]*Index, error) {
for name, content := range record {
switch name {
case "NON_UNIQUE":
if "YES" == string(content) {
if "YES" == string(content) || string(content) == "1" {
indexType = IndexType
} else {
indexType = UniqueType

View File

@ -282,7 +282,10 @@ func (db *postgres) GetIndexes(tableName string) (map[string]*Index, error) {
continue
}
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
indexName = indexName[5+len(tableName) : len(indexName)]
newIdxName := indexName[5+len(tableName) : len(indexName)]
if newIdxName != "" {
indexName = newIdxName
}
}
index := &Index{Name: indexName, Type: indexType, Cols: make([]string, 0)}

View File

@ -982,6 +982,43 @@ func (session *Session) isIndexExist(tableName, idxName string, unique bool) (bo
return len(results) > 0, err
}
func sliceEq(left, right []string) bool {
for _, l := range left {
var find bool
for _, r := range right {
if l == r {
find = true
break
}
}
if !find {
return false
}
}
return true
}
// find if index is exist according cols
func (session *Session) isIndexExist2(tableName string, cols []string, unique bool) (bool, error) {
indexes, err := session.Engine.dialect.GetIndexes(tableName)
if err != nil {
return false, err
}
for _, index := range indexes {
//fmt.Println(i, "new:", cols, "-old:", index.Cols)
if sliceEq(index.Cols, cols) {
if unique {
return index.Type == UniqueType, nil
} else {
return index.Type == IndexType, nil
}
}
}
return false, nil
}
func (session *Session) addColumn(colName string) error {
err := session.newDb()
if err != nil {

View File

@ -230,7 +230,7 @@ func tag(table *xorm.Table, col *xorm.Column) string {
} else if index.Type == xorm.IndexType {
uistr = "index"
}
if index.Name != col.Name {
if len(index.Cols) > 1 {
uistr += "(" + index.Name + ")"
}
res = append(res, uistr)