mirror of
https://gitee.com/johng/gf
synced 2026-07-07 06:15:15 +08:00
96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// 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,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package oracle
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
// DoInsert inserts or updates data for given table.
|
|
func (d *Driver) DoInsert(
|
|
ctx context.Context, link gdb.Link, table string, list gdb.List, option gdb.DoInsertOption,
|
|
) (result sql.Result, err error) {
|
|
switch option.InsertOption {
|
|
case gdb.InsertOptionSave:
|
|
return nil, gerror.NewCode(
|
|
gcode.CodeNotSupported,
|
|
`Save operation is not supported by oracle driver`,
|
|
)
|
|
|
|
case gdb.InsertOptionReplace:
|
|
return nil, gerror.NewCode(
|
|
gcode.CodeNotSupported,
|
|
`Replace operation is not supported by oracle driver`,
|
|
)
|
|
}
|
|
var (
|
|
keys []string
|
|
values []string
|
|
params []interface{}
|
|
)
|
|
// Retrieve the table fields and length.
|
|
var (
|
|
listLength = len(list)
|
|
valueHolder = make([]string, 0)
|
|
)
|
|
for k := range list[0] {
|
|
keys = append(keys, k)
|
|
valueHolder = append(valueHolder, "?")
|
|
}
|
|
var (
|
|
batchResult = new(gdb.SqlResult)
|
|
charL, charR = d.GetChars()
|
|
keyStr = charL + strings.Join(keys, charL+","+charR) + charR
|
|
valueHolderStr = strings.Join(valueHolder, ",")
|
|
)
|
|
// Format "INSERT...INTO..." statement.
|
|
intoStrArray := make([]string, 0)
|
|
for i := 0; i < len(list); i++ {
|
|
for _, k := range keys {
|
|
if s, ok := list[i][k].(gdb.Raw); ok {
|
|
params = append(params, gconv.String(s))
|
|
} else {
|
|
params = append(params, list[i][k])
|
|
}
|
|
}
|
|
values = append(values, valueHolderStr)
|
|
intoStrArray = append(
|
|
intoStrArray,
|
|
fmt.Sprintf(
|
|
"INTO %s(%s) VALUES(%s)",
|
|
table, keyStr, valueHolderStr,
|
|
),
|
|
)
|
|
if len(intoStrArray) == option.BatchCount || (i == listLength-1 && len(valueHolder) > 0) {
|
|
r, err := d.DoExec(ctx, link, fmt.Sprintf(
|
|
"INSERT ALL %s SELECT * FROM DUAL",
|
|
strings.Join(intoStrArray, " "),
|
|
), params...)
|
|
if err != nil {
|
|
return r, err
|
|
}
|
|
if n, err := r.RowsAffected(); err != nil {
|
|
return r, err
|
|
} else {
|
|
batchResult.Result = r
|
|
batchResult.Affected += n
|
|
}
|
|
params = params[:0]
|
|
intoStrArray = intoStrArray[:0]
|
|
}
|
|
}
|
|
return batchResult, nil
|
|
}
|