Skip to content

Commit 3fa3a29

Browse files
committed
sql json parser verbose
1 parent 7616b8f commit 3fa3a29

File tree

5 files changed

+86
-104
lines changed

5 files changed

+86
-104
lines changed

.idea/workspace.xml

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
-1.03 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 81 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,52 @@
11
package com.inzapp.jsonToSqlParser;
22

3-
import com.inzapp.jsonToSqlParser.config.Config;
43
import com.inzapp.jsonToSqlParser.core.Parser;
54
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
65
import org.json.JSONObject;
76

8-
import java.io.BufferedReader;
9-
import java.io.FileOutputStream;
10-
import java.io.FileReader;
11-
import java.util.Stack;
12-
137
public class JsonToSqlParser extends Parser {
14-
/**
15-
* entry point in execution jar file
16-
*
17-
* @param args [0] : input file name, default is "input.json"
18-
* [1] : output file name, default is "output.txt"
19-
*/
20-
public static void main(String[] args) {
21-
String inputFileName = Config.INPUT_FILE_NAME;
22-
String outputFileName = Config.OUTPUT_FILE_NAME;
23-
if (args != null && args.length == 2) {
24-
inputFileName = args[0];
25-
outputFileName = args[1];
26-
}
27-
28-
JsonToSqlParser jsonToSqlParser = new JsonToSqlParser();
29-
JSONObject json = jsonToSqlParser.readJsonFromFile(inputFileName);
30-
if (json == null) {
31-
System.out.println("failed to load json");
32-
return;
33-
}
34-
35-
String sql = jsonToSqlParser.parse(json);
36-
if (sql == null) {
37-
System.out.println("parse failure");
38-
return;
39-
}
40-
41-
try {
42-
System.out.println("input json\n");
43-
System.out.println(json.toString(4));
44-
System.out.println();
45-
46-
System.out.println("output sql\n");
47-
System.out.println(sql);
48-
System.out.println();
49-
} catch (Exception e) {
50-
e.printStackTrace();
51-
}
52-
53-
jsonToSqlParser.saveFile(sql, outputFileName);
54-
System.out.println("parse success");
55-
}
56-
57-
private String removeOuterBracket(String sql) {
58-
if (sql.charAt(0) == '(' && sql.charAt(sql.length() - 1) == ')') {
59-
Stack<Boolean> bracketStack = new Stack<>();
60-
for (int i = 0; i < sql.length(); ++i) {
61-
if (sql.charAt(i) == '(') bracketStack.push(true);
62-
else if (sql.charAt(i) == ')') {
63-
bracketStack.pop();
64-
if (bracketStack.size() == 0 && i != sql.length() - 1)
65-
return sql;
66-
}
67-
}
68-
} else return "";
69-
return "";
70-
}
8+
// /**
9+
// * entry point in execution jar file
10+
// *
11+
// * @param args [0] : input file name, default is "input.json"
12+
// * [1] : output file name, default is "output.txt"
13+
// */
14+
// public static void main(String[] args) {
15+
// String inputFileName = Config.INPUT_FILE_NAME;
16+
// String outputFileName = Config.OUTPUT_FILE_NAME;
17+
// if (args != null && args.length == 2) {
18+
// inputFileName = args[0];
19+
// outputFileName = args[1];
20+
// }
21+
//
22+
// JsonToSqlParser jsonToSqlParser = new JsonToSqlParser();
23+
// JSONObject json = jsonToSqlParser.readJsonFromFile(inputFileName);
24+
// if (json == null) {
25+
// System.out.println("failed to load json");
26+
// return;
27+
// }
28+
//
29+
// String sql = jsonToSqlParser.parse(json);
30+
// if (sql == null) {
31+
// System.out.println("parse failure");
32+
// return;
33+
// }
34+
//
35+
// try {
36+
// System.out.println("input json\n");
37+
// System.out.println(json.toString(4));
38+
// System.out.println();
39+
//
40+
// System.out.println("output sql\n");
41+
// System.out.println(sql);
42+
// System.out.println();
43+
// } catch (Exception e) {
44+
// e.printStackTrace();
45+
// }
46+
//
47+
// jsonToSqlParser.saveFile(sql, outputFileName);
48+
// System.out.println("parse success");
49+
// }
7150

7251
/**
7352
* head method as java library
@@ -85,43 +64,43 @@ public String parse(String jsonString) {
8564
}
8665
}
8766

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

0 commit comments

Comments
 (0)