Advertisement
Guest User

Untitled

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