From 7d0f249cf53ce476234064a73025144a99dfb31b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 30 Jun 2016 16:35:04 +0800 Subject: [PATCH] resolved #416 --- engine.go | 12 +++++++----- mssql_dialect.go | 2 +- mysql_dialect.go | 2 +- oracle_dialect.go | 2 +- postgres_dialect.go | 2 +- sqlite3_dialect.go | 2 +- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/engine.go b/engine.go index 3e607561..3cd1ea9d 100644 --- a/engine.go +++ b/engine.go @@ -381,7 +381,7 @@ func (engine *Engine) DBMetas() ([]*core.Table, error) { for _, index := range indexes { for _, name := range index.Cols { if col := table.GetColumn(name); col != nil { - col.Indexes[index.Name] = true + col.Indexes[index.Name] = index.Type } else { return nil, fmt.Errorf("Unknown col "+name+" in indexes %v of table", index, table.ColumnsSeq()) } @@ -876,12 +876,12 @@ func (engine *Engine) TableInfo(bean interface{}) *core.Table { func addIndex(indexName string, table *core.Table, col *core.Column, indexType int) { if index, ok := table.Indexes[indexName]; ok { index.AddColumn(col.Name) - col.Indexes[index.Name] = true + col.Indexes[index.Name] = indexType } else { index := core.NewIndex(indexName, indexType) index.AddColumn(col.Name) table.AddIndex(index) - col.Indexes[index.Name] = true + col.Indexes[index.Name] = indexType } } @@ -930,7 +930,7 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table { if ormTagStr != "" { col = &core.Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false, - IsAutoIncrement: false, MapType: core.TWOSIDES, Indexes: make(map[string]bool)} + IsAutoIncrement: false, MapType: core.TWOSIDES, Indexes: make(map[string]int)} tags := splitTag(ormTagStr) if len(tags) > 0 { @@ -954,8 +954,10 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table { for _, col := range parentTable.Columns() { col.FieldName = fmt.Sprintf("%v.%v", t.Field(i).Name, col.FieldName) table.AddColumn(col) + for indexName, indexType := range col.Indexes { + addIndex(indexName, table, col, indexType) + } } - continue default: //TODO: warning diff --git a/mssql_dialect.go b/mssql_dialect.go index adca5b5a..966c7fc2 100644 --- a/mssql_dialect.go +++ b/mssql_dialect.go @@ -352,7 +352,7 @@ where a.object_id=object_id('` + tableName + `')` } col := new(core.Column) - col.Indexes = make(map[string]bool) + col.Indexes = make(map[string]int) col.Length = maxLen col.Name = strings.Trim(name, "` ") diff --git a/mysql_dialect.go b/mysql_dialect.go index a9997f6a..5c789a14 100644 --- a/mysql_dialect.go +++ b/mysql_dialect.go @@ -311,7 +311,7 @@ func (db *mysql) GetColumns(tableName string) ([]string, map[string]*core.Column colSeq := make([]string, 0) for rows.Next() { col := new(core.Column) - col.Indexes = make(map[string]bool) + col.Indexes = make(map[string]int) var columnName, isNullable, colType, colKey, extra string var colDefault *string diff --git a/oracle_dialect.go b/oracle_dialect.go index 6a767b7c..f074a4ec 100644 --- a/oracle_dialect.go +++ b/oracle_dialect.go @@ -692,7 +692,7 @@ func (db *oracle) GetColumns(tableName string) ([]string, map[string]*core.Colum colSeq := make([]string, 0) for rows.Next() { col := new(core.Column) - col.Indexes = make(map[string]bool) + col.Indexes = make(map[string]int) var colName, colDefault, nullable, dataType, dataPrecision, dataScale *string var dataLen int diff --git a/postgres_dialect.go b/postgres_dialect.go index 773de726..8316e29c 100644 --- a/postgres_dialect.go +++ b/postgres_dialect.go @@ -936,7 +936,7 @@ WHERE c.relkind = 'r'::char AND c.relname = $1 AND s.table_schema = $2 AND f.att for rows.Next() { col := new(core.Column) - col.Indexes = make(map[string]bool) + col.Indexes = make(map[string]int) var colName, isNullable, dataType string var maxLenStr, colDefault, numPrecision, numRadix *string diff --git a/sqlite3_dialect.go b/sqlite3_dialect.go index 1fe8aa1c..20c75a4a 100644 --- a/sqlite3_dialect.go +++ b/sqlite3_dialect.go @@ -311,7 +311,7 @@ func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Colu colStr = reg.ReplaceAllString(colStr, ",") fields := strings.Fields(strings.TrimSpace(colStr)) col := new(core.Column) - col.Indexes = make(map[string]bool) + col.Indexes = make(map[string]int) col.Nullable = true col.DefaultIsEmpty = true for idx, field := range fields {