2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-10-26 22:08:52 +08:00
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2020-01-06 20:43:59 +08:00
|
|
|
|
//
|
|
|
|
|
// Note:
|
|
|
|
|
// 1. It needs manually import: _ "github.com/mattn/go-oci8"
|
|
|
|
|
// 2. It does not support Save/Replace features.
|
|
|
|
|
// 3. It does not support LastInsertId.
|
2019-03-05 17:52:34 +08:00
|
|
|
|
|
2018-10-26 22:08:52 +08:00
|
|
|
|
package gdb
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
2019-08-12 16:50:57 +08:00
|
|
|
|
"errors"
|
2018-10-26 22:08:52 +08:00
|
|
|
|
"fmt"
|
2020-02-24 21:09:19 +08:00
|
|
|
|
"github.com/gogf/gf/internal/intlog"
|
2020-09-27 00:15:11 +08:00
|
|
|
|
"github.com/gogf/gf/os/gcache"
|
2020-02-22 14:26:36 +08:00
|
|
|
|
"github.com/gogf/gf/text/gstr"
|
2019-08-12 16:50:57 +08:00
|
|
|
|
"reflect"
|
2018-10-26 22:08:52 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2019-09-02 15:48:25 +08:00
|
|
|
|
|
|
|
|
|
"github.com/gogf/gf/text/gregex"
|
2018-10-26 22:08:52 +08:00
|
|
|
|
)
|
|
|
|
|
|
2020-03-08 00:17:42 +08:00
|
|
|
|
// DriverOracle is the driver for oracle database.
|
|
|
|
|
type DriverOracle struct {
|
|
|
|
|
*Core
|
2018-10-26 22:08:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 16:50:57 +08:00
|
|
|
|
const (
|
|
|
|
|
tableAlias1 = "GFORM1"
|
|
|
|
|
tableAlias2 = "GFORM2"
|
|
|
|
|
)
|
|
|
|
|
|
2020-03-08 00:17:42 +08:00
|
|
|
|
// New creates and returns a database object for oracle.
|
2020-03-08 11:03:18 +08:00
|
|
|
|
// It implements the interface of gdb.Driver for extra database driver installation.
|
2020-03-08 00:17:42 +08:00
|
|
|
|
func (d *DriverOracle) New(core *Core, node *ConfigNode) (DB, error) {
|
|
|
|
|
return &DriverOracle{
|
|
|
|
|
Core: core,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 19:55:53 +08:00
|
|
|
|
// Open creates and returns a underlying sql.DB object for oracle.
|
2020-03-08 00:17:42 +08:00
|
|
|
|
func (d *DriverOracle) Open(config *ConfigNode) (*sql.DB, error) {
|
2018-10-26 22:08:52 +08:00
|
|
|
|
var source string
|
2019-04-02 14:37:46 +08:00
|
|
|
|
if config.LinkInfo != "" {
|
|
|
|
|
source = config.LinkInfo
|
2018-10-26 22:08:52 +08:00
|
|
|
|
} else {
|
2018-12-15 15:50:39 +08:00
|
|
|
|
source = fmt.Sprintf("%s/%s@%s", config.User, config.Pass, config.Name)
|
2018-10-26 22:08:52 +08:00
|
|
|
|
}
|
2020-02-24 21:09:19 +08:00
|
|
|
|
intlog.Printf("Open: %s", source)
|
2018-10-26 22:08:52 +08:00
|
|
|
|
if db, err := sql.Open("oci8", source); err == nil {
|
|
|
|
|
return db, nil
|
|
|
|
|
} else {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 00:17:42 +08:00
|
|
|
|
// GetChars returns the security char for this type of database.
|
|
|
|
|
func (d *DriverOracle) GetChars() (charLeft string, charRight string) {
|
2018-12-14 18:35:51 +08:00
|
|
|
|
return "\"", "\""
|
2018-10-26 22:08:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 22:00:01 +08:00
|
|
|
|
// HandleSqlBeforeCommit deals with the sql string before commits it to underlying sql driver.
|
2020-03-22 23:26:15 +08:00
|
|
|
|
func (d *DriverOracle) HandleSqlBeforeCommit(link Link, sql string, args []interface{}) (string, []interface{}) {
|
2020-02-29 19:55:53 +08:00
|
|
|
|
var index int
|
2020-07-15 20:10:54 +08:00
|
|
|
|
// Convert place holder char '?' to string ":vx".
|
2020-03-22 23:26:15 +08:00
|
|
|
|
str, _ := gregex.ReplaceStringFunc("\\?", sql, func(s string) string {
|
2018-10-26 22:08:52 +08:00
|
|
|
|
index++
|
2020-07-15 20:10:54 +08:00
|
|
|
|
return fmt.Sprintf(":v%d", index)
|
2018-10-26 22:08:52 +08:00
|
|
|
|
})
|
|
|
|
|
str, _ = gregex.ReplaceString("\"", "", str)
|
2020-03-09 21:53:58 +08:00
|
|
|
|
return d.parseSql(str), args
|
2018-10-26 22:08:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 20:44:20 +08:00
|
|
|
|
// parseSql does some replacement of the sql before commits it to underlying driver,
|
|
|
|
|
// for support of oracle server.
|
2020-03-08 00:17:42 +08:00
|
|
|
|
func (d *DriverOracle) parseSql(sql string) string {
|
2020-09-03 22:38:07 +08:00
|
|
|
|
var (
|
|
|
|
|
patten = `^\s*(?i)(SELECT)|(LIMIT\s*(\d+)\s*,{0,1}\s*(\d*))`
|
|
|
|
|
allMatch, _ = gregex.MatchAllString(patten, sql)
|
|
|
|
|
)
|
|
|
|
|
if len(allMatch) == 0 {
|
2018-10-26 22:08:52 +08:00
|
|
|
|
return sql
|
|
|
|
|
}
|
2020-07-15 20:10:54 +08:00
|
|
|
|
var (
|
|
|
|
|
index = 0
|
2020-09-03 22:38:07 +08:00
|
|
|
|
keyword = strings.ToUpper(strings.TrimSpace(allMatch[index][0]))
|
2020-07-15 20:10:54 +08:00
|
|
|
|
)
|
2018-10-26 22:08:52 +08:00
|
|
|
|
index++
|
|
|
|
|
switch keyword {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
case "SELECT":
|
2020-09-03 22:38:07 +08:00
|
|
|
|
if len(allMatch) < 2 || strings.HasPrefix(allMatch[index][0], "LIMIT") == false {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if gregex.IsMatchString("((?i)SELECT)(.+)((?i)LIMIT)", sql) == false {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
queryExpr, _ := gregex.MatchString("((?i)SELECT)(.+)((?i)LIMIT)", sql)
|
2020-09-03 22:38:07 +08:00
|
|
|
|
if len(queryExpr) != 4 ||
|
|
|
|
|
strings.EqualFold(queryExpr[1], "SELECT") == false ||
|
2020-03-23 20:44:20 +08:00
|
|
|
|
strings.EqualFold(queryExpr[3], "LIMIT") == false {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
first, limit := 0, 0
|
2020-09-03 22:38:07 +08:00
|
|
|
|
for i := 1; i < len(allMatch[index]); i++ {
|
|
|
|
|
if len(strings.TrimSpace(allMatch[index][i])) == 0 {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
continue
|
2018-10-26 22:08:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 22:38:07 +08:00
|
|
|
|
if strings.HasPrefix(allMatch[index][i], "LIMIT") {
|
|
|
|
|
if allMatch[index][i+2] != "" {
|
|
|
|
|
first, _ = strconv.Atoi(allMatch[index][i+1])
|
|
|
|
|
limit, _ = strconv.Atoi(allMatch[index][i+2])
|
|
|
|
|
} else {
|
|
|
|
|
limit, _ = strconv.Atoi(allMatch[index][i+1])
|
|
|
|
|
}
|
2018-10-26 22:08:52 +08:00
|
|
|
|
break
|
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
2020-02-29 19:55:53 +08:00
|
|
|
|
sql = fmt.Sprintf(
|
2020-03-23 20:44:20 +08:00
|
|
|
|
"SELECT * FROM "+
|
|
|
|
|
"(SELECT GFORM.*, ROWNUM ROWNUM_ FROM (%s %s) GFORM WHERE ROWNUM <= %d)"+
|
|
|
|
|
" WHERE ROWNUM_ >= %d",
|
2020-02-29 19:55:53 +08:00
|
|
|
|
queryExpr[1], queryExpr[2], limit, first,
|
|
|
|
|
)
|
2018-10-26 22:08:52 +08:00
|
|
|
|
}
|
|
|
|
|
return sql
|
|
|
|
|
}
|
2019-01-02 18:17:01 +08:00
|
|
|
|
|
2020-02-29 19:55:53 +08:00
|
|
|
|
// Tables retrieves and returns the tables of current schema.
|
2020-03-08 11:03:18 +08:00
|
|
|
|
// It's mainly used in cli tool chain for automatically generating the models.
|
2020-03-06 11:01:03 +08:00
|
|
|
|
// Note that it ignores the parameter <schema> in oracle database, as it is not necessary.
|
2020-03-08 00:17:42 +08:00
|
|
|
|
func (d *DriverOracle) Tables(schema ...string) (tables []string, err error) {
|
2020-03-02 23:41:33 +08:00
|
|
|
|
var result Result
|
2020-03-08 00:17:42 +08:00
|
|
|
|
result, err = d.DB.DoGetAll(nil, "SELECT TABLE_NAME FROM USER_TABLES ORDER BY TABLE_NAME")
|
2020-03-02 23:41:33 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, m := range result {
|
|
|
|
|
for _, v := range m {
|
2020-03-06 11:01:03 +08:00
|
|
|
|
tables = append(tables, v.String())
|
2020-03-02 23:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-02 15:48:25 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 19:55:53 +08:00
|
|
|
|
// TableFields retrieves and returns the fields information of specified table of current schema.
|
2020-03-08 00:17:42 +08:00
|
|
|
|
func (d *DriverOracle) TableFields(table string, schema ...string) (fields map[string]*TableField, err error) {
|
2020-04-08 21:26:14 +08:00
|
|
|
|
charL, charR := d.GetChars()
|
|
|
|
|
table = gstr.Trim(table, charL+charR)
|
2020-02-22 14:26:36 +08:00
|
|
|
|
if gstr.Contains(table, " ") {
|
2020-04-08 21:26:14 +08:00
|
|
|
|
return nil, errors.New("function TableFields supports only single table operations")
|
2020-02-22 14:26:36 +08:00
|
|
|
|
}
|
2020-03-08 00:17:42 +08:00
|
|
|
|
checkSchema := d.DB.GetSchema()
|
2020-01-07 22:14:32 +08:00
|
|
|
|
if len(schema) > 0 && schema[0] != "" {
|
|
|
|
|
checkSchema = schema[0]
|
|
|
|
|
}
|
2020-10-09 20:59:49 +08:00
|
|
|
|
v, _ := gcache.GetOrSetFunc(
|
2020-01-07 22:14:32 +08:00
|
|
|
|
fmt.Sprintf(`oracle_table_fields_%s_%s`, table, checkSchema),
|
2020-10-09 20:59:49 +08:00
|
|
|
|
func() (interface{}, error) {
|
2020-01-07 22:14:32 +08:00
|
|
|
|
result := (Result)(nil)
|
2020-03-08 00:17:42 +08:00
|
|
|
|
result, err = d.DB.GetAll(fmt.Sprintf(`
|
2020-01-07 22:14:32 +08:00
|
|
|
|
SELECT COLUMN_NAME AS FIELD, CASE DATA_TYPE
|
|
|
|
|
WHEN 'NUMBER' THEN DATA_TYPE||'('||DATA_PRECISION||','||DATA_SCALE||')'
|
|
|
|
|
WHEN 'FLOAT' THEN DATA_TYPE||'('||DATA_PRECISION||','||DATA_SCALE||')'
|
|
|
|
|
ELSE DATA_TYPE||'('||DATA_LENGTH||')' END AS TYPE
|
|
|
|
|
FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '%s' ORDER BY COLUMN_ID`, strings.ToUpper(table)))
|
|
|
|
|
if err != nil {
|
2020-10-09 20:59:49 +08:00
|
|
|
|
return nil, err
|
2019-09-02 15:48:25 +08:00
|
|
|
|
}
|
2020-01-07 22:14:32 +08:00
|
|
|
|
fields = make(map[string]*TableField)
|
|
|
|
|
for i, m := range result {
|
|
|
|
|
fields[strings.ToLower(m["FIELD"].String())] = &TableField{
|
|
|
|
|
Index: i,
|
|
|
|
|
Name: strings.ToLower(m["FIELD"].String()),
|
|
|
|
|
Type: strings.ToLower(m["TYPE"].String()),
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-09 20:59:49 +08:00
|
|
|
|
return fields, nil
|
2020-01-07 22:14:32 +08:00
|
|
|
|
}, 0)
|
2019-01-02 18:17:01 +08:00
|
|
|
|
if err == nil {
|
2019-09-02 15:48:25 +08:00
|
|
|
|
fields = v.(map[string]*TableField)
|
2019-01-02 18:17:01 +08:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-08-12 16:50:57 +08:00
|
|
|
|
|
2020-03-08 00:17:42 +08:00
|
|
|
|
func (d *DriverOracle) getTableUniqueIndex(table string) (fields map[string]map[string]string, err error) {
|
2019-08-12 16:50:57 +08:00
|
|
|
|
table = strings.ToUpper(table)
|
2020-10-09 20:59:49 +08:00
|
|
|
|
v, _ := gcache.GetOrSetFunc(
|
|
|
|
|
"table_unique_index_"+table,
|
|
|
|
|
func() (interface{}, error) {
|
|
|
|
|
res := (Result)(nil)
|
|
|
|
|
res, err = d.DB.GetAll(fmt.Sprintf(`
|
2019-08-12 16:50:57 +08:00
|
|
|
|
SELECT INDEX_NAME,COLUMN_NAME,CHAR_LENGTH FROM USER_IND_COLUMNS
|
|
|
|
|
WHERE TABLE_NAME = '%s'
|
|
|
|
|
AND INDEX_NAME IN(SELECT INDEX_NAME FROM USER_INDEXES WHERE TABLE_NAME='%s' AND UNIQUENESS='UNIQUE')
|
|
|
|
|
ORDER BY INDEX_NAME,COLUMN_POSITION`, table, table))
|
2020-10-09 20:59:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
fields := make(map[string]map[string]string)
|
|
|
|
|
for _, v := range res {
|
|
|
|
|
mm := make(map[string]string)
|
|
|
|
|
mm[v["COLUMN_NAME"].String()] = v["CHAR_LENGTH"].String()
|
|
|
|
|
fields[v["INDEX_NAME"].String()] = mm
|
|
|
|
|
}
|
|
|
|
|
return fields, nil
|
|
|
|
|
}, 0)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
if err == nil {
|
|
|
|
|
fields = v.(map[string]map[string]string)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 21:53:58 +08:00
|
|
|
|
func (d *DriverOracle) DoInsert(link Link, table string, data interface{}, option int, batch ...int) (result sql.Result, err error) {
|
2019-08-12 16:50:57 +08:00
|
|
|
|
var fields []string
|
|
|
|
|
var values []string
|
|
|
|
|
var params []interface{}
|
|
|
|
|
var dataMap Map
|
|
|
|
|
rv := reflect.ValueOf(data)
|
|
|
|
|
kind := rv.Kind()
|
|
|
|
|
if kind == reflect.Ptr {
|
|
|
|
|
rv = rv.Elem()
|
|
|
|
|
kind = rv.Kind()
|
|
|
|
|
}
|
|
|
|
|
switch kind {
|
|
|
|
|
case reflect.Slice:
|
|
|
|
|
fallthrough
|
|
|
|
|
case reflect.Array:
|
2020-03-08 00:17:42 +08:00
|
|
|
|
return d.DB.DoBatchInsert(link, table, data, option, batch...)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
case reflect.Map:
|
|
|
|
|
fallthrough
|
|
|
|
|
case reflect.Struct:
|
2020-03-08 11:03:18 +08:00
|
|
|
|
dataMap = DataToMapDeep(data)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
default:
|
|
|
|
|
return result, errors.New(fmt.Sprint("unsupported data type:", kind))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
indexs := make([]string, 0)
|
|
|
|
|
indexMap := make(map[string]string)
|
|
|
|
|
indexExists := false
|
2019-09-29 14:27:09 +08:00
|
|
|
|
if option != gINSERT_OPTION_DEFAULT {
|
2020-03-08 00:17:42 +08:00
|
|
|
|
index, err := d.getTableUniqueIndex(table)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(index) > 0 {
|
|
|
|
|
for _, v := range index {
|
|
|
|
|
for k, _ := range v {
|
|
|
|
|
indexs = append(indexs, k)
|
|
|
|
|
}
|
|
|
|
|
indexMap = v
|
|
|
|
|
indexExists = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subSqlStr := make([]string, 0)
|
|
|
|
|
onStr := make([]string, 0)
|
|
|
|
|
updateStr := make([]string, 0)
|
|
|
|
|
|
2020-03-08 00:17:42 +08:00
|
|
|
|
charL, charR := d.DB.GetChars()
|
2019-08-12 16:50:57 +08:00
|
|
|
|
for k, v := range dataMap {
|
|
|
|
|
k = strings.ToUpper(k)
|
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
|
// 操作类型为REPLACE/SAVE时且存在唯一索引才使用merge,否则使用insert
|
2019-09-29 14:27:09 +08:00
|
|
|
|
if (option == gINSERT_OPTION_REPLACE || option == gINSERT_OPTION_SAVE) && indexExists {
|
2019-08-12 16:50:57 +08:00
|
|
|
|
fields = append(fields, tableAlias1+"."+charL+k+charR)
|
|
|
|
|
values = append(values, tableAlias2+"."+charL+k+charR)
|
2020-02-07 20:58:47 +08:00
|
|
|
|
params = append(params, v)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
|
|
|
|
|
subSqlStr = append(subSqlStr, fmt.Sprintf("%s?%s %s", charL, charR, k))
|
|
|
|
|
|
|
|
|
|
//merge中的on子句中由唯一索引组成,update子句中不含唯一索引
|
|
|
|
|
if _, ok := indexMap[k]; ok {
|
|
|
|
|
onStr = append(onStr, fmt.Sprintf("%s.%s = %s.%s ", tableAlias1, k, tableAlias2, k))
|
|
|
|
|
} else {
|
|
|
|
|
updateStr = append(updateStr, fmt.Sprintf("%s.%s = %s.%s ", tableAlias1, k, tableAlias2, k))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fields = append(fields, charL+k+charR)
|
|
|
|
|
values = append(values, "?")
|
2020-02-07 20:58:47 +08:00
|
|
|
|
params = append(params, v)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if link == nil {
|
2020-03-08 00:17:42 +08:00
|
|
|
|
if link, err = d.DB.Master(); err != nil {
|
2019-08-12 16:50:57 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 14:27:09 +08:00
|
|
|
|
if indexExists && option != gINSERT_OPTION_DEFAULT {
|
2019-08-12 16:50:57 +08:00
|
|
|
|
switch option {
|
2019-09-29 14:27:09 +08:00
|
|
|
|
case gINSERT_OPTION_REPLACE:
|
2019-08-12 16:50:57 +08:00
|
|
|
|
fallthrough
|
2019-09-29 14:27:09 +08:00
|
|
|
|
case gINSERT_OPTION_SAVE:
|
2020-02-10 20:37:53 +08:00
|
|
|
|
tmp := fmt.Sprintf(
|
|
|
|
|
"MERGE INTO %s %s USING(SELECT %s FROM DUAL) %s ON(%s) WHEN MATCHED THEN UPDATE SET %s WHEN NOT MATCHED THEN INSERT (%s) VALUES(%s)",
|
2019-08-12 16:50:57 +08:00
|
|
|
|
table, tableAlias1, strings.Join(subSqlStr, ","), tableAlias2,
|
2020-02-10 20:37:53 +08:00
|
|
|
|
strings.Join(onStr, "AND"), strings.Join(updateStr, ","), strings.Join(fields, ","), strings.Join(values, ","),
|
|
|
|
|
)
|
2020-03-08 00:17:42 +08:00
|
|
|
|
return d.DB.DoExec(link, tmp, params...)
|
2019-09-29 14:27:09 +08:00
|
|
|
|
case gINSERT_OPTION_IGNORE:
|
2020-03-08 00:17:42 +08:00
|
|
|
|
return d.DB.DoExec(link,
|
2020-02-10 20:37:53 +08:00
|
|
|
|
fmt.Sprintf(
|
|
|
|
|
"INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX(%s(%s)) */ INTO %s(%s) VALUES(%s)",
|
|
|
|
|
table, strings.Join(indexs, ","), table, strings.Join(fields, ","), strings.Join(values, ","),
|
|
|
|
|
),
|
2019-08-12 16:50:57 +08:00
|
|
|
|
params...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 00:17:42 +08:00
|
|
|
|
return d.DB.DoExec(
|
2020-02-10 20:37:53 +08:00
|
|
|
|
link,
|
|
|
|
|
fmt.Sprintf(
|
|
|
|
|
"INSERT INTO %s(%s) VALUES(%s)",
|
|
|
|
|
table, strings.Join(fields, ","), strings.Join(values, ","),
|
|
|
|
|
),
|
|
|
|
|
params...)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 21:53:58 +08:00
|
|
|
|
func (d *DriverOracle) DoBatchInsert(link Link, table string, list interface{}, option int, batch ...int) (result sql.Result, err error) {
|
2019-08-12 16:50:57 +08:00
|
|
|
|
var keys []string
|
|
|
|
|
var values []string
|
|
|
|
|
var params []interface{}
|
|
|
|
|
listMap := (List)(nil)
|
|
|
|
|
switch v := list.(type) {
|
|
|
|
|
case Result:
|
2019-10-01 16:03:18 +08:00
|
|
|
|
listMap = v.List()
|
2019-08-12 16:50:57 +08:00
|
|
|
|
case Record:
|
2019-10-01 16:03:18 +08:00
|
|
|
|
listMap = List{v.Map()}
|
2019-08-12 16:50:57 +08:00
|
|
|
|
case List:
|
|
|
|
|
listMap = v
|
|
|
|
|
case Map:
|
|
|
|
|
listMap = List{v}
|
|
|
|
|
default:
|
|
|
|
|
rv := reflect.ValueOf(list)
|
|
|
|
|
kind := rv.Kind()
|
|
|
|
|
if kind == reflect.Ptr {
|
|
|
|
|
rv = rv.Elem()
|
|
|
|
|
kind = rv.Kind()
|
|
|
|
|
}
|
|
|
|
|
switch kind {
|
|
|
|
|
// 如果是slice,那么转换为List类型
|
|
|
|
|
case reflect.Slice:
|
|
|
|
|
fallthrough
|
|
|
|
|
case reflect.Array:
|
|
|
|
|
listMap = make(List, rv.Len())
|
|
|
|
|
for i := 0; i < rv.Len(); i++ {
|
2020-03-08 11:03:18 +08:00
|
|
|
|
listMap[i] = DataToMapDeep(rv.Index(i).Interface())
|
2019-08-12 16:50:57 +08:00
|
|
|
|
}
|
|
|
|
|
case reflect.Map:
|
|
|
|
|
fallthrough
|
|
|
|
|
case reflect.Struct:
|
2020-03-08 11:03:18 +08:00
|
|
|
|
listMap = List{Map(DataToMapDeep(list))}
|
2019-08-12 16:50:57 +08:00
|
|
|
|
default:
|
|
|
|
|
return result, errors.New(fmt.Sprint("unsupported list type:", kind))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 判断长度
|
|
|
|
|
if len(listMap) < 1 {
|
|
|
|
|
return result, errors.New("empty data list")
|
|
|
|
|
}
|
|
|
|
|
if link == nil {
|
2020-03-08 00:17:42 +08:00
|
|
|
|
if link, err = d.DB.Master(); err != nil {
|
2019-08-12 16:50:57 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 首先获取字段名称及记录长度
|
|
|
|
|
holders := []string(nil)
|
|
|
|
|
for k, _ := range listMap[0] {
|
|
|
|
|
keys = append(keys, k)
|
|
|
|
|
holders = append(holders, "?")
|
|
|
|
|
}
|
2020-03-13 17:21:30 +08:00
|
|
|
|
batchResult := new(SqlResult)
|
2020-03-08 00:17:42 +08:00
|
|
|
|
charL, charR := d.DB.GetChars()
|
2019-08-12 16:50:57 +08:00
|
|
|
|
keyStr := charL + strings.Join(keys, charL+","+charR) + charR
|
|
|
|
|
valueHolderStr := strings.Join(holders, ",")
|
|
|
|
|
|
|
|
|
|
// 当操作类型非insert时调用单笔的insert功能
|
2019-09-29 14:27:09 +08:00
|
|
|
|
if option != gINSERT_OPTION_DEFAULT {
|
2019-08-12 16:50:57 +08:00
|
|
|
|
for _, v := range listMap {
|
2020-03-08 00:17:42 +08:00
|
|
|
|
r, err := d.DB.DoInsert(link, table, v, option, 1)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return r, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n, err := r.RowsAffected(); err != nil {
|
|
|
|
|
return r, err
|
|
|
|
|
} else {
|
2020-03-13 17:21:30 +08:00
|
|
|
|
batchResult.result = r
|
|
|
|
|
batchResult.affected += n
|
2019-08-12 16:50:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return batchResult, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构造批量写入数据格式(注意map的遍历是无序的)
|
|
|
|
|
batchNum := gDEFAULT_BATCH_NUM
|
|
|
|
|
if len(batch) > 0 {
|
|
|
|
|
batchNum = batch[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intoStr := make([]string, 0) //组装into语句
|
|
|
|
|
for i := 0; i < len(listMap); i++ {
|
|
|
|
|
for _, k := range keys {
|
2020-02-07 20:58:47 +08:00
|
|
|
|
params = append(params, listMap[i][k])
|
2019-08-12 16:50:57 +08:00
|
|
|
|
}
|
|
|
|
|
values = append(values, valueHolderStr)
|
|
|
|
|
intoStr = append(intoStr, fmt.Sprintf(" INTO %s(%s) VALUES(%s) ", table, keyStr, valueHolderStr))
|
|
|
|
|
if len(intoStr) == batchNum {
|
2020-03-08 00:17:42 +08:00
|
|
|
|
r, err := d.DB.DoExec(link, fmt.Sprintf("INSERT ALL %s SELECT * FROM DUAL", strings.Join(intoStr, " ")), params...)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return r, err
|
|
|
|
|
}
|
|
|
|
|
if n, err := r.RowsAffected(); err != nil {
|
|
|
|
|
return r, err
|
|
|
|
|
} else {
|
2020-03-13 17:21:30 +08:00
|
|
|
|
batchResult.result = r
|
|
|
|
|
batchResult.affected += n
|
2019-08-12 16:50:57 +08:00
|
|
|
|
}
|
|
|
|
|
params = params[:0]
|
|
|
|
|
intoStr = intoStr[:0]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 处理最后不构成指定批量的数据
|
|
|
|
|
if len(intoStr) > 0 {
|
2020-03-08 00:17:42 +08:00
|
|
|
|
r, err := d.DB.DoExec(link, fmt.Sprintf("INSERT ALL %s SELECT * FROM DUAL", strings.Join(intoStr, " ")), params...)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return r, err
|
|
|
|
|
}
|
|
|
|
|
if n, err := r.RowsAffected(); err != nil {
|
|
|
|
|
return r, err
|
|
|
|
|
} else {
|
2020-03-13 17:21:30 +08:00
|
|
|
|
batchResult.result = r
|
|
|
|
|
batchResult.affected += n
|
2019-08-12 16:50:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return batchResult, nil
|
|
|
|
|
}
|