Skip to content

Commit 9bae885

Browse files
committed
Fix raw datatype
1 parent 6e21e2a commit 9bae885

3 files changed

Lines changed: 94 additions & 2 deletions

File tree

jql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (jql *Json2Sql) isStringNumeric(s string) bool {
114114
}
115115

116116
func (jql *Json2Sql) MaskedQueryValue(query string) (string, []interface{}) {
117-
sRegex := fmt.Sprintf(`%s'([^\s]+)'%s|%s([^\s]+)%s`, JQL_FLAG_OPEN, JQL_FLAG_CLOSE, JQL_FLAG_OPEN, JQL_FLAG_CLOSE)
117+
sRegex := fmt.Sprintf(`%s(.*?)%s`, JQL_FLAG_OPEN, JQL_FLAG_CLOSE)
118118
re := regexp.MustCompile(sRegex)
119119
matches := re.FindAllStringSubmatch(query, -1)
120120

jql_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gojson2sql
22

33
import (
4+
"fmt"
45
"reflect"
56
"strings"
67
"testing"
@@ -1078,6 +1079,96 @@ func TestGenerateJsonToSql(t *testing.T) {
10781079
assert.Equal(t, []interface{}{float64(1), "foo", true, float64(100), float64(1), "2020-01-01", "2023-01-01", "2", float64(10)}, filter)
10791080
}
10801081

1082+
func TestGenerateWithStaticCond(t *testing.T) {
1083+
jsonData := `
1084+
{
1085+
"table": "v_transaction_redemptions",
1086+
"selectFields": [
1087+
"v_transaction_redemptions.transaction_number",
1088+
"v_transaction_redemptions.customer_id",
1089+
"v_transaction_redemptions.customer_name",
1090+
"v_transaction_redemptions.amounts"
1091+
],
1092+
"conditions": [
1093+
{
1094+
"operand": "and",
1095+
"clause": "v_transaction_redemptions.transaction_date",
1096+
"datatype": "STRING",
1097+
"isStatic": false,
1098+
"operator": "=",
1099+
"value": "2024-03-08 23:59:59"
1100+
},
1101+
{
1102+
"operand": "and",
1103+
"clause": "v_transaction_redemptions.customer_id",
1104+
"datatype": "STRING",
1105+
"isStatic": true,
1106+
"operator": "=",
1107+
"value": "15"
1108+
}
1109+
]
1110+
}
1111+
`
1112+
1113+
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{})
1114+
sql, _, _ := jql.Generate()
1115+
1116+
fmt.Println(sql)
1117+
1118+
// strExpectation := "SELECT table_1.a, table_1.b AS foo_bar, (SELECT * FROM table_4 WHERE a = ? LIMIT 1) AS baz, table_2.b, table_3.a, table_3.b FROM table_1 JOIN table_2 ON table_2.a = table_1.a LEFT JOIN table_3 ON table_3.a = table_2.a WHERE table_1.a = ? AND table_1.b = ? AND table_2.a > sum(?) AND table_2.b = (SELECT * FROM table_4 WHERE a = ? LIMIT 1) OR (table_3.a BETWEEN ? AND ? AND table_3.b = ?) GROUP BY table_1.a HAVING COUNT(table_2.a) > ? ORDER BY table_1.a, table_2.a ASC LIMIT 1 OFFSET 0"
1119+
// assert.Equal(t, strExpectation, sql)
1120+
// assert.Equal(t, []interface{}{float64(1), "foo", true, float64(100), float64(1), "2020-01-01", "2023-01-01", "2", float64(10)}, filter)
1121+
}
1122+
1123+
func TestRawFunction(t *testing.T) {
1124+
jsonData := `
1125+
{
1126+
"selectFields": [
1127+
{
1128+
"alias": "amounts",
1129+
"addFunction": {
1130+
"sqlFunc": {
1131+
"name": "sum",
1132+
"isField": true,
1133+
"params": [
1134+
"v_transaction_redemptions.amounts"
1135+
]
1136+
}
1137+
}
1138+
}
1139+
],
1140+
"conditions": [
1141+
{
1142+
"isStatic": false,
1143+
"datatype": "raw",
1144+
"clause": "v_transaction_redemptions.transaction_date",
1145+
"operator": ">=",
1146+
"operand": "AND",
1147+
"value": "CURRENT_DATE - INTERVAL '3 months'"
1148+
},
1149+
{
1150+
"isStatic": false,
1151+
"datatype": "number",
1152+
"clause": "v_transaction_redemptions.customer_id",
1153+
"operator": "=",
1154+
"operand": "AND",
1155+
"value": 15
1156+
}
1157+
],
1158+
"table": "v_transaction_redemptions"
1159+
}
1160+
`
1161+
1162+
jql, _ := NewJson2Sql([]byte(jsonData), &Json2SqlConf{WithSanitizedInjection: true})
1163+
sql, _, _ := jql.Generate()
1164+
1165+
fmt.Println(sql)
1166+
1167+
// strExpectation := "SELECT table_1.a, table_1.b AS foo_bar, (SELECT * FROM table_4 WHERE a = ? LIMIT 1) AS baz, table_2.b, table_3.a, table_3.b FROM table_1 JOIN table_2 ON table_2.a = table_1.a LEFT JOIN table_3 ON table_3.a = table_2.a WHERE table_1.a = ? AND table_1.b = ? AND table_2.a > sum(?) AND table_2.b = (SELECT * FROM table_4 WHERE a = ? LIMIT 1) OR (table_3.a BETWEEN ? AND ? AND table_3.b = ?) GROUP BY table_1.a HAVING COUNT(table_2.a) > ? ORDER BY table_1.a, table_2.a ASC LIMIT 1 OFFSET 0"
1168+
// assert.Equal(t, strExpectation, sql)
1169+
// assert.Equal(t, []interface{}{float64(1), "foo", true, float64(100), float64(1), "2020-01-01", "2023-01-01", "2", float64(10)}, filter)
1170+
}
1171+
10811172
func TestBuildRawUnion(t *testing.T) {
10821173
jsonData := `
10831174
[

sql_datatype.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ func ExtractValueByDataType(datatype SQLDataTypeEnum, value json.RawMessage, isS
112112
json.Unmarshal(value, &valueNumber)
113113
return jqlFlagOpen + string(value) + jqlFlagClose
114114
case Raw:
115-
return jqlFlagOpen + string(value) + jqlFlagClose
115+
// fmt.Println("YAP", jqlFlagOpen+strings.Trim(string(value), `"`)+jqlFlagClose)
116+
return jqlFlagOpen + strings.Trim(string(value), `"`) + jqlFlagClose
116117
case Array:
117118
return ArrayConversionToStringExpression(value, isStatic)
118119
case Function:

0 commit comments

Comments
 (0)