-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
152 lines (130 loc) · 3.68 KB
/
model.go
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
package libQuery
import (
"context"
"database/sql"
"database/sql/driver"
"github.com/hmmftg/requestCore/response"
)
type QueryRunnerModel struct {
DB *sql.DB
ProgramName string
ModuleName string
SetVariable string
Mode DBMode
}
//go:generate enumer -type=DBMode -json -output dbModeEnum.go
type DBMode int
const (
Oracle DBMode = iota
Postgres
MockDB
)
func Init(
DB *sql.DB,
ProgramName string,
ModuleName string,
mode DBMode) QueryRunnerModel {
model := QueryRunnerModel{
DB: DB,
ProgramName: ProgramName,
ModuleName: ModuleName,
}
switch mode {
case Oracle:
model.SetVariable = OracleSetVariableCommand
case Postgres:
model.SetVariable = PostgresSetVariableCommand
default:
model.SetVariable = "none"
}
return model
}
type QueryRunnerInterface interface {
QueryRunner(querySql string, args ...any) (int, []map[string]any, error)
QueryToStruct(querySql string, target any, args ...any) (int, any, error)
CallDbFunction(callString string, args ...any) (int, string, error)
GetModule() (string, string)
InsertRow(insert string, args ...any) (sql.Result, error)
Dml(ctx context.Context, moduleName, methodName, command string, args ...any) (sql.Result, error)
SetVariableCommand() string
//Used in mock db for test
Close()
}
type DmlModel interface {
PreControlCommands() map[string][]DmlCommand
DmlCommands() map[string][]DmlCommand
FinalizeCommands() map[string][]DmlCommand
}
type Updatable interface {
SetParams(args map[string]string) any
GetUniqueId() []any
GetCountCommand() string
GetUpdateCommand() (string, []any)
Finalize(QueryRunnerInterface) (string, error)
}
//go:generate enumer -type=DmlCommandType -json -output dmlEnum.go
type DmlCommandType int
type DmlCommand struct {
Name string
Command string
Args []any
Type DmlCommandType
CustomError response.ErrorState
}
//go:generate enumer -type=QueryCommandType -json -output queryEnum.go
type QueryCommandType int
type QueryCommand struct {
Name string
Command string
Type QueryCommandType
Args []string
}
func (q QueryCommand) GetDriverArgs(req any) []driver.Value {
args := []driver.Value{}
for id := range q.Args {
_, val, err := GetFormTagValue(q.Args[id], req)
if err != nil {
return nil
}
args = append(args, val)
}
return args
}
type QueryRequest interface {
QueryArgs() map[string][]any
}
type QueryResult interface {
GetID() string
GetValue() any
}
type QueryWithDeps interface {
GetFillable(core QueryRunnerInterface) (map[string]any, error)
}
type DmlResult struct {
Rows map[string]string `json:"rows" form:"rows"`
LastInsertId int64 `json:"lastId" form:"lastId"`
RowsAffected int64 `json:"rowsAffected" form:"rowsAffected"`
}
type QueryData struct {
DataRaw string `json:"result,omitempty" db:"result"`
Key string `json:"key,omitempty" db:"key"`
Value string `json:"value,omitempty" db:"value"`
ValueArray []string `json:"valueArray,omitempty" db:"values"`
MapList string `json:"mapList,omitempty" db:"map_list"`
}
type RecordDataGet interface {
GetId() string
GetControlId(string) string
GetIdList() []any
GetSubCategory() string
GetValue() any
}
type RecordDataDml interface {
SetId(string)
CheckDuplicate(core QueryRunnerInterface) (int, string, error)
Filler(headers map[string][]string, core QueryRunnerInterface, args ...any) (string, error)
Post(core QueryRunnerInterface, args map[string]string) (DmlResult, int, string, error)
CheckExistence(core QueryRunnerInterface) (int, string, error)
PreControl(core QueryRunnerInterface) (int, string, error)
Put(core QueryRunnerInterface, args map[string]string) (DmlResult, int, string, error)
}