Reputation: 41
It is only a very simple SQL parsing process, no HAVING
, ORDER BY
or GROUP BY
would be in the statement that needs to be parsed, but something about matching an asterisk in SELECT part won't work (e.g. SELECT * .......
)
Here's the code part:
String span="SELECT * FROM table1 t, table2";
span=span.toUpperCase();
System.out.println(span);
// Segments
String column="(\\w+\\s*(\\w+\\s*){0,1})";
String columns=column+"(,\\s*"+column+")*";
String ownerenable="((\\w+\\.){0,1}\\w+\\s*(\\w+\\s*){0,1})";
String ownerenables=ownerenable+"(,\\s*"+ownerenable+")*";
String from="FROM\\s+"+columns;
String condition="(\\w+\\.){0,1}\\w+\\s*(=|<|>|LIKE|IS)\\s*'?(\\w+\\.){0,1}[\\w%]+'?";
String conditions=condition+"(\\s+(AND|OR)\\s*"+condition+"\\s*)*";
String where="(WHERE\\s+"+conditions+"){0,1}";
String pattern = "SELECT\\s+(\\*|" + ownerenables + "\\s+" + from + ")\\s+" + where + "\\s*" + "(;){0,1}";
System.out.println(pattern);
System.out.println(span.matches(pattern));
Now if I set span to something like
"SELECT t.id, product p FROM table1 t, table2 WHERE t.id=10;"
The match result would be true
================================
But if I set it to something like this:
"SELECT * FROM table1 t, table2 WHERE t.id=10;"
(It's an asterisk I'm using for the SELECT)
It won't work in this case
Am I doing anything wrong about that asterisk?
Upvotes: 0
Views: 526
Reputation: 9658
The parenthesis must be closed before the from
:
String pattern = "SELECT\\s+(\\*|" + ownerenables + ")\\s+" + from + "\\s+" + where + "\\s*" + "(;){0,1}";
Upvotes: 1
Reputation: 59
you select table2 but don't select anything from it. you must specify the join between table1 and table2 if you want to do an * select.
This is your issue.
Upvotes: 0