Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Exercise 2: parsing, regex

  • Loading branch information...
commit 048641f29cabc2ba81a01bae7b698be97e9245eb 1 parent 99cf1cf
Ralf Vogler authored

Showing 2 changed files with 80 additions and 26 deletions. Show diff stats Hide diff stats

  1. +2 26 .gitignore
  2. +78 0 01/tinydb/samples/Exercise2.java
28 .gitignore
@@ -16,32 +16,8 @@
16 16 *.log
17 17 *.aux
18 18 *.synctex.gz
  19 +*.class
19 20
20 21 #ignore thumbnails created by windows
21 22 Thumbs.db
22   -#Ignore files build by Visual Studio
23   -*.obj
24   -*.exe
25   -*.pdb
26   -*.user
27   -*.aps
28   -*.pch
29   -*.vspscc
30   -*_i.c
31   -*_p.c
32   -*.ncb
33   -*.suo
34   -*.tlb
35   -*.tlh
36   -*.bak
37   -*.cache
38   -*.ilk
39   -*.log
40   -[Bb]in
41   -[Dd]ebug*/
42   -*.lib
43   -*.sbr
44   -obj/
45   -[Rr]elease*/
46   -_ReSharper*/
47   -[Tt]est[Rr]esult*
  23 +[Bb]in
78 01/tinydb/samples/Exercise2.java
... ... @@ -0,0 +1,78 @@
  1 +import java.io.BufferedReader;
  2 +import java.io.InputStreamReader;
  3 +import java.util.HashMap;
  4 +import java.util.Map;
  5 +import java.util.regex.Matcher;
  6 +import java.util.regex.Pattern;
  7 +
  8 +import tinydb.Database;
  9 +import tinydb.Table;
  10 +
  11 +public class Exercise2 {
  12 +
  13 + // select (*|attribute(,attribute)*)
  14 + // from relation binding(,relation binding)*
  15 + // where binding.attribute=(binding.attribute|constant)
  16 + // (and binding.attribute=(binding.attribute|constant))*
  17 + public static void main(String[] args) throws java.io.IOException {
  18 + Database db = Database.open("../data/uni");
  19 + System.out.println("Enter your query below. Type 'quit' to exit.");
  20 +
  21 + BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  22 + loop:
  23 + while(true){
  24 + String q = br.readLine();
  25 + if(q.equals("quit")) break;
  26 + Pattern p = Pattern.compile("select (.+) from (.+) where (.+)");
  27 + Matcher m = p.matcher(q);
  28 + if(!m.matches()){
  29 + System.err.println("Query doesn't match the pattern: "+p.toString());
  30 + continue;
  31 + }
  32 + String selections = m.group(1);
  33 + String relations = m.group(2);
  34 + String joincond = m.group(3);
  35 + // (*|attribute(,attribute)*)
  36 + p = Pattern.compile("\\*|\\w+(,\\w+)*");
  37 + if(!p.matcher(selections).matches()){
  38 + System.err.println("Selections don't match the pattern: "+p.toString());
  39 + System.err.println("Your input: "+selections);
  40 + continue;
  41 + }
  42 + // relation binding(,relation binding)*
  43 + p = Pattern.compile("\\w+ \\w+(,\\w+ \\w+)*");
  44 + if(!p.matcher(relations).matches()){
  45 + System.err.println("Relations don't match the pattern: "+p.toString());
  46 + System.err.println("Your input: "+relations);
  47 + continue;
  48 + }
  49 + // binding.attribute=(binding.attribute|constant)
  50 + // (and binding.attribute=(binding.attribute|constant))*
  51 + p = Pattern.compile("\\w+\\.\\w+=\\w+(and \\w+\\.\\w+=\\w+)*");
  52 + if(!p.matcher(joincond).matches()){
  53 + System.err.println("Join conditions don't match the pattern: "+p.toString());
  54 + System.err.println("Your input: "+joincond);
  55 + continue;
  56 + }
  57 +
  58 + // TODO save values in file
  59 +
  60 + // handle relations
  61 + Map<String, Table> h_rel = new HashMap<String, Table>();
  62 + String[] a_rel = relations.split(",");
  63 + for(String s : a_rel){
  64 + String[] binding = s.split(" ");
  65 + System.out.println("trying to open "+binding[0]);
  66 + Table table = db.getTable(binding[0]);
  67 + if(table == null){ // TODO java.io.IOException: Stream closed
  68 + System.err.println("Table "+binding[0]+" doesn't exist");
  69 + continue loop;
  70 + }
  71 + h_rel.put(binding[1], table);
  72 + }
  73 +
  74 +
  75 + br.close();
  76 + }
  77 + }
  78 +}

0 comments on commit 048641f

Please sign in to comment.
Something went wrong with that request. Please try again.