Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

What's your favorite SQL generation module?

by perrin (Chancellor)
on Jun 06, 2006 at 21:53 UTC ( #553926=perlquestion: print w/replies, xml ) Need Help??

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

I have a need for a module that will generate very complex SQL based on a search spec. This will include things like left joins and subqueries. I'm looking for recommendations on CPAN modules that will give me a head start.

Note that I am not looking for an object-relational mapper here. I just want to generate SQL.

UPDATE: After checking out the suggestions here and looking through CPAN results for SQL and DBIx, it's coming down to a decision between SQL::Interpolate and Rose::DB::Object::QueryBuilder.

  • Comment on What's your favorite SQL generation module?

Replies are listed 'Best First'.
recommending SQL::Intepolate for SQL generation
by markjugg (Curate) on Jun 07, 2006 at 01:37 UTC

    I've worked with several over these over the years. I started with the ones that were integrated with DBI, like DBIx::Abstract. I eventually found it cumbersome that this approach left very little space for me to further tweak the SQL.

    I was then drawn to SQL::Abstract for the simplicity of it-- just generating SQL and staying away from the database handle.

    I had more flexibility, but eventually found SQL::Abstract's where clause generation too abstract and inflexible-- some kinds of AND/OR nesting just wasn't possible.

    This brings me to my current recommendation, which is SQL::Interpolate. It allows you to express parts of SQL in Perlish ways, with a result that is natural looking and still open generating the rest of the SQL however you want.

    No SQL statement has been too complex to use with SQL::Interpolate. Just to bring things full circle, I use it through DBIx::Interpolate, which simply passes the result on to DBI without getting the way further.

    Still, it may not be a complete solution for what you are describing. Today I ran into a problem that perhaps like what you described. I needed to take a simple keyword field, split up the words, and generate some nested SQL to regular expression matches against several fields.

    For that I wrote SQL::KeywordSearch, which I hope to put on CPAN soon. It can either generate $sql and @bind directly, or it can alternately generate a syntax that would plug directly into a larger SQL::Interpolate query.

    If there's interest, I could prioritize wrapping up SQL::KeywordSearch for publishing. It's definitely a niche tool, but may at least be helpful as an example.

    Note: I think SQL::Interpolate 0.40 may be released soon, with some improved syntax and docs.

      I for one would be interested in seeing SQL::KeywordSearch as this sounds like an interesting project. When do you think you would have a beta version available?


        I'll try to publish this on CPAN in the next few days. Working code, docs and tests are already written. Just needs Polish and Publish. :)
Re: What's your favorite SQL generation module?
by davidrw (Prior) on Jun 06, 2006 at 22:30 UTC
Re: What's your favorite SQL generation module?
by xdg (Monsignor) on Jun 07, 2006 at 00:21 UTC

    I've seen some acclaim for SQL::Interpolate, though I've personally only tried it for a minor experiment. It won't figure out the joins for you -- you write the query in SQL and it just makes it easy to plug in the variable parts as perl data structures.


    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: What's your favorite SQL generation module?
by UnderMine (Friar) on Jun 07, 2006 at 05:40 UTC
    There are arguments for and against SQL generation.

    Coming from a DBA background I personally tend to do a minimal wrapping of DBI as a lot of these modules don't process transactions effectively and can be quite difficult to get to write optimised SQL code.

    SQL::Interpolate does a mimimal amount of work for you. Allowing you to be helped by it(i.e. selects, inserts, updates especially) but still have control enough to bypass it during a transaction if you have to.

    My opinion is you should always hand code a delete if you have to do it as these are too risky to leave to a code generator. However it is better to do a logical delete and then you can roll it back at any time.

    Hope it helps

      I have data from a form that has to be turned into a select statement and it has too many options to write out every possibility, so there is no other option but to generate SQL to some degree. Since I'm only looking for something to create the actual SQL, not to handle any of the DBI work, transaction management will be up to me.
        Since no-one has mentioned DBIx::Class and its powerful synergy with SQL::Abstract (a good SQL generator on its own), I now mention it. Jesse Vincent's DBIx::SearchBuilder is heavily used. And we can't forget DBIx::Recordset for a change in approach, though I am not happy with what happened to all my bugfixes to it. qw
Re: What's your favorite SQL generation module?
by spatterson (Pilgrim) on Jun 08, 2006 at 14:52 UTC
    I've found Text-Query-SQL-0.09 handy on the few occasions where generating my own SQL would be tedious and ungood.

    just another cpan module author

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://553926]
Approved by kvale
Front-paged by ChemBoy
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-06-07 05:13 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (29 votes). Check out past polls.