Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Database Interface*/
- package eg.edu.alexu.csd.oop.db;
- public interface Database {
- /**
- * Create database with the given name, or use it if exists. This method
- * performs a call to executeStructureQuery() internally to create or drop
- * the database.
- *
- * @param databaseName
- * Database name, can be a path not a name only
- * @param dropIfExists
- * is true, then delete the database and recreate it again.
- * @return the absolute path of the database directory wherein data is
- * stored
- */
- public String createDatabase(String databaseName, boolean dropIfExists);
- /**
- * Creates/drops table or database.
- *
- * @param query
- * create or drop, table or database query
- * @returns true if success, or false otherwise
- * @throws SQLException
- * syntax error
- */
- public boolean executeStructureQuery(String query)
- throws java.sql.SQLException;
- /**
- * Select from table
- *
- * @param query
- * select query
- * @return the selected records or an empty array if no records match.
- * Columns types must be preserved (i.e. int column returns Integer
- * objects)
- * @throws SQLException
- * syntax error
- */
- public Object[][] executeQuery(String query) throws java.sql.SQLException;
- /**
- * Insert or update or delete the data
- *
- * @param query
- * data manipulation query
- * @return the updated rows count
- * @throws SQLException
- * syntax error
- */
- public int executeUpdateQuery(String query) throws java.sql.SQLException;
- }
- ///////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////
- /*class implement interface*/
- package eg.edu.alexu.csd.oop.db;
- import java.io.File;
- import java.sql.SQLException;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class databasee implements Database {
- String dataname;
- private String getname(String s) {
- String[] arr = s.split(" ");
- String x = "";
- for(int i=0;i<arr[2].indexOf("(");i++) {
- x+=arr[2].charAt(i);
- }
- return x;
- }
- @Override
- public String createDatabase(String databaseName, boolean dropIfExists) {
- dataname=databaseName;
- Files y = new Files();
- try {
- if (y.isDatabaseExist(databaseName) && dropIfExists) {
- executeStructureQuery("DROP database " + databaseName);
- } else if (y.isDatabaseExist(databaseName) && !dropIfExists) {
- executeStructureQuery("DROP database " + databaseName);
- executeStructureQuery("CREATE database " + databaseName);
- } else {
- executeStructureQuery("CREATE database " + databaseName);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- System.out.println(y.getpathof(databaseName));
- return y.getpathof(databaseName);
- }
- @Override
- public boolean executeStructureQuery(String query) throws SQLException {
- query= query.toLowerCase();
- if (query.contains("create") && query.contains("database")) {
- String databasename =getname(query);
- Files f=new Files();
- f.createDatabase(databasename);
- System.out.println( f.isDatabaseExist(databasename));
- return f.isDatabaseExist(databasename);
- }
- if(query.contains("create") && query.contains("table")) {
- Files f=new Files();
- System.out.println(dataname);
- System.out.println(f.isDatabaseExist(dataname));
- if(!f.isDatabaseExist(dataname)) {
- return false;
- }
- String tablename=getname(query);
- f.createtable(tablename, dataname);
- System.out.println(f.isTableExist(tablename, dataname));
- return f.isTableExist(tablename, dataname);
- }
- if(!query.contains("create") || !query.contains("database")||!query.contains("drop") || !query.contains("table")){
- return false;
- }
- return false;
- }
- @Override
- public Object[][] executeQuery(String query) throws SQLException {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public int executeUpdateQuery(String query) throws SQLException {
- // TODO Auto-generated method stub
- return 0;
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////
- /*class for handling files*/
- package eg.edu.alexu.csd.oop.db;
- import java.io.File;
- import java.io.IOException;
- public class Files {
- private File path;
- private String fs = System.getProperty("file.separator");
- Files() {
- path = new File("DD");
- path.mkdirs();
- }
- public String getpathof(String n) {
- return path.getAbsolutePath() + fs + n;
- }
- public boolean isDatabaseExist(String n) {
- File f = new File(path + fs + n);
- return f.exists();
- }
- public void createDatabase(String n) {
- File newDataBase = new File(path + fs + n);
- newDataBase.mkdirs();
- }
- public void DropDatabase(String n) {
- File p = new File(path + fs + n);
- File[] f = p.listFiles();
- for (File c : f) {
- c.delete();
- }
- p.delete();
- }
- public void createtable(String tn, String dtn) {
- File xml = new File(path + fs + dtn + fs + tn + ".xml");
- File dtd = new File(path + fs + dtn + fs + tn + ".dtd");
- try {
- xml.createNewFile();
- dtd.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public boolean isTableExist(String tn, String dtn) {
- File xml = new File(path + fs + dtn + fs + tn + ".xml");
- File dtd = new File(path + fs + dtn + fs + tn + ".dtd");
- return xml.exists() && dtd.exists();
- }
- public void dropTable(String tn, String dtn) {
- File xml = new File(path + fs + dtn + fs + tn + ".xml");
- File dtd = new File(path + fs + dtn + fs + tn + ".dtd");
- xml.delete();
- dtd.delete();
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////////////
- package eg.edu.alexu.csd.oop.db;
- public class cell {
- private Object value;
- public cell() {
- value=new Object();
- }
- public void setcell(Object c) {
- value=c;
- }
- public Object getcell() {
- return value;
- }
- }
- //////////////////////////////////////////////////
- package eg.edu.alexu.csd.oop.db;
- import java.util.Vector;
- public class column {
- String name;
- Vector<cell>c;
- public column() {
- name=new String();
- c=new Vector<cell>();
- }
- public String getname() {
- return name;
- }
- public void setname(String n) {
- name=n;
- }
- public Vector<cell> getcolumn() {
- return c;
- }
- public void setcolumn(Vector<cell>c) {
- this.c=c;
- }
- public void addcell(cell x) {
- c.add(x);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////
- package eg.edu.alexu.csd.oop.db;
- import java.util.Vector;
- public class table {
- String name;
- Vector<column> t ;
- public table() {
- name=new String();
- t=new Vector<column>();
- }
- public String getname() {
- return name;
- }
- public void setname(String n) {
- name=n;
- }
- public void settable(Vector<column> t) {
- this.t = t;
- }
- public Vector<column> gettable() {
- return t;
- }
- public void addcolumn(column c) {
- t.add(c);
- }
- public void removecolumn(String n) {
- for (int i = 0; i<t.size(); i++) {
- if (t.elementAt(i).getname().equals(n)) {
- t.remove(i);
- }
- }
- }
- }
- /////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////
- package eg.edu.alexu.csd.oop.db;
- import java.util.Vector;
- public class databasetables {
- String name ;
- Vector<table>dt;
- public databasetables() {
- name=new String();
- dt=new Vector<table>();
- }
- public String getname() {
- return name;
- }
- public void setname(String n) {
- name=n;
- }
- public void setdatabase(Vector<table>dt) {
- this.dt=dt;
- }
- public Vector<table> getdatabase(){
- return dt;
- }
- public void addtable(table t) {
- dt.add(t);
- }
- public void removetable(String n) {
- for (int i = 0; i<dt.size(); i++) {
- if (dt.elementAt(i).getname().equals(n)) {
- dt.remove(i);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement