Skip to content

Commit 2c88d44

Browse files
committed
Refactor code
1 parent c9326dd commit 2c88d44

File tree

5 files changed

+210
-40
lines changed

5 files changed

+210
-40
lines changed

code.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,24 @@ type DynamicSqlLoader struct {
6363
type Query struct {
6464
DB *sql.DB
6565
Select string
66+
Get string
6667
ParameterCount int
68+
Build func(i int) string
6769
Map func(col string) string
6870
driver string
6971
colMap map[string]int
7072
modelType reflect.Type
7173
}
72-
func NewDefaultQuery(db *sql.DB, query string, options ...int) (*Query, error) {
74+
func NewDefaultQuery(db *sql.DB, query string, getQuery string, options ...int) (*Query, error) {
7375
var parameterCount int
7476
if len(options) > 0 {
7577
parameterCount = options[0]
7678
} else {
7779
parameterCount = 1
7880
}
79-
return NewQuery(db, query, parameterCount, true)
81+
return NewQuery(db, query, getQuery, parameterCount, true)
8082
}
81-
func NewQuery(db *sql.DB, query string, parameterCount int, options ...bool) (*Query, error) {
83+
func NewQuery(db *sql.DB, query string, getQuery string, parameterCount int, options ...bool) (*Query, error) {
8284
driver := getDriver(db)
8385
var mp func(string) string
8486
if driver == driverOracle {
@@ -100,6 +102,7 @@ func NewQuery(db *sql.DB, query string, parameterCount int, options ...bool) (*Q
100102
} else {
101103
handleDriver = true
102104
}
105+
build := getBuild(db)
103106
if handleDriver {
104107
if driver == driverOracle || driver == driverPostgres || driver == driverMssql {
105108
var x string
@@ -116,7 +119,7 @@ func NewQuery(db *sql.DB, query string, parameterCount int, options ...bool) (*Q
116119
}
117120
}
118121
}
119-
return &Query{DB: db, Select: query, ParameterCount: parameterCount, Map: mp, colMap: fieldsIndex, modelType: modelType}, nil
122+
return &Query{DB: db, Select: query, Get: getQuery, Build: build,ParameterCount: parameterCount, Map: mp, colMap: fieldsIndex, modelType: modelType}, nil
120123
}
121124
func (l Query) Query(ctx context.Context, key string, max int64) ([]Model, error) {
122125
if max <= 0 {
@@ -172,6 +175,46 @@ func (l Query) Query(ctx context.Context, key string, max int64) ([]Model, error
172175
}
173176
return models, nil
174177
}
178+
func (l Query) Load(ctx context.Context, key []string) ([]Model, error) {
179+
models := make([]Model, 0)
180+
var rows *sql.Rows
181+
var er1 error
182+
le := len(key)
183+
args := make([]interface{}, 0)
184+
params := make([]string, 0)
185+
for i := 1; i <= le; i++ {
186+
params = append(params, l.Build(i))
187+
args = append(args, key[i - 1])
188+
}
189+
query := l.Get + fmt.Sprintf(" (%s)", strings.Join(params, ","))
190+
rows, er1 = l.DB.QueryContext(ctx, query, args...)
191+
if er1 != nil {
192+
return models, er1
193+
}
194+
defer rows.Close()
195+
columns, er2 := rows.Columns()
196+
if er2 != nil {
197+
return models, er2
198+
}
199+
200+
fieldsIndexSelected := make([]int, 0)
201+
fieldsIndex := l.colMap
202+
for _, columnsName := range columns {
203+
if index, ok := fieldsIndex[columnsName]; ok {
204+
fieldsIndexSelected = append(fieldsIndexSelected, index)
205+
}
206+
}
207+
tb, er4 := scanType(rows, l.modelType, fieldsIndexSelected)
208+
if er4 != nil {
209+
return models, er4
210+
}
211+
for _, v := range tb {
212+
if c, ok := v.(*Model); ok {
213+
models = append(models, *c)
214+
}
215+
}
216+
return models, nil
217+
}
175218
func NewDefaultDynamicSqlCodeLoader(db *sql.DB, query string, options ...int) (*DynamicSqlLoader, error) {
176219
var parameterCount int
177220
if len(options) > 0 {

echo/handler.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package echo
22

33
import (
44
"context"
5+
"encoding/json"
56
co "github.com/core-go/code"
67
"github.com/labstack/echo/v4"
78
"io/ioutil"
@@ -106,23 +107,30 @@ func (h *Handler) Load(ctx echo.Context) error {
106107
}
107108

108109
type QueryHandler struct {
109-
Load func(ctx context.Context, key string, max int64) ([]co.Model, error)
110+
Get func(ctx context.Context, key string, max int64) ([]co.Model, error)
111+
Select func(ctx context.Context, key []string) ([]co.Model, error)
110112
LogError func(context.Context, string, ...map[string]interface{})
111113
Keyword string
112114
Max string
115+
Q string
113116
}
114117

115-
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]co.Model, error), logError func(context.Context, string, ...map[string]interface{}), opts ...string) *QueryHandler {
116-
keyword := "q"
118+
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]co.Model, error), getData func(ctx context.Context, key []string) ([]co.Model, error), logError func(context.Context, string, ...map[string]interface{}), opts ...string) *QueryHandler {
119+
q := "q"
117120
if len(opts) > 0 && len(opts[0]) > 0 {
118-
keyword = opts[0]
121+
q = opts[0]
119122
}
120-
max := "max"
123+
keyword := "q"
121124
if len(opts) > 1 && len(opts[1]) > 0 {
122-
max = opts[1]
125+
keyword = opts[1]
126+
}
127+
max := "max"
128+
if len(opts) > 2 && len(opts[2]) > 0 {
129+
max = opts[2]
123130
}
124-
return &QueryHandler{load, logError, keyword, max}
131+
return &QueryHandler{load, getData, logError, keyword, max, q}
125132
}
133+
126134
func (h *QueryHandler) Query(ctx echo.Context) error {
127135
ps := ctx.Request().URL.Query()
128136
keyword := ps.Get(h.Keyword)
@@ -138,7 +146,7 @@ func (h *QueryHandler) Query(ctx echo.Context) error {
138146
if i < 0 {
139147
i = 20
140148
}
141-
vs, err := h.Load(ctx.Request().Context(), keyword, i)
149+
vs, err := h.Get(ctx.Request().Context(), keyword, i)
142150
if err != nil {
143151
h.LogError(ctx.Request().Context(), err.Error())
144152
return ctx.String(http.StatusInternalServerError, internalServerError)
@@ -147,7 +155,32 @@ func (h *QueryHandler) Query(ctx echo.Context) error {
147155
}
148156
}
149157
}
150-
158+
func (h *QueryHandler) Load(ctx echo.Context) error {
159+
r := ctx.Request()
160+
var req = make([]string, 0)
161+
method := r.Method
162+
if method == http.MethodGet {
163+
q := r.URL.Query().Get(h.Q)
164+
if len(q) > 0 {
165+
req = strings.Split(q, ",")
166+
}
167+
} else {
168+
err := json.NewDecoder(r.Body).Decode(&req)
169+
if err != nil {
170+
return ctx.String(http.StatusBadRequest, err.Error())
171+
}
172+
}
173+
if len(req) == 0 {
174+
return ctx.JSON(http.StatusOK, req)
175+
}
176+
models, err := h.Select(r.Context(), req)
177+
if err != nil {
178+
h.LogError(r.Context(), err.Error())
179+
return ctx.String(http.StatusInternalServerError, internalServerError)
180+
} else {
181+
return ctx.JSON(http.StatusOK, models)
182+
}
183+
}
151184
func respond(ctx echo.Context, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string, success bool, desc string) error {
152185
err := ctx.JSON(code, result)
153186
if writeLog != nil {

echo_v3/handler.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package echo
22

33
import (
44
"context"
5+
"encoding/json"
56
co "github.com/core-go/code"
67
"github.com/labstack/echo"
78
"io/ioutil"
@@ -106,23 +107,30 @@ func (h *Handler) Load(ctx echo.Context) error {
106107
}
107108

108109
type QueryHandler struct {
109-
Load func(ctx context.Context, key string, max int64) ([]co.Model, error)
110+
Get func(ctx context.Context, key string, max int64) ([]co.Model, error)
111+
Select func(ctx context.Context, key []string) ([]co.Model, error)
110112
LogError func(context.Context, string, ...map[string]interface{})
111113
Keyword string
112114
Max string
115+
Q string
113116
}
114117

115-
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]co.Model, error), logError func(context.Context, string, ...map[string]interface{}), opts ...string) *QueryHandler {
116-
keyword := "q"
118+
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]co.Model, error), getData func(ctx context.Context, key []string) ([]co.Model, error), logError func(context.Context, string, ...map[string]interface{}), opts ...string) *QueryHandler {
119+
q := "q"
117120
if len(opts) > 0 && len(opts[0]) > 0 {
118-
keyword = opts[0]
121+
q = opts[0]
119122
}
120-
max := "max"
123+
keyword := "q"
121124
if len(opts) > 1 && len(opts[1]) > 0 {
122-
max = opts[1]
125+
keyword = opts[1]
126+
}
127+
max := "max"
128+
if len(opts) > 2 && len(opts[2]) > 0 {
129+
max = opts[2]
123130
}
124-
return &QueryHandler{load, logError, keyword, max}
131+
return &QueryHandler{load, getData, logError, keyword, max, q}
125132
}
133+
126134
func (h *QueryHandler) Query(ctx echo.Context) error {
127135
ps := ctx.Request().URL.Query()
128136
keyword := ps.Get(h.Keyword)
@@ -138,7 +146,7 @@ func (h *QueryHandler) Query(ctx echo.Context) error {
138146
if i < 0 {
139147
i = 20
140148
}
141-
vs, err := h.Load(ctx.Request().Context(), keyword, i)
149+
vs, err := h.Get(ctx.Request().Context(), keyword, i)
142150
if err != nil {
143151
h.LogError(ctx.Request().Context(), err.Error())
144152
return ctx.String(http.StatusInternalServerError, internalServerError)
@@ -147,7 +155,32 @@ func (h *QueryHandler) Query(ctx echo.Context) error {
147155
}
148156
}
149157
}
150-
158+
func (h *QueryHandler) Load(ctx echo.Context) error {
159+
r := ctx.Request()
160+
var req = make([]string, 0)
161+
method := r.Method
162+
if method == http.MethodGet {
163+
q := r.URL.Query().Get(h.Q)
164+
if len(q) > 0 {
165+
req = strings.Split(q, ",")
166+
}
167+
} else {
168+
err := json.NewDecoder(r.Body).Decode(&req)
169+
if err != nil {
170+
return ctx.String(http.StatusBadRequest, err.Error())
171+
}
172+
}
173+
if len(req) == 0 {
174+
return ctx.JSON(http.StatusOK, req)
175+
}
176+
models, err := h.Select(r.Context(), req)
177+
if err != nil {
178+
h.LogError(r.Context(), err.Error())
179+
return ctx.String(http.StatusInternalServerError, internalServerError)
180+
} else {
181+
return ctx.JSON(http.StatusOK, models)
182+
}
183+
}
151184
func respond(ctx echo.Context, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string, success bool, desc string) error {
152185
err := ctx.JSON(code, result)
153186
if writeLog != nil {

gin/handler.go

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gin
22

33
import (
44
"context"
5+
"encoding/json"
56
co "github.com/core-go/code"
67
"github.com/gin-gonic/gin"
78
"io/ioutil"
@@ -106,22 +107,28 @@ func (h *Handler) Load(ctx *gin.Context) {
106107
}
107108

108109
type QueryHandler struct {
109-
Load func(ctx context.Context, key string, max int64) ([]co.Model, error)
110+
Get func(ctx context.Context, key string, max int64) ([]co.Model, error)
111+
Select func(ctx context.Context, key []string) ([]co.Model, error)
110112
LogError func(context.Context, string, ...map[string]interface{})
111113
Keyword string
112114
Max string
115+
Q string
113116
}
114117

115-
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]co.Model, error), logError func(context.Context, string, ...map[string]interface{}), opts ...string) *QueryHandler {
116-
keyword := "q"
118+
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]co.Model, error), getData func(ctx context.Context, key []string) ([]co.Model, error), logError func(context.Context, string, ...map[string]interface{}), opts ...string) *QueryHandler {
119+
q := "q"
117120
if len(opts) > 0 && len(opts[0]) > 0 {
118-
keyword = opts[0]
121+
q = opts[0]
119122
}
120-
max := "max"
123+
keyword := "q"
121124
if len(opts) > 1 && len(opts[1]) > 0 {
122-
max = opts[1]
125+
keyword = opts[1]
126+
}
127+
max := "max"
128+
if len(opts) > 2 && len(opts[2]) > 0 {
129+
max = opts[2]
123130
}
124-
return &QueryHandler{load, logError, keyword, max}
131+
return &QueryHandler{load, getData, logError, keyword, max, q}
125132
}
126133
func (h *QueryHandler) Query(ctx *gin.Context) {
127134
ps := ctx.Request.URL.Query()
@@ -138,7 +145,7 @@ func (h *QueryHandler) Query(ctx *gin.Context) {
138145
if i < 0 {
139146
i = 20
140147
}
141-
vs, err := h.Load(ctx.Request.Context(), keyword, i)
148+
vs, err := h.Get(ctx.Request.Context(), keyword, i)
142149
if err != nil {
143150
h.LogError(ctx.Request.Context(), err.Error())
144151
ctx.String(http.StatusInternalServerError, internalServerError)
@@ -147,7 +154,34 @@ func (h *QueryHandler) Query(ctx *gin.Context) {
147154
}
148155
}
149156
}
150-
157+
func (h *QueryHandler) Load(ctx *gin.Context) {
158+
r := ctx.Request
159+
var req = make([]string, 0)
160+
method := r.Method
161+
if method == http.MethodGet {
162+
q := r.URL.Query().Get(h.Q)
163+
if len(q) > 0 {
164+
req = strings.Split(q, ",")
165+
}
166+
} else {
167+
err := json.NewDecoder(r.Body).Decode(&req)
168+
if err != nil {
169+
ctx.String(http.StatusBadRequest, err.Error())
170+
return
171+
}
172+
}
173+
if len(req) == 0 {
174+
ctx.JSON(http.StatusOK, req)
175+
} else {
176+
models, err := h.Select(r.Context(), req)
177+
if err != nil {
178+
h.LogError(r.Context(), err.Error())
179+
ctx.String(http.StatusInternalServerError, internalServerError)
180+
} else {
181+
ctx.JSON(http.StatusOK, models)
182+
}
183+
}
184+
}
151185
func respond(ctx *gin.Context, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string, success bool, desc string) {
152186
ctx.JSON(code, result)
153187
if writeLog != nil {

0 commit comments

Comments
 (0)