Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1.  
  2. import java.io.BufferedWriter;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.OutputStreamWriter;
  9. import java.io.Writer;
  10. import java.util.HashMap;
  11. import java.util.Scanner;
  12. import java.util.Vector;
  13.  
  14. import org.gibello.zql.*;
  15.  
  16. /* Code belongs to Kartik Kohli*/
  17.  
  18. public class DBQueries {
  19.  
  20. static HashMap<String, Integer> tableNameToInt = new HashMap<String, Integer>();
  21. static DBSystem dbs;
  22. public static String tableName = "";
  23. public DBQueries(HashMap<String, Integer> x, DBSystem dbs) {
  24. tableNameToInt = x;
  25. this.dbs = dbs;
  26. }
  27. /*
  28. public static void main(String[] args) throws ParseException {
  29.  
  30. System.out.println("Reading SQL from stdin");
  31. Scanner s = new Scanner(System.in);
  32. String input = s.nextLine();
  33. System.out.println("Scanned: " + input);
  34. /*
  35. p = new ZqlParser(System.in);
  36. ZStatement st = p.readStatement();
  37. queryType((ZQuery)st);
  38.  
  39.  
  40. queryType(input);
  41. }
  42. */
  43. public static void queryType(String input) throws ParseException {
  44. /*
  45. Determine the type of the query (select/create) and
  46. invoke appropriate method for it.
  47. */
  48.  
  49. //p.initParser(new ByteArrayInputStream(input.getBytes()));
  50. String type = input.toString().split(" ")[0];
  51. //System.out.println(dbs.tables.size());
  52. if(type.equalsIgnoreCase("create")) {
  53. System.out.println("QueryType:" + type);
  54. createCommand(input);
  55. }
  56. else if(type.equalsIgnoreCase("select")) {
  57. ZqlParser p = new ZqlParser();
  58. p.initParser(new ByteArrayInputStream(input.getBytes()));
  59. ZStatement st = p.readStatement();
  60. //System.out.println(st.toString()); // Display the statement
  61. System.out.println("QueryType:" + type);
  62. selectCommand((ZQuery)st);
  63. }
  64. else {
  65. System.out.println("Invalid Command");
  66. }
  67. }
  68.  
  69. static void createCommand(String st)
  70. {
  71. String [] words = st.split("[ ,\n;]+");
  72. //System.out.println(words.length + "");
  73. tableName = words[2];
  74. String [] attributes = new String[10005];
  75. String [] datatype = new String[10005];
  76. int j = 0;
  77. int k = 0;
  78. for (int i = 4;i < words.length-1;i ++)
  79. {
  80. if (i % 2 == 0)
  81. attributes[j++] = words[i];
  82. else
  83. datatype[k++] = words[i];
  84. }
  85. String path = "/tmp/" + tableName + ".data";
  86. String path2 = "/tmp/" + tableName + ".csv";
  87. File f = new File(path);
  88. try {
  89. f.createNewFile();
  90. } catch (IOException e) {
  91. // TODO Auto-generated catch block
  92. System.out.println("Cannot create the data file!");
  93. }
  94.  
  95. File f2 = new File(path2);
  96. try {
  97. f2.createNewFile();
  98. } catch (IOException e) {
  99. // TODO Auto-generated catch block
  100. System.out.println("Cannot create the csv file!");
  101. }
  102. Writer writer = null;
  103.  
  104. try {
  105. writer = new BufferedWriter(new OutputStreamWriter(
  106. new FileOutputStream(path), "utf-8"));
  107. writer.write(tableName + ".data"+"\n");
  108. for(int i = 0;i < j;i ++)
  109. {
  110. if (i != j - 1)
  111. writer.write(attributes[i]+":"+datatype[i]+",");
  112. else
  113. writer.write(attributes[i]+":"+datatype[i]);
  114. }
  115. } catch (IOException ex) {
  116. System.out.println("Cannot write in the file");
  117. } finally {
  118. try {writer.close();} catch (Exception ex) {}
  119. }
  120.  
  121. //Add to the config.txt file
  122.  
  123.  
  124. try
  125. {
  126. String filename = "config.txt";
  127. FileWriter fw = new FileWriter(filename,true); //the true will append the new data
  128. fw.write("BEGIN\n");//appends the string to the file
  129. fw.write(tableName+"\n");
  130. for (int i = 0;i < j;i ++)
  131. {
  132. fw.write(attributes[i] + ", " + datatype[i]+ "\n");
  133. }
  134. fw.write("END\n");
  135. fw.close();
  136. }
  137. catch(IOException ioe)
  138. {
  139. System.err.println("IOException: " + ioe.getMessage());
  140. }
  141.  
  142. //System.out.println("Querytype: create");
  143. System.out.println("Tablename: " + tableName);
  144. System.out.print("Attributes:");
  145. for (int i = 0;i < j;i ++)
  146. {
  147. if (i != j - 1)
  148. System.out.print(" "+ attributes[i] + " " + datatype[i] + ",");
  149. else
  150. System.out.print(" "+ attributes[i] + " " + datatype[i]);
  151. }
  152. System.out.println("");
  153. return;
  154. }
  155.  
  156. static void selectCommand(ZQuery st) throws ParseException {
  157. /*
  158. Use any SQL parser to parse the input query. Perform all validations (table
  159. name, attributes, datatypes, operations). Print the query tokens as specified
  160. below.
  161. */
  162. Vector fromTable = st.getFrom();
  163. //System.out.println(tableNameToInt.toString() + "");
  164. for(int i=0;i<fromTable.size();i++) {
  165. if(tableNameToInt.containsKey(fromTable.elementAt(i)+"")==false) {
  166. System.out.println("Invalid Query");
  167. return;
  168. }
  169. }
  170.  
  171. //System.out.println(st.getWhere().toString()); // Display the statement
  172. System.out.println("TableName: " + st.getFrom());
  173. System.out.println("Columns: " + st.getSelect());
  174.  
  175. Table t = new Table(dbs.tables.get(tableNameToInt.get(fromTable.elementAt(0)+"")).tableName,
  176. dbs.tables.get(tableNameToInt.get(fromTable.elementAt(0)+"")).field,
  177. dbs.tables.get(tableNameToInt.get(fromTable.elementAt(0)+"")).records,
  178. dbs.tables.get(tableNameToInt.get(fromTable.elementAt(0)+"")).columns);
  179.  
  180.  
  181. Vector<String> result;
  182.  
  183. //Where clause
  184. Vector< Vector<Integer> > indices;
  185. if(st.getWhere()!=null) {
  186. //System.out.println("Condition: " + ((ZExpression)st.getWhere()).getOperands() + ":" + ((ZExpression)st.getWhere()).getOperator());
  187. System.out.println( ((ZExpression)st.getWhere()).getOperands().get(0).getClass().getName() );
  188. /*
  189. int index = t.field.get(z.getOperands().get(0));
  190. System.out.println(index + "");
  191. for(int i=0;i<t.records.size();i++) {
  192.  
  193. }
  194. */
  195. }
  196. else {
  197. System.out.println("Condition:N/A");
  198. }
  199.  
  200. //Order by Clause
  201. if(st.getOrderBy()!=null) {
  202. System.out.println("Orderby: " + st.getOrderBy());
  203. }
  204. else {
  205. System.out.println("Orderby:N/A");
  206. }
  207.  
  208. //Project clause
  209. Vector<Integer> cols = new Vector<Integer>();
  210. for(int i=0;i<st.getSelect().size();i++) {
  211. cols.add(t.field.get(st.getSelect().elementAt(i)+""));
  212. }
  213.  
  214. for(int i=0;i<result.size();i++) {
  215. String[] rec = result.get(i).split(",");
  216. for(int j=0;j<cols.size();j++) {
  217. System.out.print(rec[cols.get(j)]);
  218. if(j!=cols.size()-1) System.out.print(",");
  219. }
  220. System.out.print("\n");
  221. }
  222.  
  223. //Group by clause
  224. if(st.getGroupBy()!=null) {
  225. System.out.println("Groupby: " + st.getGroupBy());
  226. //Having clause
  227. if(st.getGroupBy().toString().contains("having")) {
  228. System.out.println("Having:" + st.getGroupBy().toString().split("having")[1]);
  229. }
  230. }
  231. else {
  232. System.out.println("Groupby:N/A");
  233. }
  234.  
  235. /*
  236. if(st instanceof ZQuery) { // An SQL query: query the DB
  237. queryDB((ZQuery)st);
  238. } else if(st instanceof ZInsert){ // An SQL insert
  239. insertDB((ZInsert)st);
  240. }
  241. */
  242.  
  243. }
  244.  
  245. public static class Table {
  246. public String tableName;
  247. public HashMap<String,Integer> field;
  248. Vector<String> records;
  249. Vector<Integer> columns;
  250. public Table(String tableName,HashMap<String,Integer> field,Vector<String> records,Vector<Integer> columns) {
  251. this.tableName = tableName;
  252. this.field = field;
  253. this.records = records;
  254. this.columns = columns;
  255. }
  256. }
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement