Skip to content

Commit 4bf7a2d

Browse files
committed
Change limit and offset value
1 parent 5ccde81 commit 4bf7a2d

4 files changed

Lines changed: 93 additions & 8 deletions

File tree

.editorconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ indent_size = 2
99

1010
[{Makefile,go.mod,go.sum,*.go,.gitmodules}]
1111
indent_style = tab
12-
indent_size = 4
12+
indent_size = 2
1313

1414
[*.md]
15-
indent_size = 4
15+
indent_size = 2
1616
trim_trailing_whitespace = false

jql.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ func (jql *Json2Sql) JsonRawCaseDefauleValue(raw json.RawMessage) (CaseDefauleVa
6161
return v, err == nil
6262
}
6363

64+
func (jql *Json2Sql) JsonRawLimitOffsetValue(raw json.RawMessage) (LimitOffsetValue, bool) {
65+
var v LimitOffsetValue
66+
err := json.Unmarshal(raw, &v)
67+
return v, err == nil
68+
}
69+
6470
func (jql *Json2Sql) isStringNumeric(s string) bool {
6571
for _, char := range s {
6672
if !unicode.IsDigit(char) {
@@ -345,19 +351,35 @@ func (jql *Json2Sql) GenerateConditions(conditions ...Condition) string {
345351

346352
func (jql *Json2Sql) GenerateLimit() string {
347353
var sql = ""
348-
349354
if jql.sqlJson.Limit != nil {
350-
sql += fmt.Sprintf(" LIMIT %d", *jql.sqlJson.Limit)
355+
v, b := jql.JsonRawLimitOffsetValue(*jql.sqlJson.Limit)
356+
if b {
357+
if v.IsStatic {
358+
sql += fmt.Sprintf(" LIMIT %s", strconv.Itoa(v.Value))
359+
} else {
360+
sql += fmt.Sprintf(" LIMIT %s%s%s", JQL_FLAG_OPEN, strconv.Itoa(v.Value), JQL_FLAG_CLOSE)
361+
}
362+
} else {
363+
sql += fmt.Sprintf(" LIMIT %s", *jql.sqlJson.Limit)
364+
}
351365
}
352366

353367
return sql
354368
}
355369

356370
func (jql *Json2Sql) GenerateOffset() string {
357371
var sql = ""
358-
359372
if jql.sqlJson.Offset != nil {
360-
sql += fmt.Sprintf(" OFFSET %d", *jql.sqlJson.Offset)
373+
v, b := jql.JsonRawLimitOffsetValue(*jql.sqlJson.Offset)
374+
if b {
375+
if v.IsStatic {
376+
sql += fmt.Sprintf(" OFFSET %s", strconv.Itoa(v.Value))
377+
} else {
378+
sql += fmt.Sprintf(" OFFSET %s%s%s", JQL_FLAG_OPEN, strconv.Itoa(v.Value), JQL_FLAG_CLOSE)
379+
}
380+
} else {
381+
sql += fmt.Sprintf(" OFFSET %s", *jql.sqlJson.Offset)
382+
}
361383
}
362384

363385
return sql

jql_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,64 @@ func TestGenerateConditions_SubQuery(t *testing.T) {
665665
assert.Equal(t, strExpected, strings.TrimSpace(str))
666666
}
667667

668+
func TestLimit_Static(t *testing.T) {
669+
strTest := `{
670+
"limit": {
671+
"isStatic": true,
672+
"value": 10
673+
}
674+
}`
675+
strExpected := `LIMIT 10`
676+
677+
jql, _ := NewJson2Sql([]byte(strTest))
678+
str := jql.GenerateLimit()
679+
680+
assert.Equal(t, strExpected, strings.TrimSpace(str))
681+
}
682+
683+
func TestLimit_ToParam(t *testing.T) {
684+
strTest := `{
685+
"limit": {
686+
"value": 10
687+
}
688+
}`
689+
strExpected := `LIMIT JQL_VALUE:10:END_JQL_VALUE`
690+
691+
jql, _ := NewJson2Sql([]byte(strTest))
692+
str := jql.GenerateLimit()
693+
694+
assert.Equal(t, strExpected, strings.TrimSpace(str))
695+
}
696+
697+
func TestOffset_Static(t *testing.T) {
698+
strTest := `{
699+
"offset": {
700+
"isStatic": true,
701+
"value": 10
702+
}
703+
}`
704+
strExpected := `OFFSET 10`
705+
706+
jql, _ := NewJson2Sql([]byte(strTest))
707+
str := jql.GenerateOffset()
708+
709+
assert.Equal(t, strExpected, strings.TrimSpace(str))
710+
}
711+
712+
func TestOffset_ToParam(t *testing.T) {
713+
strTest := `{
714+
"offset": {
715+
"value": 10
716+
}
717+
}`
718+
strExpected := `OFFSET JQL_VALUE:10:END_JQL_VALUE`
719+
720+
jql, _ := NewJson2Sql([]byte(strTest))
721+
str := jql.GenerateOffset()
722+
723+
assert.Equal(t, strExpected, strings.TrimSpace(str))
724+
}
725+
668726
func TestBuildJsonToSql(t *testing.T) {
669727
jsonData := `
670728
{

sql_struct.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type SQLJson struct {
2727
Fields []string `json:"fields"`
2828
Sort *string `json:"sort"`
2929
} `json:"orderBy"`
30-
Limit *int `json:"limit"`
31-
Offset *int `json:"offset"`
30+
Limit *json.RawMessage `json:"limit"`
31+
Offset *json.RawMessage `json:"offset"`
3232
}
3333

3434
type Join struct {
@@ -68,5 +68,10 @@ type ValueAdjacent struct {
6868
IsStatic *bool `json:"isStatic"`
6969
}
7070

71+
type LimitOffsetValue struct {
72+
IsStatic bool `json:"isStatic"`
73+
Value int `json:"value"`
74+
}
75+
7176
type ExpectationField ValueAdjacent
7277
type CaseDefauleValue ValueAdjacent

0 commit comments

Comments
 (0)