Scenario: You have a Perl application that performs lots of stuff but you are handed a SQL text file that you need to run on a regular basis from within your application.

Update: I fixed the code to actually use the iterator this time :)

Read the SQL text file and send each batch to the database using Perl. In this case, we aren’t performing any real parsing of the SQL itself, we are simply retrieving the individual SQL batches. I’m using Rintaro Ishizaki’s Iterator::Simple Perl module so we can very easily get the next SQL batch.

package dbS::Sybase::Parse::SQL_File; use warnings; use strict; use Iterator::Simple qw(iterator); BEGIN { use Exporter (); our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); $VERSION = 1.0.0; @ISA = qw(Exporter); @EXPORT_OK = qw(&batch); } our $FH; ############################# sub batch { my $file = shift; open ($FH, "<", $file) or die ("unable to open sql file\n"); iterator { my $query = ""; while (my $line = <$FH>) { chomp $line; last if ($line =~ m/^go\s*$/i); $query .= $line . " "; } return $query; } } 1;
Obtaining the individual batches are now very easy. Note, that we are making several assumptions:
  1. SQL batches end with a go (case insensitive)
  2. SQL code is valid
  3. security of the SQL text file is handled by the operating system (we're not going to worry about SQL injection attacks at this level)
use dbS::Sybase::Parse::SQL_File qw(open_file next_batch); .... if ( my $batch = dbS::Sybase::Parse::SQL_File::batch("SQL/SNAP.sql") ) + { print "-"x40 . "\n"; print " Performing IGOR\n"; print "-"x40 . "\n"; while ( my $sql_batch = $batch->next ) { dbh_do($local_dbh, $sql_batch); } }

Granted, we could have performed this without the iterator, but this is just the first revision. I expect to be adding a lot more to it (e.g. T-SQL verifier) so that I can hide the complexity behind the iterator.


In reply to How to read batches of SQL from a file in Perl by jfroebe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.