|
1 | 1 | package gojson2sql |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "reflect" |
5 | 6 | "strings" |
6 | 7 | "testing" |
@@ -1078,6 +1079,96 @@ func TestGenerateJsonToSql(t *testing.T) { |
1078 | 1079 | assert.Equal(t, []interface{}{float64(1), "foo", true, float64(100), float64(1), "2020-01-01", "2023-01-01", "2", float64(10)}, filter) |
1079 | 1080 | } |
1080 | 1081 |
|
| 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 | + |
1081 | 1172 | func TestBuildRawUnion(t *testing.T) { |
1082 | 1173 | jsonData := ` |
1083 | 1174 | [ |
|
0 commit comments