· 6 years ago · Nov 21, 2019, 02:08 PM
1/*Database Interface*/
2package eg.edu.alexu.csd.oop.db;
3
4public interface Database {
5 /**
6 * Create database with the given name, or use it if exists. This method
7 * performs a call to executeStructureQuery() internally to create or drop
8 * the database.
9 *
10 * @param databaseName
11 * Database name, can be a path not a name only
12 * @param dropIfExists
13 * is true, then delete the database and recreate it again.
14 * @return the absolute path of the database directory wherein data is
15 * stored
16 */
17 public String createDatabase(String databaseName, boolean dropIfExists);
18
19 /**
20 * Creates/drops table or database.
21 *
22 * @param query
23 * create or drop, table or database query
24 * @returns true if success, or false otherwise
25 * @throws SQLException
26 * syntax error
27 */
28 public boolean executeStructureQuery(String query)
29 throws java.sql.SQLException;
30
31 /**
32 * Select from table
33 *
34 * @param query
35 * select query
36 * @return the selected records or an empty array if no records match.
37 * Columns types must be preserved (i.e. int column returns Integer
38 * objects)
39 * @throws SQLException
40 * syntax error
41 */
42 public Object[][] executeQuery(String query) throws java.sql.SQLException;
43
44 /**
45 * Insert or update or delete the data
46 *
47 * @param query
48 * data manipulation query
49 * @return the updated rows count
50 * @throws SQLException
51 * syntax error
52 */
53 public int executeUpdateQuery(String query) throws java.sql.SQLException;
54}
55///////////////////////////////////////////////////////////////////////////////////
56//////////////////////////////////////////////////////////////////////////////////
57/*class implement interface*/
58package eg.edu.alexu.csd.oop.db;
59
60import java.io.File;
61import java.sql.SQLException;
62import java.util.regex.Matcher;
63import java.util.regex.Pattern;
64
65public class databasee implements Database {
66 String dataname;
67
68
69private String getname(String s) {
70 String[] arr = s.split(" ");
71
72String x = "";
73for(int i=0;i<arr[2].indexOf("(");i++) {
74x+=arr[2].charAt(i);
75}
76 return x;
77
78}
79 @Override
80 public String createDatabase(String databaseName, boolean dropIfExists) {
81 dataname=databaseName;
82 Files y = new Files();
83 try {
84 if (y.isDatabaseExist(databaseName) && dropIfExists) {
85 executeStructureQuery("DROP database " + databaseName);
86 } else if (y.isDatabaseExist(databaseName) && !dropIfExists) {
87 executeStructureQuery("DROP database " + databaseName);
88 executeStructureQuery("CREATE database " + databaseName);
89 } else {
90 executeStructureQuery("CREATE database " + databaseName);
91 }
92 } catch (SQLException e) {
93 e.printStackTrace();
94 }
95 System.out.println(y.getpathof(databaseName));
96 return y.getpathof(databaseName);
97 }
98
99 @Override
100 public boolean executeStructureQuery(String query) throws SQLException {
101 query= query.toLowerCase();
102
103
104 if (query.contains("create") && query.contains("database")) {
105
106 String databasename =getname(query);
107 Files f=new Files();
108 f.createDatabase(databasename);
109 System.out.println( f.isDatabaseExist(databasename));
110 return f.isDatabaseExist(databasename);
111
112 }
113 if(query.contains("create") && query.contains("table")) {
114 Files f=new Files();
115 System.out.println(dataname);
116 System.out.println(f.isDatabaseExist(dataname));
117 if(!f.isDatabaseExist(dataname)) {
118 return false;
119 }
120 String tablename=getname(query);
121 f.createtable(tablename, dataname);
122 System.out.println(f.isTableExist(tablename, dataname));
123 return f.isTableExist(tablename, dataname);
124
125 }
126 if(!query.contains("create") || !query.contains("database")||!query.contains("drop") || !query.contains("table")){
127 return false;
128 }
129 return false;
130 }
131
132 @Override
133 public Object[][] executeQuery(String query) throws SQLException {
134 // TODO Auto-generated method stub
135 return null;
136 }
137
138 @Override
139 public int executeUpdateQuery(String query) throws SQLException {
140 // TODO Auto-generated method stub
141 return 0;
142 }
143
144}
145/////////////////////////////////////////////////////////////////////////////////////////////
146////////////////////////////////////////////////////////////////////////////////////////////
147/*class for handling files*/
148package eg.edu.alexu.csd.oop.db;
149
150import java.io.File;
151import java.io.IOException;
152
153public class Files {
154 private File path;
155 private String fs = System.getProperty("file.separator");
156
157 Files() {
158 path = new File("DD");
159 path.mkdirs();
160 }
161
162 public String getpathof(String n) {
163 return path.getAbsolutePath() + fs + n;
164 }
165
166 public boolean isDatabaseExist(String n) {
167 File f = new File(path + fs + n);
168 return f.exists();
169 }
170
171 public void createDatabase(String n) {
172 File newDataBase = new File(path + fs + n);
173 newDataBase.mkdirs();
174 }
175
176 public void DropDatabase(String n) {
177 File p = new File(path + fs + n);
178 File[] f = p.listFiles();
179 for (File c : f) {
180 c.delete();
181 }
182 p.delete();
183 }
184
185 public void createtable(String tn, String dtn) {
186 File xml = new File(path + fs + dtn + fs + tn + ".xml");
187 File dtd = new File(path + fs + dtn + fs + tn + ".dtd");
188 try {
189 xml.createNewFile();
190 dtd.createNewFile();
191 } catch (IOException e) {
192 e.printStackTrace();
193 }
194
195 }
196
197 public boolean isTableExist(String tn, String dtn) {
198 File xml = new File(path + fs + dtn + fs + tn + ".xml");
199 File dtd = new File(path + fs + dtn + fs + tn + ".dtd");
200 return xml.exists() && dtd.exists();
201 }
202
203 public void dropTable(String tn, String dtn) {
204 File xml = new File(path + fs + dtn + fs + tn + ".xml");
205 File dtd = new File(path + fs + dtn + fs + tn + ".dtd");
206 xml.delete();
207 dtd.delete();
208 }
209
210}
211//////////////////////////////////////////////////////////////////////////////////////////////////
212/////////////////////////////////////////////////////////////////////////////////////////////////
213package eg.edu.alexu.csd.oop.db;
214
215public class cell {
216private Object value;
217public cell() {
218 value=new Object();
219}
220public void setcell(Object c) {
221 value=c;
222}
223public Object getcell() {
224 return value;
225
226}
227}
228//////////////////////////////////////////////////
229package eg.edu.alexu.csd.oop.db;
230
231import java.util.Vector;
232
233public class column {
234String name;
235Vector<cell>c;
236public column() {
237 name=new String();
238 c=new Vector<cell>();
239}
240public String getname() {
241 return name;
242}
243public void setname(String n) {
244 name=n;
245}
246public Vector<cell> getcolumn() {
247 return c;
248}
249public void setcolumn(Vector<cell>c) {
250this.c=c;
251}
252public void addcell(cell x) {
253 c.add(x);
254}
255}
256////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
257///////////////////////////////////////////////////////
258package eg.edu.alexu.csd.oop.db;
259
260import java.util.Vector;
261
262public class table {
263 String name;
264 Vector<column> t ;
265 public table() {
266 name=new String();
267 t=new Vector<column>();
268 }
269 public String getname() {
270 return name;
271 }
272 public void setname(String n) {
273 name=n;
274 }
275 public void settable(Vector<column> t) {
276 this.t = t;
277 }
278
279 public Vector<column> gettable() {
280 return t;
281 }
282
283 public void addcolumn(column c) {
284 t.add(c);
285 }
286
287 public void removecolumn(String n) {
288 for (int i = 0; i<t.size(); i++) {
289 if (t.elementAt(i).getname().equals(n)) {
290 t.remove(i);
291 }
292 }
293 }
294
295}
296/////////////////////////////////////////////////
297///////////////////////////////////////////////////////////////////////
298package eg.edu.alexu.csd.oop.db;
299
300import java.util.Vector;
301
302public class databasetables {
303String name ;
304Vector<table>dt;
305public databasetables() {
306 name=new String();
307 dt=new Vector<table>();
308}
309public String getname() {
310 return name;
311}
312public void setname(String n) {
313 name=n;
314}
315public void setdatabase(Vector<table>dt) {
316 this.dt=dt;
317}
318public Vector<table> getdatabase(){
319 return dt;
320
321}
322public void addtable(table t) {
323 dt.add(t);
324}
325public void removetable(String n) {
326 for (int i = 0; i<dt.size(); i++) {
327 if (dt.elementAt(i).getname().equals(n)) {
328 dt.remove(i);
329 }
330 }
331}
332}