-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQueryHandler.java
More file actions
257 lines (197 loc) · 6.11 KB
/
Copy pathQueryHandler.java
File metadata and controls
257 lines (197 loc) · 6.11 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package database;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class QueryHandler{
public static void parse(String query)
{
String[] substrings = query.split(" ");
switch (substrings[0])
{
case "create":
createQuery(query);
break;
case "insert":
insertQuery(query);
break;
case "select":
selectQuery(query);
break;
case "drop":
dropQuery(query);
break;
case "update":
updateQuery(query);
break;
case "show":
String tables = substrings[1];
System.out.println();
if(tables.equals("tables")){
Commands.show();
}
else{
System.out.println("\nIncorrect input. Please check the readme.txt file to know the supported commands\n");
}
break;
case "exit":
System.out.println();
break;
case "version":
System.out.println("\nDavisBase: Version 1.0\n");
break;
default:
System.out.println("\nIncorrect input. Please check the readme.txt file to know the supported commands\n");
break;
}
}
public static void createQuery(String query){
String[] subs = query.split(" ");
if(subs[1].equals("table")){
String tablename = subs[2];
String[] temp = query.split(tablename);
String temp2 = temp[1].trim();
int length=temp2.length();
if(temp2.charAt(0)=='(' && temp2.charAt(length-1)==')'){
String[] colnames = temp2.substring(1, temp2.length() - 1).split(",");
for (int i = 0; i < colnames.length; i++){
colnames[i] = colnames[i].trim();
}
if (!DavisBase.tableExist(tablename))
{
Commands.createTable(tablename, colnames);
System.out.println("Table "+tablename+" created successfully.\n");
}
else{
System.out.println("Table " + tablename + " already exists.\n");
}
}
else{
System.out.println("Incorrect input. Please check the readme.txt file to know about the supported commands\n");
}
}
else{
System.out.println("Incorrect input. Please check the readme.txt file to know about the supported commands\n");
}
}
public static void insertQuery(String query){
String[] subs = query.split(" ");
String insert_table = subs[2];
String insert_vals1 = query.split("values")[1].trim();
insert_vals1 = insert_vals1.substring(1, insert_vals1.length() - 1);
String[] insert_vals2 = insert_vals1.split(",");
for (int i = 0; i < insert_vals2.length; i++)
insert_vals2[i] = insert_vals2[i].trim();
if (!DavisBase.tableExist(insert_table)) {
System.out.println("Table " + insert_table + " doesn't exist.\n");
return;
}
RandomAccessFile insertfile;
try {
insertfile = new RandomAccessFile("data//user_data//"+insert_table+"//"+insert_table+".tbl", "rw");
Commands.insertInto(insertfile,insert_table, insert_vals2);
System.out.println("Inserted Successfully\n");
insertfile.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void selectQuery(String query){
String[] subs = query.split(" ");
String[] condition;
String[] select_column;
String[] temp = query.split("where");
String[] temp1 = temp[0].split("from");
String table = temp1[1].trim();
String columns = temp1[0].replace("select", "").trim();
if(table.equals("davisbase_tables") || table.equals("davisbase_columns"))
{
if (columns.contains("*"))
{
select_column = new String[1];
select_column[0] = "*";
}
else {
select_column = columns.split(",");
for (int i = 0; i < select_column.length; i++)
select_column[i] = select_column[i].trim();
}
if (temp.length > 1) {
String filter = temp[1].trim();
condition = DavisBase.parserEquation(filter);
} else {
condition = new String[0];
}
if(table.equals("davisbase_tables")){
Commands.select("data//catalog//davisbase_tables.tbl", table, select_column, condition);
System.out.println();
return;
}
else{
Commands.select("data//catalog//davisbase_columns.tbl", table, select_column, condition);
System.out.println();
return;
}
}
else{
if(!DavisBase.tableExist(table)) {
System.out.println("Table " + table + " doesn't exist.");
System.out.println("Please enter the correct table name.\n");
return;
}
}
if (temp.length > 1)
{
String filter = temp[1].trim();
condition = DavisBase.parserEquation(filter);
}
else {
condition = new String[0];
}
if (columns.contains("*")) {
select_column = new String[1];
select_column[0] = "*";
} else {
select_column = columns.split(",");
for (int i = 0; i < select_column.length; i++)
select_column[i] = select_column[i].trim();
}
Commands.select("data//user_data//"+table+"//"+table+".tbl", table, select_column, condition);
System.out.println();
}
public static void updateQuery(String query){
String[] subs = query.split(" ");
String updateTable = subs[1];
String[] update_temp1 = query.split("set");
String[] update_temp2 = update_temp1[1].split("where");
String where = update_temp2[1];
String set = update_temp2[0];
String[] filteredset = DavisBase.parserEquation(set);
String[] filteredwhere = DavisBase.parserEquation(where);
if (!DavisBase.tableExist(updateTable))
{
System.out.println("Table " + updateTable + " does not exist.\n");
return;
}
Commands.update(updateTable, filteredset, filteredwhere);
System.out.println("Table "+updateTable+" updated successfully.\n");
}
public static void dropQuery(String query){
String[] subs = query.split(" ");
if(subs[1].equals("table")){
String dropTable = subs[2];
if (DavisBase.tableExist(dropTable))
{
Commands.drop(dropTable,"user_data");
System.out.println("Table "+dropTable+" dropped successfully.\n");
}
else{
System.out.println("Table " + dropTable + " does not exist.\n");
}
}
else{
System.out.println("Incorrect input. Please check the readme.txt file to know the supported commands\n");
}
}
}