From ede84cde60a754dff47dc8cc83ae5fc61692c801 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 3 Mar 2015 15:14:44 +0800 Subject: [PATCH 1/7] bug fixed --- session.go | 2 +- statement.go | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/session.go b/session.go index cf566902..4c414209 100644 --- a/session.go +++ b/session.go @@ -3367,7 +3367,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 if session.Statement.ColumnStr == "" { colNames, args = buildUpdates(session.Engine, table, bean, false, false, false, false, session.Statement.allUseBool, session.Statement.useAllCols, - session.Statement.mustColumnMap, true) + session.Statement.mustColumnMap, session.Statement.columnMap, true) } else { colNames, args, err = genCols(table, session, bean, true, true) if err != nil { diff --git a/statement.go b/statement.go index f645a1d3..74a21e1e 100644 --- a/statement.go +++ b/statement.go @@ -286,7 +286,7 @@ func (statement *Statement) Table(tableNameOrBean interface{}) *Statement { func buildUpdates(engine *Engine, table *core.Table, bean interface{}, includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool, allUseBool bool, useAllCols bool, - mustColumnMap map[string]bool, update bool) ([]string, []interface{}) { + mustColumnMap map[string]bool, columnMap map[string]bool, update bool) ([]string, []interface{}) { colNames := make([]string, 0) var args = make([]interface{}, 0) @@ -306,6 +306,9 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{}, if col.IsDeleted { continue } + if use, ok := columnMap[col.Name]; ok && !use { + continue + } if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text { continue From 7acdf292bf67ad865ee2a718b89c23181e357d34 Mon Sep 17 00:00:00 2001 From: ilisin Date: Thu, 5 Mar 2015 09:56:15 +0800 Subject: [PATCH 2/7] for oracle support modify base on lastest version of xorm --- engine.go | 3 +++ session.go | 7 +++++++ statement.go | 12 ++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/engine.go b/engine.go index 447853b9..b47fda89 100644 --- a/engine.go +++ b/engine.go @@ -1404,6 +1404,9 @@ func (engine *Engine) NowTime2(sqlTypeName string) (interface{}, time.Time) { } func (engine *Engine) FormatTime(sqlTypeName string, t time.Time) (v interface{}) { + if engine.dialect.DBType() == core.ORACLE { + return t + } switch sqlTypeName { case core.Time: s := engine.TZTime(t).Format("2006-01-02 15:04:05") //time.RFC3339 diff --git a/session.go b/session.go index 4c414209..3f6e9853 100644 --- a/session.go +++ b/session.go @@ -448,6 +448,13 @@ func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, er return session.Engine.LogSQLExecutionTime(sqlStr, args, func() (sql.Result, error) { if session.IsAutoCommit { + //oci8 can not auto commit (github.com/mattn/go-oci8) + if session.Engine.dialect.DBType() == core.ORACLE { + session.Begin() + r, err := session.Tx.Exec(sqlStr, args...) + session.Commit() + return r, err + } return session.innerExec(sqlStr, args...) } return session.Tx.Exec(sqlStr, args...) diff --git a/statement.go b/statement.go index 74a21e1e..c7d881f4 100644 --- a/statement.go +++ b/statement.go @@ -1221,7 +1221,11 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) { } var fromStr string = " FROM " + statement.Engine.Quote(statement.TableName()) if statement.TableAlias != "" { - fromStr += " AS " + statement.Engine.Quote(statement.TableAlias) + if statement.Engine.dialect.DBType() == core.ORACLE { + fromStr += " " + statement.Engine.Quote(statement.TableAlias) + } else { + fromStr += " AS " + statement.Engine.Quote(statement.TableAlias) + } } if statement.JoinStr != "" { fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr) @@ -1277,12 +1281,16 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) { if statement.OrderStr != "" { a = fmt.Sprintf("%v ORDER BY %v", a, statement.OrderStr) } - if statement.Engine.dialect.DBType() != core.MSSQL { + if statement.Engine.dialect.DBType() != core.MSSQL && statement.Engine.dialect.DBType() != core.ORACLE { if statement.Start > 0 { a = fmt.Sprintf("%v LIMIT %v OFFSET %v", a, statement.LimitN, statement.Start) } else if statement.LimitN > 0 { a = fmt.Sprintf("%v LIMIT %v", a, statement.LimitN) } + } else if statement.Engine.dialect.DBType() == core.ORACLE { + if statement.Start != 0 || statement.LimitN != 0 { + a = fmt.Sprintf("SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d", columnStr, columnStr, a, statement.Start+statement.LimitN, statement.Start) + } } return From d227340c7aabe9e03d7b570c9087dd61f774a74b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E9=87=91=E5=87=AF?= Date: Fri, 6 Mar 2015 16:06:19 +0800 Subject: [PATCH 3/7] bugfix #213 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 参考帮助文档 [自动映射的规则](http://gobook.io/read/go-xorm/manual-zh-cn/chapter-02/4.columns.html) 中的说明 > 如果field名称为Id而且类型为int64并且没有定义tag,则会被xorm视为主键,并且拥有自增属性。如果想用Id以外的名字或非int64类型做为主键名,必须在对应的Tag上加上xorm:"pk"来定义主键,加上xorm:"autoincr"作为自增。这里需要注意的是,有些数据库并不允许非主键的自增属性。 因此在使用`core.GonicMapper`时,忽略了ID主键 --- engine.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine.go b/engine.go index b47fda89..0e063da5 100644 --- a/engine.go +++ b/engine.go @@ -933,7 +933,7 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table { table.AddColumn(col) - if fieldType.Kind() == reflect.Int64 && (col.FieldName == "Id" || strings.HasSuffix(col.FieldName, ".Id")) { + if fieldType.Kind() == reflect.Int64 && (strings.ToUpper(col.FieldName) == "ID" || strings.HasSuffix(strings.ToUpper(col.FieldName), ".ID")) { idFieldColName = col.Name } } // end for From 74a9355457c427c2b2e24ea485f8f22cb6aeea4c Mon Sep 17 00:00:00 2001 From: Shintaro Kaneko Date: Fri, 6 Mar 2015 12:37:05 +0000 Subject: [PATCH 4/7] Fix to refer invalid memory access on helpers.go --- helpers.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/helpers.go b/helpers.go index a0e894e0..7eaa1dd1 100644 --- a/helpers.go +++ b/helpers.go @@ -282,7 +282,10 @@ func query2(db *core.DB, sqlStr string, params ...interface{}) (resultsSlice []m } func setColumnTime(bean interface{}, col *core.Column, t time.Time) { - v, _ := col.ValueOf(bean) + v, err := col.ValueOf(bean) + if err != nil { + return + } if v.CanSet() { switch v.Type().Kind() { case reflect.Struct: From 16b813a159422408dd94f15e0a01fb8b6619125c Mon Sep 17 00:00:00 2001 From: Apphost <125987965@qq.com> Date: Mon, 9 Mar 2015 22:00:34 +0800 Subject: [PATCH 5/7] fix non-int pk issues --- statement.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/statement.go b/statement.go index c7d881f4..baf4d9b0 100644 --- a/statement.go +++ b/statement.go @@ -599,7 +599,9 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{}, if table, ok := engine.Tables[fieldValue.Type()]; ok { if len(table.PrimaryKeys) == 1 { pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) - if pkField.Int() != 0 { + // fix non-int pk issues + //if pkField.Int() != 0 { + if pkField.IsValid() { val = pkField.Interface() } else { continue From d298f91cebc5ac3b885619b373da5c8311c00085 Mon Sep 17 00:00:00 2001 From: Zhou Date: Tue, 10 Mar 2015 09:15:50 +0800 Subject: [PATCH 6/7] func buildUpdates has same issue, fixed --- statement.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/statement.go b/statement.go index baf4d9b0..bdc1dc98 100644 --- a/statement.go +++ b/statement.go @@ -421,7 +421,9 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{}, if table, ok := engine.Tables[fieldValue.Type()]; ok { if len(table.PrimaryKeys) == 1 { pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) - if pkField.Int() != 0 { + // fix non-int pk issues + //if pkField.Int() != 0 { + if pkField.IsValid() { val = pkField.Interface() } else { continue From 9ae245b54bed7552a2dc300ac7627ac9398998bb Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 12 Mar 2015 17:46:24 +0800 Subject: [PATCH 7/7] bug fixed --- session.go | 8 ++++++++ statement.go | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/session.go b/session.go index 3f6e9853..55b91e4c 100644 --- a/session.go +++ b/session.go @@ -3477,6 +3477,10 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 } } + if st.LimitN > 0 { + condition = condition + fmt.Sprintf(" LIMIT %d", st.LimitN) + } + sqlStr = fmt.Sprintf("UPDATE %v SET %v, %v %v", session.Engine.Quote(session.Statement.TableName()), strings.Join(colNames, ", "), @@ -3503,6 +3507,10 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 } } + if st.LimitN > 0 { + condition = condition + fmt.Sprintf(" LIMIT %d", st.LimitN) + } + sqlStr = fmt.Sprintf("UPDATE %v SET %v %v", session.Engine.Quote(session.Statement.TableName()), strings.Join(colNames, ", "), diff --git a/statement.go b/statement.go index bdc1dc98..92ff991d 100644 --- a/statement.go +++ b/statement.go @@ -423,13 +423,14 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{}, pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) // fix non-int pk issues //if pkField.Int() != 0 { - if pkField.IsValid() { + if pkField.IsValid() && !isZero(pkField.Interface()) { val = pkField.Interface() } else { continue } } else { //TODO: how to handler? + panic("not supported") } } else { val = fieldValue.Interface() @@ -603,13 +604,14 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{}, pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) // fix non-int pk issues //if pkField.Int() != 0 { - if pkField.IsValid() { + if pkField.IsValid() && !isZero(pkField.Interface()) { val = pkField.Interface() } else { continue } } else { //TODO: how to handler? + panic("not supported") } } else { val = fieldValue.Interface()