SQL::Script - An object representing a series of SQL statements, normally stored in a file
For far too long we have been throwing SQL scripts at standalone binary clients, it's about time we had some way to throw them at the DBI instead.
Since I'm sick and tired of waiting for someone that knows more about SQL than me to do it properly, I shall implement it myself, and wait for said people to send me patches to fix anything I do wrong.
At least this way I know the API will be done in a usable way.
This module provides a very simple and straight forward way to work with a file or string that contains a series of SQL statements.
In essense, all this module really does is slurp in a file and split it by semi-colon+newline.
However, by providing an initial data object and API for this function, my hope is that as more people use this module, better mechanisms can be implemented underneath the same API at a later date to read and split the script in a more thorough and complete way.
It may well become the case that SQL::Script acts as a front end for a whole quite of format-specific SQL splitters.
# Default naive split $script = SQL::Script->new; # Custom split (string) $script = SQL::Script->new( split_by => "\n\n;" ); # Custom split (regexp) $script = SQL::Script->new( split_by => qr/\n\n;/ ); # Create a script object from pre-split statements $script = SQL::Script->new( statements => \@sql );
The new constructor create a new SQL script object, containing zero statements.
new
It takes a single option param or split_by which can be either a string or regexp by which to split the SQL.
split_by
Returns a new SQL::String object, or throws an exception on error.
# Read a SQL script from one of several sources $script->read( 'filename.sql' ); $script->read( \$sql_string ); $script->read( $io_handle );
The read method is used to read SQL from an input source (which can be provided as either a file name, a reference to a SCALAR containing the SQL, or as an IO handle) and split it into a set of statements.
read
If the SQL::Script object already contains a set of statements, they will be overwritten and replaced.
Returns true on success, or throw an exception on error.
The split_by accessor returns the string or regexp that will be used to split the SQL into statements.
In list context, the statements method returns a list of all the individual statements for the script.
statements
In scalar context, it returns the number of statements.
The split_sql method takes a reference to a SCALAR containing a string of SQL statements, and splits it into the separate statements, returning them as a reference to an ARRAY, or throwing an exception on error.
split_sql
This method does NOT update the internal state, it simply applies the appropriate parsing rules.
The run method executes the SQL statements in the script object.
run
Returns true if ALL queries are executed successfully, or undef on error.
undef
(These return values may be changed in future, probably to a style where all the successfully executed queries are returned, and the object throws an exception on error)
Bugs should be reported via the CPAN bug tracker at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SQL-Script
For other issues, contact the author.
Adam Kennedy <adamk@cpan.org>
Copyright 2007 - 2009 Adam Kennedy.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
To install SQL::Script, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SQL::Script
CPAN shell
perl -MCPAN -e shell install SQL::Script
For more information on module installation, please visit the detailed CPAN module installation guide.