in reply to Abstracting SQL without Stored Procedures

Personally I keep my SQL in a config-like file that lets me load the statements into a hash and keeps the SQL *completely* separate from the perl. My config files look something like this:
[drop] DROP TABLE cbwb; DROP TABLE cbwb_topic; [insert] INSERT INTO cbwb (id,puser,ptopic) VALUES (?,?,?);

Note that a hash elements are separated by double-newline, the hash keys are on the first line of the element in square brackets, and the values can contain multiple SQL statements separated by a semicolon+newline combination. For hash values that contain multiple statements, I run them like this: $dbh->do($_) for split /;\n/, $sql{drop};

Another thought is to use dbish (DBI::Shell) which allows you to call SQL statements from named files. That also lets you keep a straight SQL file that has *no* perl in it.

updateOh, I forgot: these days I don't use the square brackets, I put the hash keys in as SQL comments so that the config file is actually all SQL.

Replies are listed 'Best First'.
Re^2: Abstracting SQL without Stored Procedures
by paulbort (Hermit) on Sep 30, 2004 at 18:16 UTC
    Using a hash is a good idea, regardless of how you store it, because it allows you to dynamically switch between statements, for things like different sort orders, or different inserts. ( My app inserts a row in either a queue table or a log table depending on outside data. By constructing the inserts to use the same parameter list, I can just use the appropriate hash key. )

    Another way to store the statements would be with Data::Dumper.

    --
    Spring: Forces, Coiled Again!