NAME
    SQL::SplitStatement - Split any SQL code into atomic statements

VERSION
    Version 0.01000

SYNOPSIS
        my $sql_code = <<'SQL';
        CREATE TABLE parent(a, b, c   , d    );
        CREATE TABLE child (x, y, "w;", "z;z");
        CREATE TRIGGER "check;delete;parent;" BEFORE DELETE ON parent WHEN
            EXISTS (SELECT 1 FROM child WHERE old.a = x AND old.b = y)
        BEGIN
            SELECT RAISE(ABORT, 'constraint failed;');
        END;
        INSERT INTO parent (a, b, c, d) VALUES ('pippo;', 'pluto;', NULL, NULL);
        SQL
    
        use SQL::SplitStatement;
    
        my $sql_splitter = SQL::SplitStatement->new;
    
        my @statements = $sql_splitter->split($sql_code);
    
        # @statements now is:
        #
        # (
        #     'CREATE TABLE parent(a, b, c   , d    )',
        #     'CREATE TABLE child (x, y, "w;", "z;z")',
        #     'CREATE TRIGGER "check;delete;parent;" BEFORE DELETE ON parent WHEN
        #     EXISTS (SELECT 1 FROM child WHERE old.a = x AND old.b = y)
        # BEGIN
        #     SELECT RAISE(ABORT, \'constraint failed;\');
        # END',
        #     'INSERT INTO parent (a, b, c, d) VALUES (\'pippo;\', \'pluto;\', NULL, NULL)'
        # )

DESCRIPTION
    This is a very simple module which permits to split any (not only DDL)
    SQL code into the atomic statements it is composed of.

    The logic used to split the SQL code is more sophisticated than a raw
    "split" on the ";" (semicolon) character, so that SQL::SplitStatement is
    able to correctly handle the presence of the semicolon inside
    identifiers, values or "BEGIN..END" blocks (even nested blocks), as
    exemplified in the synopsis above.

    Consider however that this is by no mean a validating parser: it
    requests its input to be syntactically valid SQL, otherwise it can
    return unusable statements (that shouldn't be a problem though, as the
    original SQL code would have been unusable anyway).

    As long as the given SQL code is valid, it is guaranteed however that it
    will be split correctly (otherwise it is a bug, that will be corrected
    once reported).

    If your atomic statements are to be fed to a DBMS, you are encouraged to
    use DBIx::MultiStatementDo instead, which uses this module and also
    (optionally) offer automatic transactions support, so that you'll have
    the *all-or-nothing* behavior you would probably want.

METHODS
  "new"
    *   "SQL::SplitStatement->new( \%options )"

    It creates and returns a new SQL::SplitStatement object. It accepts its
    options as an hashref.

    The following options are recognized:

    *   "keep_semicolon"

        A boolean option which causes, when set to a false value (which is
        the default), the trailing semicolon to be discarded in the returned
        atomic statements.

        When set to a true value, the trailing semicolons are kept instead.

        If your statements are to be fed to a DBMS, you are strongly
        encouraged to keep this option to its default (false) value, since
        some drivers/DBMSs don't accept the semicolon at the end of a
        statement.

        (Note that the last, possibly empty, statement of a given SQL code,
        never has a trailing semicolon. See below for an example.)

    *   "keep_extra_spaces"

        A boolean option which causes, when set to a false value (which is
        the default), the spaces ("\s") around the statements to be trimmed.

        When set to a true value, these spaces are kept instead.

        When "keep_semicolon" is set to false as well, the semicolon is
        discarded first (regardless of the spaces around it) and the
        trailing spaces are trimmed then. This ensures that if
        "keep_extra_spaces" is set to false, the returned statements will
        never have trailing (nor leading) spaces, regardless of the
        "keep_semicolon" value.

    *   "keep_empty_statements"

        A boolean option which causes, when set to a false value (which is
        the default), the empty statements to be discarded.

        When set to a true value, the empty statements are returned instead.

        A statement is considered empty when it contains no character other
        than the semicolon and space characters ("\s").

        Note that this option is completely independent to the others, that
        is, an empty statement is recognized as such regardless of the
        values of the above options "keep_semicolon" and
        "keep_extra_spaces".

    These options are basically to be kept to their default (false) values,
    especially if the atomic statements are to be given to a DBMS.

    They are intented mainly for *cosmetic* reasons, or if you want to count
    by how many atomic statements, including the empty ones, your original
    SQL code was composed of.

    Another situation where they are useful (necessary, really), is when you
    want to retain the ability to verbatim rebuild the original SQL string
    from the returned statements:

        my $verbatim_splitter = SQL::SplitStatement->new({
            keep_semicolon        => 1,
            keep_extra_spaces     => 1,
            keep_empty_statements => 1
        });
    
        my @verbatim_statements = $verbatim_splitter->split($sql);
    
        $sql eq join '', @verbatim_statements; # Always true, given the constructor above.

    Other than this, again, you are highly recommended to stick with the
    defaults.

  "split"
    *   "$sql_splitter->split( $sql_string )"

    This is the method which actually splits the SQL code into its atomic
    components.

    It returns a list containing the atomic statements, in the same order
    they appear in the original SQL code.

    Note that, as mentioned above, an SQL string which terminates with a
    semicolon contains a trailing empty statement: this is correct and it is
    treated accordingly (if "keep_empty_statements" is set to a true value):

        my $sql_splitter = SQL::SplitStatement->new({
            keep_empty_statements => 1
        });
    
        my @statements = $sql_splitter->split( 'SELECT 1;' );
    
        print 'The SQL code contains ' . scalar(@statements) . ' statements.';
        # The SQL code contains 2 statements.

  "keep_semicolon"
    *   "$sql_splitter->keep_semicolon"

    *   "$sql_splitter->keep_semicolon( $boolean )"

        Getter/setter method for the "keep_semicolon" option explained
        above.

  "keep_extra_spaces"
    *   "$sql_splitter->keep_extra_spaces"

    *   "$sql_splitter->keep_extra_spaces( $boolean )"

        Getter/setter method for the "keep_extra_spaces" option explained
        above.

  "keep_empty_statements"
    *   "$sql_splitter->keep_empty_statements"

    *   "$sql_splitter->keep_empty_statements( $boolean )"

        Getter/setter method for the "keep_empty_statements" option
        explained above.

DEPENDENCIES
    SQL::SplitStatement depends on the following modules:

    *   Class::Accessor::Fast

    *   SQL::Tokenizer

AUTHOR
    Emanuele Zeppieri, "<emazep@cpan.org>"

BUGS
    Please report any bugs or feature requests to "bug-sql-SplitStatement at
    rt.cpan.org", or through the web interface at
    <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SQL-SplitStatement>. 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 SQL::SplitStatement

    You can also look for information at:

    *   RT: CPAN's request tracker

        <http://rt.cpan.org/NoAuth/Bugs.html?Dist=SQL-SplitStatement>

    *   AnnoCPAN: Annotated CPAN documentation

        <http://annocpan.org/dist/SQL-SplitStatement>

    *   CPAN Ratings

        <http://cpanratings.perl.org/d/SQL-SplitStatement>

    *   Search CPAN

        <http://search.cpan.org/dist/SQL-SplitStatement/>

ACKNOWLEDGEMENTS
    Igor Sutton for his excellent SQL::Tokenizer, which made writing this
    module a joke.

SEE ALSO
    *   DBIx::MultiStatementDo

LICENSE AND COPYRIGHT
    Copyright 2010 Emanuele Zeppieri.

    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.

