@@ -2,9 +2,11 @@ package db
22
33import (
44 "context"
5+ "github.com/glennliao/apijson-go/config"
56 "github.com/gogf/gf/v2/database/gdb"
67 "github.com/gogf/gf/v2/frame/g"
78 "github.com/gogf/gf/v2/util/gconv"
9+ "regexp"
810 "strings"
911)
1012
@@ -25,12 +27,17 @@ func NewSqlExecutor(ctx context.Context, tableName string, accessVerify bool) (*
2527
2628 m := g .DB ().Model (tableName )
2729
30+ var columns []string
31+ for _ , column := range tableMap [tableName ].Columns {
32+ columns = append (columns , column .Name )
33+ }
34+
2835 return & SqlExecutor {
2936 ctx : ctx ,
3037 Table : tableName ,
3138 m : m ,
3239 builder : m .Builder (),
33- Columns : nil ,
40+ Columns : columns ,
3441 Order : "" ,
3542 Group : "" ,
3643 }, nil
@@ -39,6 +46,8 @@ func NewSqlExecutor(ctx context.Context, tableName string, accessVerify bool) (*
3946func (e * SqlExecutor ) ParseCondition (conditions g.MapStrAny ) error {
4047
4148 for k , condition := range conditions {
49+ k = config .ToDbField (k ) // 将请求字段转化为数据库字段风格
50+
4251 switch {
4352 case strings .HasSuffix (k , "{}" ):
4453 e .parseMultiCondition (k [0 :len (k )- 2 ], condition )
@@ -111,24 +120,34 @@ func (e *SqlExecutor) parseMultiCondition(k string, condition any) {
111120
112121}
113122
114- func (e * SqlExecutor ) ParseCtrl (m g.Map ) error {
123+ var exp = regexp .MustCompile (`^[\s\w][\w()]+` ) // 匹配 field, COUNT(field)
124+
125+ func (e * SqlExecutor ) ParseCtrl (ctrl g.Map ) error {
126+
127+ for k , v := range ctrl {
128+ // https://github.com/Tencent/APIJSON/blob/master/Document.md
129+ // 应该用分号 ; 隔开 SQL 函数,改为 "@column":"store_id;sum(amt):totAmt")
130+ fieldStr := strings .ReplaceAll (gconv .String (v ), ";" , Separator )
131+
132+ fieldList := strings .Split (fieldStr , "," )
133+ for i , item := range fieldList {
134+ fieldList [i ] = exp .ReplaceAllStringFunc (item , config .ToDbField ) // 将请求字段转化为数据库字段风格
135+ }
136+
137+ fieldStr = strings .Join (fieldList , Separator )
115138
116- for k , v := range m {
117139 switch k {
118140
119141 case "@order" :
120- order := strings .Replace ( gconv . String ( v ) , "-" , " desc" , - 1 )
121- order = strings .Replace (order , "+" , " " , - 1 )
142+ order := strings .ReplaceAll ( fieldStr , "-" , DESC )
143+ order = strings .ReplaceAll (order , "+" , " " )
122144 e .Order = order
123145
124146 case "@column" :
125- columns := gconv .String (v )
126- columns = strings .Replace (columns , ";" , ", " , - 1 )
127- columns = strings .Replace (columns , ":" , " as " , - 1 )
128- e .Columns = strings .Split (columns , "," )
147+ e .Columns = fieldList
129148
130149 case "@group" :
131- e .Group = gconv . String ( v )
150+ e .Group = fieldStr
132151 }
133152 }
134153
@@ -160,32 +179,21 @@ func (e *SqlExecutor) List(page int, count int, needTotal bool) (list []g.Map, t
160179 m := e .build ()
161180
162181 if needTotal {
163- total , err = m .Fields ( "*" ). Count ()
164- if err != nil {
182+ total , err = m .Count ()
183+ if err != nil || total == 0 {
165184 return nil , 0 , err
166185 }
167186 }
168187
169- // 无需下一步查询
170- if needTotal && total == 0 {
171- return nil , 0 , err
172- }
173-
174- if e .Columns != nil {
175- m = m .Fields (e .Columns )
176- }
188+ m = m .Fields (e .JsonFields ())
177189
178190 m = m .Page (page , count )
179191 all , err := m .All ()
180192 if err != nil {
181193 return nil , 0 , err
182194 }
183195
184- for _ , item := range all .List () {
185- list = append (list , item )
186- }
187-
188- return
196+ return all .List (), total , nil
189197}
190198
191199func (e * SqlExecutor ) One () (g.Map , error ) {
@@ -195,11 +203,28 @@ func (e *SqlExecutor) One() (g.Map, error) {
195203
196204 m := e .build ()
197205
198- if e .Columns != nil {
199- m = m .Fields (e .Columns )
200- }
206+ m = m .Fields (e .JsonFields ())
201207
202208 one , err := m .One ()
203209
204210 return one .Map (), err
205211}
212+
213+ // JsonFields 返回 config.ToJsonField 指定的字段格式.
214+ func (e * SqlExecutor ) JsonFields () []string {
215+
216+ var fields = make ([]string , 0 , len (e .Columns ))
217+ for _ , column := range e .Columns {
218+ column = strings .ReplaceAll (column , ":" , AS )
219+ if ! strings .Contains (column , AS ) {
220+ field := config .ToJsonField (column )
221+ if field != column {
222+ column = column + AS + field
223+ }
224+ }
225+
226+ fields = append (fields , column )
227+ }
228+
229+ return fields
230+ }
0 commit comments