diff --git a/engine.go b/engine.go index 96071a36..4103738e 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 @@ -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/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: diff --git a/session.go b/session.go index c0c4d4dc..41afe844 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...) @@ -3369,7 +3376,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 { @@ -3472,6 +3479,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, ", "), @@ -3498,6 +3509,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 18137440..998d8d68 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 @@ -418,13 +421,16 @@ 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() && !isZero(pkField.Interface()) { val = pkField.Interface() } else { continue } } else { //TODO: how to handler? + panic("not supported") } } else { val = fieldValue.Interface() @@ -596,13 +602,16 @@ 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() && !isZero(pkField.Interface()) { val = pkField.Interface() } else { continue } } else { //TODO: how to handler? + panic("not supported") } } else { val = fieldValue.Interface() @@ -1219,7 +1228,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) @@ -1275,12 +1288,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