Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Mixing Perl and SQL

by toma (Vicar)
on Nov 04, 2009 at 04:38 UTC ( [id://804840] : perlquestion . print w/replies, xml ) Need Help??

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

I have been doing more with SQL lately, and I have lots of SQL code to run from Perl using DBI. There is so much of it that lately I prefer to have the SQL code in a separate file from my Perl, because the vim editor highlights the SQL and Perl syntax correctly, based on the filename. However, I think this makes a module that uses the SQL file harder to deploy, because the Perl code has to find the SQL file based on where it was installed by the module.

I can think of several approaches to fixing this. One would be to fix my editor so that all 'here docs' are interpreted as SQL, or to learn some new trick about paths and module installation. Or likely there is an even a better approach that I haven't thought of.

Suggestions, perl lang vimrc patches, perl module tricks all welcome! Thanks.

It should work perfectly the first time! - toma

Replies are listed 'Best First'.
Re: Mixing Perl and SQL
by happy.barney (Friar) on Nov 04, 2009 at 08:11 UTC
    look at %INC
    use Class::Date; print $INC{'Class/'};
      This works fine, thanks!

      If anyone else has other ideas, I will try those, also.

      It should work perfectly the first time! - toma
Re: Mixing Perl and SQL
by mje (Curate) on Nov 04, 2009 at 17:11 UTC

    This does not strictly answer your question but it works well for us and you might find it useful. The ONLY SQL we have in our application is:

    begin synonym.proc_name(?, ? ....); end; begin ? := synonym.func_name(?, ?, ...); end;

    All the SQL is in procedures and functions in the database - including those that simply return a result-set. This allows us to keep all the logic in the database. Of course you can only do this if your DBD and database supports procedures and the returning of reference cursors which DBD::Oracle and Oracle does.

      My code has to create the whole database from scratch, so I would need to include the SQL code that creates the stored procedures, and therefore I think I would be left with the same mixed-language issue.

      For example, one of my programs first uses modules to transform the input data from several formats into flat files. Then it uses modules that include the SQL to:

      1. Create an empty database with tables and views.
      2. Load the data from flat files.
      3. Transform the data into another set of tables that use a different indexing scheme.
      4. Query the data to generate a report.

      It should work perfectly the first time! - toma
Re: Mixing Perl and SQL
by metaperl (Curate) on Nov 04, 2009 at 16:07 UTC
    Do you envision one SQL file per module?
      Some modules use one file per module. It would be nice if it was possible to have more, unless there is some benefit to keeping it 1:1.

      If there is a reason to do so, please assume that I can split up my Perl modules so that there is at most one file per module.

      It should work perfectly the first time! - toma