Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package eg.edu.alexu.csd.oop.db;
- import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
- import java.io.FileNotFoundException;
- import java.sql.Array;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.StringTokenizer;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.xml.stream.XMLStreamException;
- /**
- *
- * @author Mustafa
- */
- public class SQL {
- final Pattern createtable;
- final Pattern droptable ;
- final Pattern insertinto ;
- final Pattern deletefrom ;
- final Pattern select ;
- Pattern []patternarray = new Pattern[5] ;
- public SQL(){
- this.createtable = Pattern.compile("(?i)create\\s+table\\s+\\w+\\s*(.*);");
- this. droptable = Pattern.compile("(?i)drop\\s+table\\s+\\w+\\s*;");
- this.insertinto = Pattern.compile("(?i)insert\\s+into\\s+\\w+\\s*(.*)values\\s*(.*);");
- this.deletefrom = Pattern.compile("(?i)delete\\s+from\\s+\\w+\\s*where\\s*(.*);");
- this.select = Pattern.compile("(?i)select\\s*(.*)\\s*from\\s+\\w+\\s*(where\\s*(.*));");
- patternarray[0] = createtable;
- patternarray[1] = droptable;
- patternarray[2] = insertinto;
- patternarray[3] = deletefrom;
- patternarray[4] = select;
- }
- public void query (String query) throws XMLStreamException, FileNotFoundException{
- StringTokenizer token = new StringTokenizer(query);
- StringBuilder std = new StringBuilder();
- while (token.hasMoreTokens()){
- std.append(token.nextToken());
- std.append(" ");
- }
- for (int i = 0 ;i <patternarray.length;i++){
- // System.out.println(std.toString());
- Matcher m = patternarray[i].matcher(std.toString());
- if (m.find()){
- // System.out.println(i);
- // method i
- CaptureTheQuery(i,std.toString());
- }
- }
- }
- public void CaptureTheQuery(int i, String query) throws XMLStreamException, FileNotFoundException {
- switch (i) {
- case 0:
- /*
- Matcher matcher = Pattern.compile("CREATE\\s+TABLE\\s+\"?(\\w+)").matcher(query);
- if (matcher.find()) {
- String name = matcher.group(1);
- //System.out.print(name);
- }*/
- String name = null;
- Matcher matcher = Pattern.compile("(?i)\\s*CREATE\\s+TABLE?\\s+(\\w+)").matcher(query);
- if (matcher.find()) {
- name = matcher.group(1);
- // System.out.print(name + "\n");
- }
- StringBuilder a = new StringBuilder();
- boolean paren = false;
- for (char ch: query.toCharArray())
- {
- if(ch == '(')
- paren = true;
- if(ch == ')')
- paren = false;
- if(paren == true&& ch !='(' && ch != '\n' && ch!= '\t')
- {
- a.append(ch);
- }
- }
- // System.out.println(a.toString());
- // String subject = "HELLO , THERE ,WORLD";
- String[] couple = a.toString().split(",");
- String[] column_name = new String[couple.length];
- String[] data_types = new String[couple.length];
- for(int z=0; z<couple.length; z++)
- {
- // System.out.print(couple[z]);
- Matcher iii = Pattern.compile("(?i)(\\w+)\\s*(\\w+)").matcher(couple[z]);
- // System.out.print(couple[z]);
- if (iii.find()) {
- column_name[z] = iii.group(1);
- data_types[z] = iii.group(2);
- }
- // System.out.println(column_name[z]);
- // column_name[z] = matcher.group(1);
- // data_types[z] = matcher.group(2);
- }
- // System.out.println(Arrays.toString(data_types) +" "+ Arrays.toString(column_name) );
- List<Map<String, String>> Data = new ArrayList();
- Controller.controller().Create(name, Data);
- // System.out.println(name + " created successfully ");
- // creat table
- break;
- case 1:
- matcher = Pattern.compile("(?i)\\s*DROP\\s+TABLE?\\s+(\\w+);").matcher(query);
- if (matcher.find()) {
- name = matcher.group(1);
- Controller.controller().Drop(name);
- // System.out.print(name+ " Dropped");
- }
- // drop table
- break;
- case 2:
- // insert into table
- int parenth_lvl = 0;
- a = new StringBuilder();
- StringBuilder b = new StringBuilder();
- matcher = Pattern.compile("(?i)\\s*INSERT\\s*INTO\\s*(\\w+)\\s*").matcher(query);
- if (matcher.find()) {
- name = matcher.group(1);
- for (char ch: query.toCharArray())
- {
- if(ch == '(')
- parenth_lvl++;
- if(ch == ')')
- {
- parenth_lvl++;
- }
- if(parenth_lvl ==1&& ch !='(' && ch != '\n' && ch != ' ' && ch!= '\t')
- {
- a.append(ch);
- }
- if(parenth_lvl ==3&& ch !='(' && ch != '\n' && ch != ' ' && ch!= '\t')
- {
- b.append(ch);
- }
- }
- String[] Columns = a.toString().split(",");
- String[] Values = b.toString().split(",");
- Map<String,String> new_data = new LinkedHashMap<String,String>();
- for( i =0; i<Columns.length;i++ )
- {
- new_data.put(Columns[i], Values[i]);
- }
- Controller.controller().Insert(name, new_data);
- // System.out.println(Arrays.toString(Values) +" " + Arrays.toString(Columns));
- }
- break;
- case 3:
- // delete from
- matcher = Pattern.compile("(?i)\\s*DELETE\\s+FROM\\s*(\\w+)\\s*where\\s*(\\w+)\\s*(=|<|>)\\s*(\\w+)\\s*;").matcher(query);
- if (matcher.find()) {
- name = matcher.group(1);
- String op = matcher.group(3);
- String LHS = matcher.group(2);
- String RHS = matcher.group(4);
- //Delete (String tableName ,char operator, String col, String val )
- char[] operand = op.toCharArray();
- System.out.print( name + " " +LHS + " "+op + " " + RHS );
- Controller.controller().Delete(name, operand[0], LHS, RHS);
- }
- break;
- case 4:
- // selsct from
- matcher = Pattern.compile("(?i)\\s*SELECT\\s*((\\w+),\\s*|(\\w+))*\\s*FROM\\s*(\\w+)\\s*WHERE\\s*(\\w+)\\s*(=|<|>)\\s*(\\w+)\\s*;").matcher(query);
- if (matcher.find()) {
- name = matcher.group(4);
- String op = matcher.group(6);
- String LHS = matcher.group(5);
- String RHS = matcher.group(7);
- // System.out.print( name + " " +LHS + " "+op + " " + RHS );
- // System.out.print( Arrays.toString(Columns_names) );
- a= new StringBuilder();
- b= new StringBuilder();
- boolean flag = false;
- for (char ch: query.toCharArray())
- {
- a.append(ch);
- if(flag == true && ch != ' ' )
- b.append(ch);
- if(a.toString().equalsIgnoreCase("select") )
- {
- flag = true;
- }
- }
- String[] Columns = b.toString().split(",");
- matcher = Pattern.compile("(?i)\\s*(\\w+)\\s*FROM").matcher(Columns[Columns.length-1]);
- if (matcher.find()) {
- Columns[Columns.length-1] = matcher.group(1);
- }
- //(String tableName, String[] columns, char operator, String col, String val)
- char[] operand = op.toCharArray();
- // System.out.print( Arrays.toString(Columns) +name + RHS +operand[0] + LHS+ " " );
- Controller.controller().Select(name, Columns ,operand[0] , LHS , RHS );
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement