Fix batch insert

This commit is contained in:
Lunny Xiao 2020-03-09 17:37:13 +08:00
parent 3c7daece76
commit 095d7b9f3d
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
4 changed files with 23 additions and 9 deletions

View File

@ -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()
}

View File

@ -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")
}

View File

@ -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:
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:

View File

@ -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)
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{}) {