bug fixed

This commit is contained in:
Lunny Xiao 2014-05-31 12:19:46 +08:00
parent bd1487ba55
commit 6384ada2bb
3 changed files with 13 additions and 3 deletions

View File

@ -134,8 +134,8 @@ func (engine *Engine) NoCascade() *Session {
// Set a table use a special cacher // Set a table use a special cacher
func (engine *Engine) MapCacher(bean interface{}, cacher core.Cacher) { func (engine *Engine) MapCacher(bean interface{}, cacher core.Cacher) {
v := rValue(bean) v := rValue(bean)
engine.autoMapType(v) tb := engine.autoMapType(v)
engine.Tables[v.Type()].Cacher = cacher tb.Cacher = cacher
} }
// NewDB provides an interface to operate database directly // NewDB provides an interface to operate database directly
@ -483,7 +483,7 @@ func (engine *Engine) Desc(colNames ...string) *Session {
return session.Desc(colNames...) return session.Desc(colNames...)
} }
// Method Asc will generate "ORDER BY column1 DESC, column2 Asc" // Method Asc will generate "ORDER BY column1,column2 Asc"
// This method can chainable use. // This method can chainable use.
// //
// engine.Desc("name").Asc("age").Find(&users) // engine.Desc("name").Asc("age").Find(&users)
@ -561,6 +561,7 @@ func (engine *Engine) newTable() *core.Table {
} }
func (engine *Engine) mapType(v reflect.Value) *core.Table { func (engine *Engine) mapType(v reflect.Value) *core.Table {
fmt.Println("has", v.NumField(), "fields")
t := v.Type() t := v.Type()
table := engine.newTable() table := engine.newTable()
method := v.MethodByName("TableName") method := v.MethodByName("TableName")
@ -587,10 +588,12 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table {
for i := 0; i < t.NumField(); i++ { for i := 0; i < t.NumField(); i++ {
tag := t.Field(i).Tag tag := t.Field(i).Tag
ormTagStr := tag.Get(engine.TagIdentifier) ormTagStr := tag.Get(engine.TagIdentifier)
var col *core.Column var col *core.Column
fieldValue := v.Field(i) fieldValue := v.Field(i)
fieldType := fieldValue.Type() fieldType := fieldValue.Type()
fmt.Println(table.Name, "===", t.Field(i).Name)
if ormTagStr != "" { if ormTagStr != "" {
col = &core.Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false, col = &core.Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false,
@ -764,6 +767,7 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table {
sqlType = core.SQLType{core.Text, 0, 0} sqlType = core.SQLType{core.Text, 0, 0}
} else { } else {
sqlType = core.Type2SQLType(fieldType) sqlType = core.Type2SQLType(fieldType)
fmt.Println(t.Field(i).Name, "...", sqlType)
} }
col = core.NewColumn(engine.ColumnMapper.Obj2Table(t.Field(i).Name), col = core.NewColumn(engine.ColumnMapper.Obj2Table(t.Field(i).Name),
t.Field(i).Name, sqlType, sqlType.DefaultLength, t.Field(i).Name, sqlType, sqlType.DefaultLength,

View File

@ -3333,6 +3333,7 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
args = append(args, session.Engine.NowTime(col.SQLType.Name)) args = append(args, session.Engine.NowTime(col.SQLType.Name))
} else if col.IsVersion && session.Statement.checkVersion { } else if col.IsVersion && session.Statement.checkVersion {
args = append(args, 1) args = append(args, 1)
//} else if !col.DefaultIsEmpty {
} else { } else {
arg, err := session.value2Interface(col, fieldValue) arg, err := session.value2Interface(col, fieldValue)
if err != nil { if err != nil {

View File

@ -264,6 +264,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
colNames := make([]string, 0) colNames := make([]string, 0)
var args = make([]interface{}, 0) var args = make([]interface{}, 0)
fmt.Println(table.ColumnsSeq())
for _, col := range table.Columns() { for _, col := range table.Columns() {
if !includeVersion && col.IsVersion { if !includeVersion && col.IsVersion {
continue continue
@ -281,6 +282,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text { if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text {
continue continue
} }
fmt.Println("===", col.Name)
fieldValuePtr, err := col.ValueOf(bean) fieldValuePtr, err := col.ValueOf(bean)
if err != nil { if err != nil {
engine.LogError(err) engine.LogError(err)
@ -291,6 +293,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
fieldType := reflect.TypeOf(fieldValue.Interface()) fieldType := reflect.TypeOf(fieldValue.Interface())
requiredField := useAllCols requiredField := useAllCols
includeNil := useAllCols
if b, ok := mustColumnMap[strings.ToLower(col.Name)]; ok { if b, ok := mustColumnMap[strings.ToLower(col.Name)]; ok {
if b { if b {
requiredField = true requiredField = true
@ -323,9 +326,11 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
goto APPEND goto APPEND
} }
fmt.Println(col.Name, "is", fieldValue)
if fieldType.Kind() == reflect.Ptr { if fieldType.Kind() == reflect.Ptr {
if fieldValue.IsNil() { if fieldValue.IsNil() {
if includeNil { if includeNil {
fmt.Println(col.Name, "is nil")
args = append(args, nil) args = append(args, nil)
colNames = append(colNames, fmt.Sprintf("%v=?", engine.Quote(col.Name))) colNames = append(colNames, fmt.Sprintf("%v=?", engine.Quote(col.Name)))
} }