NAME

sql-split - SQL splitting command line utility

SYNOPSIS

sql-split [ OPTIONS ] [ FILE(S) ]
sql-split --man

DESCRIPTION

This program tries to split any SQL code (even containing non-standard and/or procedural extensions, at least the ones from the most popular DBMSs) into the atomic statements it is composed of.

The given FILES are read and split one by one, and the resulting statements are printed to the standard output, separated by a customizable string (see below). Each given file must contain only full SQL statements, that is, no single atomic statement can span multiple files.

If no file is given, or if one of the file names is a - (dash), the SQL code is read from STDIN, so that this program can be used as a filter or even interactively.

Consider however that this is by no means a validating parser, so that errors in SQL code will not be detected (and can even lead to incorrect splitting). Additionally, this program is subject to some further (hopefully minor) limitations (for the details, please see the "LIMITATIONS" section below).

OPTIONS

-T, --terminators

It causes the trailing terminator tokens to be kept in the returned atomic statements (by default they are discarded instead).

The strings currently recognized as terminators (depending on the context) are:

  • ; (the semicolon character);

  • / (the forward-slash character);

  • a semicolon followed by a forward-slash on its own line (which is treated as a single token);

  • any string defined by the DELIMITER command.

-S, --spaces, --extra-spaces

It causes the space characters around the statements, if any, to be kept in the returned atomic statements (by default they are trimmed instead).

-C, --comments

It causes the comments, if any, to be kept in the returned atomic statements (by default any comment is discarded instead).

Both SQL and multi-line C-style comments are recognized.

-E, --empty, --empty-statements

It causes the empty statements to be returned (by default, they are discarded instead).

A statement is considered empty when it contains no characters other than the terminator and space characters. A statement composed solely of comments is not recognized as empty and it is therefore returned, if the --comments option is used. Note instead that an empty statement is recognized as such regardless of the use of the --terminators and --extra-spaces options.

-s, --oss, --output-statement-separator string

The string which will be printed between every pair of returned atomic statements. By default, it is a -- (double dash) on its own line.

To use special characters (such as newlines) when passing such string, please consult your shell docs (for example, in Bash the above mentioned default separator could be defined as $'\n--\n').

Note that the last returned statement (for each processed file) will not be followed by such separator.

-f, --ofs, --output-file-separator string

The string which will be printed between the groups of statements coming from different files. By default it is the -- >>>*<<< -- string on its own line.

Similarly to the statement separator, the file separator will not be printed after the last file.

-e, --error, --on-error value

It controls the program behavior in case one of the given files is not accessible.

It can take the following values:

  • stop or 0, which causes the program to die at the first file which can not be opened, but it prints all the statements split that far (this is the default value);

  • continue or 1, which causes the program, when it encounters a file error, to just emit a warning (on STDERR) and continue with the next file;

  • no-output or 2, which, just like stop, causes the program to die at the first file error, but in this case it does not print any statement, not even those coming from the previous (already read) files; in other words, the statements are printed out only if (and after) all of the given files have been successfully read.

The above listed string values are case-insensitive.

-h, -?, --help

It prints a brief help message and exits.

--man

It shows the full man page.

--version

It prints the program version and exits.

SUPPORTED DBMSs

sql-split aims to cover the widest possible range of DBMSs, SQL dialects and extensions (even proprietary), in a fully transparent way for the user.

Currently it has been tested mainly on SQLite, PostgreSQL, MySQL and Oracle.

LIMITATIONS

To be split correctly, the given SQL code is subject to the following limitations.

  • Identifiers

    The keywords BEGIN, DECLARE, FUNCTION and PROCEDURE (case-insensitive) cannot be used as (bare) object identifiers (e.g. table names, field names etc.)

    They can however be used, as long as they are quoted, as shown here:

    CREATE TABLE  declare  (  begin  VARCHAR ); -- Wrong, though accepted by some DBMSs.
    CREATE TABLE "declare" ( "begin" VARCHAR ); -- Correct!
  • Procedural extensions

    Currently any block of code which start with DECLARE, CREATE or CALL is correctly recognized, as well as Bare BEGIN ... END blocks and dollar quoted blocks, therefore a wide range of procedural extensions should be handled correctly. However, only PL/SQL and PL/PgSQL code has been tested so far.

  • PL/SQL

    If a package contains also an initialization block, then it must have the package name after the END or it must terminate with a semicolon and a slash (which is the recommended practice anyway).

    For example, these two package (pseudo-)definitions will be correctly split:

    -- OK since it has the trailing slash
    CREATE OR REPLACE PACKAGE BODY my_package_1 AS
        ...
    BEGIN
        ...
    END;
    /
    
    -- OK since it has the package name after the END
    CREATE OR REPLACE PACKAGE BODY my_package_2 AS
        ...
    BEGIN
        ...
    END my_package_2;

    while this one wouldn't, since it contains an initialization block and it lacks both the package name after the END and the trailing slash:

    CREATE OR REPLACE PACKAGE BODY my_package AS
        ...
    BEGIN
        ...
    END;

    Note however that if the initialization block is absent, the package block will be correctly recognized even if it lacks both the package name after the END and the trailing slash.

Non-limitations

To be split correctly, the given input must, in general, be syntactically valid SQL. For example, an unbalanced BEGIN or a misspelled keyword could, under certain circumstances, confuse the parser and make it trip over the next statement terminator, thus returning wrongly split statements. This should not be a problem though, as the original (invalid) SQL code would have been unusable anyway (remember that this is NOT a validating parser!)

SEE ALSO

COPYRIGHT

Copyright 2010-2011 Emanuele Zeppieri <emazep@cpan.org>.

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html

NO WARRANTY

This program comes with NO WARRANTIES of any kind. It not only may cause loss of data and hardware damaging, but it may also cause several bad diseases to nearby people, including, but not limited to, diarrhoea, gonorrhea and dysmenorrhea. Don't say you haven't been warned.