-4

I have a query like this:

"select * from aaa where id < 10 and name = 'test'"

From this query I want to find out the number of conditions after where (2 in this case).

Also I want to find out how many times < and = were repeated.

brimborium
  • 9,362
  • 9
  • 48
  • 76
user1632194
  • 33
  • 2
  • 5
  • Very difficult in general without a full-fledged SQL parser. Maybe this helps: http://stackoverflow.com/questions/660609/sql-parser-library-for-java – Thilo Dec 03 '12 at 10:54
  • 2
    Can't you just count the `AND`s and `OR`s + 1 (for the first condition)? – Kai Dec 03 '12 at 10:56
  • Have you tried something yet? If not, try something and if you can't get it to work, come back and show us what you did. If yes, show us your code. – brimborium Dec 03 '12 at 10:56
  • try something first there are plenty of string methods , regex expressions , string tokeniser , try what you can first – Hussain Akhtar Wahid 'Ghouri' Dec 03 '12 at 10:58
  • You can perform `"select count(1) from aaa where id < 10"` and `"select count(1) from aaa where name = 'test'"` – Peter Lawrey Dec 03 '12 at 11:11

1 Answers1

0

you can simply count all the > and = with a loop

bool whereCondition = false;
int counterCompare = 0;
int counterConditions = 0;
// select is your SQL String
for(int i = 0;i<select.length;++i)
{
   if(whereCondition){
       if(select.charAt(i) = '<' || select.charAt(i) = '>') {
          ++counterCompare;
       }
       if(select.charAt(i) = '=' && !(select.charAt(i+1) = '<' || select.charAt(i+1) = '>')) {
          ++counterCompare;
       }
       if (select.charAt(i) = 'A') {
         if(i+2 < select.length) {
            if(select.charAt(i+1) = 'N' &&
               select.charAt(i+2) = 'D')
                  ++counterCondition;
         }
      }
      if (select.charAt(i) = 'O') {
         if(i+4 < select.length) {
            if(select.charAt(i+1) = 'R' &&
               select.charAt(i+2) = 'D' &&
               select.charAt(i+3) = 'E' &&
               select.charAt(i+4) = 'R' &&)
                  whereCondition = false;
         }
      }
   }
   else {
      if (select.charAt(i) = 'W') {
          if(i+4 < select.length) {
             if(select.charAt(i+1) = 'H' &&
                select.charAt(i+2) = 'E' &&
                select.charAt(i+3) = 'R' &&
                select.charAt(i+4) = 'E' &&)
                   whereCondition = true;
          }
      }
   }
}

i don´t promise this code to work. you also have to deal with a where conditions based on order or an And compare. so you need to check, if you found an And , if it´s not a String you´re comparing in your table. you also have to check if you got small or tall letters. but this is just a basic compare

SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33