-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery.go
More file actions
162 lines (144 loc) · 4.1 KB
/
Copy pathquery.go
File metadata and controls
162 lines (144 loc) · 4.1 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
package libQuery
import (
"database/sql"
"errors"
"net/http"
"slices"
"github.com/hmmftg/requestCore/libError"
"github.com/hmmftg/requestCore/status"
)
// CommandInterface defines the interface for query commands that provide SQL and arguments.
type CommandInterface interface {
GetCommand(DBMode) string
GetArgs() []any
GetType() int
}
// GetQuery executes a SQL query and returns the result as a slice of the target type.
func GetQuery[R any](query string, core QueryRunnerInterface, args ...any) ([]R, error) {
//Query
rows, err := QueryToStruct[R](core, query, args...)
if err != nil {
return nil, errors.Join(
err,
libError.NewWithDescription(status.InternalServerError, DBReadError, "unable to execute query"),
)
}
if len(rows) == 0 {
return nil, libError.NewWithDescription(
http.StatusBadRequest,
NoDataFound,
"no data found: %s,%v", query, args,
)
}
return rows, nil
}
// QueryToStruct executes a SQL query and scans the results into a slice of the target type.
func QueryToStruct[Target any](q QueryRunnerInterface, querySQL string, args ...any) ([]Target, error) {
stmt, err := q.NewStatement(querySQL)
if err != nil {
return nil, errors.Join(err,
libError.NewWithDescription(
status.InternalServerError,
"UNABLE_TO_INITIALIZE_STATEMENT",
"queryRunner[prepare](%s,%v)", querySQL, args,
))
}
defer func() { _ = stmt.Close() }()
rows, err := stmt.Query(args...)
if err != nil {
return nil, errors.Join(err,
libError.NewWithDescription(
status.InternalServerError,
"UNABLE_TO_QUERY_STATEMENT",
"queryRunner[query](%s,%v)", querySQL, args,
))
}
defer func() { _ = rows.Close() }()
columnTypes, err := rows.ColumnTypes()
if err != nil {
return nil, errors.Join(err,
libError.NewWithDescription(
status.InternalServerError,
"UNABLE_TO_GET_COLUMN_TYPES",
"queryRunner[ColumnTypes](%s,%v)", querySQL, args,
))
}
count := len(columnTypes)
baseArgs := make([]any, count)
for i := range columnTypes {
baseArgs[i] = new(sql.Null[any])
}
finalRows := make([]Target, 0)
for rows.Next() {
scanArgs := slices.Clone(baseArgs)
err := rows.Scan(scanArgs...)
if err != nil {
return nil, errors.Join(err,
libError.NewWithDescription(
status.InternalServerError,
"UNABLE_TO_GET_SCAN_ROW",
"queryRunner[Scan](%s,%v)", querySQL, scanArgs,
))
}
masterData := map[string]any{}
for i, v := range columnTypes {
masterData[v.Name()] = scanArgs[i].(*sql.Null[any]).V
}
parsed, err := ParseMap[Target](masterData)
if parsed == nil {
return nil, errors.Join(err,
libError.NewWithDescription(
status.InternalServerError,
"UNABLE_TO_GET_SCAN_ROW",
"queryRunner[parse](%s,%v)", querySQL, masterData,
))
}
finalRows = append(finalRows, *parsed)
}
if err := rows.Err(); err != nil {
return nil, errors.Join(err,
libError.NewWithDescription(
status.InternalServerError,
"UNABLE_TO_QUERY_STATEMENT",
"queryRunner[rows.Err](%s,%v)", querySQL, args,
))
}
//resp, _ := json.Marshal(finalRows)
return finalRows, nil
}
// Query executes a command interface query and returns results based on the command type.
func Query[R any](command CommandInterface, core QueryRunnerInterface, args ...any) ([]R, error) {
if command.GetType() == int(QueryMap) {
return nil, libError.NewWithDescription(status.BadRequest, DBReadError, "unsupported command type")
}
query := command.GetCommand(core.GetDbMode())
//Query
rows, err := QueryToStruct[R](core, query, args...)
if err != nil {
return nil, errors.Join(
err,
libError.NewWithDescription(status.InternalServerError, DBReadError, "unable to execute query"),
)
}
switch command.GetType() {
case int(QuerySingle):
if len(rows) == 0 {
return nil, libError.NewWithDescription(
http.StatusBadRequest,
NoDataFound,
"no data found: %s,%v", query, args,
)
}
if len(rows) > 1 {
return nil, libError.NewWithDescription(
http.StatusBadRequest,
DuplicateFound,
"duplicate data found: %s,%v,%v", query, args, rows,
)
}
return rows, nil
case int(QueryAll):
return rows, nil
}
return nil, nil
}