· 6 years ago · Dec 05, 2019, 07:42 PM
1package eg.edu.alexu.csd.oop.db.cs39;
2
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.FileReader;
6import java.io.FileWriter;
7import java.io.IOException;
8import java.sql.SQLException;
9import java.util.HashMap;
10import java.util.Map;
11import java.util.Vector;
12
13
14
15
16
17public class IDataBase implements Database {
18
19 private static IDataBase uniqueInstance = new IDataBase();
20 private IDataBase() {
21 }
22 public static IDataBase getUniqueInstance() {
23 return uniqueInstance;
24 }
25 Parser parser = new Parser();
26 Partitions p = new Partitions();
27 // Map of all created databases
28 Map<String, DB> m = new HashMap<String, DB>();
29 DB lastDB;
30 Command DBcommandCreate;
31 Command DBcommandDrop;
32 Command DropTable;
33 Command CreateTable;
34 Select selecTable;
35 Insert insertTable;
36 Update updateTable;
37 String querySmall;
38 Delete deleteTable;
39 String LastDBpath;
40 //The Methods added here are added to access Vectors of : names,types,table name in the statment class
41 //to put it in the constructor of the resultset
42 public Select getSelectCommand()
43 {
44 return selecTable;
45 }
46 public void SetPath(String path)
47 {
48 String[] pathnames = path.trim().split("(/)");
49 String DesiredDB = pathnames[pathnames.length-1];
50 this.lastDB=m.get(DesiredDB);
51 }
52
53 public void QueryManagement(String query) throws SQLException {
54 // create db
55 if (parser.checkInput(query) == 2) {
56 // drop-if-exist should be handled
57 p.CreateDatabase(query);
58 LastDBpath = this.createDatabase(p.getDatabasename().toUpperCase(), false);
59
60 } else if (parser.checkInput(query) == 3) {
61 this.executeStructureQuery(query);
62 }
63 // create table,drop table,called internally when create db , drop db
64 else if (parser.checkInput(query) == 4 || parser.checkInput(query) == 8) {
65
66 this.executeStructureQuery(query);
67 }
68 // update method
69 else if (parser.checkInput(query) == 5 || parser.checkInput(query) == 6 || parser.checkInput(query) == 7|| parser.checkInput(query) == 11||parser.checkInput(query)==12) {
70
71 this.executeUpdateQuery(query);
72 }
73 // select
74 else if (parser.checkInput(query) == 9||parser.checkInput(query) == 1||parser.checkInput(query)==10) {
75 this.executeQuery(query);
76 } else {
77
78 }
79
80 }
81
82 @Override
83 public String createDatabase(String databaseName, boolean dropIfExists) {
84
85 if (m.containsKey(databaseName.toUpperCase()) == false) {
86
87 DBcommandCreate = new CreateDB(databaseName.toUpperCase());
88 try {
89 executeStructureQuery("createdatabase");
90 } catch (SQLException e) {
91 e.printStackTrace();
92 }
93 return DBcommandCreate.getpathofDB();
94 } else if (m.containsKey(databaseName.toUpperCase()) == true && dropIfExists) {
95 DBcommandDrop = new DropDB(databaseName.toUpperCase(), m);
96 try {
97 executeStructureQuery("dropdatabase");
98 } catch (SQLException e) {
99 e.printStackTrace();
100 }
101 DBcommandCreate = new CreateDB(databaseName.toUpperCase());
102 try {
103 executeStructureQuery("createdatabase");
104 } catch (SQLException e) {
105 e.printStackTrace();
106 }
107 //System.out.println(DBcommandCreate.getpathofDB());
108 return DBcommandCreate.getpathofDB();
109 } else // It exists and it is not required to drop it
110 {
111 return m.get(databaseName.toUpperCase()).getAbsolutePath();
112 }
113
114 }
115
116 @Override
117 public boolean executeStructureQuery(String query) throws SQLException {
118
119 if(query.matches("CREATE DATABASE\\s+\\w+")) {
120 p.CreateDatabase(query);
121
122 LastDBpath = this.createDatabase(p.getDatabasename().toUpperCase(), false);
123 DBcommandCreate = new CreateDB(p.getDatabasename().toUpperCase());
124 DBcommandCreate.execute();
125 m.put(DBcommandCreate.getnameofDB().toUpperCase(), DBcommandCreate.getDB());
126 lastDB = DBcommandCreate.getDB();
127 return true;
128 }
129
130 querySmall = query.toLowerCase();
131 if (query == "createdatabase") {
132 DBcommandCreate.execute();
133 m.put(DBcommandCreate.getnameofDB().toUpperCase(), DBcommandCreate.getDB());
134 lastDB = DBcommandCreate.getDB();
135 return true;
136 } else if (query == "dropdatabase") {
137 DBcommandDrop.execute();
138 m.remove(DBcommandDrop.getnameofDB().toUpperCase());
139
140 return true;
141 } else if (querySmall.contains("drop") && querySmall.contains("database")) {
142 p.DropDatabase(query);
143 //System.out.println(p.getDropDataBaseName()+" zx");
144 DBcommandDrop = new DropDB(p.getDropDataBaseName().toUpperCase(), m);
145 DBcommandDrop.execute();
146 if(lastDB.getDatabaseName().compareTo(p.getDropDataBaseName())==0)
147 {
148 lastDB = null;
149 }
150 m.remove(p.getDropDataBaseName().toUpperCase());
151
152
153
154
155 return true;
156 } else if (querySmall.contains("create") && querySmall.contains("table")) {
157
158 Boolean tableExist = false ;
159 p.CreateTable(query);
160 if(parser.checkInput(query)==0) {
161 throw new SQLException("sdfsdf");
162 }
163 for(int i = 0 ; i < lastDB.Tables.size() ; i++)
164 {
165 if( p.getTablename().compareTo(lastDB.Tables.get(i).getTable_Name()) == 0 )
166 {
167 tableExist = true;
168 }
169 }
170 if(tableExist)
171 {
172 return false;
173 }
174 else
175 {
176 CreateTable = new CreateTable(p.getTablename(), p.getcolumns(), p.gettypes(), lastDB.getDatabaseName(),lastDB);
177 CreateTable.execute();
178 return true;
179 }
180 } else if (querySmall.contains("drop") && querySmall.contains("table")) {
181 // i will call class partitions with string query ..to get table name.
182 p.DropTable(query);
183 DropTable = new DropTable(p.getTablename(), lastDB);
184 DropTable.execute();
185
186 return true;
187 } else {
188 // no query is right ...
189 return false;
190 }
191
192 }
193
194 public Vector<String> getNames(String query) throws SQLException {
195
196 selecTable = new Select(query, lastDB, 0, null, query);
197 try {
198 return selecTable.getNames();
199 } catch (Exception e) {
200 e.printStackTrace();
201 return null; // should be handled
202 }
203 }
204
205 @Override
206 public Object[][] executeQuery(String query) throws SQLException {
207 if (parser.checkInput(query) == 1) {
208 p.SelectTable(query);
209 selecTable = new Select(p.getTablename(), lastDB, 0, null, query);
210 try {
211 return selecTable.execute();
212 } catch (Exception e) {
213 e.printStackTrace();
214 return null; // should be handled
215 }
216 }
217
218 else if (parser.checkInput(query) == 10) {
219 p.selecttwocolumnscondition(query);
220 selecTable = new Select(p.getTablename(),lastDB,p.getOperator(),p.getselectconditioncloumn1(),p.getselectconditioncloumn2(),p.getselectconditionvalue());
221 try {
222 return selecTable.executeColumn();
223 } catch (Exception e) {
224 e.printStackTrace();
225 return null; // should be handled
226 }
227 }
228 else {
229 p.Select(query);
230 selecTable = new Select(p.getTablename(), lastDB, p.getOperator(), p.getSelectcolumn(), p.getSelectvalue());
231 try {
232 return selecTable.execute();
233 } catch (Exception e) {
234 e.printStackTrace();
235 return null; // should be handled
236 }
237 }
238 }
239
240 @Override
241 public int executeUpdateQuery(String query) throws java.sql.SQLException {
242
243 // insert //update //delete
244 querySmall = query.toLowerCase();
245 if (querySmall.contains("insert")) {
246 p.parent=lastDB;
247 p.Insert(query);
248 insertTable = new Insert(p.getTablename() , lastDB, p.getvalues());
249 try {
250
251 return insertTable.execute();
252
253 } catch (Exception e) {
254 e.printStackTrace();
255
256 return 0;
257 }
258
259 }else if(querySmall.contains("update")&&querySmall.contains("where")&&querySmall.contains(",")) {
260 p.Updatecolumnsconditions(query);
261 //updateTable = new Update(p.getTablename(),lastDB,)
262 updateTable = new Update(p.getTablename(), lastDB,p.getcolumns(),p.getvalues(),p.getUpdatecolumn1(),p.getUpdatevalue1());
263 try {
264 return updateTable.execute();
265 } catch (Exception e) {
266 return 0;
267 }
268 }
269 else if (querySmall.contains("update")&&querySmall.contains("where")) {
270 p.Update(query);
271 updateTable = new Update(p.getTablename(), lastDB, p.getOperator(), p.getUpdatevalue2(),
272 p.getUpdatecolumn2(), p.getUpdatecolumn1(), p.getUpdatevalue1());
273 //System.out.println(p.getOperator());
274
275 try {
276 return updateTable.execute();
277 } catch (Exception e) {
278 return 0;
279 }
280 } else if(querySmall.contains("update")) {
281 p.Updatecolumns(query);
282 //System.out.println("d5al hna fel update");
283 updateTable = new Update(p.getTablename(),lastDB,p.getcolumns(),p.getvalues());
284 try {
285 return updateTable.execute();
286 } catch (Exception e) {
287 // TODO Auto-generated catch block
288
289 //System.out.println("asdasdasd");
290 e.printStackTrace();
291 return 0;
292 }
293 }
294
295 else if (querySmall.contains("delete")) {
296 if(parser.checkInput(query)==5) {
297 p.Delete(query);
298 // System.out.println(p.getDeletevalue()+p.getDeletecolumn());
299 deleteTable = new Delete(p.getTablename(), lastDB, p.getDeletevalue(), p.getDeletecolumn());
300 try {
301 return deleteTable.execute();
302 } catch (Exception e) {
303 e.printStackTrace();
304 return 0;
305 }
306 }else if (parser.checkInput(query)==11) {
307 p.DeleteAll(query);
308// System.out.println(p.getDeletevalue()+p.getDeletecolumn());
309 deleteTable = new Delete(p.getTablename(), lastDB,null,null);
310 //System.out.println(p.getTablename()+"deleteall");
311 try {
312 return deleteTable.execute();
313 } catch (Exception e) {
314 e.printStackTrace();
315 return 0;
316 }
317 }
318
319
320
321
322
323 } else {
324 return 0;
325 }
326 return 0;
327 }
328 public void save() throws Exception
329 {
330 savedatabasenames();
331 for (Map.Entry<String, DB> entry : m.entrySet()) {
332 entry.getValue().SaveDataBase();
333 }
334 }
335
336 public void savedatabasenames() throws Exception
337 {
338 BufferedWriter bw = new BufferedWriter(new FileWriter(".\\DatabaseNames.txt"));
339 bw.write(lastDB.getDatabaseName());
340 bw.newLine();
341 for (Map.Entry<String, DB> entry : m.entrySet()) {
342 bw.write(entry.getValue().getDatabaseName());
343 bw.newLine();
344 }
345 bw.close();
346 }
347 public void load() throws IOException
348 {
349 try {
350 BufferedReader br = new BufferedReader(new FileReader(".\\DatabaseNames.txt"));
351 String databaseaname;
352 //this.lastDB.LoadDataBase(br.readLine());
353 try {
354 this.lastDB = new DB().LoadDataBase(br.readLine());
355 } catch (Exception e) {
356 // TODO: handle exception
357 }
358
359 //System.out.println(lastDB.getDatabaseName());
360 // System.out.println(lastDB.Tables.size());
361 while ( (databaseaname = br.readLine()) !=null)
362 {
363 DB loaded = new DB().LoadDataBase(databaseaname);
364 m.put(databaseaname, loaded);
365 }
366 br.close();
367 } catch (Exception e) {
368 //System.out.println(lastDB.getDatabaseName());
369 // System.out.println(lastDB.Tables.size());
370 // TODO: handle exception
371 }
372
373 }
374
375}