This commit is contained in:
Lunny Xiao 2013-09-01 11:01:10 +08:00
parent d90967009a
commit 6ecfc78d8d
4 changed files with 24 additions and 7 deletions

View File

@ -207,9 +207,10 @@ func (engine *Engine) AutoMap(bean interface{}) *Table {
} }
func (engine *Engine) MapType(t reflect.Type) *Table { func (engine *Engine) MapType(t reflect.Type) *Table {
table := &Table{Name: engine.Mapper.Obj2Table(t.Name()), Type: t, table := NewTable()
Indexes: map[string][]string{}, Uniques: map[string][]string{}} table.Name = engine.Mapper.Obj2Table(t.Name())
table.Columns = make(map[string]*Column) table.Type = t
var idFieldColName string var idFieldColName string
for i := 0; i < t.NumField(); i++ { 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 { for name, col := range parentTable.Columns {
col.FieldName = fmt.Sprintf("%v.%v", fieldType.Name(), col.FieldName) col.FieldName = fmt.Sprintf("%v.%v", fieldType.Name(), col.FieldName)
table.Columns[name] = col table.Columns[name] = col
table.ColumnsSeq = append(table.ColumnsSeq, name)
} }
table.PrimaryKey = parentTable.PrimaryKey table.PrimaryKey = parentTable.PrimaryKey
@ -344,7 +346,8 @@ func (engine *Engine) MapType(t reflect.Type) *Table {
if col.IsPrimaryKey { if col.IsPrimaryKey {
table.PrimaryKey = col.Name table.PrimaryKey = col.Name
} }
table.Columns[col.Name] = col table.AddColumn(col)
if col.FieldName == "Id" || strings.HasSuffix(col.FieldName, ".Id") { if col.FieldName == "Id" || strings.HasSuffix(col.FieldName, ".Id") {
idFieldColName = col.Name idFieldColName = col.Name
} }

View File

@ -34,8 +34,8 @@ func test(engine *xorm.Engine) {
return return
} }
engine.Pool.SetMaxConns(10) engine.Pool.SetMaxConns(50)
size := 100 size := 1000
queue := make(chan int, size) queue := make(chan int, size)
for i := 0; i < size; i++ { for i := 0; i < size; i++ {

View File

@ -212,7 +212,8 @@ func (statement *Statement) genColumnStr() string {
func (statement *Statement) genCreateSQL() string { func (statement *Statement) genCreateSQL() string {
sql := "CREATE TABLE IF NOT EXISTS " + statement.Engine.Quote(statement.TableName()) + " (" 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 += col.String(statement.Engine)
sql = strings.TrimSpace(sql) sql = strings.TrimSpace(sql)
sql += ", " sql += ", "

View File

@ -210,6 +210,7 @@ func (col *Column) ValueOf(bean interface{}) reflect.Value {
type Table struct { type Table struct {
Name string Name string
Type reflect.Type Type reflect.Type
ColumnsSeq []string
Columns map[string]*Column Columns map[string]*Column
Indexes map[string][]string Indexes map[string][]string
Uniques map[string][]string Uniques map[string][]string
@ -220,6 +221,18 @@ func (table *Table) PKColumn() *Column {
return table.Columns[table.PrimaryKey] 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 { type Conversion interface {
FromDB([]byte) error FromDB([]byte) error
ToDB() ([]byte, error) ToDB() ([]byte, error)