Skip to content

Commit

Permalink
Update property
Browse files Browse the repository at this point in the history
  • Loading branch information
bonkzero404 committed Feb 13, 2024
1 parent c4b8321 commit 6e21e2a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
12 changes: 6 additions & 6 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,45 +162,45 @@ func BenchmarkJson2Sql_Union_BuildRaw(b *testing.B) {
var newData = "[" + jsonData + "," + jsonData + "]"

for i := 0; i < b.N; i++ {
jql, _ := NewJson2Sql([]byte(newData), &Json2SqlConf{withUnion: true})
jql, _ := NewJson2Sql([]byte(newData), &Json2SqlConf{WithUnion: true})
jql.BuildUnion()
}
}

func BenchmarkJson2Sql_Union_Generate(b *testing.B) {
var newData = "[" + jsonData + "," + jsonData + "]"
for i := 0; i < b.N; i++ {
jql, _ := NewJson2Sql([]byte(newData), &Json2SqlConf{withUnion: true})
jql, _ := NewJson2Sql([]byte(newData), &Json2SqlConf{WithUnion: true})
jql.GenerateUnion()
}
}

func BenchmarkJson2Sql_BuildRaw_WithSanitizedSQLi(b *testing.B) {
for i := 0; i < b.N; i++ {
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{withSanitizedInjection: true})
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{WithSanitizedInjection: true})
jql.Build()
}
}

func BenchmarkJson2Sql_Generate_WithSanitizedSQLi(b *testing.B) {
for i := 0; i < b.N; i++ {
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{withSanitizedInjection: true})
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{WithSanitizedInjection: true})
jql.Generate()
}
}

func BenchmarkJson2Sql_Union_BuildRaw_WithSanitizedSQLi(b *testing.B) {
var newData = "[" + jsonData + "," + jsonData + "]"
for i := 0; i < b.N; i++ {
jql, _ := NewJson2Sql([]byte(newData), &Json2SqlConf{withSanitizedInjection: true, withUnion: true})
jql, _ := NewJson2Sql([]byte(newData), &Json2SqlConf{WithSanitizedInjection: true, WithUnion: true})
jql.BuildUnion()
}
}

func BenchmarkJson2Sql_Union_Generate_WithSanitizedSQLi(b *testing.B) {
var newData = "[" + jsonData + "," + jsonData + "]"
for i := 0; i < b.N; i++ {
jql, _ := NewJson2Sql([]byte(newData), &Json2SqlConf{withSanitizedInjection: true, withUnion: true})
jql, _ := NewJson2Sql([]byte(newData), &Json2SqlConf{WithSanitizedInjection: true, WithUnion: true})
jql.GenerateUnion()
}
}
22 changes: 11 additions & 11 deletions jql.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

