Skip to content

Commit c4b8321

Browse files
committed
Add clean operand where
1 parent 0377125 commit c4b8321

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

jql.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ func cleanSpaces(input string) string {
5252
return result
5353
}
5454

55+
func cleanWhereCond(input string) string {
56+
re := regexp.MustCompile(`(?i)where\s*and|where or`)
57+
cleanedInput := re.ReplaceAllString(input, "WHERE")
58+
return cleanedInput
59+
}
60+
5561
func sanitizeInjection(input string) string {
5662
re := regexp.MustCompile(`(?i)[;]|--|drop\s*table|@@\s*version|insert\s*into|if\s*\(|sleep\s*\(|"|\/\*|\*\/|\\0|\\'|\\"|\\b|\\n|\\r|\\t|\\Z|\\\\|\\%|\\_`)
5763
cleanedInput := re.ReplaceAllString(input, "")
@@ -239,7 +245,7 @@ func (jql *Json2Sql) GenerateWhere() string {
239245
var sql = ""
240246

241247
if jql.sqlJson.Conditions != nil {
242-
sql += " WHERE " + jql.GenerateConditions(*jql.sqlJson.Conditions...)
248+
sql += cleanWhereCond(" WHERE " + jql.GenerateConditions(*jql.sqlJson.Conditions...))
243249
}
244250

245251
return sql

jql_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,35 @@ func TestBetweenWithOperand(t *testing.T) {
377377
assert.Equal(t, strings.TrimSpace(strExpected), strings.TrimSpace(str))
378378
}
379379

380+
func TestWhereIs(t *testing.T) {
381+
var sqlTest = `
382+
{
383+
"conditions": [
384+
{
385+
"operand": "or",
386+
"clause": "a",
387+
"datatype": "STRING",
388+
"operator": "is not null",
389+
"value": null
390+
},
391+
{
392+
"operand": "and",
393+
"clause": "b",
394+
"datatype": "STRING",
395+
"operator": "is null",
396+
"value": null
397+
}
398+
]
399+
}
400+
`
401+
402+
strExpected := `WHERE a IS NOT NULL AND b IS NULL`
403+
jql, _ := NewJson2Sql([]byte(sqlTest), &Json2SqlConf{})
404+
str := jql.GenerateWhere()
405+
406+
assert.Equal(t, strings.TrimSpace(strExpected), strings.TrimSpace(str))
407+
}
408+
380409
func TestCompositeWithoutOperand(t *testing.T) {
381410
var sqlTest = `
382411
{

0 commit comments

Comments
 (0)