From 6ecfc78d8d4db5c1ad56b8520a8a864241be9ec7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 1 Sep 2013 11:01:10 +0800 Subject: [PATCH] fix bug #7 --- engine.go | 11 +++++++---- examples/maxconnect.go | 4 ++-- statement.go | 3 ++- table.go | 13 +++++++++++++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/engine.go b/engine.go index a03bfb29..59f3ecb4 100644 --- a/engine.go +++ b/engine.go @@ -207,9 +207,10 @@ func (engine *Engine) AutoMap(bean interface{}) *Table { } func (engine *Engine) MapType(t reflect.Type) *Table { - table := &Table{Name: engine.Mapper.Obj2Table(t.Name()), Type: t, - Indexes: map[string][]string{}, Uniques: map[string][]string{}} - table.Columns = make(map[string]*Column) + table := NewTable() + table.Name = engine.Mapper.Obj2Table(t.Name()) + table.Type = t + var idFieldColName string for i := 0; i < t.NumField(); i++ { @@ -233,6 +234,7 @@ func (engine *Engine) MapType(t reflect.Type) *Table { for name, col := range parentTable.Columns { col.FieldName = fmt.Sprintf("%v.%v", fieldType.Name(), col.FieldName) table.Columns[name] = col + table.ColumnsSeq = append(table.ColumnsSeq, name) } table.PrimaryKey = parentTable.PrimaryKey @@ -344,7 +346,8 @@ func (engine *Engine) MapType(t reflect.Type) *Table { if col.IsPrimaryKey { table.PrimaryKey = col.Name } - table.Columns[col.Name] = col + table.AddColumn(col) + if col.FieldName == "Id" || strings.HasSuffix(col.FieldName, ".Id") { idFieldColName = col.Name } diff --git a/examples/maxconnect.go b/examples/maxconnect.go index 41c66d20..636d531e 100644 --- a/examples/maxconnect.go +++ b/examples/maxconnect.go @@ -34,8 +34,8 @@ func test(engine *xorm.Engine) { return } - engine.Pool.SetMaxConns(10) - size := 100 + engine.Pool.SetMaxConns(50) + size := 1000 queue := make(chan int, size) for i := 0; i < size; i++ { diff --git a/statement.go b/statement.go index 1156e353..39a786f8 100644 --- a/statement.go +++ b/statement.go @@ -212,7 +212,8 @@ func (statement *Statement) genColumnStr() string { func (statement *Statement) genCreateSQL() string { sql := "CREATE TABLE IF NOT EXISTS " + statement.Engine.Quote(statement.TableName()) + " (" - for _, col := range statement.RefTable.Columns { + for _, colName := range statement.RefTable.ColumnsSeq { + col := statement.RefTable.Columns[colName] sql += col.String(statement.Engine) sql = strings.TrimSpace(sql) sql += ", " diff --git a/table.go b/table.go index c31a9d5d..3e3f1a4a 100644 --- a/table.go +++ b/table.go @@ -210,6 +210,7 @@ func (col *Column) ValueOf(bean interface{}) reflect.Value { type Table struct { Name string Type reflect.Type + ColumnsSeq []string Columns map[string]*Column Indexes map[string][]string Uniques map[string][]string @@ -220,6 +221,18 @@ func (table *Table) PKColumn() *Column { return table.Columns[table.PrimaryKey] } +func (table *Table) AddColumn(col *Column) { + table.ColumnsSeq = append(table.ColumnsSeq, col.Name) + table.Columns[col.Name] = col +} + +func NewTable() *Table { + table := &Table{Indexes: map[string][]string{}, Uniques: map[string][]string{}} + table.Columns = make(map[string]*Column) + table.ColumnsSeq = make([]string, 0) + return table +} + type Conversion interface { FromDB([]byte) error ToDB() ([]byte, error)