0
  Pattern subsetPattern = Pattern.compile("select(.*)from.*");
  Pattern resourcePattern = Pattern.compile(".*from(.*)(where|limit|;).*");
  Pattern filterPattern = Pattern.compile(".*where(.*)(limit|;).*");     

public Query(String queryString) {

        System.out.println(queryString);

        // removes all the newlines in query.
        queryString = queryString.replace("\n", "").replace("\r", "");
        queryString = queryString.toLowerCase();

        Matcher subsetMatcher = subsetPattern.matcher(queryString);
        Matcher resourceMatcher = resourcePattern.matcher(queryString);
        Matcher filterMatcher = filterPattern.matcher(queryString);

        if (subsetMatcher.matches()) {
          subset = subsetMatcher.group(1).trim();
        }
        if (resourceMatcher.matches()) {
          resource = resourceMatcher.group(1).trim();
        }
        if (filterMatcher.matches()) {
          filter = filterMatcher.group(1).trim();
        }

        System.out.println(subset + "\n" + resource + "\n" + filter + "\n" + limit);
        System.out.println("===========");
      }

Above code prints out the following output

SELECT col_x, col_y FROM table_name WHERE col_z = 'yello'  LIMIT k;
col_x, col_y
table_name where col_z = 'yello'  limit k
col_z = 'yello'  limit k
null
===========

why does the resource group matcher do not stop at the where ?

dinesh707
  • 11,448
  • 22
  • 77
  • 127

0 Answers0