I'm working on a custom query system that needs to be very dynamic.
Right you can create a type of query by adding the querytype array.
So...
$querytype[] = array('select','%COLUMN%','from','%TABLE%','!%WHERE%!','!%COLUMN%!','!%OPERATOR%!','!%VALUE%!');
A % symbol means that it's a special value and must be compared to a list of knowns. A ! symbol means it's optional and is not required.
Am I going about this the right way?
Also, I'm looking for advice on the best way to parse the query string it receives, right now I'm pulling to query into an array by exploding it with spaces.
Any ideas?
Best way to parse a "query" string...
Moderator: General Moderators
-
aluminumpork
- Forum Newbie
- Posts: 14
- Joined: Mon Oct 24, 2005 8:54 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Best way to parse a "query" string...
I guess the syntax you choose is up to you (did you say you're writing a custom language?).aluminumpork wrote:I'm working on a custom query system that needs to be very dynamic.
Right you can create a type of query by adding the querytype array.
So...
$querytype[] = array('select','%COLUMN%','from','%TABLE%','!%WHERE%!','!%COLUMN%!','!%OPERATOR%!','!%VALUE%!');
A % symbol means that it's a special value and must be compared to a list of knowns. A ! symbol means it's optional and is not required.
Am I going about this the right way?
Also, I'm looking for advice on the best way to parse the query string it receives, right now I'm pulling to query into an array by exploding it with spaces.
Any ideas?
To parse the string... don't explode at whitespace, it'll break strings etc which are quoted.
Tokenize it using a regular expression -- be warned... these kinds of regex can get very very scary
EDIT | This one does a fairly basic job of tokenizing the syntax apart for you, as well as leaving strings and octal/hexadecimal numbers in tact.
Code: Select all
$re = "#(?:(?<!\\\\)\'.*?(?<!\\\\)\')|(?:(?<!\\\\)".*?(?<!\\\\)")|(?:(?<!\\\\)//.*?\n)|(?:(?<!\\\\)/\\*.*?\\*/)|0x[a-z0-9]+|\\s+|\\W|\\w+#ism";