Skip to content

Commit d362d87

Browse files
committed
jar test와 parse 메소드의 결과가 달랐다
1 parent c756777 commit d362d87

File tree

8 files changed

+108
-88
lines changed

8 files changed

+108
-88
lines changed

.idea/workspace.xml

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>json-to-sql-parser</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
</natures>
11+
</projectDescription>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"COLUMN":["EC_DV AS staff_gbn"],"CRUD":["SELECT"],"GROUP_BY":["GROUP BY EC_DV"],"TABLE":["ETCH004M"],"UNION 1":["SELECT EC_CLPS_DV_CD AS staff_gbn FROM ETCH005M WHERE NVL(USE_AYN, 'Y') <> 'N'"],"UNION ANALYSE 1":{"COLUMN":["EC_CLPS_DV_CD AS staff_gbn"],"CRUD":["SELECT"],"TABLE":["ETCH005M"],"WHERE":["NVL(USE_AYN, 'Y') <> 'N'"]},"WHERE":["(TO_DATE(SCH_REG_DT, 'YYYY-MM-DD') BETWEEN TO_DATE(SYSDATE, 'YYYY-MM-DD') AND TO_DATE(SYSDATE, 'YYYY-MM-DD'))"]}
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(SELECT EC_DV AS staff_gbn FROM ETCH004M WHERE (TO_DATE(SCH_REG_DT, 'YYYY-MM-DD') BETWEEN TO_DATE(SYSDATE, 'YYYY-MM-DD') AND TO_DATE(SYSDATE, 'YYYY-MM-DD')) GROUP BY GROUP BY EC_DV) UNION (SELECT EC_CLPS_DV_CD AS staff_gbn FROM ETCH005M WHERE NVL(USE_AYN, 'Y') <> 'N')
Lines changed: 88 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,57 @@
11
package com.inzapp.jsonToSqlParser;
22

3+
import com.inzapp.jsonToSqlParser.config.Config;
34
import com.inzapp.jsonToSqlParser.core.Parser;
45
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
56
import org.json.JSONObject;
67

7-
import java.nio.charset.StandardCharsets;
8-
import java.security.MessageDigest;
9-
import java.security.NoSuchAlgorithmException;
8+
import java.io.BufferedReader;
9+
import java.io.FileOutputStream;
10+
import java.io.FileReader;
1011

