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