44 "encoding/json"
55 "fmt"
66 "regexp"
7+ "strconv"
78 "strings"
9+ "unicode"
810)
911
1012type Json2Sql struct {
@@ -59,17 +61,37 @@ func (jql *Json2Sql) JsonRawCaseDefauleValue(raw json.RawMessage) (CaseDefauleVa
5961 return v , err == nil
6062}
6163
62- func (jql * Json2Sql ) MaskedQueryValue (query string ) (string , []string ) {
63- re := regexp .MustCompile (`JQL_VALUE:(\d+)|JQL_VALUE:'([^']+)'` )
64+ func (jql * Json2Sql ) isStringNumeric (s string ) bool {
65+ for _ , char := range s {
66+ if ! unicode .IsDigit (char ) {
67+ return false
68+ }
69+ }
70+ return true
71+ }
72+
73+ func (jql * Json2Sql ) MaskedQueryValue (query string ) (string , []interface {}) {
74+ sRegex := fmt .Sprintf (`%s'([^\s]+)'%s|%s([^\s]+)%s` , JQL_FLAG_OPEN , JQL_FLAG_CLOSE , JQL_FLAG_OPEN , JQL_FLAG_CLOSE )
75+ re := regexp .MustCompile (sRegex )
6476 matches := re .FindAllStringSubmatch (query , - 1 )
6577
66- var values []string
78+ var values []interface {}
6779 for _ , match := range matches {
68- if len (match ) == 3 {
69- if match [1 ] != "" {
70- values = append (values , match [1 ])
71- } else {
72- values = append (values , match [2 ])
80+ for i := 1 ; i < len (match ); i += 2 {
81+ if match [i ] != "" {
82+ values = append (values , match [i ])
83+ } else if match [i + 1 ] != "" {
84+ var m = match [i + 1 ]
85+ if strings .ToLower (m ) == "true" || strings .ToLower (m ) == "false" {
86+ booleanValue , _ := strconv .ParseBool (m )
87+ values = append (values , booleanValue )
88+ } else if jql .isStringNumeric (m ) {
89+ intValue , _ := strconv .ParseFloat (m , 64 )
90+ values = append (values , intValue )
91+ } else {
92+ values = append (values , m )
93+ }
94+
7395 }
7496 }
7597 }
@@ -82,7 +104,9 @@ func (jql *Json2Sql) MaskedQueryValue(query string) (string, []string) {
82104}
83105
84106func (jql * Json2Sql ) rawValueExtractor (query string ) string {
85- replacedQuery := strings .ReplaceAll (query , "JQL_VALUE:" , "" )
107+ replacedQuery := strings .ReplaceAll (query , JQL_FLAG_OPEN , "" )
108+ replacedQuery = strings .ReplaceAll (replacedQuery , JQL_FLAG_CLOSE , "" )
109+
86110 return replacedQuery
87111}
88112
@@ -119,7 +143,7 @@ func (jql *Json2Sql) GenerateSelectFrom(selection ...json.RawMessage) string {
119143 jsonBytes , _ := json .Marshal (* sqlSelectDetail .SubQuery )
120144 jql , _ := NewJson2Sql (jsonBytes )
121145
122- field = fmt .Sprintf ("(%s) AS %s" , jql .Build (), * sqlSelectDetail .Alias )
146+ field = fmt .Sprintf ("(%s) AS %s" , jql .rawBuild (), * sqlSelectDetail .Alias )
123147 }
124148 }
125149
@@ -145,7 +169,7 @@ func (jql *Json2Sql) GenerateSelectFrom(selection ...json.RawMessage) string {
145169 if selectExpect .SubQuery != nil {
146170 jsonBytes , _ := json .Marshal (* selectExpect .SubQuery )
147171 jql , _ := NewJson2Sql (jsonBytes )
148- defaultValue = fmt .Sprintf ("(%s)" , jql .Build ())
172+ defaultValue = fmt .Sprintf ("(%s)" , jql .rawBuild ())
149173 }
150174 }
151175 }
@@ -282,7 +306,7 @@ func (jql *Json2Sql) GenerateConditions(conditions ...Condition) string {
282306 if selectSub .SubQuery != nil {
283307 jsonBytes , _ := json .Marshal (* selectSub .SubQuery )
284308 jql , _ := NewJson2Sql (jsonBytes )
285- expression = string (condition .Operator ) + " " + fmt .Sprintf ("(%s)" , jql .Build ())
309+ expression = string (condition .Operator ) + " " + fmt .Sprintf ("(%s)" , jql .rawBuild ())
286310 }
287311 }
288312 }
@@ -306,7 +330,7 @@ func (jql *Json2Sql) GenerateConditions(conditions ...Condition) string {
306330 if selectExpect .SubQuery != nil {
307331 jsonBytes , _ := json .Marshal (* selectExpect .SubQuery )
308332 jql , _ := NewJson2Sql (jsonBytes )
309- expect = fmt .Sprintf ("(%s)" , jql .Build ())
333+ expect = fmt .Sprintf ("(%s)" , jql .rawBuild ())
310334 }
311335 }
312336 }
@@ -319,20 +343,41 @@ func (jql *Json2Sql) GenerateConditions(conditions ...Condition) string {
319343 return strings .Join (conditionsStr , " " )
320344}
321345
322- func (jql * Json2Sql ) rawBuild () string {
323- sql := jql .GenerateSelectFrom () + jql .GenerateJoin () + jql .GenerateWhere () + jql .GenerateGroupBy () + jql .GenerateHaving () + jql .GenerateOrderBy ()
346+ func (jql * Json2Sql ) GenerateLimit () string {
347+ var sql = ""
348+
349+ if jql .sqlJson .Limit != nil {
350+ sql += fmt .Sprintf (" LIMIT %d" , * jql .sqlJson .Limit )
351+ }
324352
325- return cleanSpaces (sql )
353+ return sql
354+ }
355+
356+ func (jql * Json2Sql ) GenerateOffset () string {
357+ var sql = ""
358+
359+ if jql .sqlJson .Offset != nil {
360+ sql += fmt .Sprintf (" OFFSET %d" , * jql .sqlJson .Offset )
361+ }
362+
363+ return sql
364+ }
365+
366+ func (jql * Json2Sql ) concateQueryString () string {
367+ return jql .GenerateSelectFrom () + jql .GenerateJoin () + jql .GenerateWhere () + jql .GenerateGroupBy () + jql .GenerateHaving () + jql .GenerateOrderBy () + jql .GenerateLimit () + jql .GenerateOffset ()
368+ }
369+
370+ func (jql * Json2Sql ) rawBuild () string {
371+ return cleanSpaces (jql .concateQueryString ())
326372}
327373
328374func (jql * Json2Sql ) Build () string {
329- sql := jql .GenerateSelectFrom () + jql .GenerateJoin () + jql .GenerateWhere () + jql .GenerateGroupBy () + jql .GenerateHaving () + jql .GenerateOrderBy ()
330- sqlCleanValue := jql .rawValueExtractor (sql )
375+ sqlCleanValue := jql .rawValueExtractor (jql .concateQueryString ())
331376
332377 return cleanSpaces (sqlCleanValue )
333378}
334379
335- func (jql * Json2Sql ) Generate () (string , []string , error ) {
380+ func (jql * Json2Sql ) Generate () (string , []interface {} , error ) {
336381 sql := jql .rawBuild ()
337382 newQuery , values := jql .MaskedQueryValue (sql )
338383
0 commit comments