From cb75b2cd9c4b86ab54779b06c358c4955bb86ff4 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 7 May 2015 17:14:06 +0800 Subject: [PATCH 1/9] bug fixed --- session.go | 6 +++--- xorm.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/session.go b/session.go index 34738b89..80a74a81 100644 --- a/session.go +++ b/session.go @@ -1002,9 +1002,9 @@ func (session *Session) Get(bean interface{}) (bool, error) { var err error session.queryPreprocess(&sqlStr, args...) if session.IsAutoCommit { - stmt, err := session.doPrepare(sqlStr) - if err != nil { - return false, err + stmt, errPrepare := session.doPrepare(sqlStr) + if errPrepare != nil { + return false, errPrepare } // defer stmt.Close() // !nashtsai! don't close due to stmt is cached and bounded to this session rawRows, err = stmt.Query(args...) diff --git a/xorm.go b/xorm.go index 8b6cc568..309329ae 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.4.3.0428" + Version string = "0.4.3.0507" ) func regDrvsNDialects() bool { From a7e72881636cf9a157882b168f509bf39cbd0393 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 18 May 2015 17:04:25 +0800 Subject: [PATCH 2/9] bug fixed --- engine.go | 6 ++---- session.go | 21 ++++++++------------- statement.go | 2 +- xorm.go | 2 +- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/engine.go b/engine.go index 5f652101..02099295 100644 --- a/engine.go +++ b/engine.go @@ -646,20 +646,18 @@ func (engine *Engine) Having(conditions string) *Session { func (engine *Engine) autoMapType(v reflect.Value) *core.Table { t := v.Type() - engine.mutex.RLock() + engine.mutex.Lock() table, ok := engine.Tables[t] - engine.mutex.RUnlock() if !ok { table = engine.mapType(v) - engine.mutex.Lock() engine.Tables[t] = table if v.CanAddr() { engine.GobRegister(v.Addr().Interface()) } else { engine.GobRegister(v.Interface()) } - engine.mutex.Unlock() } + engine.mutex.Unlock() return table } diff --git a/session.go b/session.go index 80a74a81..b097c44d 100644 --- a/session.go +++ b/session.go @@ -2896,20 +2896,15 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val case reflect.String: return fieldValue.String(), nil case reflect.Struct: - if fieldType == core.TimeType { - switch fieldValue.Interface().(type) { - case time.Time: - t := fieldValue.Interface().(time.Time) - if session.Engine.dialect.DBType() == core.MSSQL { - if t.IsZero() { - return nil, nil - } + if fieldType.ConvertibleTo(core.TimeType) { + t := fieldValue.Convert(core.TimeType).Interface().(time.Time) + if session.Engine.dialect.DBType() == core.MSSQL { + if t.IsZero() { + return nil, nil } - tf := session.Engine.FormatTime(col.SQLType.Name, t) - return tf, nil - default: - return fieldValue.Interface(), nil } + tf := session.Engine.FormatTime(col.SQLType.Name, t) + return tf, nil } if fieldTable, ok := session.Engine.Tables[fieldValue.Type()]; ok { if len(fieldTable.PrimaryKeys) == 1 { @@ -2919,7 +2914,7 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val return 0, fmt.Errorf("no primary key for col %v", col.Name) } } else { - return 0, fmt.Errorf("Unsupported type %v\n", fieldValue.Type()) + return 0, fmt.Errorf("Unsupported type %v", fieldValue.Type()) } case reflect.Complex64, reflect.Complex128: bytes, err := json.Marshal(fieldValue.Interface()) diff --git a/statement.go b/statement.go index c7ee11cf..9a62d6f0 100644 --- a/statement.go +++ b/statement.go @@ -615,7 +615,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{}, } } else { //TODO: how to handler? - panic("not supported") + panic(fmt.Sprintln("not supported", fieldValue.Interface(), "as", table.PrimaryKeys)) } } else { val = fieldValue.Interface() diff --git a/xorm.go b/xorm.go index 309329ae..e0151c32 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.4.3.0507" + Version string = "0.4.3.0518" ) func regDrvsNDialects() bool { From f9c968d98a17137732c1f5e34d55e4e5f2b14726 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 19 May 2015 22:39:50 +0800 Subject: [PATCH 3/9] dialect interface changed --- engine.go | 2 +- mssql_dialect.go | 4 ++-- oracle_dialect.go | 4 ++-- postgres_dialect.go | 4 ++-- sqlite3_dialect.go | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/engine.go b/engine.go index 02099295..fa852bad 100644 --- a/engine.go +++ b/engine.go @@ -1114,7 +1114,7 @@ func (engine *Engine) Sync(beans ...interface{}) error { session := engine.NewSession() session.Statement.RefTable = table defer session.Close() - isExist, err := session.Engine.dialect.IsColumnExist(table.Name, col) + isExist, err := session.Engine.dialect.IsColumnExist(table.Name, col.Name) if err != nil { return err } diff --git a/mssql_dialect.go b/mssql_dialect.go index a910406b..57985ad9 100644 --- a/mssql_dialect.go +++ b/mssql_dialect.go @@ -315,10 +315,10 @@ func (db *mssql) IndexCheckSql(tableName, idxName string) (string, []interface{} return sql, args }*/ -func (db *mssql) IsColumnExist(tableName string, col *core.Column) (bool, error) { +func (db *mssql) IsColumnExist(tableName, colName string) (bool, error) { query := `SELECT "COLUMN_NAME" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "COLUMN_NAME" = ?` - return db.HasRecords(query, tableName, col.Name) + return db.HasRecords(query, tableName, colName) } func (db *mssql) TableCheckSql(tableName string) (string, []interface{}) { diff --git a/oracle_dialect.go b/oracle_dialect.go index b4f89edf..dc64c00e 100644 --- a/oracle_dialect.go +++ b/oracle_dialect.go @@ -665,8 +665,8 @@ func (db *oracle) MustDropTable(tableName string) error { " AND column_name = ?", args }*/ -func (db *oracle) IsColumnExist(tableName string, col *core.Column) (bool, error) { - args := []interface{}{tableName, col.Name} +func (db *oracle) IsColumnExist(tableName, colName string) (bool, error) { + args := []interface{}{tableName, colName} query := "SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = :1" + " AND column_name = :2" rows, err := db.DB().Query(query, args...) diff --git a/postgres_dialect.go b/postgres_dialect.go index 04713cf5..67ceecd0 100644 --- a/postgres_dialect.go +++ b/postgres_dialect.go @@ -896,8 +896,8 @@ func (db *postgres) DropIndexSql(tableName string, index *core.Index) string { return fmt.Sprintf("DROP INDEX %v", quote(idxName)) } -func (db *postgres) IsColumnExist(tableName string, col *core.Column) (bool, error) { - args := []interface{}{tableName, col.Name} +func (db *postgres) IsColumnExist(tableName, colName string) (bool, error) { + args := []interface{}{tableName, colName} query := "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = $1" + " AND column_name = $2" rows, err := db.DB().Query(query, args...) diff --git a/sqlite3_dialect.go b/sqlite3_dialect.go index 8a770f78..c525e5be 100644 --- a/sqlite3_dialect.go +++ b/sqlite3_dialect.go @@ -249,9 +249,9 @@ func (db *sqlite3) DropIndexSql(tableName string, index *core.Index) string { return sql, args }*/ -func (db *sqlite3) IsColumnExist(tableName string, col *core.Column) (bool, error) { +func (db *sqlite3) IsColumnExist(tableName, colName string) (bool, error) { args := []interface{}{tableName} - query := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + col.Name + "`%') or (sql like '%[" + col.Name + "]%'))" + query := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))" rows, err := db.DB().Query(query, args...) if db.Logger != nil { db.Logger.Info("[sql]", query, args) From fb995894f00f1a8f8f013fcaaa500b3546a1ff5d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 20 May 2015 10:27:01 +0800 Subject: [PATCH 4/9] bug fixed --- VERSION | 2 +- session.go | 2 +- xorm.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/VERSION b/VERSION index 988f30d1..af3e71ec 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -xorm v0.4.3.0428 +xorm v0.4.3.0520 diff --git a/session.go b/session.go index b097c44d..bb30ab97 100644 --- a/session.go +++ b/session.go @@ -877,7 +877,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in for j := 0; j < len(temps); j++ { bean := temps[j] if bean == nil { - session.Engine.LogWarn("[cacheFind] cache no hit:", tableName, ides[j], temps) + session.Engine.LogWarn("[cacheFind] cache no hit:", tableName, ids[j], temps) // return errors.New("cache error") // !nashtsai! no need to return error, but continue instead continue } diff --git a/xorm.go b/xorm.go index e0151c32..bb7d6461 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.4.3.0518" + Version string = "0.4.3.0520" ) func regDrvsNDialects() bool { From 439cc27466e29d37e39bc1d08562afd5a1e7091c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 24 May 2015 21:32:27 +0800 Subject: [PATCH 5/9] added Select method for special select column express --- engine.go | 6 ++++++ session.go | 55 ++++++++++++++++++++++++++++++++++------------------ statement.go | 38 +++++++++++++++++++++++------------- xorm.go | 2 +- 4 files changed, 68 insertions(+), 33 deletions(-) diff --git a/engine.go b/engine.go index fa852bad..bbe8eb47 100644 --- a/engine.go +++ b/engine.go @@ -510,6 +510,12 @@ func (engine *Engine) Distinct(columns ...string) *Session { return session.Distinct(columns...) } +func (engine *Engine) Select(str string) *Session { + session := engine.NewSession() + session.IsAutoClose = true + return session.Select(str) +} + // only use the paramters as select or update columns func (engine *Engine) Cols(columns ...string) *Session { session := engine.NewSession() diff --git a/session.go b/session.go index bb30ab97..f4c56a49 100644 --- a/session.go +++ b/session.go @@ -172,6 +172,12 @@ func (session *Session) SetExpr(column string, expression string) *Session { return session } +// Method Cols provides some columns to special +func (session *Session) Select(str string) *Session { + session.Statement.Select(str) + return session +} + // Method Cols provides some columns to special func (session *Session) Cols(columns ...string) *Session { session.Statement.Cols(columns...) @@ -621,12 +627,20 @@ func (statement *Statement) convertIdSql(sqlStr string) string { return "" } -func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interface{}) (has bool, err error) { - // if has no reftable, then don't use cache currently +func (session *Session) canCache() bool { if session.Statement.RefTable == nil || session.Statement.JoinStr != "" || session.Statement.RawSQL != "" || - session.Tx != nil { + session.Tx != nil || + len(session.Statement.selectStr) > 0 { + return false + } + return true +} + +func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interface{}) (has bool, err error) { + // if has no reftable, then don't use cache currently + if !session.canCache() { return false, ErrCacheFailed } @@ -724,10 +738,9 @@ func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interf } func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr interface{}, args ...interface{}) (err error) { - if session.Statement.RefTable == nil || + if !session.canCache() || indexNoCase(sqlStr, "having") != -1 || - indexNoCase(sqlStr, "group by") != -1 || - session.Tx != nil { + indexNoCase(sqlStr, "group by") != -1 { return ErrCacheFailed } @@ -1183,20 +1196,24 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) var args []interface{} if session.Statement.RawSQL == "" { var columnStr string = session.Statement.ColumnStr - if session.Statement.JoinStr == "" { - if columnStr == "" { - if session.Statement.GroupByStr != "" { - columnStr = session.Statement.Engine.Quote(strings.Replace(session.Statement.GroupByStr, ",", session.Engine.Quote(","), -1)) - } else { - columnStr = session.Statement.genColumnStr() - } - } + if len(session.Statement.selectStr) > 0 { + columnStr = session.Statement.selectStr } else { - if columnStr == "" { - if session.Statement.GroupByStr != "" { - columnStr = session.Statement.Engine.Quote(strings.Replace(session.Statement.GroupByStr, ",", session.Engine.Quote(","), -1)) - } else { - columnStr = "*" + if session.Statement.JoinStr == "" { + if columnStr == "" { + if session.Statement.GroupByStr != "" { + columnStr = session.Statement.Engine.Quote(strings.Replace(session.Statement.GroupByStr, ",", session.Engine.Quote(","), -1)) + } else { + columnStr = session.Statement.genColumnStr() + } + } + } else { + if columnStr == "" { + if session.Statement.GroupByStr != "" { + columnStr = session.Statement.Engine.Quote(strings.Replace(session.Statement.GroupByStr, ",", session.Engine.Quote(","), -1)) + } else { + columnStr = "*" + } } } } diff --git a/statement.go b/statement.go index 9a62d6f0..e55b5349 100644 --- a/statement.go +++ b/statement.go @@ -49,6 +49,7 @@ type Statement struct { GroupByStr string HavingStr string ColumnStr string + selectStr string columnMap map[string]bool useAllCols bool OmitStr string @@ -100,6 +101,7 @@ func (statement *Statement) Init() { statement.UseAutoTime = true statement.IsDistinct = false statement.TableAlias = "" + statement.selectStr = "" statement.allUseBool = false statement.useAllCols = false statement.mustColumnMap = make(map[string]bool) @@ -862,6 +864,12 @@ func (statement *Statement) Distinct(columns ...string) *Statement { return statement } +// replace select +func (s *Statement) Select(str string) *Statement { + s.selectStr = str + return s +} + // Generate "col1, col2" statement func (statement *Statement) Cols(columns ...string) *Statement { newColumns := col2NewCols(columns...) @@ -1146,20 +1154,24 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) statement.BeanArgs = args var columnStr string = statement.ColumnStr - if len(statement.JoinStr) == 0 { - if len(columnStr) == 0 { - if statement.GroupByStr != "" { - columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1)) - } else { - columnStr = statement.genColumnStr() - } - } + if len(statement.selectStr) > 0 { + columnStr = statement.selectStr } else { - if len(columnStr) == 0 { - if statement.GroupByStr != "" { - columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1)) - } else { - columnStr = "*" + if len(statement.JoinStr) == 0 { + if len(columnStr) == 0 { + if statement.GroupByStr != "" { + columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1)) + } else { + columnStr = statement.genColumnStr() + } + } + } else { + if len(columnStr) == 0 { + if statement.GroupByStr != "" { + columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1)) + } else { + columnStr = "*" + } } } } diff --git a/xorm.go b/xorm.go index bb7d6461..d9c8330c 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.4.3.0520" + Version string = "0.4.3.0524" ) func regDrvsNDialects() bool { From ff6f3b7a8deb093f929a59a69cad237e89e5771a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 25 May 2015 17:49:28 +0800 Subject: [PATCH 6/9] bug fixed for auto condition on join --- session.go | 9 ++++++--- statement.go | 26 +++++++++++++++++++------- xorm.go | 2 +- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/session.go b/session.go index f4c56a49..b47d2780 100644 --- a/session.go +++ b/session.go @@ -1178,9 +1178,11 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) } if len(condiBean) > 0 { + var addedTableName = (len(session.Statement.JoinStr) > 0) colNames, args := buildConditions(session.Engine, table, condiBean[0], true, true, false, true, session.Statement.allUseBool, session.Statement.useAllCols, - session.Statement.unscoped, session.Statement.mustColumnMap) + session.Statement.unscoped, session.Statement.mustColumnMap, + session.Statement.TableName(), addedTableName) session.Statement.ConditionStr = strings.Join(colNames, " AND ") session.Statement.BeanArgs = args } else { @@ -3467,7 +3469,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 if len(condiBean) > 0 { condiColNames, condiArgs = buildConditions(session.Engine, session.Statement.RefTable, condiBean[0], true, true, false, true, session.Statement.allUseBool, session.Statement.useAllCols, - session.Statement.unscoped, session.Statement.mustColumnMap) + session.Statement.unscoped, session.Statement.mustColumnMap, session.Statement.TableName(), false) } var condition = "" @@ -3687,7 +3689,8 @@ func (session *Session) Delete(bean interface{}) (int64, error) { session.Statement.RefTable = table colNames, args := buildConditions(session.Engine, table, bean, true, true, false, true, session.Statement.allUseBool, session.Statement.useAllCols, - session.Statement.unscoped, session.Statement.mustColumnMap) + session.Statement.unscoped, session.Statement.mustColumnMap, + session.Statement.TableName(), false) var condition = "" var andStr = session.Engine.dialect.AndStr() diff --git a/statement.go b/statement.go index e55b5349..97ca9d82 100644 --- a/statement.go +++ b/statement.go @@ -498,8 +498,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{}, func buildConditions(engine *Engine, table *core.Table, bean interface{}, includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool, - mustColumnMap map[string]bool) ([]string, []interface{}) { - + mustColumnMap map[string]bool, tableName string, addedTableName bool) ([]string, []interface{}) { colNames := make([]string, 0) var args = make([]interface{}, 0) for _, col := range table.Columns() { @@ -516,6 +515,14 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{}, if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text { continue } + + var colName string + if addedTableName { + colName = engine.Quote(tableName)+"."+engine.Quote(col.Name) + } else { + colName = engine.Quote(col.Name) + } + fieldValuePtr, err := col.ValueOf(bean) if err != nil { engine.LogError(err) @@ -523,7 +530,8 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{}, } if col.IsDeleted && !unscoped { // tag "deleted" is enabled - colNames = append(colNames, fmt.Sprintf("(%v IS NULL or %v = '0001-01-01 00:00:00')", engine.Quote(col.Name), engine.Quote(col.Name))) + colNames = append(colNames, fmt.Sprintf("(%v IS NULL or %v = '0001-01-01 00:00:00')", + colName, colName)) } fieldValue := *fieldValuePtr @@ -545,7 +553,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{}, if fieldValue.IsNil() { if includeNil { args = append(args, nil) - colNames = append(colNames, fmt.Sprintf("%v %s ?", engine.Quote(col.Name), engine.dialect.EqStr())) + colNames = append(colNames, fmt.Sprintf("%v %s ?", colName, engine.dialect.EqStr())) } continue } else if !fieldValue.IsValid() { @@ -668,7 +676,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{}, if col.IsPrimaryKey && engine.dialect.DBType() == "ql" { condi = "id() == ?" } else { - condi = fmt.Sprintf("%v %s ?", engine.Quote(col.Name), engine.dialect.EqStr()) + condi = fmt.Sprintf("%v %s ?", colName, engine.dialect.EqStr()) } colNames = append(colNames, condi) } @@ -1146,9 +1154,11 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) table = statement.RefTable } + var addedTableName = (len(statement.JoinStr) > 0) + colNames, args := buildConditions(statement.Engine, table, bean, true, true, false, true, statement.allUseBool, statement.useAllCols, - statement.unscoped, statement.mustColumnMap) + statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ") statement.BeanArgs = args @@ -1205,9 +1215,11 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{} table := statement.Engine.TableInfo(bean) statement.RefTable = table + var addedTableName = (len(statement.JoinStr) > 0) + colNames, args := buildConditions(statement.Engine, table, bean, true, true, false, true, statement.allUseBool, statement.useAllCols, - statement.unscoped, statement.mustColumnMap) + statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ") statement.BeanArgs = args diff --git a/xorm.go b/xorm.go index d9c8330c..b045c533 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.4.3.0524" + Version string = "0.4.3.0525" ) func regDrvsNDialects() bool { From 8506d65874293647681ce80741347b449c769138 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 26 May 2015 14:06:54 +0800 Subject: [PATCH 7/9] bug fixed for custom time --- helpers.go | 4 +- statement.go | 120 +-------------------------------------------------- xorm.go | 2 +- 3 files changed, 5 insertions(+), 121 deletions(-) diff --git a/helpers.go b/helpers.go index 5208137a..d8268737 100644 --- a/helpers.go +++ b/helpers.go @@ -133,8 +133,8 @@ func reflect2value(rawValue *reflect.Value) (str string, err error) { } //时间类型 case reflect.Struct: - if aa == core.TimeType { - str = rawValue.Interface().(time.Time).Format(time.RFC3339Nano) + if aa.ConvertibleTo(core.TimeType) { + str = rawValue.Convert(core.TimeType).Interface().(time.Time).Format(time.RFC3339Nano) } else { err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name()) } diff --git a/statement.go b/statement.go index 97ca9d82..dcd7fefa 100644 --- a/statement.go +++ b/statement.go @@ -172,122 +172,6 @@ func (statement *Statement) Table(tableNameOrBean interface{}) *Statement { return statement } -/*func (statement *Statement) genFields(bean interface{}) map[string]interface{} { - results := make(map[string]interface{}) - table := statement.Engine.TableInfo(bean) - for _, col := range table.Columns { - fieldValue := col.ValueOf(bean) - fieldType := reflect.TypeOf(fieldValue.Interface()) - var val interface{} - switch fieldType.Kind() { - case reflect.Bool: - if allUseBool { - val = fieldValue.Interface() - } else if _, ok := boolColumnMap[col.Name]; ok { - val = fieldValue.Interface() - } else { - // if a bool in a struct, it will not be as a condition because it default is false, - // please use Where() instead - continue - } - case reflect.String: - if fieldValue.String() == "" { - continue - } - // for MyString, should convert to string or panic - if fieldType.String() != reflect.String.String() { - val = fieldValue.String() - } else { - val = fieldValue.Interface() - } - case reflect.Int8, reflect.Int16, reflect.Int, reflect.Int32, reflect.Int64: - if fieldValue.Int() == 0 { - continue - } - val = fieldValue.Interface() - case reflect.Float32, reflect.Float64: - if fieldValue.Float() == 0.0 { - continue - } - val = fieldValue.Interface() - case reflect.Uint8, reflect.Uint16, reflect.Uint, reflect.Uint32, reflect.Uint64: - if fieldValue.Uint() == 0 { - continue - } - val = fieldValue.Interface() - case reflect.Struct: - if fieldType == reflect.TypeOf(time.Now()) { - t := fieldValue.Interface().(time.Time) - if t.IsZero() || !fieldValue.IsValid() { - continue - } - var str string - if col.SQLType.Name == Time { - s := t.UTC().Format("2006-01-02 15:04:05") - val = s[11:19] - } else if col.SQLType.Name == Date { - str = t.Format("2006-01-02") - val = str - } else { - val = t - } - } else { - engine.autoMapType(fieldValue.Type()) - if table, ok := engine.Tables[fieldValue.Type()]; ok { - pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumn().FieldName) - if pkField.Int() != 0 { - val = pkField.Interface() - } else { - continue - } - } else { - val = fieldValue.Interface() - } - } - case reflect.Array, reflect.Slice, reflect.Map: - if fieldValue == reflect.Zero(fieldType) { - continue - } - if fieldValue.IsNil() || !fieldValue.IsValid() { - continue - } - - if col.SQLType.IsText() { - bytes, err := json.Marshal(fieldValue.Interface()) - if err != nil { - engine.LogError(err) - continue - } - val = string(bytes) - } else if col.SQLType.IsBlob() { - var bytes []byte - var err error - if (fieldType.Kind() == reflect.Array || fieldType.Kind() == reflect.Slice) && - fieldType.Elem().Kind() == reflect.Uint8 { - if fieldValue.Len() > 0 { - val = fieldValue.Bytes() - } else { - continue - } - } else { - bytes, err = json.Marshal(fieldValue.Interface()) - if err != nil { - engine.LogError(err) - continue - } - val = bytes - } - } else { - continue - } - default: - val = fieldValue.Interface() - } - results[col.Name] = val - } - return results -}*/ - // Auto generating conditions according a struct func buildUpdates(engine *Engine, table *core.Table, bean interface{}, includeVersion bool, includeUpdated bool, includeNil bool, @@ -416,8 +300,8 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{}, t := int64(fieldValue.Uint()) val = reflect.ValueOf(&t).Interface() case reflect.Struct: - if fieldType == reflect.TypeOf(time.Now()) { - t := fieldValue.Interface().(time.Time) + if fieldType.ConvertibleTo(core.TimeType) { + t := fieldValue.Convert(core.TimeType).Interface().(time.Time) if !requiredField && (t.IsZero() || !fieldValue.IsValid()) { continue } diff --git a/xorm.go b/xorm.go index b045c533..036d420f 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.4.3.0525" + Version string = "0.4.3.0526" ) func regDrvsNDialects() bool { From 88cba93cd2a5dbba742f40a94de04382c43c7f5f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 26 May 2015 16:27:18 +0800 Subject: [PATCH 8/9] add qreader case --- README.md | 2 ++ README_CN.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/README.md b/README.md index 3c69a35b..25fbc7b2 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ Or # Cases +* [github.com/m3ng9i/qreader](https://github.com/m3ng9i/qreader) + * [Wego](http://github.com/go-tango/wego) * [Docker.cn](https://docker.cn/) diff --git a/README_CN.md b/README_CN.md index 07a26284..fb08040b 100644 --- a/README_CN.md +++ b/README_CN.md @@ -79,6 +79,8 @@ xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作 ## 案例 +* [github.com/m3ng9i/qreader](https://github.com/m3ng9i/qreader) + * [Wego](http://github.com/go-tango/wego) * [Docker.cn](https://docker.cn/) From a4f9a9f877e3421df5e33313473f4c0b77bbb411 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 2 Jun 2015 23:05:42 +0800 Subject: [PATCH 9/9] bug fixed for #251 --- helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers.go b/helpers.go index d8268737..979a67a1 100644 --- a/helpers.go +++ b/helpers.go @@ -134,7 +134,7 @@ func reflect2value(rawValue *reflect.Value) (str string, err error) { //时间类型 case reflect.Struct: if aa.ConvertibleTo(core.TimeType) { - str = rawValue.Convert(core.TimeType).Interface().(time.Time).Format(time.RFC3339Nano) + str = vv.Convert(core.TimeType).Interface().(time.Time).Format(time.RFC3339Nano) } else { err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name()) }