type Json2SqlConf struct {
withUnion bool
withSanitizedInjection bool
WithUnion bool
WithSanitizedInjection bool
}
type Json2Sql struct {
sqlJson *SQLJson
Expand All @@ -24,7 +24,7 @@ func NewJson2Sql(jsonData []byte, conf *Json2SqlConf) (*Json2Sql, error) {
var sqlJson *SQLJson
var sqlJsonUnion *[]SQLJson

if conf != nil && conf.withUnion {
if conf != nil && conf.WithUnion {

err := json.Unmarshal(jsonData, &sqlJsonUnion)
if err != nil {
Expand Down Expand Up @@ -185,7 +185,7 @@ func (jql *Json2Sql) GenerateSelectFrom(selection ...json.RawMessage) string {
if sqlSelectDetail.SubQuery != nil {
jsonBytes, _ := json.Marshal(*sqlSelectDetail.SubQuery)

jql, _ := NewJson2Sql(jsonBytes, &Json2SqlConf{withSanitizedInjection: jql.config.withSanitizedInjection})
jql, _ := NewJson2Sql(jsonBytes, &Json2SqlConf{WithSanitizedInjection: jql.config.WithSanitizedInjection})

field = fmt.Sprintf("(%s) AS %s", jql.rawBuild(), *sqlSelectDetail.Alias)

Expand Down Expand Up @@ -218,7 +218,7 @@ func (jql *Json2Sql) GenerateSelectFrom(selection ...json.RawMessage) string {
if isSelectExpect {
if selectExpect.SubQuery != nil {
jsonBytes, _ := json.Marshal(*selectExpect.SubQuery)
jql, _ := NewJson2Sql(jsonBytes, &Json2SqlConf{withSanitizedInjection: jql.config.withSanitizedInjection})
jql, _ := NewJson2Sql(jsonBytes, &Json2SqlConf{WithSanitizedInjection: jql.config.WithSanitizedInjection})
defaultValue = fmt.Sprintf("(%s)", jql.rawBuild())
}
}
Expand Down Expand Up @@ -355,7 +355,7 @@ func (jql *Json2Sql) GenerateConditions(conditions ...Condition) string {
if isSelectSub {
if selectSub.SubQuery != nil {
jsonBytes, _ := json.Marshal(*selectSub.SubQuery)
jql, _ := NewJson2Sql(jsonBytes, &Json2SqlConf{withSanitizedInjection: jql.config.withSanitizedInjection})
jql, _ := NewJson2Sql(jsonBytes, &Json2SqlConf{WithSanitizedInjection: jql.config.WithSanitizedInjection})
expression = string(condition.Operator) + " " + fmt.Sprintf("(%s)", jql.rawBuild())
}
}
Expand All @@ -379,7 +379,7 @@ func (jql *Json2Sql) GenerateConditions(conditions ...Condition) string {
if isSelectExpect {
if selectExpect.SubQuery != nil {
jsonBytes, _ := json.Marshal(*selectExpect.SubQuery)
jql, _ := NewJson2Sql(jsonBytes, &Json2SqlConf{withSanitizedInjection: jql.config.withSanitizedInjection})
jql, _ := NewJson2Sql(jsonBytes, &Json2SqlConf{WithSanitizedInjection: jql.config.WithSanitizedInjection})
expect = fmt.Sprintf("(%s)", jql.rawBuild())
}
}
Expand Down Expand Up @@ -440,7 +440,7 @@ func (jql *Json2Sql) rawBuild() string {
func (jql *Json2Sql) Build() string {
sqlCleanValue := jql.rawValueExtractor(jql.concateQueryString())

if jql.config != nil && jql.config.withSanitizedInjection && !isValidSQL(sqlCleanValue) {
if jql.config != nil && jql.config.WithSanitizedInjection && !isValidSQL(sqlCleanValue) {
return "Invalid sql string you've got sanitized SQL string"
}

Expand All @@ -450,7 +450,7 @@ func (jql *Json2Sql) Build() string {
func (jql *Json2Sql) Generate() (string, []interface{}, error) {
sql := jql.rawBuild()

if jql.config != nil && jql.config.withSanitizedInjection && !isValidSQL(sql) {
if jql.config != nil && jql.config.WithSanitizedInjection && !isValidSQL(sql) {
return "", nil, fmt.Errorf("error: %s", "Invalid sql string you've got sanitized SQL string")
}

Expand Down Expand Up @@ -480,7 +480,7 @@ func (jql *Json2Sql) buildRawUnion() string {
func (jql *Json2Sql) BuildUnion() string {
sqlCleanValue := jql.rawValueExtractor(jql.buildRawUnion())

if jql.config != nil && jql.config.withSanitizedInjection && !isValidSQL(sqlCleanValue) {
if jql.config != nil && jql.config.WithSanitizedInjection && !isValidSQL(sqlCleanValue) {
return "Invalid sql string you've got sanitized SQL string"
}

Expand All @@ -490,7 +490,7 @@ func (jql *Json2Sql) BuildUnion() string {
func (jql *Json2Sql) GenerateUnion() (string, []interface{}, error) {
sql := jql.buildRawUnion()

if jql.config != nil && jql.config.withSanitizedInjection && !isValidSQL(sql) {
if jql.config != nil && jql.config.WithSanitizedInjection && !isValidSQL(sql) {
return "", nil, fmt.Errorf("error: %s", "Invalid sql string you've got sanitized SQL string")
}

Expand Down
14 changes: 7 additions & 7 deletions jql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestConstructor_Fail(t *testing.T) {
}

func TestConstructor_Fail_Union(t *testing.T) {
var _, err = NewJson2Sql([]byte(`[{"table":"test"`), &Json2SqlConf{withUnion: true})
var _, err = NewJson2Sql([]byte(`[{"table":"test"`), &Json2SqlConf{WithUnion: true})
assert.NotNil(t, err)
}

Expand Down Expand Up @@ -1158,7 +1158,7 @@ func TestBuildRawUnion(t *testing.T) {
]
`

jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{withUnion: true})
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{WithUnion: true})
sql := jql.BuildUnion()

strExpectation := "SELECT a, b, table_1.b AS foo_bar, (SELECT a, b FROM table_4 WHERE a = 1 LIMIT 1) AS baz FROM table_1 WHERE a = 1 LIMIT 1 UNION SELECT a, b, table_1.b AS foo_bar, (SELECT a, b FROM table_4 WHERE a = 1 LIMIT 1) AS baz FROM table_2 WHERE a = 1 LIMIT 1"
Expand Down Expand Up @@ -1204,7 +1204,7 @@ func TestGenerateUnion(t *testing.T) {
]
`

jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{withUnion: true})
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{WithUnion: true})
sql, filter, _ := jql.GenerateUnion()

strExpectation := "SELECT a, b FROM table_1 WHERE a = ? LIMIT 1 UNION SELECT a, b FROM table_2 WHERE a = ? LIMIT 1"
Expand Down Expand Up @@ -1233,7 +1233,7 @@ func TestGenerateBuild_PreventInjection(t *testing.T) {
}
`

jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{withSanitizedInjection: true})
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{WithSanitizedInjection: true})
sql := jql.Build()

strExpectation := "Invalid sql string you've got sanitized SQL string"
Expand Down Expand Up @@ -1261,7 +1261,7 @@ func TestGenerate_PreventInjection(t *testing.T) {
}
`

jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{withSanitizedInjection: true})
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{WithSanitizedInjection: true})
_, _, err := jql.Generate()

assert.NotNil(t, err)
Expand Down Expand Up @@ -1305,7 +1305,7 @@ func TestGenerateBuildUnion_PreventInjection(t *testing.T) {
]
`

jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{withSanitizedInjection: true, withUnion: true})
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{WithSanitizedInjection: true, WithUnion: true})
sql := jql.BuildUnion()

strExpectation := "Invalid sql string you've got sanitized SQL string"
Expand Down Expand Up @@ -1351,7 +1351,7 @@ func TestGenerateUnion_PreventInjection(t *testing.T) {
]
`

jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{withSanitizedInjection: true, withUnion: true})
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{WithSanitizedInjection: true, WithUnion: true})
_, _, err := jql.GenerateUnion()

assert.NotNil(t, err)
Expand Down

0 comments on commit 6e21e2a

Please sign in to comment.