feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
435
backend/domain/workflow/internal/nodes/database/common.go
Normal file
435
backend/domain/workflow/internal/nodes/database/common.go
Normal file
@@ -0,0 +1,435 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/eino/compose"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/database"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/logs"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/sonic"
|
||||
)
|
||||
|
||||
const rowNum = "rowNum"
|
||||
const outputList = "outputList"
|
||||
const TimeFormat = "2006-01-02 15:04:05 -0700 MST"
|
||||
|
||||
func toString(in any) (any, error) {
|
||||
switch in := in.(type) {
|
||||
case []byte:
|
||||
return string(in), nil
|
||||
case string:
|
||||
return in, nil
|
||||
case int64:
|
||||
return strconv.FormatInt(in, 10), nil
|
||||
case float64:
|
||||
return strconv.FormatFloat(in, 'f', -1, 64), nil
|
||||
case time.Time:
|
||||
return in.Format(TimeFormat), nil
|
||||
case bool:
|
||||
return strconv.FormatBool(in), nil
|
||||
case map[string]any, []any:
|
||||
return sonic.MarshalString(in)
|
||||
default:
|
||||
return "", fmt.Errorf("unknown type: %T", in)
|
||||
}
|
||||
}
|
||||
|
||||
func toInteger(in any) (any, error) {
|
||||
switch in := in.(type) {
|
||||
case []byte:
|
||||
return strconv.ParseInt(string(in), 10, 64)
|
||||
case string:
|
||||
return strconv.ParseInt(in, 10, 64)
|
||||
case int64:
|
||||
return in, nil
|
||||
case float64:
|
||||
return int64(in), nil
|
||||
case time.Time, bool:
|
||||
return nil, fmt.Errorf(`type '%T' can't convert to int64'`, in)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown type: %T", in)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func toNumber(in any) (any, error) {
|
||||
switch in := in.(type) {
|
||||
case []byte:
|
||||
i, err := strconv.ParseFloat(string(in), 64)
|
||||
return i, err
|
||||
case string:
|
||||
return strconv.ParseFloat(in, 64)
|
||||
case int64:
|
||||
return float64(in), nil
|
||||
case float64:
|
||||
return in, nil
|
||||
case time.Time, bool:
|
||||
return nil, fmt.Errorf(`type '%T' can't convert to float64'`, in)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown type: %T", in)
|
||||
}
|
||||
}
|
||||
|
||||
func toTime(in any) (any, error) {
|
||||
switch in := in.(type) {
|
||||
case []byte:
|
||||
return string(in), nil
|
||||
case string:
|
||||
return in, nil
|
||||
case int64:
|
||||
return strconv.FormatInt(in, 10), nil
|
||||
case float64:
|
||||
return strconv.FormatFloat(in, 'f', -1, 64), nil
|
||||
case time.Time:
|
||||
return in.Format(TimeFormat), nil
|
||||
case bool:
|
||||
if in {
|
||||
return "1", nil
|
||||
}
|
||||
return "0", nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown type: %T", in)
|
||||
}
|
||||
}
|
||||
|
||||
func toBool(in any) (any, error) {
|
||||
switch in := in.(type) {
|
||||
case []byte:
|
||||
return strconv.ParseBool(string(in))
|
||||
case string:
|
||||
return strconv.ParseBool(in)
|
||||
case int64:
|
||||
return strconv.ParseBool(strconv.FormatInt(in, 10))
|
||||
case float64:
|
||||
return strconv.ParseBool(strconv.FormatFloat(in, 'f', -1, 64))
|
||||
case time.Time:
|
||||
return strconv.ParseBool(in.Format(TimeFormat))
|
||||
case bool:
|
||||
return in, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown type: %T", in)
|
||||
}
|
||||
}
|
||||
|
||||
// formatted convert the interface type according to the datatype type.
|
||||
// notice: object is currently not supported by database, and ignore it.
|
||||
func formatted(in any, ty *vo.TypeInfo) any {
|
||||
switch ty.Type {
|
||||
case vo.DataTypeString:
|
||||
r, err := toString(in)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted string error: %v", err)
|
||||
return nil
|
||||
}
|
||||
return r
|
||||
case vo.DataTypeNumber:
|
||||
r, err := toNumber(in)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted number error: %v", err)
|
||||
return nil
|
||||
}
|
||||
return r
|
||||
case vo.DataTypeInteger:
|
||||
r, err := toInteger(in)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted integer error: %v", err)
|
||||
return nil
|
||||
}
|
||||
return r
|
||||
case vo.DataTypeBoolean:
|
||||
r, err := toBool(in)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted boolean error: %v", err)
|
||||
}
|
||||
return r
|
||||
case vo.DataTypeTime:
|
||||
r, err := toTime(in)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted time error: %v", err)
|
||||
return nil
|
||||
}
|
||||
return r
|
||||
case vo.DataTypeArray:
|
||||
arrayIn := make([]any, 0)
|
||||
inStr, err := toString(in)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted array error: %v", err)
|
||||
return []any{}
|
||||
}
|
||||
|
||||
err = sonic.UnmarshalString(inStr.(string), &arrayIn)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted array unmarshal error: %v", err)
|
||||
return []any{}
|
||||
}
|
||||
result := make([]any, 0)
|
||||
switch ty.ElemTypeInfo.Type {
|
||||
case vo.DataTypeTime:
|
||||
for _, in := range arrayIn {
|
||||
r, err := toTime(in)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted time: %v", err)
|
||||
continue
|
||||
}
|
||||
result = append(result, r)
|
||||
}
|
||||
return result
|
||||
case vo.DataTypeString:
|
||||
for _, in := range arrayIn {
|
||||
r, err := toString(in)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted string failed: %v", err)
|
||||
continue
|
||||
}
|
||||
result = append(result, r)
|
||||
}
|
||||
return result
|
||||
case vo.DataTypeInteger:
|
||||
for _, in := range arrayIn {
|
||||
r, err := toInteger(in)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted interger failed: %v", err)
|
||||
continue
|
||||
}
|
||||
result = append(result, r)
|
||||
}
|
||||
return result
|
||||
case vo.DataTypeBoolean:
|
||||
for _, in := range arrayIn {
|
||||
r, err := toBool(in)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted bool failed: %v", err)
|
||||
continue
|
||||
}
|
||||
result = append(result, r)
|
||||
}
|
||||
return result
|
||||
case vo.DataTypeNumber:
|
||||
for _, in := range arrayIn {
|
||||
r, err := toNumber(in)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted number failed: %v", err)
|
||||
continue
|
||||
}
|
||||
result = append(result, r)
|
||||
}
|
||||
return result
|
||||
case vo.DataTypeObject:
|
||||
properties := ty.ElemTypeInfo.Properties
|
||||
if len(properties) == 0 {
|
||||
for idx := range arrayIn {
|
||||
in := arrayIn[idx]
|
||||
if _, ok := in.(database.Object); ok {
|
||||
result = append(result, in)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
for idx := range arrayIn {
|
||||
in := arrayIn[idx]
|
||||
object, ok := in.(database.Object)
|
||||
if !ok {
|
||||
object = make(database.Object)
|
||||
for key := range properties {
|
||||
object[key] = nil
|
||||
}
|
||||
result = append(result, object)
|
||||
} else {
|
||||
result = append(result, objectFormatted(ty.ElemTypeInfo.Properties, object))
|
||||
|
||||
}
|
||||
}
|
||||
return result
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func objectFormatted(props map[string]*vo.TypeInfo, object database.Object) map[string]any {
|
||||
ret := make(map[string]any)
|
||||
|
||||
// if config is nil, it agrees to convert to string type as the default value
|
||||
if len(props) == 0 {
|
||||
for k, v := range object {
|
||||
val, err := toString(v)
|
||||
if err != nil {
|
||||
logs.Warnf("formatted string error: %v", err)
|
||||
continue
|
||||
}
|
||||
ret[k] = val
|
||||
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
for k, v := range props {
|
||||
if r, ok := object[k]; ok && r != nil {
|
||||
formattedValue := formatted(r, v)
|
||||
ret[k] = formattedValue
|
||||
} else {
|
||||
// if key not existed, assign nil
|
||||
ret[k] = nil
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
// responseFormatted convert the object list returned by "response" into the field mapping of the "config output" configuration,
|
||||
// If the conversion fail, set the output list to null. If there are missing fields, set the missing fields to null.
|
||||
func responseFormatted(configOutput map[string]*vo.TypeInfo, response *database.Response) (map[string]any, error) {
|
||||
ret := make(map[string]any)
|
||||
list := make([]any, 0, len(configOutput))
|
||||
|
||||
outputListTypeInfo, ok := configOutput["outputList"]
|
||||
if !ok {
|
||||
return ret, fmt.Errorf("outputList key is required")
|
||||
}
|
||||
if outputListTypeInfo.Type != vo.DataTypeArray {
|
||||
return nil, fmt.Errorf("output list type info must array,but got %v", outputListTypeInfo.Type)
|
||||
}
|
||||
if outputListTypeInfo.ElemTypeInfo == nil {
|
||||
return nil, fmt.Errorf("output list must be an array and the array must contain element type info")
|
||||
}
|
||||
if outputListTypeInfo.ElemTypeInfo.Type != vo.DataTypeObject {
|
||||
return nil, fmt.Errorf("output list must be an array and element must object, but got %v", outputListTypeInfo.ElemTypeInfo.Type)
|
||||
}
|
||||
|
||||
props := outputListTypeInfo.ElemTypeInfo.Properties
|
||||
|
||||
for _, object := range response.Objects {
|
||||
list = append(list, objectFormatted(props, object))
|
||||
}
|
||||
|
||||
ret[outputList] = list
|
||||
if response.RowNumber != nil {
|
||||
ret[rowNum] = *response.RowNumber
|
||||
} else {
|
||||
ret[rowNum] = nil
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func convertClauseGroupToConditionGroup(ctx context.Context, clauseGroup *database.ClauseGroup, input map[string]any) (*database.ConditionGroup, error) {
|
||||
var (
|
||||
rightValue any
|
||||
ok bool
|
||||
)
|
||||
|
||||
conditionGroup := &database.ConditionGroup{
|
||||
Conditions: make([]*database.Condition, 0),
|
||||
Relation: database.ClauseRelationAND,
|
||||
}
|
||||
|
||||
if clauseGroup.Single != nil {
|
||||
clause := clauseGroup.Single
|
||||
if !notNeedTakeMapValue(clause.Operator) {
|
||||
rightValue, ok = nodes.TakeMapValue(input, compose.FieldPath{"__condition_right_0"})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cannot take single clause from input")
|
||||
}
|
||||
}
|
||||
|
||||
conditionGroup.Conditions = append(conditionGroup.Conditions, &database.Condition{
|
||||
Left: clause.Left,
|
||||
Operator: clause.Operator,
|
||||
Right: rightValue,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
if clauseGroup.Multi != nil {
|
||||
conditionGroup.Relation = clauseGroup.Multi.Relation
|
||||
|
||||
conditionGroup.Conditions = make([]*database.Condition, len(clauseGroup.Multi.Clauses))
|
||||
multiSelect := clauseGroup.Multi
|
||||
for idx, clause := range multiSelect.Clauses {
|
||||
if !notNeedTakeMapValue(clause.Operator) {
|
||||
rightValue, ok = nodes.TakeMapValue(input, compose.FieldPath{fmt.Sprintf("__condition_right_%d", idx)})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cannot take multi clause from input")
|
||||
}
|
||||
}
|
||||
conditionGroup.Conditions[idx] = &database.Condition{
|
||||
Left: clause.Left,
|
||||
Operator: clause.Operator,
|
||||
Right: rightValue,
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return conditionGroup, nil
|
||||
}
|
||||
|
||||
func convertClauseGroupToUpdateInventory(ctx context.Context, clauseGroup *database.ClauseGroup, input map[string]any) (*UpdateInventory, error) {
|
||||
conditionGroup, err := convertClauseGroupToConditionGroup(ctx, clauseGroup, input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fields := parseToInput(input)
|
||||
inventory := &UpdateInventory{
|
||||
ConditionGroup: conditionGroup,
|
||||
Fields: fields,
|
||||
}
|
||||
return inventory, nil
|
||||
}
|
||||
|
||||
func isDebugExecute(ctx context.Context) bool {
|
||||
execCtx := execute.GetExeCtx(ctx)
|
||||
if execCtx == nil {
|
||||
panic(fmt.Errorf("unable to get exe context"))
|
||||
}
|
||||
return execCtx.RootCtx.ExeCfg.Mode == vo.ExecuteModeDebug || execCtx.RootCtx.ExeCfg.Mode == vo.ExecuteModeNodeDebug
|
||||
}
|
||||
|
||||
func getExecUserID(ctx context.Context) int64 {
|
||||
execCtx := execute.GetExeCtx(ctx)
|
||||
if execCtx == nil {
|
||||
panic(fmt.Errorf("unable to get exe context"))
|
||||
}
|
||||
return execCtx.RootCtx.ExeCfg.Operator
|
||||
}
|
||||
|
||||
func parseToInput(input map[string]any) map[string]any {
|
||||
result := make(map[string]any, len(input))
|
||||
for key, value := range input {
|
||||
if strings.HasPrefix(key, "__setting_field_") {
|
||||
key = strings.TrimPrefix(key, "__setting_field_")
|
||||
result[key] = value
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
127
backend/domain/workflow/internal/nodes/database/customsql.go
Normal file
127
backend/domain/workflow/internal/nodes/database/customsql.go
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/database"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/sonic"
|
||||
)
|
||||
|
||||
type CustomSQLConfig struct {
|
||||
DatabaseInfoID int64
|
||||
SQLTemplate string
|
||||
OutputConfig map[string]*vo.TypeInfo
|
||||
CustomSQLExecutor database.DatabaseOperator
|
||||
}
|
||||
|
||||
func NewCustomSQL(_ context.Context, cfg *CustomSQLConfig) (*CustomSQL, error) {
|
||||
if cfg == nil {
|
||||
return nil, errors.New("config is required")
|
||||
}
|
||||
if cfg.DatabaseInfoID == 0 {
|
||||
return nil, errors.New("database info id is required and greater than 0")
|
||||
}
|
||||
if cfg.SQLTemplate == "" {
|
||||
return nil, errors.New("sql template is required")
|
||||
}
|
||||
if cfg.CustomSQLExecutor == nil {
|
||||
return nil, errors.New("custom sqler is required")
|
||||
}
|
||||
return &CustomSQL{
|
||||
config: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type CustomSQL struct {
|
||||
config *CustomSQLConfig
|
||||
}
|
||||
|
||||
func (c *CustomSQL) Execute(ctx context.Context, input map[string]any) (map[string]any, error) {
|
||||
|
||||
req := &database.CustomSQLRequest{
|
||||
DatabaseInfoID: c.config.DatabaseInfoID,
|
||||
IsDebugRun: isDebugExecute(ctx),
|
||||
UserID: getExecUserID(ctx),
|
||||
}
|
||||
|
||||
inputBytes, err := sonic.Marshal(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
templateSQL := ""
|
||||
templateParts := nodes.ParseTemplate(c.config.SQLTemplate)
|
||||
sqlParams := make([]database.SQLParam, 0, len(templateParts))
|
||||
var nilError = errors.New("field is nil")
|
||||
for _, templatePart := range templateParts {
|
||||
if !templatePart.IsVariable {
|
||||
templateSQL += templatePart.Value
|
||||
continue
|
||||
}
|
||||
|
||||
templateSQL += "?"
|
||||
val, err := templatePart.Render(inputBytes, nodes.WithNilRender(func() (string, error) {
|
||||
return "", nilError
|
||||
}),
|
||||
nodes.WithCustomRender(reflect.TypeOf(false), func(val any) (string, error) {
|
||||
b := val.(bool)
|
||||
if b {
|
||||
return "1", nil
|
||||
}
|
||||
return "0", nil
|
||||
}))
|
||||
|
||||
if err != nil {
|
||||
if !errors.Is(err, nilError) {
|
||||
return nil, err
|
||||
}
|
||||
sqlParams = append(sqlParams, database.SQLParam{
|
||||
IsNull: true,
|
||||
})
|
||||
} else {
|
||||
sqlParams = append(sqlParams, database.SQLParam{
|
||||
Value: val,
|
||||
IsNull: false,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// replace sql template '?' to ?
|
||||
templateSQL = strings.Replace(templateSQL, "'?'", "?", -1)
|
||||
templateSQL = strings.Replace(templateSQL, "`?`", "?", -1)
|
||||
req.SQL = templateSQL
|
||||
req.Params = sqlParams
|
||||
response, err := c.config.CustomSQLExecutor.Execute(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret, err := responseFormatted(c.config.OutputConfig, response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/bytedance/mockey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uber.org/mock/gomock"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/database"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/database/databasemock"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
)
|
||||
|
||||
type mockCustomSQLer struct {
|
||||
validate func(req *database.CustomSQLRequest)
|
||||
}
|
||||
|
||||
func (m mockCustomSQLer) Execute() func(ctx context.Context, request *database.CustomSQLRequest) (*database.Response, error) {
|
||||
return func(ctx context.Context, request *database.CustomSQLRequest) (*database.Response, error) {
|
||||
m.validate(request)
|
||||
r := &database.Response{
|
||||
Objects: []database.Object{
|
||||
database.Object{
|
||||
"v1": "v1_ret",
|
||||
"v2": "v2_ret",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomSQL_Execute(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
mockSQLer := mockCustomSQLer{
|
||||
validate: func(req *database.CustomSQLRequest) {
|
||||
assert.Equal(t, int64(111), req.DatabaseInfoID)
|
||||
ps := []database.SQLParam{
|
||||
database.SQLParam{Value: "v1_value"},
|
||||
database.SQLParam{Value: "v2_value"},
|
||||
database.SQLParam{Value: "v3_value"},
|
||||
}
|
||||
assert.Equal(t, ps, req.Params)
|
||||
assert.Equal(t, "select * from v1 where v1 = ? and v2 = ? and v3 = ?", req.SQL)
|
||||
},
|
||||
}
|
||||
|
||||
defer mockey.Mock(execute.GetExeCtx).Return(&execute.Context{
|
||||
RootCtx: execute.RootCtx{
|
||||
ExeCfg: vo.ExecuteConfig{
|
||||
Mode: vo.ExecuteModeDebug,
|
||||
Operator: 123,
|
||||
BizType: vo.BizTypeWorkflow,
|
||||
},
|
||||
},
|
||||
}).Build().UnPatch()
|
||||
|
||||
mockDatabaseOperator := databasemock.NewMockDatabaseOperator(ctrl)
|
||||
mockDatabaseOperator.EXPECT().Execute(gomock.Any(), gomock.Any()).DoAndReturn(mockSQLer.Execute()).AnyTimes()
|
||||
|
||||
cfg := &CustomSQLConfig{
|
||||
DatabaseInfoID: 111,
|
||||
SQLTemplate: "select * from v1 where v1 = {{v1}} and v2 = '{{v2}}' and v3 = `{{v3}}`",
|
||||
CustomSQLExecutor: mockDatabaseOperator,
|
||||
OutputConfig: map[string]*vo.TypeInfo{
|
||||
"outputList": {Type: vo.DataTypeArray, ElemTypeInfo: &vo.TypeInfo{Type: vo.DataTypeObject, Properties: map[string]*vo.TypeInfo{
|
||||
"v1": {Type: vo.DataTypeString},
|
||||
"v2": {Type: vo.DataTypeString},
|
||||
}}},
|
||||
"rowNum": {Type: vo.DataTypeInteger},
|
||||
},
|
||||
}
|
||||
cl := &CustomSQL{
|
||||
config: cfg,
|
||||
}
|
||||
|
||||
ret, err := cl.Execute(t.Context(), map[string]any{
|
||||
"v1": "v1_value",
|
||||
"v2": "v2_value",
|
||||
"v3": "v3_value",
|
||||
})
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, "v1_ret", ret[outputList].([]any)[0].(database.Object)["v1"])
|
||||
assert.Equal(t, "v2_ret", ret[outputList].([]any)[0].(database.Object)["v2"])
|
||||
|
||||
}
|
||||
111
backend/domain/workflow/internal/nodes/database/delete.go
Normal file
111
backend/domain/workflow/internal/nodes/database/delete.go
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/database"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
)
|
||||
|
||||
type DeleteConfig struct {
|
||||
DatabaseInfoID int64
|
||||
ClauseGroup *database.ClauseGroup
|
||||
OutputConfig map[string]*vo.TypeInfo
|
||||
|
||||
Deleter database.DatabaseOperator
|
||||
}
|
||||
type Delete struct {
|
||||
config *DeleteConfig
|
||||
}
|
||||
|
||||
func NewDelete(_ context.Context, cfg *DeleteConfig) (*Delete, error) {
|
||||
if cfg == nil {
|
||||
return nil, errors.New("config is required")
|
||||
}
|
||||
if cfg.DatabaseInfoID == 0 {
|
||||
return nil, errors.New("database info id is required and greater than 0")
|
||||
}
|
||||
|
||||
if cfg.ClauseGroup == nil {
|
||||
return nil, errors.New("clauseGroup is required")
|
||||
}
|
||||
if cfg.Deleter == nil {
|
||||
return nil, errors.New("deleter is required")
|
||||
}
|
||||
|
||||
return &Delete{
|
||||
config: cfg,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func (d *Delete) Delete(ctx context.Context, in map[string]any) (map[string]any, error) {
|
||||
conditionGroup, err := convertClauseGroupToConditionGroup(ctx, d.config.ClauseGroup, in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
request := &database.DeleteRequest{
|
||||
DatabaseInfoID: d.config.DatabaseInfoID,
|
||||
ConditionGroup: conditionGroup,
|
||||
IsDebugRun: isDebugExecute(ctx),
|
||||
UserID: getExecUserID(ctx),
|
||||
}
|
||||
|
||||
response, err := d.config.Deleter.Delete(ctx, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret, err := responseFormatted(d.config.OutputConfig, response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (d *Delete) ToCallbackInput(_ context.Context, in map[string]any) (map[string]any, error) {
|
||||
conditionGroup, err := convertClauseGroupToConditionGroup(context.Background(), d.config.ClauseGroup, in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return d.toDatabaseDeleteCallbackInput(conditionGroup)
|
||||
}
|
||||
|
||||
func (d *Delete) toDatabaseDeleteCallbackInput(conditionGroup *database.ConditionGroup) (map[string]any, error) {
|
||||
databaseID := d.config.DatabaseInfoID
|
||||
result := make(map[string]any)
|
||||
|
||||
result["databaseInfoList"] = []string{fmt.Sprintf("%d", databaseID)}
|
||||
result["deleteParam"] = map[string]any{}
|
||||
|
||||
condition, err := convertToCondition(conditionGroup)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
type Field struct {
|
||||
FieldID string `json:"fieldId"`
|
||||
IsDistinct bool `json:"isDistinct"`
|
||||
}
|
||||
result["deleteParam"] = map[string]any{
|
||||
"condition": condition}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
102
backend/domain/workflow/internal/nodes/database/insert.go
Normal file
102
backend/domain/workflow/internal/nodes/database/insert.go
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/database"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
)
|
||||
|
||||
type InsertConfig struct {
|
||||
DatabaseInfoID int64
|
||||
OutputConfig map[string]*vo.TypeInfo
|
||||
Inserter database.DatabaseOperator
|
||||
}
|
||||
|
||||
type Insert struct {
|
||||
config *InsertConfig
|
||||
}
|
||||
|
||||
func NewInsert(_ context.Context, cfg *InsertConfig) (*Insert, error) {
|
||||
if cfg == nil {
|
||||
return nil, errors.New("config is required")
|
||||
}
|
||||
if cfg.DatabaseInfoID == 0 {
|
||||
return nil, errors.New("database info id is required and greater than 0")
|
||||
}
|
||||
|
||||
if cfg.Inserter == nil {
|
||||
return nil, errors.New("inserter is required")
|
||||
}
|
||||
return &Insert{
|
||||
config: cfg,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func (is *Insert) Insert(ctx context.Context, input map[string]any) (map[string]any, error) {
|
||||
|
||||
fields := parseToInput(input)
|
||||
req := &database.InsertRequest{
|
||||
DatabaseInfoID: is.config.DatabaseInfoID,
|
||||
Fields: fields,
|
||||
IsDebugRun: isDebugExecute(ctx),
|
||||
UserID: getExecUserID(ctx),
|
||||
}
|
||||
|
||||
response, err := is.config.Inserter.Insert(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret, err := responseFormatted(is.config.OutputConfig, response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (is *Insert) ToCallbackInput(_ context.Context, input map[string]any) (map[string]any, error) {
|
||||
databaseID := is.config.DatabaseInfoID
|
||||
fs := parseToInput(input)
|
||||
result := make(map[string]any)
|
||||
result["databaseInfoList"] = []string{fmt.Sprintf("%d", databaseID)}
|
||||
|
||||
type FieldInfo struct {
|
||||
FieldID string `json:"fieldId"`
|
||||
FieldValue any `json:"fieldValue"`
|
||||
}
|
||||
|
||||
fieldInfo := make([]*FieldInfo, 0)
|
||||
for k, v := range fs {
|
||||
fieldInfo = append(fieldInfo, &FieldInfo{
|
||||
FieldID: k,
|
||||
FieldValue: v,
|
||||
})
|
||||
}
|
||||
result["insertParam"] = map[string]any{
|
||||
"fieldInfo": fieldInfo,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
221
backend/domain/workflow/internal/nodes/database/query.go
Normal file
221
backend/domain/workflow/internal/nodes/database/query.go
Normal file
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/database"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
)
|
||||
|
||||
type QueryConfig struct {
|
||||
DatabaseInfoID int64
|
||||
QueryFields []string
|
||||
OrderClauses []*database.OrderClause
|
||||
OutputConfig map[string]*vo.TypeInfo
|
||||
ClauseGroup *database.ClauseGroup
|
||||
Limit int64
|
||||
Op database.DatabaseOperator
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
config *QueryConfig
|
||||
}
|
||||
|
||||
func NewQuery(_ context.Context, cfg *QueryConfig) (*Query, error) {
|
||||
if cfg == nil {
|
||||
return nil, errors.New("config is required")
|
||||
}
|
||||
if cfg.DatabaseInfoID == 0 {
|
||||
return nil, errors.New("database info id is required and greater than 0")
|
||||
}
|
||||
|
||||
if cfg.Limit == 0 {
|
||||
return nil, errors.New("limit is required and greater than 0")
|
||||
}
|
||||
|
||||
if cfg.Op == nil {
|
||||
return nil, errors.New("op is required")
|
||||
}
|
||||
|
||||
return &Query{config: cfg}, nil
|
||||
|
||||
}
|
||||
|
||||
func (ds *Query) Query(ctx context.Context, in map[string]any) (map[string]any, error) {
|
||||
conditionGroup, err := convertClauseGroupToConditionGroup(ctx, ds.config.ClauseGroup, in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req := &database.QueryRequest{
|
||||
DatabaseInfoID: ds.config.DatabaseInfoID,
|
||||
OrderClauses: ds.config.OrderClauses,
|
||||
SelectFields: ds.config.QueryFields,
|
||||
Limit: ds.config.Limit,
|
||||
IsDebugRun: isDebugExecute(ctx),
|
||||
UserID: getExecUserID(ctx),
|
||||
}
|
||||
|
||||
req.ConditionGroup = conditionGroup
|
||||
|
||||
response, err := ds.config.Op.Query(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret, err := responseFormatted(ds.config.OutputConfig, response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func notNeedTakeMapValue(op database.Operator) bool {
|
||||
return op == database.OperatorIsNull || op == database.OperatorIsNotNull
|
||||
}
|
||||
|
||||
func (ds *Query) ToCallbackInput(ctx context.Context, in map[string]any) (map[string]any, error) {
|
||||
conditionGroup, err := convertClauseGroupToConditionGroup(ctx, ds.config.ClauseGroup, in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return toDatabaseQueryCallbackInput(ds.config, conditionGroup)
|
||||
}
|
||||
|
||||
func toDatabaseQueryCallbackInput(config *QueryConfig, conditionGroup *database.ConditionGroup) (map[string]any, error) {
|
||||
result := make(map[string]any)
|
||||
|
||||
databaseID := config.DatabaseInfoID
|
||||
result["databaseInfoList"] = []string{fmt.Sprintf("%d", databaseID)}
|
||||
result["selectParam"] = map[string]any{}
|
||||
|
||||
condition, err := convertToCondition(conditionGroup)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
type Field struct {
|
||||
FieldID string `json:"fieldId"`
|
||||
IsDistinct bool `json:"isDistinct"`
|
||||
}
|
||||
fieldList := make([]Field, 0, len(config.QueryFields))
|
||||
for _, f := range config.QueryFields {
|
||||
fieldList = append(fieldList, Field{FieldID: f})
|
||||
}
|
||||
type Order struct {
|
||||
FieldID string `json:"fieldId"`
|
||||
IsAsc bool `json:"isAsc"`
|
||||
}
|
||||
|
||||
OrderList := make([]Order, 0)
|
||||
for _, c := range config.OrderClauses {
|
||||
OrderList = append(OrderList, Order{
|
||||
FieldID: c.FieldID,
|
||||
IsAsc: c.IsAsc,
|
||||
})
|
||||
}
|
||||
result["selectParam"] = map[string]any{
|
||||
"condition": condition,
|
||||
"fieldList": fieldList,
|
||||
"limit": config.Limit,
|
||||
"orderByList": OrderList,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
|
||||
type ConditionItem struct {
|
||||
Left string `json:"left"`
|
||||
Operation string `json:"operation"`
|
||||
Right any `json:"right"`
|
||||
}
|
||||
type Condition struct {
|
||||
ConditionList []ConditionItem `json:"conditionList"`
|
||||
Logic string `json:"logic"`
|
||||
}
|
||||
|
||||
func convertToCondition(conditionGroup *database.ConditionGroup) (*Condition, error) {
|
||||
logic, err := convertToLogic(conditionGroup.Relation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
condition := &Condition{
|
||||
ConditionList: make([]ConditionItem, 0),
|
||||
Logic: logic,
|
||||
}
|
||||
for _, c := range conditionGroup.Conditions {
|
||||
op, err := convertToOperation(c.Operator)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid operator: %s", c.Operator)
|
||||
}
|
||||
condition.ConditionList = append(condition.ConditionList, ConditionItem{
|
||||
Left: c.Left,
|
||||
Operation: op,
|
||||
Right: c.Right,
|
||||
})
|
||||
|
||||
}
|
||||
return condition, nil
|
||||
}
|
||||
|
||||
func convertToOperation(Op database.Operator) (string, error) {
|
||||
switch Op {
|
||||
case database.OperatorEqual:
|
||||
return "EQUAL", nil
|
||||
case database.OperatorNotEqual:
|
||||
return "NOT_EQUAL", nil
|
||||
case database.OperatorGreater:
|
||||
return "GREATER_THAN", nil
|
||||
case database.OperatorLesser:
|
||||
return "LESS_THAN", nil
|
||||
case database.OperatorGreaterOrEqual:
|
||||
return "GREATER_EQUAL", nil
|
||||
case database.OperatorLesserOrEqual:
|
||||
return "LESS_EQUAL", nil
|
||||
case database.OperatorIn:
|
||||
return "IN", nil
|
||||
case database.OperatorNotIn:
|
||||
return "NOT_IN", nil
|
||||
case database.OperatorIsNull:
|
||||
return "IS_NULL", nil
|
||||
case database.OperatorIsNotNull:
|
||||
return "IS_NOT_NULL", nil
|
||||
case database.OperatorLike:
|
||||
return "LIKE", nil
|
||||
case database.OperatorNotLike:
|
||||
return "NOT LIKE", nil
|
||||
}
|
||||
return "", fmt.Errorf("not a valid database Operator")
|
||||
|
||||
}
|
||||
|
||||
func convertToLogic(rel database.ClauseRelation) (string, error) {
|
||||
switch rel {
|
||||
case database.ClauseRelationOR:
|
||||
return "OR", nil
|
||||
case database.ClauseRelationAND:
|
||||
return "AND", nil
|
||||
default:
|
||||
return "", fmt.Errorf("unknown clause relation %v", rel)
|
||||
|
||||
}
|
||||
}
|
||||
456
backend/domain/workflow/internal/nodes/database/query_test.go
Normal file
456
backend/domain/workflow/internal/nodes/database/query_test.go
Normal file
@@ -0,0 +1,456 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/bytedance/mockey"
|
||||
"go.uber.org/mock/gomock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/database"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/database/databasemock"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
)
|
||||
|
||||
type mockDsSelect struct {
|
||||
t *testing.T
|
||||
objects []database.Object
|
||||
validate func(request *database.QueryRequest)
|
||||
}
|
||||
|
||||
func (m *mockDsSelect) Query() func(ctx context.Context, request *database.QueryRequest) (*database.Response, error) {
|
||||
return func(ctx context.Context, request *database.QueryRequest) (*database.Response, error) {
|
||||
n := int64(1)
|
||||
|
||||
m.validate(request)
|
||||
|
||||
return &database.Response{
|
||||
RowNumber: &n,
|
||||
Objects: m.objects,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestDataset_Query(t *testing.T) {
|
||||
defer mockey.Mock(execute.GetExeCtx).Return(&execute.Context{
|
||||
RootCtx: execute.RootCtx{
|
||||
ExeCfg: vo.ExecuteConfig{
|
||||
Mode: vo.ExecuteModeDebug,
|
||||
Operator: 123,
|
||||
BizType: vo.BizTypeWorkflow,
|
||||
},
|
||||
},
|
||||
}).Build().UnPatch()
|
||||
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
t.Run("string case", func(t *testing.T) {
|
||||
t.Run("single", func(t *testing.T) {
|
||||
objects := make([]database.Object, 0)
|
||||
objects = append(objects, database.Object{
|
||||
"v1": "1",
|
||||
"v2": int64(2),
|
||||
})
|
||||
|
||||
cfg := &QueryConfig{
|
||||
DatabaseInfoID: 111,
|
||||
ClauseGroup: &database.ClauseGroup{
|
||||
Single: &database.Clause{
|
||||
Left: "v1",
|
||||
Operator: database.OperatorLike,
|
||||
},
|
||||
},
|
||||
OrderClauses: []*database.OrderClause{{FieldID: "v1", IsAsc: false}},
|
||||
QueryFields: []string{"v1", "v2"},
|
||||
OutputConfig: map[string]*vo.TypeInfo{
|
||||
"outputList": {Type: vo.DataTypeArray, ElemTypeInfo: &vo.TypeInfo{
|
||||
Type: vo.DataTypeObject,
|
||||
Properties: map[string]*vo.TypeInfo{
|
||||
"v1": {Type: vo.DataTypeString},
|
||||
"v2": {Type: vo.DataTypeString},
|
||||
},
|
||||
}},
|
||||
"rowNum": {Type: vo.DataTypeInteger},
|
||||
},
|
||||
}
|
||||
|
||||
mockQuery := &mockDsSelect{objects: objects, t: t, validate: func(request *database.QueryRequest) {
|
||||
if request.DatabaseInfoID != cfg.DatabaseInfoID {
|
||||
t.Fatal("database id should be equal")
|
||||
}
|
||||
cGroup := request.ConditionGroup
|
||||
assert.Equal(t, cGroup.Conditions[0].Left, cfg.ClauseGroup.Single.Left)
|
||||
assert.Equal(t, cGroup.Conditions[0].Operator, cfg.ClauseGroup.Single.Operator)
|
||||
|
||||
}}
|
||||
mockDatabaseOperator := databasemock.NewMockDatabaseOperator(ctrl)
|
||||
mockDatabaseOperator.EXPECT().Query(gomock.Any(), gomock.Any()).DoAndReturn(mockQuery.Query())
|
||||
|
||||
cfg.Op = mockDatabaseOperator
|
||||
|
||||
ds := Query{
|
||||
config: cfg,
|
||||
}
|
||||
|
||||
in := map[string]interface{}{
|
||||
"__condition_right_0": 1,
|
||||
}
|
||||
|
||||
result, err := ds.Query(t.Context(), in)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "1", result["outputList"].([]any)[0].(database.Object)["v1"])
|
||||
assert.Equal(t, "2", result["outputList"].([]any)[0].(database.Object)["v2"])
|
||||
})
|
||||
|
||||
t.Run("multi", func(t *testing.T) {
|
||||
cfg := &QueryConfig{
|
||||
DatabaseInfoID: 111,
|
||||
ClauseGroup: &database.ClauseGroup{
|
||||
Multi: &database.MultiClause{
|
||||
Relation: database.ClauseRelationOR,
|
||||
Clauses: []*database.Clause{
|
||||
{Left: "v1", Operator: database.OperatorLike},
|
||||
{Left: "v2", Operator: database.OperatorLike},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
OrderClauses: []*database.OrderClause{{FieldID: "v1", IsAsc: false}},
|
||||
QueryFields: []string{"v1", "v2"},
|
||||
|
||||
OutputConfig: map[string]*vo.TypeInfo{
|
||||
"outputList": {Type: vo.DataTypeArray, ElemTypeInfo: &vo.TypeInfo{
|
||||
Type: vo.DataTypeObject,
|
||||
Properties: map[string]*vo.TypeInfo{
|
||||
"v1": {Type: vo.DataTypeString},
|
||||
"v2": {Type: vo.DataTypeString},
|
||||
},
|
||||
}},
|
||||
"rowNum": {Type: vo.DataTypeInteger},
|
||||
},
|
||||
}
|
||||
|
||||
objects := make([]database.Object, 0)
|
||||
objects = append(objects, database.Object{
|
||||
"v1": "1",
|
||||
"v2": int64(2),
|
||||
})
|
||||
|
||||
mockQuery := &mockDsSelect{objects: objects, t: t, validate: func(request *database.QueryRequest) {
|
||||
if request.DatabaseInfoID != cfg.DatabaseInfoID {
|
||||
t.Fatal("database id should be equal")
|
||||
|
||||
}
|
||||
cGroup := request.ConditionGroup
|
||||
assert.Equal(t, cGroup.Conditions[0].Right, 1)
|
||||
assert.Equal(t, cGroup.Conditions[1].Right, 2)
|
||||
assert.Equal(t, cGroup.Relation, cfg.ClauseGroup.Multi.Relation)
|
||||
|
||||
}}
|
||||
mockDatabaseOperator := databasemock.NewMockDatabaseOperator(ctrl)
|
||||
mockDatabaseOperator.EXPECT().Query(gomock.Any(), gomock.Any()).DoAndReturn(mockQuery.Query()).AnyTimes()
|
||||
|
||||
cfg.Op = mockDatabaseOperator
|
||||
|
||||
ds := Query{
|
||||
config: cfg,
|
||||
}
|
||||
|
||||
in := map[string]any{
|
||||
"__condition_right_0": 1,
|
||||
"__condition_right_1": 2,
|
||||
}
|
||||
|
||||
result, err := ds.Query(t.Context(), in)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "1", result["outputList"].([]any)[0].(database.Object)["v1"])
|
||||
assert.Equal(t, "2", result["outputList"].([]any)[0].(database.Object)["v2"])
|
||||
})
|
||||
|
||||
t.Run("formated error", func(t *testing.T) {
|
||||
cfg := &QueryConfig{
|
||||
DatabaseInfoID: 111,
|
||||
ClauseGroup: &database.ClauseGroup{
|
||||
Single: &database.Clause{
|
||||
Left: "v1",
|
||||
Operator: database.OperatorLike,
|
||||
},
|
||||
},
|
||||
OrderClauses: []*database.OrderClause{{FieldID: "v1", IsAsc: false}},
|
||||
QueryFields: []string{"v1", "v2"},
|
||||
|
||||
OutputConfig: map[string]*vo.TypeInfo{
|
||||
"outputList": {Type: vo.DataTypeArray, ElemTypeInfo: &vo.TypeInfo{
|
||||
Type: vo.DataTypeObject,
|
||||
Properties: map[string]*vo.TypeInfo{
|
||||
"v1": {Type: vo.DataTypeInteger},
|
||||
"v2": {Type: vo.DataTypeInteger},
|
||||
},
|
||||
}},
|
||||
"rowNum": {Type: vo.DataTypeInteger},
|
||||
},
|
||||
}
|
||||
objects := make([]database.Object, 0)
|
||||
objects = append(objects, database.Object{
|
||||
"v1": "abc",
|
||||
"v2": int64(2),
|
||||
})
|
||||
|
||||
mockQuery := &mockDsSelect{objects: objects, t: t, validate: func(request *database.QueryRequest) {
|
||||
if request.DatabaseInfoID != cfg.DatabaseInfoID {
|
||||
t.Fatal("database id should be equal")
|
||||
|
||||
}
|
||||
cGroup := request.ConditionGroup
|
||||
assert.Equal(t, cGroup.Conditions[0].Left, cfg.ClauseGroup.Single.Left)
|
||||
assert.Equal(t, cGroup.Conditions[0].Operator, cfg.ClauseGroup.Single.Operator)
|
||||
|
||||
}}
|
||||
mockDatabaseOperator := databasemock.NewMockDatabaseOperator(ctrl)
|
||||
mockDatabaseOperator.EXPECT().Query(gomock.Any(), gomock.Any()).DoAndReturn(mockQuery.Query()).AnyTimes()
|
||||
|
||||
cfg.Op = mockDatabaseOperator
|
||||
|
||||
ds := Query{
|
||||
config: cfg,
|
||||
}
|
||||
|
||||
in := map[string]any{
|
||||
"__condition_right_0": 1,
|
||||
}
|
||||
|
||||
result, err := ds.Query(t.Context(), in)
|
||||
assert.NoError(t, err)
|
||||
fmt.Println(result)
|
||||
assert.Equal(t, map[string]any{
|
||||
"v1": nil,
|
||||
"v2": int64(2),
|
||||
}, result["outputList"].([]any)[0])
|
||||
|
||||
})
|
||||
|
||||
t.Run("redundancy return field", func(t *testing.T) {
|
||||
cfg := &QueryConfig{
|
||||
DatabaseInfoID: 111,
|
||||
ClauseGroup: &database.ClauseGroup{
|
||||
Single: &database.Clause{
|
||||
Left: "v1",
|
||||
Operator: database.OperatorLike,
|
||||
},
|
||||
},
|
||||
OrderClauses: []*database.OrderClause{{FieldID: "v1", IsAsc: false}},
|
||||
QueryFields: []string{"v1", "v2"},
|
||||
|
||||
OutputConfig: map[string]*vo.TypeInfo{
|
||||
"outputList": {Type: vo.DataTypeArray, ElemTypeInfo: &vo.TypeInfo{
|
||||
Type: vo.DataTypeObject,
|
||||
Properties: map[string]*vo.TypeInfo{
|
||||
"v1": {Type: vo.DataTypeInteger},
|
||||
"v2": {Type: vo.DataTypeInteger},
|
||||
"v3": {Type: vo.DataTypeInteger},
|
||||
},
|
||||
}},
|
||||
"rowNum": {Type: vo.DataTypeInteger},
|
||||
},
|
||||
}
|
||||
objects := make([]database.Object, 0)
|
||||
objects = append(objects, database.Object{
|
||||
"v1": "1",
|
||||
"v2": int64(2),
|
||||
})
|
||||
mockQuery := &mockDsSelect{objects: objects, t: t, validate: func(request *database.QueryRequest) {
|
||||
if request.DatabaseInfoID != cfg.DatabaseInfoID {
|
||||
t.Fatal("database id should be equal")
|
||||
}
|
||||
cGroup := request.ConditionGroup
|
||||
assert.Equal(t, cGroup.Conditions[0].Left, cfg.ClauseGroup.Single.Left)
|
||||
assert.Equal(t, cGroup.Conditions[0].Operator, cfg.ClauseGroup.Single.Operator)
|
||||
}}
|
||||
mockDatabaseOperator := databasemock.NewMockDatabaseOperator(ctrl)
|
||||
mockDatabaseOperator.EXPECT().Query(gomock.Any(), gomock.Any()).DoAndReturn(mockQuery.Query()).AnyTimes()
|
||||
|
||||
cfg.Op = mockDatabaseOperator
|
||||
|
||||
ds := Query{
|
||||
config: cfg,
|
||||
}
|
||||
|
||||
in := map[string]any{"__condition_right_0": 1}
|
||||
|
||||
result, err := ds.Query(t.Context(), in)
|
||||
assert.NoError(t, err)
|
||||
fmt.Println(result)
|
||||
assert.Equal(t, int64(1), result["outputList"].([]any)[0].(database.Object)["v1"])
|
||||
assert.Equal(t, int64(2), result["outputList"].([]any)[0].(database.Object)["v2"])
|
||||
assert.Equal(t, nil, result["outputList"].([]any)[0].(database.Object)["v3"])
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
t.Run("other case", func(t *testing.T) {
|
||||
|
||||
cfg := &QueryConfig{
|
||||
DatabaseInfoID: 111,
|
||||
ClauseGroup: &database.ClauseGroup{
|
||||
Single: &database.Clause{
|
||||
Left: "v1",
|
||||
Operator: database.OperatorLike,
|
||||
},
|
||||
},
|
||||
OrderClauses: []*database.OrderClause{{FieldID: "v1", IsAsc: false}},
|
||||
QueryFields: []string{"v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"},
|
||||
|
||||
OutputConfig: map[string]*vo.TypeInfo{
|
||||
"outputList": {Type: vo.DataTypeArray,
|
||||
ElemTypeInfo: &vo.TypeInfo{Type: vo.DataTypeObject, Properties: map[string]*vo.TypeInfo{
|
||||
"v1": {Type: vo.DataTypeInteger},
|
||||
"v2": {Type: vo.DataTypeNumber},
|
||||
"v3": {Type: vo.DataTypeBoolean},
|
||||
"v4": {Type: vo.DataTypeBoolean},
|
||||
"v5": {Type: vo.DataTypeTime},
|
||||
"v6": {Type: vo.DataTypeArray, ElemTypeInfo: &vo.TypeInfo{Type: vo.DataTypeInteger}},
|
||||
"v7": {Type: vo.DataTypeArray, ElemTypeInfo: &vo.TypeInfo{Type: vo.DataTypeBoolean}},
|
||||
"v8": {Type: vo.DataTypeArray, ElemTypeInfo: &vo.TypeInfo{Type: vo.DataTypeNumber}},
|
||||
},
|
||||
}},
|
||||
"rowNum": {Type: vo.DataTypeInteger},
|
||||
},
|
||||
}
|
||||
|
||||
objects := make([]database.Object, 0)
|
||||
objects = append(objects, database.Object{
|
||||
"v1": "1",
|
||||
"v2": "2.1",
|
||||
"v3": int64(0),
|
||||
"v4": "true",
|
||||
"v5": "2020-02-20T10:10:10",
|
||||
"v6": `["1","2","3"]`,
|
||||
"v7": `[false,true,"true"]`,
|
||||
"v8": `["1.2",2.1, 3.9]`,
|
||||
})
|
||||
|
||||
mockQuery := &mockDsSelect{objects: objects, t: t, validate: func(request *database.QueryRequest) {
|
||||
if request.DatabaseInfoID != cfg.DatabaseInfoID {
|
||||
t.Fatal("database id should be equal")
|
||||
}
|
||||
cGroup := request.ConditionGroup
|
||||
assert.Equal(t, cGroup.Conditions[0].Left, cfg.ClauseGroup.Single.Left)
|
||||
assert.Equal(t, cGroup.Conditions[0].Operator, cfg.ClauseGroup.Single.Operator)
|
||||
|
||||
}}
|
||||
mockDatabaseOperator := databasemock.NewMockDatabaseOperator(ctrl)
|
||||
mockDatabaseOperator.EXPECT().Query(gomock.Any(), gomock.Any()).DoAndReturn(mockQuery.Query()).AnyTimes()
|
||||
|
||||
cfg.Op = mockDatabaseOperator
|
||||
|
||||
ds := Query{
|
||||
config: cfg,
|
||||
}
|
||||
|
||||
in := map[string]any{
|
||||
"__condition_right_0": 1,
|
||||
}
|
||||
|
||||
result, err := ds.Query(t.Context(), in)
|
||||
assert.NoError(t, err)
|
||||
object := result["outputList"].([]any)[0].(database.Object)
|
||||
|
||||
assert.Equal(t, int64(1), object["v1"])
|
||||
assert.Equal(t, 2.1, object["v2"])
|
||||
assert.Equal(t, false, object["v3"])
|
||||
assert.Equal(t, true, object["v4"])
|
||||
assert.Equal(t, "2020-02-20T10:10:10", object["v5"])
|
||||
assert.Equal(t, []any{int64(1), int64(2), int64(3)}, object["v6"])
|
||||
assert.Equal(t, []any{false, true, true}, object["v7"])
|
||||
assert.Equal(t, []any{1.2, 2.1, 3.9}, object["v8"])
|
||||
|
||||
})
|
||||
|
||||
t.Run("config output list is nil", func(t *testing.T) {
|
||||
|
||||
cfg := &QueryConfig{
|
||||
DatabaseInfoID: 111,
|
||||
ClauseGroup: &database.ClauseGroup{
|
||||
Single: &database.Clause{
|
||||
Left: "v1",
|
||||
Operator: database.OperatorLike,
|
||||
},
|
||||
},
|
||||
OrderClauses: []*database.OrderClause{{FieldID: "v1", IsAsc: false}},
|
||||
QueryFields: []string{"v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"},
|
||||
OutputConfig: map[string]*vo.TypeInfo{
|
||||
"outputList": {Type: vo.DataTypeArray, ElemTypeInfo: &vo.TypeInfo{Type: vo.DataTypeObject, Properties: map[string]*vo.TypeInfo{}}},
|
||||
"rowNum": {Type: vo.DataTypeInteger},
|
||||
},
|
||||
}
|
||||
|
||||
objects := make([]database.Object, 0)
|
||||
objects = append(objects, database.Object{
|
||||
"v1": int64(1),
|
||||
"v2": "2.1",
|
||||
"v3": int64(0),
|
||||
"v4": "true",
|
||||
"v5": "2020-02-20T10:10:10",
|
||||
"v6": `["1","2","3"]`,
|
||||
"v7": `[false,true,"true"]`,
|
||||
"v8": `["1.2",2.1, 3.9]`,
|
||||
})
|
||||
mockQuery := &mockDsSelect{objects: objects, t: t, validate: func(request *database.QueryRequest) {
|
||||
if request.DatabaseInfoID != cfg.DatabaseInfoID {
|
||||
t.Fatal("database id should be equal")
|
||||
}
|
||||
cGroup := request.ConditionGroup
|
||||
assert.Equal(t, cGroup.Conditions[0].Left, cfg.ClauseGroup.Single.Left)
|
||||
assert.Equal(t, cGroup.Conditions[0].Operator, cfg.ClauseGroup.Single.Operator)
|
||||
|
||||
}}
|
||||
mockDatabaseOperator := databasemock.NewMockDatabaseOperator(ctrl)
|
||||
mockDatabaseOperator.EXPECT().Query(gomock.Any(), gomock.Any()).DoAndReturn(mockQuery.Query()).AnyTimes()
|
||||
|
||||
cfg.Op = mockDatabaseOperator
|
||||
ds := Query{
|
||||
config: cfg,
|
||||
}
|
||||
|
||||
in := map[string]any{
|
||||
"__condition_right_0": 1,
|
||||
}
|
||||
|
||||
result, err := ds.Query(t.Context(), in)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, result["outputList"].([]any)[0].(database.Object), database.Object{
|
||||
"v1": "1",
|
||||
"v2": "2.1",
|
||||
"v3": "0",
|
||||
"v4": "true",
|
||||
"v5": "2020-02-20T10:10:10",
|
||||
"v6": `["1","2","3"]`,
|
||||
"v7": `[false,true,"true"]`,
|
||||
"v8": `["1.2",2.1, 3.9]`,
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
133
backend/domain/workflow/internal/nodes/database/update.go
Normal file
133
backend/domain/workflow/internal/nodes/database/update.go
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/database"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
)
|
||||
|
||||
type UpdateConfig struct {
|
||||
DatabaseInfoID int64
|
||||
ClauseGroup *database.ClauseGroup
|
||||
OutputConfig map[string]*vo.TypeInfo
|
||||
Updater database.DatabaseOperator
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
config *UpdateConfig
|
||||
}
|
||||
type UpdateInventory struct {
|
||||
ConditionGroup *database.ConditionGroup
|
||||
Fields map[string]any
|
||||
}
|
||||
|
||||
func NewUpdate(_ context.Context, cfg *UpdateConfig) (*Update, error) {
|
||||
if cfg == nil {
|
||||
return nil, errors.New("config is required")
|
||||
}
|
||||
if cfg.DatabaseInfoID == 0 {
|
||||
return nil, errors.New("database info id is required and greater than 0")
|
||||
}
|
||||
|
||||
if cfg.ClauseGroup == nil {
|
||||
return nil, errors.New("clause group is required and greater than 0")
|
||||
}
|
||||
|
||||
if cfg.Updater == nil {
|
||||
return nil, errors.New("updater is required")
|
||||
}
|
||||
|
||||
return &Update{config: cfg}, nil
|
||||
}
|
||||
|
||||
func (u *Update) Update(ctx context.Context, in map[string]any) (map[string]any, error) {
|
||||
inventory, err := convertClauseGroupToUpdateInventory(ctx, u.config.ClauseGroup, in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fields := make(map[string]any)
|
||||
|
||||
for key, value := range inventory.Fields {
|
||||
fields[key] = value
|
||||
}
|
||||
|
||||
req := &database.UpdateRequest{
|
||||
DatabaseInfoID: u.config.DatabaseInfoID,
|
||||
ConditionGroup: inventory.ConditionGroup,
|
||||
Fields: fields,
|
||||
IsDebugRun: isDebugExecute(ctx),
|
||||
UserID: getExecUserID(ctx),
|
||||
}
|
||||
|
||||
response, err := u.config.Updater.Update(ctx, req)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret, err := responseFormatted(u.config.OutputConfig, response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (u *Update) ToCallbackInput(_ context.Context, in map[string]any) (map[string]any, error) {
|
||||
inventory, err := convertClauseGroupToUpdateInventory(context.Background(), u.config.ClauseGroup, in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return u.toDatabaseUpdateCallbackInput(inventory)
|
||||
}
|
||||
|
||||
func (u *Update) toDatabaseUpdateCallbackInput(inventory *UpdateInventory) (map[string]any, error) {
|
||||
databaseID := u.config.DatabaseInfoID
|
||||
result := make(map[string]any)
|
||||
result["databaseInfoList"] = []string{fmt.Sprintf("%d", databaseID)}
|
||||
result["updateParam"] = map[string]any{}
|
||||
|
||||
condition, err := convertToCondition(inventory.ConditionGroup)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
type FieldInfo struct {
|
||||
fieldID string
|
||||
fieldValue any
|
||||
}
|
||||
|
||||
fieldInfo := make([]FieldInfo, 0)
|
||||
for k, v := range inventory.Fields {
|
||||
fieldInfo = append(fieldInfo, FieldInfo{
|
||||
fieldID: k,
|
||||
fieldValue: v,
|
||||
})
|
||||
}
|
||||
|
||||
result["updateParam"] = map[string]any{
|
||||
"condition": condition,
|
||||
"fieldInfo": fieldInfo,
|
||||
}
|
||||
return result, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user