0

I am trying to check cross join by query expression. For example i have the following query.

string  query="select t1.c1, t2.c2" from t1,t2";

this query will cross join both t1 and t2 table. i have tried the following way to check.

string  query="select t1.c1, t2.c2" from t1,t2";

var rx = new Regex(@"\from\s\t1(.*)\,\t2");
if(rx.Match(query)){return "cross-join"}

but my regular expression caught error. i couldn't understand where i went wrong because i am new in regex in c#. Is there any other way to check cross join in c#

MAT14
  • 129
  • 4
  • 17
  • Possible duplicate of [Parsing SQL code in C#](http://stackoverflow.com/questions/589096/parsing-sql-code-in-c-sharp) – ASh Dec 28 '15 at 08:19

1 Answers1

1

Lets analyze what your regex means:

\from\s\t1(.*)\,\t2
  • \f is form-feed character
  • rom - rom
  • \s - one whitespace
  • \t - tab
  • 1 - 1
  • (.*) - as many characters as possible
  • \, - comma
  • \t - another tab
  • 2 - 2

It other words, you added a bunch of escapes that you didn't need and change the meaning of your regex.

from\st1(.*)\,t2


Also note that you have random " in your queries:
"select t1.c1, t2.c2" from t1,t2"
                    ^
ndnenkov
  • 35,425
  • 9
  • 72
  • 104
  • note that the `(.*)` makes the regex practically useless. You probably want `\s*` here. the .* will ensure that `YOUR QUERY ... ORDER BY t1.c1 ,t2.c2` will also be detected as a cross join. – jessehouwing Dec 28 '15 at 11:02
  • regex is hardly a good idea to validate cross join presence. counter examples: 1. inner join `select * from t1, t2 where t1.id = t2.id` 2.cross join `select * from t1 cross join t2` 3. cross join effectively `select * from t1 inner join t2 on 1 = 1` – ASh Dec 28 '15 at 11:34
  • @ASh, this comment should be in OP's question, not the answer. – ndnenkov Dec 28 '15 at 12:29