1112
public class JsonToSqlParser extends Parser {
12-
// /**
13-
// * entry point in execution jar file
14-
// *
15-
// * @param args [0] : input file name, default is "input.json"
16-
// * [1] : output file name, default is "output.txt"
17-
// */
18-
// public static void main(String[] args) {
19-
// String inputFileName = Config.INPUT_FILE_NAME;
20-
// String outputFileName = Config.OUTPUT_FILE_NAME;
21-
// if (args != null && args.length == 2) {
22-
// inputFileName = args[0];
23-
// outputFileName = args[1];
24-
// }
25-
//
26-
// JsonToSqlParser jsonToSqlParser = new JsonToSqlParser();
27-
// JSONObject json = jsonToSqlParser.readJsonFromFile(inputFileName);
28-
// if (json == null) {
29-
// System.out.println("failed to load json");
30-
// return;
31-
// }
32-
//
33-
// String sql = jsonToSqlParser.parse(json);
34-
// if (sql == null) {
35-
// System.out.println("parse failure");
36-
// return;
37-
// }
38-
//
39-
// try {
40-
// System.out.println("input json\n");
41-
// System.out.println(json.toString(4));
42-
// System.out.println();
43-
//
44-
// System.out.println("output sql\n");
45-
// System.out.println(sql);
46-
// System.out.println();
47-
// } catch (Exception e) {
48-
// e.printStackTrace();
49-
// }
50-
//
51-
// jsonToSqlParser.saveFile(sql, outputFileName);
52-
// System.out.println("parse success");
53-
// }
13+
/**
14+
* entry point in execution jar file
15+
*
16+
* @param args [0] : input file name, default is "input.json"
17+
* [1] : output file name, default is "output.txt"
18+
*/
19+
public static void main(String[] args) {
20+
String inputFileName = Config.INPUT_FILE_NAME;
21+
String outputFileName = Config.OUTPUT_FILE_NAME;
22+
if (args != null && args.length == 2) {
23+
inputFileName = args[0];
24+
outputFileName = args[1];
25+
}
26+
27+
JsonToSqlParser jsonToSqlParser = new JsonToSqlParser();
28+
JSONObject json = jsonToSqlParser.readJsonFromFile(inputFileName);
29+
if (json == null) {
30+
System.out.println("failed to load json");
31+
return;
32+
}
33+
34+
String sql = new JsonToSqlParser().parse(json.toString());
35+
if (sql == null) {
36+
System.out.println("parse failure");
37+
return;
38+
}
39+
40+
try {
41+
System.out.println("input json\n");
42+
System.out.println(json.toString(4));
43+
System.out.println();
44+
45+
System.out.println("output sql\n");
46+
System.out.println(sql);
47+
System.out.println();
48+
} catch (Exception e) {
49+
e.printStackTrace();
50+
}
51+
52+
jsonToSqlParser.saveFile(sql, outputFileName);
53+
System.out.println("parse success");
54+
}
5455

5556
/**
5657
* head method as java library
@@ -60,51 +61,53 @@ public class JsonToSqlParser extends Parser {
6061
*/
6162
public String parse(String jsonString) {
6263
try {
63-
String sql = new Parser().parse(new JSONObject(jsonString));
64+
JSONObject json = new JSONObject(jsonString);
65+
String sql = new Parser().parse(json);
6466
CCJSqlParserUtil.parse(sql); // query execution test
6567
return sql;
6668
} catch (Exception e) {
69+
e.printStackTrace();
70+
return null;
71+
}
72+
}
73+
74+
/**
75+
* read json string from file and convert is to json object
76+
*
77+
* @param fileName input file name, default is "input.json"
78+
* @return converted json object
79+
*/
80+
private JSONObject readJsonFromFile(String fileName) {
81+
try {
82+
BufferedReader br = new BufferedReader(new FileReader(fileName));
83+
StringBuilder sb = new StringBuilder();
84+
while (true) {
85+
String line = br.readLine();
86+
if (line == null)
87+
break;
88+
89+
sb.append(line).append('\n');
90+
}
91+
String jsonString = sb.toString();
92+
return new JSONObject(jsonString);
93+
} catch (Exception e) {
94+
e.printStackTrace();
6795
return null;
6896
}
6997
}
7098

71-
// /**
72-
// * read json string from file and convert is to json object
73-
// *
74-
// * @param fileName input file name, default is "input.json"
75-
// * @return converted json object
76-
// */
77-
// private JSONObject readJsonFromFile(String fileName) {
78-
// try {
79-
// BufferedReader br = new BufferedReader(new FileReader(fileName));
80-
// StringBuilder sb = new StringBuilder();
81-
// while (true) {
82-
// String line = br.readLine();
83-
// if (line == null)
84-
// break;
85-
//
86-
// sb.append(line).append('\n');
87-
// }
88-
// String jsonString = sb.toString();
89-
// return new JSONObject(jsonString);
90-
// } catch (Exception e) {
91-
// e.printStackTrace();
92-
// return null;
93-
// }
94-
// }
95-
//
96-
// /**
97-
// * save converted sql string to file
98-
// *
99-
// * @param sql converted sql from parser
100-
// * @param fileName output file name, default is "output.txt"
101-
// */
102-
// private void saveFile(String sql, String fileName) {
103-
// try {
104-
// FileOutputStream fos = new FileOutputStream(fileName);
105-
// fos.write(sql.getBytes());
106-
// } catch (Exception e) {
107-
// e.printStackTrace();
108-
// }
109-
// }
99+
/**
100+
* save converted sql string to file
101+
*
102+
* @param sql converted sql from parser
103+
* @param fileName output file name, default is "output.txt"
104+
*/
105+
private void saveFile(String sql, String fileName) {
106+
try {
107+
FileOutputStream fos = new FileOutputStream(fileName);
108+
fos.write(sql.getBytes());
109+
} catch (Exception e) {
110+
e.printStackTrace();
111+
}
112+
}
110113
}

0 commit comments

Comments
 (0)