-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.go
More file actions
189 lines (177 loc) · 6.52 KB
/
Copy pathexec.go
File metadata and controls
189 lines (177 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package libQuery
import (
"context"
"database/sql"
"errors"
"fmt"
"log/slog"
"strings"
"github.com/hmmftg/requestCore/libError"
"github.com/hmmftg/requestCore/response"
"github.com/hmmftg/requestCore/webFramework"
)
// SetVariableCommand returns the database-specific command for setting session variables.
func (m QueryRunnerModel) SetVariableCommand() string {
return m.SetVariable
}
const (
// ErrorCallingDBFunction is the error description for database function call failures.
ErrorCallingDBFunction = "ERROR_CALLING_DB_FUNCTION"
)
// CallDbFunction executes a database function with the given call string and arguments.
func (m QueryRunnerModel) CallDbFunction(callString string, args ...any) (int, string, error) {
errPing := m.DB.Ping()
if errPing != nil {
slog.Error("error in ping", slog.Any("error", errPing))
}
_, err := m.DB.Exec(callString, args...)
if err != nil {
return -3, ErrorCallingDBFunction, libError.Join(err, "CallDbFunction[Exec](%s,%v)", callString, args)
}
return 0, "OK", nil
}
const (
// QueryCheckNotExists is the DML command type for verifying a record does not exist.
QueryCheckNotExists DmlCommandType = iota
// QueryCheckExists is the DML command type for verifying a record exists.
QueryCheckExists
// Insert is the DML command type for insert operations.
Insert
// Update is the DML command type for update operations.
Update
// Delete is the DML command type for delete operations.
Delete
)
// Execute runs the DML command using a background context.
func (command DmlCommand) Execute(core QueryRunnerInterface, moduleName, methodName string) (any, error) {
return command.ExecuteWithContext(context.Background(), nil, moduleName, methodName, core)
}
// GetDmlResult builds a DmlResult from a SQL result and output parameter rows.
func GetDmlResult(resultDb sql.Result, rows map[string]string) DmlResult {
resp := DmlResult{
Rows: rows,
}
resp.LastInsertId, _ = resultDb.LastInsertId()
resp.RowsAffected, _ = resultDb.RowsAffected()
return resp
}
// GetLocalArgs resolves named arguments that reference parser local storage.
func GetLocalArgs(parser webFramework.RequestParser, args []any) []any {
result := make([]any, len(args))
for id, arg := range args {
namedArg, ok := arg.(sql.NamedArg)
if ok {
stringArg, ok := namedArg.Value.(string)
if ok && strings.HasPrefix(stringArg, "w.local:") {
parts := strings.Split(stringArg, ":")
namedArg.Value = parser.GetLocal(parts[1])
}
result[id] = namedArg
} else {
result[id] = arg
}
}
return result
}
// GetOutArgs extracts output parameter values from DML arguments and stores them in parser locals.
func GetOutArgs(parser webFramework.RequestParser, args ...any) map[string]string {
rows := map[string]string{}
for id, arg := range args {
switch dbParameter := arg.(type) {
case sql.NamedArg:
switch namedParameter := dbParameter.Value.(type) {
case sql.Out:
if namedParameter.Dest != nil {
switch outValue := namedParameter.Dest.(type) {
case string:
rows[dbParameter.Name] = outValue
case *string:
rows[dbParameter.Name] = *outValue
case int64:
rows[dbParameter.Name] = fmt.Sprintf("%d", outValue)
case *int64:
rows[dbParameter.Name] = fmt.Sprintf("%d", *outValue)
default:
slog.Error("wrong db-out parameter type", slog.Any("type", fmt.Sprintf("%T", namedParameter.Dest)))
}
parser.SetLocal(dbParameter.Name, rows[dbParameter.Name])
}
}
case sql.Out:
if dbParameter.Dest != nil {
name := fmt.Sprintf("not named arg %d", id)
switch outValue := dbParameter.Dest.(type) {
case string:
rows[name] = outValue
case *string:
rows[name] = *outValue
case int64:
rows[name] = fmt.Sprintf("%d", outValue)
case *int64:
rows[name] = fmt.Sprintf("%d", *outValue)
default:
slog.Error("wrong db-out parameter type", slog.Any("type", fmt.Sprintf("%T", dbParameter.Dest)))
}
parser.SetLocal(name, rows[name])
}
}
}
return rows
}
// ExecuteWithContext runs the DML command with the given context, parser, and query runner.
func (command DmlCommand) ExecuteWithContext(w context.Context, parser webFramework.RequestParser, moduleName, methodName string, core QueryRunnerInterface) (any, error) {
switch command.Type {
case QueryCheckExists:
result, err := Query[QueryData](command, core, GetLocalArgs(parser, command.Args)...)
if err != nil {
if command.CustomError != nil {
return nil, command.CustomError
}
if ok, err := libError.Unwrap(err); ok && err.Action().Description == NoDataFound {
return nil, errors.Join(err, fmt.Errorf("checkExists: %s=> %s", command.Name, NoDataFound))
}
return nil, errors.Join(err, fmt.Errorf("checkExists: %s=> failed", command.Name))
}
return result, nil
case QueryCheckNotExists:
_, err := Query[QueryData](command, core, GetLocalArgs(parser, command.Args)...)
if err != nil {
if ok, err := libError.Unwrap(err); ok && err.Action().Description == NoDataFound {
return nil, nil
}
return nil, errors.Join(err, fmt.Errorf("CheckNotExists: %s=> failed", command.Name))
}
return nil, response.ToError(DuplicateFound, DuplicateFoundDesc, fmt.Errorf("CheckNotExists: %s=> %s", command.Name, DuplicateFound))
case Insert:
resp, err := core.Dml(w, moduleName, methodName, command.GetCommand(core.GetDbMode()), GetLocalArgs(parser, command.Args)...)
if err != nil {
if command.CustomError != nil {
return nil, command.CustomError
}
return nil, response.ToError(ErrorExecuteDML, nil, libError.Join(err, "%s: %s", command.Type, command.Name))
}
outValues := GetOutArgs(parser, command.Args...)
return GetDmlResult(resp, outValues), nil
case Update:
resp, err := core.Dml(w, moduleName, methodName, command.GetCommand(core.GetDbMode()), GetLocalArgs(parser, command.Args)...)
if err != nil {
if command.CustomError != nil {
return nil, command.CustomError
}
return nil, response.ToError(ErrorExecuteDML, nil, libError.Join(err, "%s: %s", command.Type, command.Name))
}
outValues := GetOutArgs(parser, command.Args...)
return GetDmlResult(resp, outValues), nil
case Delete:
resp, err := core.Dml(w, moduleName, methodName, command.GetCommand(core.GetDbMode()), GetLocalArgs(parser, command.Args)...)
if err != nil {
if command.CustomError != nil {
return nil, command.CustomError
}
return nil, response.ToError(ErrorExecuteDML, nil, libError.Join(err, "%s: %s", command.Type, command.Name))
}
outValues := GetOutArgs(parser, command.Args...)
return GetDmlResult(resp, outValues), nil
}
return nil, nil
}