-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_file.java
201 lines (184 loc) · 6.23 KB
/
Main_file.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.util.*;
import java.io.*;
import java.util.Map.Entry;
import gudusoft.gsqlparser.*;
import gudusoft.gsqlparser.nodes.TTruncateTableSqlNode;
import gudusoft.gsqlparser.stmt.*;
public class Main_file {
/**
* @param args
*/
public static Map<String,List<String>> table=new HashMap<String,List<String>>();
public static Map<String,Map<String,ArrayList<Integer>>> data=new HashMap<String,Map<String,ArrayList<Integer>>>();
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String curr="",next="",table_name;
//int table_count=0,col_count=0;
BufferedReader br = new BufferedReader(new FileReader("metadata.txt"));
curr = br.readLine();
while (curr != null)
{
if(curr.equals("<begin_table>"))
{
//col_count=0;
//table_count++;
table_name=br.readLine();
//System.out.println("TAble name= "+table_name);
next=br.readLine();
ArrayList<String> cols=new ArrayList<String>();
//open file table.csv
//read line split on , agr split hone k bd m list m daal du string ko
while(!next.equals("<end_table>"))
{
//col_count++;
//System.out.println("col name: "+next);
cols.add(next);
Map<String,ArrayList<Integer>> lmap=new HashMap<String,ArrayList<Integer>>();
ArrayList<Integer> l=new ArrayList<Integer>();
lmap.put(next, l);
if(data.get(table_name)==null)
{
data.put(table_name,lmap);
}
if(!data.get(table_name).containsKey(next))
{
data.get(table_name).put(next,l);
}
//System.out.println(data);
next=br.readLine();
}
table.put(table_name,cols);
}
curr=br.readLine();
}
br.close();
//System.out.println(data);
BufferedReader br1;
int count=0;
//System.out.println("----NNum of tables---------------"+data.size());
//for every tables in map
for(Entry<String,List<String>> entry:table.entrySet())
{
ArrayList<Integer> l;
br1 = new BufferedReader(new FileReader(entry.getKey()+".csv"));
List<String> s = entry.getValue(); //list of cols of table
Map<String,ArrayList<Integer>> tmphash=new HashMap<String,ArrayList<Integer>>();
tmphash=data.get(entry.getKey());
curr=br1.readLine();
while(curr!=null)
{
//System.out.println("Curr line "+curr);
StringTokenizer st = new StringTokenizer(curr,",");
count=0;
while(st.hasMoreElements())
{
String token = st.nextToken();
//System.out.println("Token == "+token);
l=tmphash.get(s.get(count));
if(l==null)
l=new ArrayList<Integer>();
if(token.startsWith("\""))
{
//System.out.println("Starts with double quotes");
token = token.substring(1);
token=token.substring(0, token.length()-1);
int num = Integer.parseInt(token);
l.add(num);
data.get(entry.getKey()).put(s.get(count),l);
}
else
{
int num = Integer.parseInt(token);
l.add(num);
data.get(entry.getKey()).put(s.get(count),l);
//System.out.println(l.size()+" size of list \n List Added to col "+s.get(count));
}
count++;
}
curr=br1.readLine();
}
br1.close();
}
// System.out.println("Size ==="+table.size());
// System.out.println(data.get("table1"));
// System.out.println(data.get("t2"));
TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle);
//String query = "Select * from table1";
//String query = "create TABLE t1(A number ,C number)";
//String query = "insert into t1 values (1,2)";
//String query = "delete from t1 where A=1";
// String query ="TRUNCATE TABLE t1";
//String query = "DROP TABLE t1";
//String query = "select A,C from table1";
//String query = "select * from t2";
//String query = "select avg(A) from table1";
//String query = "select col1,table1.col2 from table1,table2 where col3=20";
String query = "select col1,table1.col2 from table1,table2";
while(true)
{
Scanner sc=new Scanner(System.in);
System.out.print("MySql>");
query=sc.nextLine();
if(query.equalsIgnoreCase("exit"))
System.exit(0);
//System.out.println(query);
sqlparser.sqltext = query;
//sqlparser.sqltext=""+args[0]+"";
int ret = sqlparser.parse();
if(ret == 0)
{
for(int i=0;i<sqlparser.sqlstatements.size();i++)
{
analyzeStmt(sqlparser.sqlstatements.get(i));
//System.out.println("");
}
}
else
{
System.out.println("Syntax ERROR: In Query");
System.out.println(sqlparser.getErrormessage());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
protected static void analyzeStmt(TCustomSqlStatement stmt){
switch(stmt.sqlstatementtype){
case sstselect:
//AnalyzeSelect s=new AnalyzeSelect();
//System.out.println("Select Statement");
AnalyzeSelect.analyzeSelectStmt((TSelectSqlStatement)stmt);
break;
case sstupdate:
break;
case sstTruncate:
//System.out.println("Truncate Statement");
AnalyzeTruncate.analyzeTruncateStmt((TTruncateStatement)stmt);
break;
case sstdroptable:
//System.out.println("Drop Statement");
AnalyzeDrop.analyzeDropStmt((TDropTableSqlStatement)stmt);
break;
case sstcreatetable:
//System.out.println("Create Statement");
AnalyzeCreate.analyzeCreateTableStmt((TCreateTableSqlStatement)stmt);
break;
case sstdelete:
//System.out.println("Delete Statement");
AnalyzeDelete.analyzeDeleteStmt((TDeleteSqlStatement)stmt);
break;
case sstinsert:
// System.out.println("Insert Statement");
AnalyzeInsert.analyzeInsertStmt((TInsertSqlStatement)stmt);
break;
case sstaltertable:
break;
case sstcreateview:
break;
default:
System.out.println(stmt.sqlstatementtype.toString());
}
}
}