From 4691a4f3ba0d2da7f85964e1ac77d30409646c95 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 6 Sep 2013 14:32:20 +0800 Subject: [PATCH] fixed bug #8 & #9 --- base_test.go | 25 +++++++++++++++++++++++++ mapper.go | 11 +++++++++++ statement.go | 4 ++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/base_test.go b/base_test.go index 1eb20c83..077ec9e0 100644 --- a/base_test.go +++ b/base_test.go @@ -780,6 +780,30 @@ func testCreatedAndUpdated(engine *Engine, t *testing.T) { } } +type IndexOrUnique struct { + Id int64 + Index int `xorm:"index"` + Unique int `xorm:"unique"` + Group1 int `xorm:"index(ttt)"` + Group2 int `xorm:"index(ttt)"` + UniGroup1 int `xorm:"unique(lll)"` + UniGroup2 int `xorm:"unique(lll)"` +} + +func testIndexAndUnique(engine *Engine, t *testing.T) { + err := engine.DropTables(&IndexOrUnique{}) + if err != nil { + t.Error(err) + panic(err) + } + + err = engine.CreateTables(&IndexOrUnique{}) + if err != nil { + t.Error(err) + panic(err) + } +} + func testAll(engine *Engine, t *testing.T) { directCreateTable(engine, t) mapper(engine, t) @@ -814,4 +838,5 @@ func testAll(engine *Engine, t *testing.T) { testColTypes(engine, t) testCustomType(engine, t) testCreatedAndUpdated(engine, t) + testIndexAndUnique(engine, t) } diff --git a/mapper.go b/mapper.go index a00682b5..3c6fda65 100644 --- a/mapper.go +++ b/mapper.go @@ -10,6 +10,17 @@ type IMapper interface { Table2Obj(string) string } +type SameMapper struct { +} + +func (m SameMapper) Obj2Table(o string) string { + return o +} + +func (m SameMapper) Table2Obj(t string) string { + return t +} + type SnakeMapper struct { } diff --git a/statement.go b/statement.go index 2c8770e9..2ef417e3 100644 --- a/statement.go +++ b/statement.go @@ -271,7 +271,7 @@ func (statement *Statement) genIndexSQL() []string { var sqls []string = make([]string, 0) for indexName, cols := range statement.RefTable.Indexes { sql := fmt.Sprintf("CREATE INDEX IDX_%v_%v ON %v (%v);", statement.TableName(), indexName, - statement.TableName(), strings.Join(cols, ",")) + statement.Engine.Quote(statement.TableName()), statement.Engine.Quote(strings.Join(cols, statement.Engine.Quote(",")))) sqls = append(sqls, sql) } return sqls @@ -281,7 +281,7 @@ func (statement *Statement) genUniqueSQL() []string { var sqls []string = make([]string, 0) for indexName, cols := range statement.RefTable.Uniques { sql := fmt.Sprintf("CREATE UNIQUE INDEX UQE_%v_%v ON %v (%v);", statement.TableName(), indexName, - statement.TableName(), strings.Join(cols, ",")) + statement.Engine.Quote(statement.TableName()), statement.Engine.Quote(strings.Join(cols, statement.Engine.Quote(",")))) sqls = append(sqls, sql) } return sqls