http://www.perlmonks.org?node_id=1019585

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello all

I'm sorry if this post is long, I'm trying to provide enough data to examplin my problem. I have some database tables which contain some columns, for example:

/----------------------\ | TYPE | STYPE | OTHER | \----------------------/


The data stored within these columns is something I have to use to make a SQL query. The values stored in these tables can be simple, like

/----------------------\ | TYPE | STYPE | OTHER | |----------------------| | AAA | BBB | X=CCC | \----------------------/


The desired output for this would be
select * from targettablename where type='AAA' and stype='BBB' and x=' +CCC';

Col OTHER inclues a key value pair. So far so good? Now the syntax for things like wildcard searches and or in the existing tables is like
/-----------------------------\ | TYPE | STYPE | OTHER | |-----------------------------| | AA|BB | BB% | X=CC%&Y=DDD | |-----------------------------| | AA|BB | | X=CC%&Y=DDD | |-----------------------------| | AA | BB% | X=CC%&Y=DD% | \-----------------------------/

The SQL query these should generate are:
select * from targettablename where type in ('AA','BB') and stype like + 'BB%' and X like 'CC%' and y='DDD'; select * from targettablename where type in ('AA','BB') and X like 'CC +%' and y='DDD'; select * from targettablename where type = 'AA' and stype like 'BB%' a +nd X like 'CC%' and y like 'DD%';

So as you can see this could end up pretty complex, each column value may have multiple likes or 'ors', and the others col contains key value pairs which may also have various types of searching. I'm investigating a generic solution which can generate proper SQL queries for each entry in this table. At the moment I'm a little lost as to how to actually do this. I know that I need to split any entries in the 'other' column and process them. I'm trying to visualize a generic function to which I can pass the contents of any field (and the split up key value pairs in other). Any advice would be great.