From 095d7b9f3dbf7009e98badc0c632a1530a6a9fc7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 9 Mar 2020 17:37:13 +0800 Subject: [PATCH] Fix batch insert --- core/db_test.go | 1 - dialects/oracle.go | 8 ++++---- dialects/time.go | 6 +++++- session_insert.go | 17 ++++++++++++++--- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/core/db_test.go b/core/db_test.go index 777ab0ad..046b1e89 100644 --- a/core/db_test.go +++ b/core/db_test.go @@ -92,7 +92,6 @@ func BenchmarkOriQuery(b *testing.B) { if err != nil { b.Error(err) } - //fmt.Println(Id, Name, Title, Age, Alias, NickName) } rows.Close() } diff --git a/dialects/oracle.go b/dialects/oracle.go index 013de0b3..0c79489f 100644 --- a/dialects/oracle.go +++ b/dialects/oracle.go @@ -6,6 +6,7 @@ package dialects import ( "context" + "database/sql" "errors" "fmt" "net/url" @@ -658,6 +659,9 @@ func (db *oracle) GetColumns(queryer core.Queryer, ctx context.Context, tableNam var pkName string err := queryer.QueryRowContext(ctx, s, tableName).Scan(&pkName) if err != nil { + if err == sql.ErrNoRows { + err = nil + } return nil, nil, err } @@ -715,8 +719,6 @@ func (db *oracle) GetColumns(queryer core.Queryer, ctx context.Context, tableNam if has { col.IsAutoIncrement = true } - - fmt.Println("-----", pkName, col.Name, col.IsPrimaryKey) } var ( @@ -888,8 +890,6 @@ func parseOracle(driverName, dataSourceName string) (*URI, error) { db.Passwd, _ = u.User.Password() } - fmt.Printf("%#v\n", db) - if db.DBName == "" { return nil, errors.New("dbname is empty") } diff --git a/dialects/time.go b/dialects/time.go index b0394745..d66dc8d5 100644 --- a/dialects/time.go +++ b/dialects/time.go @@ -17,7 +17,11 @@ func FormatTime(dialect Dialect, sqlTypeName string, t time.Time) (v interface{} s := t.Format("2006-01-02 15:04:05") // time.RFC3339 v = s[11:19] case schemas.Date: - v = t.Format("2006-01-02") + if dialect.URI().DBType == schemas.ORACLE { + v = t + } else { + v = t.Format("2006-01-02") + } case schemas.DateTime, schemas.TimeStamp, schemas.Varchar: // !DarthPestilane! format time when sqlTypeName is schemas.Varchar. v = t.Format("2006-01-02 15:04:05") case schemas.TimeStampz: diff --git a/session_insert.go b/session_insert.go index ffe47ef7..c1c8c608 100644 --- a/session_insert.go +++ b/session_insert.go @@ -150,6 +150,13 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error } fieldValue := *ptrFieldValue if col.IsAutoIncrement && utils.IsZero(fieldValue.Interface()) { + if session.engine.dialect.URI().DBType == schemas.ORACLE { + if i == 0 { + colNames = append(colNames, col.Name) + cols = append(cols, col) + } + colPlaces = append(colPlaces, "seq_"+tableName+".nextval") + } continue } if col.MapType == schemas.ONLYFROMDB { @@ -357,7 +364,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { } var id int64 - err = session.queryRow(fmt.Sprintf("select %s.currval from dual", tableName)).Scan(&id) + err = session.queryRow(fmt.Sprintf("select seq_%s.currval from dual", tableName)).Scan(&id) if err != nil { return 1, err } @@ -536,10 +543,14 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac } } - if (col.IsCreated || col.IsUpdated) && session.statement.UseAutoTime /*&& isZero(fieldValue.Interface())*/ { + if (col.IsCreated || col.IsUpdated) && session.statement.UseAutoTime { // if time is non-empty, then set to auto time val, t := session.engine.nowTime(col) - args = append(args, val) + if session.engine.dialect.URI().DBType == schemas.ORACLE { + args = append(args, t) + } else { + args = append(args, val) + } var colName = col.Name session.afterClosures = append(session.afterClosures, func(bean interface{}) {