-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.go
More file actions
176 lines (164 loc) · 5.54 KB
/
Copy pathexec.go
File metadata and controls
176 lines (164 loc) · 5.54 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
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"
)
func (m QueryRunnerModel) SetVariableCommand() string {
return m.SetVariable
}
const (
ERROR_CALLING_DB_FUNCTION = "ERROR_CALLING_DB_FUNCTION"
)
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, ERROR_CALLING_DB_FUNCTION, libError.Join(err, "CallDbFunction[Exec](%s,%v)", callString, args)
}
return 0, "OK", nil
}
const (
QueryCheckNotExists DmlCommandType = iota
QueryCheckExists
Insert
Update
Delete
)
func (command DmlCommand) Execute(core QueryRunnerInterface, moduleName, methodName string) (any, error) {
return command.ExecuteWithContext(nil, context.Background(), moduleName, methodName, core)
}
func GetDmlResult(resultDb sql.Result, rows map[string]string) DmlResult {
resp := DmlResult{
Rows: rows,
}
resp.LastInsertId, _ = resultDb.LastInsertId()
resp.RowsAffected, _ = resultDb.RowsAffected()
return resp
}
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
}
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
}
func (command DmlCommand) ExecuteWithContext(parser webFramework.RequestParser, w context.Context, 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 == NO_DATA_FOUND {
return nil, errors.Join(err, fmt.Errorf("checkExists: %s=> %s", command.Name, NO_DATA_FOUND))
}
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 == NO_DATA_FOUND {
return nil, nil
}
return nil, errors.Join(err, fmt.Errorf("CheckNotExists: %s=> failed", command.Name))
}
return nil, response.ToError(DUPLICATE_FOUND, DUPLICATE_FOUND_DESC, fmt.Errorf("CheckNotExists: %s=> %s", command.Name, DUPLICATE_FOUND))
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
}