0

I want to create advance query builder using PHP code.

Input :

search_String_1 AND( search_String_2 OR search_String_3) ... so on

Goal :

check AND / OR operator rules including () parenthesis and convert it into following String: Query :

db.table.search_field LIKE '
search_String_1' AND ( db.table.search_field LIKE 'search_String_2' OR db.table.search_field LIKE 'search_String_3')'

My efforts:

I have use preg_match() but I dont know how to check all operator and query sequences by best solution. Also I have exploded user input by "blank_space" , so i have all token in one array. I am still trying to find solution. If I am wrong or there is a good solution please suggest me to do so.

Thanks for your help.

Affan Pathan
  • 308
  • 1
  • 6
  • 15
  • Can we see some of your not so advanced attempts at it and build upon them? – Hanky Panky Feb 28 '13 at 06:13
  • Possible duplicate: http://stackoverflow.com/questions/10772561/writing-a-php-query-parser-with-regular-expressions – Aaron Blenkush Feb 28 '13 at 06:18
  • I recently had the same need to parse a query string as you've described into an SQL statement. It is possible in regex, but I don't recommend it, as it will be very unwieldy and not flexible. I wrote a parser that goes through the string character by character, parsing expressions as it goes. Try doing a search for `php expression parser`. You might need to 'roll your own', but there may be some source code available to get you started. – Aaron Blenkush Feb 28 '13 at 06:21
  • @AaronBlenkush I have visited [link](http://stackoverflow.com/questions/10772561/writing-a-php-query-parser-with-regular-expressions) but in my case user only input search string. Yeah I agree that I need to find character by character and check for expression. But you know I have to match all rules that SQL query parser do. e.g. There must be not more than one same operator use consecutive unless there are separated by '(' or ')' etc , so there will be more string traversal would be needed. – Affan Pathan Feb 28 '13 at 06:28
  • Define your operator rules. Mine was similar to something you'd type into the Criteria box in Microsoft Access. For example, the user might type in `="Green" OR "Black"` and my parser would output `Field = "Green" OR Field = "Black"`. You're exactly right - you can't have more than one operator in a row. So `= = "Red"` is invalid. My parser has a variable that keeps track of the next "expected" token. First expects an operator, then an expression, then either end of string or a connector (AND, OR, XOR, etc). If at any point the parser comes across an unexpected token it throws an error. – Aaron Blenkush Feb 28 '13 at 06:35
  • @AaronBlenkush Okay, There is not unique Operator that my query builder is going to support. I can give you one example of user inputted String. `document AND collaboration` OR `(file OR document) AND collaboration` – Affan Pathan Feb 28 '13 at 06:41

1 Answers1

0

Take a look at this class. It may be usefull.

http://www.phpclasses.org/browse/file/12314.html

MIIB
  • 1,849
  • 10
  • 21
  • Thanks for suggest. I have looked the code that you have suggested but there must be `WHERE` clause I need to provided by my own. And my question is how to create that `WHERE` clause based upon the user's `inputted String`. – Affan Pathan Feb 28 '13 at 06:34