NAME

DBIx::Simple::Procedure - An Alternative To SQL Stored Procedures using DBIx::Simple

VERSION

Version 1.40

SYNOPSIS

This module allow your program to process text files containing one or many commands that execute SQL statements sequentially. Please keep in mind that DBIx::Simple::Procedure is an alternative to database stored procedures and not a replacement or emulation of them. Essentially it is an interface to execute and return data from multiple queries.

Here is an example of how to setup and process a (sql) text file.

# DBIx::Simple::Procedure uses DBIx::Simple and provides an accessor through the DBIx::Simple::Procedure->{dbix} hash reference.

use DBIx::Simple::Procedure; my $db = DBIx::Simple::Procedure->new($path_to_sqlfiles, 'dbi:SQLite:dbname=file.dat'); # Will error out using DBIx::Simple if a connection error occurs.

# The queue function takes one parameter (a text file) that contains DBIx::Simple::Procedure # sql commands.

# The process_queue function processes all queued sql statements using the parameters passed # to it, similar to the execute function of DBI.

$db->queue($sql_file)->process_queue(@sql_parameters);

# The cache function returns an array of resultsets, or the resultset of the index passed, # return by the select statements encountered in the sql file. # Note! files included using the "include" command will not have there resultsets cached, # even if a "capture" command is encountered, only the select statement(s) found in the # initial sql file are cached in the order they are encountered.

foreach my $result (@{$db->cache(0)}){ # do something with the records of the first resultset $result->{...}; }

SQL FILE SYNTAX

The (sql) procedural text file to be processed may contain any text you desire, e.g. comments and other markup. DBIx::Simple::Procedure only reacts to command lines (commands). These instructions (or commands) must be placed on its own line and be prefixed with an exclamation point, a space, the command, another space, and the statement to be evaluated.

E.g. "! execute select * from foo".

Multiple commands can be used in a single sql file. Note! multi-line sql statements not supported in this release.

SQL File Commands:

! execute: This command simply execute the supplied sql statement.

! capture: This is an execute command who's dataset will be cached (stored) for later use. Note! This command can only be used with a select statement.

! replace: This is an execute command that after successfully executed, replaces the scope parameters with data from the last row in its dataset. Note! This command can only be used with a select statement.

! include: This command processes the supplied sql file in a sub transaction. Note! Included sql file processing is isolated from the current processing. Any capture commands encountered in the included sql files will not cache the dataset.

! proceed: This command should be read "proceed if" because it evaluates the string passed (perl code) for truth, if true, it continues if false it skips to the next proceed command or until the end of the sql file.

! ifvalid: This command is a synonym for proceed.

! storage: This command does absolutely nothing except store the sql statement in the commands list (queue) for processing individually from within the perl code with a method like process_command.

! declare: This command is effectively equivalent to the select .. into sql sytax and uses an sql select statement to add vairables to the scope for processing (e.g. ! declare select `name` from `foo` where `id` = $0) can be used in other instructions as $!name, e.g. ! execute update `foo` set `name` = $!name where `id` = $0.

! forward: This command takes an index and jumps to that command line and continues.

! process: This command takes an index and executes that command line.

! examine: This command is used for debugging, it errors out with the compiled statement passed to it.

EXAMPLE

# inside tables/group/insert.sql ! include tables/group/create.sql ! proceed 1 == 2 ! execute insert into `group` values (null, concat_ws(' ', 'Test A', $0)) ! proceed 1 == 1 ! execute insert into `group` values (null, concat_ws(' ', 'Test B', $0)) ! proceed 1 ! capture select * from `group`

# inside tables/group/create.sql ! execute create table if not exists `group` (`id` int(11) auto_increment, `info` varchar(255) not null, primary key(`id`) ) ! execute truncate table `group`

# inside test.pl use FindBin; use DBIx::Simple::Procedure;

# connecting to a mysql database my $fs = "$FindBin::Bin/"; my $db = DBIx::Simple::Procedure->new( $fs, 'dbi:mysql:database=foo', # dbi source specification '', '', # username and password ) or die DBIx::Simple::Procedure->error;

$db->queue('tables/group/insert.sql')->process_queue('One fish, two fish, red fish, blue fish.'); # or $db->queue('tables/group/insert.sql')->process_command(4,'One fish, two fish, red fish, blue fish.'); # the process_command method executes the 5th command in the tables/group/insert.sql file.

foreach my $result (@{$db->cache(0)}){ # $result->{...}; }

METHODS

new

The new method instantiates a new DBIx::Simple and DBIx::Simple::Procedure object and accepts all parameters required/accepted by DBIx::Simple.

queue

The queue function parses the passed (sql) text file and build the list of sql statements to be executed and how.

process_queue

The process_queue function sequentially processes the recorded commands found the (sql) text file.

cache

The cache method accesses an arrayref of resultsets that were captured using the (sql file) capture command and return the resultset of the index passed to it or an empty arrayref.

command

The command method is used to queue a command to be processed later by the process_queue method. Takes two arguments, "command" and "sql statement", e.g. command('execute', 'select * from foo').

process_command

The process_command method allows you to process the indexed sql satements from your sql file individually. It take two argument, the index of the command as it is encountered in the sql file and tries returns a resultset, and any parameters that need to be passed to it.

clear

The clear method simply clears the cache (resultset store)

TODO

...

AUTHOR

Al Newkirk, <al.newkirk at awnstudio.com>

BUGS

Please report any bugs or feature requests to bug-dbix-simple-procedure at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Simple-Procedure. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc DBIx::Simple::Procedure

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE

Copyright 2009 Al Newkirk.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.