Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedWriter;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.io.Writer;
- import java.util.HashMap;
- import java.util.Scanner;
- import java.util.Vector;
- import org.gibello.zql.*;
- public class DBQueries {
- static HashMap<String, Integer> tableNameToInt = new HashMap<String, Integer>();
- static DBSystem dbs;
- public static String tableName = "";
- public DBQueries(HashMap<String, Integer> x, DBSystem dbs) {
- tableNameToInt = x;
- this.dbs = dbs;
- }
- /*
- public static void main(String[] args) throws ParseException {
- System.out.println("Reading SQL from stdin");
- Scanner s = new Scanner(System.in);
- String input = s.nextLine();
- System.out.println("Scanned: " + input);
- /*
- p = new ZqlParser(System.in);
- ZStatement st = p.readStatement();
- queryType((ZQuery)st);
- queryType(input);
- }
- */
- public static void queryType(String input) throws ParseException {
- /*
- Determine the type of the query (select/create) and
- invoke appropriate method for it.
- */
- //p.initParser(new ByteArrayInputStream(input.getBytes()));
- String type = input.toString().split(" ")[0];
- //System.out.println(dbs.tables.size());
- if(type.equalsIgnoreCase("create")) {
- System.out.println("QueryType:" + type);
- createCommand(input);
- }
- else if(type.equalsIgnoreCase("select")) {
- ZqlParser p = new ZqlParser();
- p.initParser(new ByteArrayInputStream(input.getBytes()));
- ZStatement st = p.readStatement();
- //System.out.println(st.toString()); // Display the statement
- System.out.println("QueryType:" + type);
- selectCommand((ZQuery)st);
- }
- else {
- System.out.println("Invalid Command");
- }
- }
- static void createCommand(String st)
- {
- String [] words = st.split("[ ,\n;]+");
- //System.out.println(words.length + "");
- tableName = words[2];
- String [] attributes = new String[10005];
- String [] datatype = new String[10005];
- int j = 0;
- int k = 0;
- for (int i = 4;i < words.length-1;i ++)
- {
- if (i % 2 == 0)
- attributes[j++] = words[i];
- else
- datatype[k++] = words[i];
- }
- String path = "/tmp/" + tableName + ".data";
- String path2 = "/tmp/" + tableName + ".csv";
- File f = new File(path);
- try {
- f.createNewFile();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- System.out.println("Cannot create the data file!");
- }
- File f2 = new File(path2);
- try {
- f2.createNewFile();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- System.out.println("Cannot create the csv file!");
- }
- Writer writer = null;
- try {
- writer = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(path), "utf-8"));
- writer.write(tableName + ".data"+"\n");
- for(int i = 0;i < j;i ++)
- {
- if (i != j - 1)
- writer.write(attributes[i]+":"+datatype[i]+",");
- else
- writer.write(attributes[i]+":"+datatype[i]);
- }
- } catch (IOException ex) {
- System.out.println("Cannot write in the file");
- } finally {
- try {writer.close();} catch (Exception ex) {}
- }
- //Add to the config.txt file
- try
- {
- String filename = "config.txt";
- FileWriter fw = new FileWriter(filename,true); //the true will append the new data
- fw.write("BEGIN\n");//appends the string to the file
- fw.write(tableName+"\n");
- for (int i = 0;i < j;i ++)
- {
- fw.write(attributes[i] + ", " + datatype[i]+ "\n");
- }
- fw.write("END\n");
- fw.close();
- }
- catch(IOException ioe)
- {
- System.err.println("IOException: " + ioe.getMessage());
- }
- //System.out.println("Querytype: create");
- System.out.println("Tablename: " + tableName);
- System.out.print("Attributes:");
- for (int i = 0;i < j;i ++)
- {
- if (i != j - 1)
- System.out.print(" "+ attributes[i] + " " + datatype[i] + ",");
- else
- System.out.print(" "+ attributes[i] + " " + datatype[i]);
- }
- System.out.println("");
- return;
- }
- static void selectCommand(ZQuery st) throws ParseException {
- /*
- Use any SQL parser to parse the input query. Perform all validations (table
- name, attributes, datatypes, operations). Print the query tokens as specified
- below.
- */
- Vector fromTable = st.getFrom();
- //System.out.println(tableNameToInt.toString() + "");
- for(int i=0;i<fromTable.size();i++) {
- if(tableNameToInt.containsKey(fromTable.elementAt(i)+"")==false) {
- System.out.println("Invalid Query");
- return;
- }
- }
- //System.out.println(st.getWhere().toString()); // Display the statement
- System.out.println("TableName: " + st.getFrom());
- System.out.println("Columns: " + st.getSelect());
- Table t = new Table(dbs.tables.get(tableNameToInt.get(fromTable.elementAt(0)+"")).tableName,
- dbs.tables.get(tableNameToInt.get(fromTable.elementAt(0)+"")).field,
- dbs.tables.get(tableNameToInt.get(fromTable.elementAt(0)+"")).records,
- dbs.tables.get(tableNameToInt.get(fromTable.elementAt(0)+"")).columns);
- Vector<String> result;
- //Where clause
- Vector< Vector<Integer> > indices;
- if(st.getWhere()!=null) {
- //System.out.println("Condition: " + ((ZExpression)st.getWhere()).getOperands() + ":" + ((ZExpression)st.getWhere()).getOperator());
- System.out.println( ((ZExpression)st.getWhere()).getOperands().get(0).getClass().getName() );
- /*
- int index = t.field.get(z.getOperands().get(0));
- System.out.println(index + "");
- for(int i=0;i<t.records.size();i++) {
- }
- */
- }
- else {
- System.out.println("Condition:N/A");
- }
- //Order by Clause
- if(st.getOrderBy()!=null) {
- System.out.println("Orderby: " + st.getOrderBy());
- }
- else {
- System.out.println("Orderby:N/A");
- }
- //Project clause
- Vector<Integer> cols = new Vector<Integer>();
- for(int i=0;i<st.getSelect().size();i++) {
- cols.add(t.field.get(st.getSelect().elementAt(i)+""));
- }
- for(int i=0;i<result.size();i++) {
- String[] rec = result.get(i).split(",");
- for(int j=0;j<cols.size();j++) {
- System.out.print(rec[cols.get(j)]);
- if(j!=cols.size()-1) System.out.print(",");
- }
- System.out.print("\n");
- }
- //Group by clause
- if(st.getGroupBy()!=null) {
- System.out.println("Groupby: " + st.getGroupBy());
- //Having clause
- if(st.getGroupBy().toString().contains("having")) {
- System.out.println("Having:" + st.getGroupBy().toString().split("having")[1]);
- }
- }
- else {
- System.out.println("Groupby:N/A");
- }
- /*
- if(st instanceof ZQuery) { // An SQL query: query the DB
- queryDB((ZQuery)st);
- } else if(st instanceof ZInsert){ // An SQL insert
- insertDB((ZInsert)st);
- }
- */
- }
- public static class Table {
- public String tableName;
- public HashMap<String,Integer> field;
- Vector<String> records;
- Vector<Integer> columns;
- public Table(String tableName,HashMap<String,Integer> field,Vector<String> records,Vector<Integer> columns) {
- this.tableName = tableName;
- this.field = field;
- this.records = records;
- this.columns = columns;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement