Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Is there a module to generate SQL queries?

by rrwo (Friar)
on Oct 25, 2001 at 02:58 UTC ( #121304=perlquestion: print w/ replies, xml ) Need Help??
rrwo has asked for the wisdom of the Perl Monks concerning the following question:

Are there any modules for generating SQL queries?

I have some prototype code for generating simple queries, wyhere fields, sorting order, and where clauses with a mix of ANDs and ORs can be specified. But before I continue on this track....is there already such a module?

I've found Text::Query::BuildSQL, but this uses AltaVista-like syntax and isn't what I'm looking for.

The reason I want more detailed functionality is so that

  • I can specify a nifty search widget for specific tables on the web that lets the user choose fields to view, filter by, and sorting order (with appropriate validation for security protection, of course);
  • I can also re-use a lot of this code for various classes/objects which work on these tables;
  • And in some ways, it makes the code more readible than to inline an SQL statement.

Comment on Is there a module to generate SQL queries?
Re: Is there a module to generate SQL queries?
by andreychek (Parson) on Oct 25, 2001 at 03:26 UTC
    I think DBIx::Recordset may do what you're looking for.

    What it does is allow you to construction you SQL statements using a hash, with the hash keys mapping to field names, and the hash values mapping to the data in those fields. There is an article that appeared on perl.com that provides some examples of what all can be done with DBI::Recordset. Copying one of their examples, this seems to do what you are desiring:
    DBIx::Recordset->Insert({%formdata,%dsn});

    It can't get too much easier then that :-) However, the article also gives other examples of some pretty nifty things you can do with that module.

    Additionally, to be fair to all sides, chromatic published an article in response to the one I linked above describing how you can use DBI to do similar tasks, and how best to make use of DBI's featureset.

    I've never used it before, but I did just notice DBIx::SearchBuilder was uploaded to CPAN. It claims to assist in generating SELECT statements. Good luck!
    -Eric

      Thanks, but DBIx::RecordSet is definitely what I'm not looking for. DBIx::SearchBuilder looks nice, but like the former it does too much.

      I just want to create the SQL query, and then pass the parameters to the bindings. But I still want a statement handle to do what I will with.

      An aside: DBIx::SearchBuilder seems poorly documented, and specific to certain databases.

      Gripe: I can't stand it when there are modules that try to do everything (A+B+C), when I need a module to do A or B only, because I'm trying to do A+B+D instead. I do wish module authors would break up their modules into modules with distinct useful functions (i.e., SQL statement generation, statement handle searches, and then maybe a recordset add-on as all separate modules). It forces one to write portions of this from scratch! (Guess I'll E-mail the author about this....)

Re: Is there a module to generate SQL queries?
by Fletch (Chancellor) on Oct 25, 2001 at 07:57 UTC
Re: Is there a module to generate SQL queries?
by brother ab (Scribe) on Oct 25, 2001 at 09:35 UTC

    If you need a module that could help you form complicated but recurring queries you can look at DBIx::SearchProfile. With this module you can create your own templates of typical queries and reuse them easily with minimum of code to write.

    If you need a module to provide good SQL abstraction you can try DBIx::Abstract.

    And at last but not least, you can try various RDBMS-OO mappers that could raise you abstraction level from tables and SQL-queries to object data model. I prefer Alzabo for my own.

    -- brother ab
Re: Is there a module to generate SQL queries?
by rrwo (Friar) on Nov 08, 2001 at 09:31 UTC

    Well, my compromise was to convert the code I wrote to SQL::QueryBuilder::Simple, mention existing modules with similar functionality, and upload it to CPAN (should be there in a day or so if the link above doesn't work).

    I'm not happiest with creating another module on CPAN, but this one best suits my needs, and well, see my meditation about the subject.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://121304]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-09-01 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (13 votes), past polls