Skip to content

Commit

Permalink
Change limit and offset value
Browse files Browse the repository at this point in the history
  • Loading branch information
bonkzero404 committed Jan 19, 2024
1 parent 5ccde81 commit 4bf7a2d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ indent_size = 2

[{Makefile,go.mod,go.sum,*.go,.gitmodules}]
indent_style = tab
indent_size = 4
indent_size = 2

[*.md]
indent_size = 4
indent_size = 2
trim_trailing_whitespace = false
30 changes: 26 additions & 4 deletions jql.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ func (jql *Json2Sql) JsonRawCaseDefauleValue(raw json.RawMessage) (CaseDefauleVa
return v, err == nil
}

func (jql *Json2Sql) JsonRawLimitOffsetValue(raw json.RawMessage) (LimitOffsetValue, bool) {
var v LimitOffsetValue
err := json.Unmarshal(raw, &v)
return v, err == nil
}

func (jql *Json2Sql) isStringNumeric(s string) bool {
for _, char := range s {
if !unicode.IsDigit(char) {
Expand Down Expand Up @@ -345,19 +351,35 @@ func (jql *Json2Sql) GenerateConditions(conditions ...Condition) string {

func (jql *Json2Sql) GenerateLimit() string {
var sql = ""

if jql.sqlJson.Limit != nil {
sql += fmt.Sprintf(" LIMIT %d", *jql.sqlJson.Limit)
v, b := jql.JsonRawLimitOffsetValue(*jql.sqlJson.Limit)
if b {
if v.IsStatic {
sql += fmt.Sprintf(" LIMIT %s", strconv.Itoa(v.Value))
} else {
sql += fmt.Sprintf(" LIMIT %s%s%s", JQL_FLAG_OPEN, strconv.Itoa(v.Value), JQL_FLAG_CLOSE)
}
} else {
sql += fmt.Sprintf(" LIMIT %s", *jql.sqlJson.Limit)
}
}

return sql
}

func (jql *Json2Sql) GenerateOffset() string {
var sql = ""

if jql.sqlJson.Offset != nil {
sql += fmt.Sprintf(" OFFSET %d", *jql.sqlJson.Offset)
v, b := jql.JsonRawLimitOffsetValue(*jql.sqlJson.Offset)
if b {
if v.IsStatic {
sql += fmt.Sprintf(" OFFSET %s", strconv.Itoa(v.Value))
} else {
sql += fmt.Sprintf(" OFFSET %s%s%s", JQL_FLAG_OPEN, strconv.Itoa(v.Value), JQL_FLAG_CLOSE)
}
} else {
sql += fmt.Sprintf(" OFFSET %s", *jql.sqlJson.Offset)
}
}

return sql
Expand Down
58 changes: 58 additions & 0 deletions jql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,64 @@ func TestGenerateConditions_SubQuery(t *testing.T) {
assert.Equal(t, strExpected, strings.TrimSpace(str))
}

func TestLimit_Static(t *testing.T) {
strTest := `{
"limit": {
"isStatic": true,
"value": 10
}
}`
strExpected := `LIMIT 10`

jql, _ := NewJson2Sql([]byte(strTest))
str := jql.GenerateLimit()

assert.Equal(t, strExpected, strings.TrimSpace(str))
}

func TestLimit_ToParam(t *testing.T) {
strTest := `{
"limit": {
"value": 10
}
}`
strExpected := `LIMIT JQL_VALUE:10:END_JQL_VALUE`

jql, _ := NewJson2Sql([]byte(strTest))
str := jql.GenerateLimit()

assert.Equal(t, strExpected, strings.TrimSpace(str))
}

func TestOffset_Static(t *testing.T) {
strTest := `{
"offset": {
"isStatic": true,
"value": 10
}
}`
strExpected := `OFFSET 10`

jql, _ := NewJson2Sql([]byte(strTest))
str := jql.GenerateOffset()

assert.Equal(t, strExpected, strings.TrimSpace(str))
}

func TestOffset_ToParam(t *testing.T) {
strTest := `{
"offset": {
"value": 10
}
}`
strExpected := `OFFSET JQL_VALUE:10:END_JQL_VALUE`

jql, _ := NewJson2Sql([]byte(strTest))
str := jql.GenerateOffset()

assert.Equal(t, strExpected, strings.TrimSpace(str))
}

func TestBuildJsonToSql(t *testing.T) {
jsonData := `
{
Expand Down
9 changes: 7 additions & 2 deletions sql_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type SQLJson struct {
Fields []string `json:"fields"`
Sort *string `json:"sort"`
} `json:"orderBy"`
Limit *int `json:"limit"`
Offset *int `json:"offset"`
Limit *json.RawMessage `json:"limit"`
Offset *json.RawMessage `json:"offset"`
}

type Join struct {
Expand Down Expand Up @@ -68,5 +68,10 @@ type ValueAdjacent struct {
IsStatic *bool `json:"isStatic"`
}

type LimitOffsetValue struct {
IsStatic bool `json:"isStatic"`
Value int `json:"value"`
}

type ExpectationField ValueAdjacent
type CaseDefauleValue ValueAdjacent

0 comments on commit 4bf7a2d

Please sign in to comment.