Skip to content

Commit 1d55af7

Browse files
committed
Add featrure Union
1 parent c70a315 commit 1d55af7

2 files changed

Lines changed: 143 additions & 4 deletions

File tree

jql.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,29 @@ import (
1010
)
1111

1212
type Json2Sql struct {
13-
sqlJson *SQLJson
13+
sqlJson *SQLJson
14+
sqlJsonSelectUnion *[]SQLJson
1415
}
1516

16-
func NewJson2Sql(jsonData json.RawMessage) (*Json2Sql, error) {
17+
func NewJson2Sql(jsonData json.RawMessage, isUnion ...bool) (*Json2Sql, error) {
18+
if isUnion != nil && isUnion[0] {
19+
var sqlJsonUnion []SQLJson
20+
21+
err := json.Unmarshal([]byte(jsonData), &sqlJsonUnion)
22+
if err != nil {
23+
return nil, fmt.Errorf("error: %s", err)
24+
}
25+
return &Json2Sql{sqlJsonSelectUnion: &sqlJsonUnion}, nil
26+
}
27+
1728
var sqlJson *SQLJson
1829

1930
err := json.Unmarshal(jsonData, &sqlJson)
2031

2132
if err != nil {
2233
return nil, fmt.Errorf("error: %s", err)
2334
}
24-
25-
return &Json2Sql{sqlJson}, nil
35+
return &Json2Sql{sqlJson: sqlJson}, nil
2636
}
2737

2838
func cleanSpaces(input string) string {
@@ -405,3 +415,34 @@ func (jql *Json2Sql) Generate() (string, []interface{}, error) {
405415

406416
return newQuery, values, nil
407417
}
418+
419+
func (jql *Json2Sql) buildRawUnion() string {
420+
var sql string
421+
var sqlUnion []string
422+
423+
if jql.sqlJsonSelectUnion != nil {
424+
for _, v := range *jql.sqlJsonSelectUnion {
425+
jql.sqlJson = &v
426+
strBuild := jql.rawBuild()
427+
sqlUnion = append(sqlUnion, strBuild)
428+
}
429+
}
430+
431+
jql.sqlJson = nil
432+
sql = strings.Join(sqlUnion, " UNION ")
433+
434+
return sql
435+
}
436+
437+
func (jql *Json2Sql) BuildUnion() string {
438+
sqlCleanValue := jql.rawValueExtractor(jql.buildRawUnion())
439+
440+
return cleanSpaces(sqlCleanValue)
441+
}
442+
443+
func (jql *Json2Sql) GenerateUnion() (string, []interface{}, error) {
444+
sql := jql.buildRawUnion()
445+
newQuery, values := jql.MaskedQueryValue(sql)
446+
447+
return newQuery, values, nil
448+
}

jql_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ func TestConstructor_Fail(t *testing.T) {
1919
assert.NotNil(t, err)
2020
}
2121

22+
func TestConstructor_Fail_Union(t *testing.T) {
23+
var _, err = NewJson2Sql([]byte(`[{"table":"test"`), true)
24+
assert.NotNil(t, err)
25+
}
26+
2227
func TestRawJson_OK(t *testing.T) {
2328
var expected = "hello"
2429

@@ -1019,3 +1024,96 @@ func TestGenerateJsonToSql(t *testing.T) {
10191024
assert.Equal(t, strExpectation, sql)
10201025
assert.Equal(t, []interface{}{float64(1), "foo", true, float64(100), float64(1), "2020-01-01", "2023-01-01", "2", float64(10)}, filter)
10211026
}
1027+
1028+
func TestBuildRawUnion(t *testing.T) {
1029+
jsonData := `
1030+
[
1031+
{
1032+
"table": "table_1",
1033+
"selectFields": [
1034+
"a",
1035+
"b"
1036+
],
1037+
"conditions": [
1038+
{
1039+
"datatype": "number",
1040+
"clause": "a",
1041+
"operator": "=",
1042+
"value": 1
1043+
}
1044+
],
1045+
"limit": 1
1046+
},
1047+
{
1048+
"table": "table_2",
1049+
"selectFields": [
1050+
"a",
1051+
"b"
1052+
],
1053+
"conditions": [
1054+
{
1055+
"datatype": "number",
1056+
"clause": "a",
1057+
"operator": "=",
1058+
"value": 1
1059+
}
1060+
],
1061+
"limit": 1
1062+
}
1063+
]
1064+
`
1065+
1066+
jql, _ := NewJson2Sql([]byte(jsonData), true)
1067+
sql := jql.BuildUnion()
1068+
1069+
strExpectation := "SELECT a, b FROM table_1 WHERE a = 1 LIMIT 1 UNION SELECT a, b FROM table_2 WHERE a = 1 LIMIT 1"
1070+
1071+
assert.Equal(t, strExpectation, sql)
1072+
}
1073+
1074+
func TestGenerateUnion(t *testing.T) {
1075+
jsonData := `
1076+
[
1077+
{
1078+
"table": "table_1",
1079+
"selectFields": [
1080+
"a",
1081+
"b"
1082+
],
1083+
"conditions": [
1084+
{
1085+
"datatype": "number",
1086+
"clause": "a",
1087+
"operator": "=",
1088+
"value": 1
1089+
}
1090+
],
1091+
"limit": 1
1092+
},
1093+
{
1094+
"table": "table_2",
1095+
"selectFields": [
1096+
"a",
1097+
"b"
1098+
],
1099+
"conditions": [
1100+
{
1101+
"datatype": "number",
1102+
"clause": "a",
1103+
"operator": "=",
1104+
"value": 1
1105+
}
1106+
],
1107+
"limit": 1
1108+
}
1109+
]
1110+
`
1111+
1112+
jql, _ := NewJson2Sql([]byte(jsonData), true)
1113+
sql, filter, _ := jql.GenerateUnion()
1114+
1115+
strExpectation := "SELECT a, b FROM table_1 WHERE a = ? LIMIT 1 UNION SELECT a, b FROM table_2 WHERE a = ? LIMIT 1"
1116+
1117+
assert.Equal(t, strExpectation, sql)
1118+
assert.Equal(t, []interface{}{float64(1), float64(1)}, filter)
1119+
}

0 commit comments

Comments
 (0)