-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalyzeInsert.java
76 lines (69 loc) · 2.73 KB
/
AnalyzeInsert.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.*;
import java.util.*;
import gudusoft.gsqlparser.nodes.TMultiTarget;
import gudusoft.gsqlparser.stmt.*;
public class AnalyzeInsert {
protected static void analyzeInsertStmt(TInsertSqlStatement pStmt){
String table_name="";
// out;
String append="";
String token=",";
TMultiTarget mt = null;
if (pStmt.getTargetTable() != null){
table_name=pStmt.getTargetTable().toString();
//System.out.println("Table name:"+table_name);
}
//System.out.println("insert value type:"+pStmt.getValueType());
/* not used as no of col are fixed and must be equal to actual in table else error
if (pStmt.getColumnList() != null){
System.out.println("columns:");
for(int i=0;i<pStmt.getColumnList().size();i++){
System.out.println("\t"+pStmt.getColumnList().getObjectName(i).toString());
}
}
*/
//int []arr=new Integer();
ArrayList<Integer> v=new ArrayList<Integer>();
if (pStmt.getValues() != null){
// System.out.println("values:");
for(int i=0;i<pStmt.getValues().size();i++){
mt = pStmt.getValues().getMultiTarget(i);
for(int j=0;j<mt.getColumnList().size();j++){
// System.out.println("\t"+mt.getColumnList().getResultColumn(j).toString());
append=append+mt.getColumnList().getResultColumn(j).toString();
v.add(Integer.parseInt(mt.getColumnList().getResultColumn(j).toString()));
if(j!=mt.getColumnList().size()-1)
append=append+token;
}
}
}
// System.out.println("String to append "+append);
if(Main_file.table.containsKey(table_name))
{
//checking cols should be n
List<String> s=Main_file.table.get(table_name);
int i=0;
for(String d:s) //updating DATA of main file
{
Main_file.data.get(table_name).get(d).add(v.get(i));
i++;
}
if(mt!=null && s!=null && ((s.size()-1)==mt.getColumnList().size()-1))
{
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(table_name+".csv", true))))
{
out.println(append);
out.close();
//System.out.println("String added");
}catch (IOException e) {
System.out.println("ERROR!!!");
//System.out.println(e.printStackTrace());
}
}
else
System.out.println("ERROR:No. of columns mismatched!!!");
}
else
System.out.println("ERROR:Table Does not exist");
}
}