From be22a978d82bd96970f73266504fa19d4530d9db Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 28 Oct 2013 11:16:22 +0800 Subject: [PATCH] improved sync indexes, now check columns;xorm tool bug fiexed --- engine.go | 5 +++-- examples/sync.go | 18 +++++++++++++----- mysql.go | 2 +- postgres.go | 5 ++++- session.go | 37 +++++++++++++++++++++++++++++++++++++ xorm/go.go | 2 +- 6 files changed, 59 insertions(+), 10 deletions(-) diff --git a/engine.go b/engine.go index d1151d9d..45a61e45 100644 --- a/engine.go +++ b/engine.go @@ -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 } diff --git a/examples/sync.go b/examples/sync.go index 084d32f6..f2a460a3 100644 --- a/examples/sync.go +++ b/examples/sync.go @@ -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", diff --git a/mysql.go b/mysql.go index 55395904..09d70d86 100644 --- a/mysql.go +++ b/mysql.go @@ -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 diff --git a/postgres.go b/postgres.go index d98d2377..c02cdb0c 100644 --- a/postgres.go +++ b/postgres.go @@ -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)} diff --git a/session.go b/session.go index 0da193da..03183fe9 100644 --- a/session.go +++ b/session.go @@ -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 { diff --git a/xorm/go.go b/xorm/go.go index a5f009e4..03ad18f7 100644 --- a/xorm/go.go +++ b/xorm/go.go @@ -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)