/usr/bin/sql-split is in libsql-splitstatement-perl 1.00020-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | #!/usr/bin/env perl
###########################################################################
# Copyright 2011 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. #
###########################################################################
use strict;
use warnings;
use SQL::SplitStatement;
our $VERSION = $SQL::SplitStatement::VERSION;
use Getopt::Long;
Getopt::Long::Configure qw(
auto_help auto_version bundling require_order gnu_compat
);
use Pod::Usage;
### Options ###
my $man;
my $help;
my $keep_terminators;
my $keep_comments;
my $keep_empty_statements;
my $keep_extra_spaces;
my $slash_terminates;
# Global variables (UPPERCASED)
my $OUTPUT_STATEMENT_SEP = "\n--\n";
my $OUTPUT_FILE_SEP = "\n-- >>>*<<< --\n";
my $ON_ERROR = 0;
GetOptions(
'help|h|?' => \$help,
'man' => \$man,
'terminators|T!' => \$keep_terminators,
'extra-spaces|spaces|S!' => \$keep_extra_spaces,
'comments|C!' => \$keep_comments,
'empty-statements|empty|E!' => \$keep_empty_statements,
'slash-terminates|slash!' => \$slash_terminates,
'output-statement-separator|oss|s=s' => \$OUTPUT_STATEMENT_SEP,
'output-file-separator|ofs|f=s' => \$OUTPUT_FILE_SEP,
'on-error|error|e=s' => \$ON_ERROR
) or pod2usage(2);
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage(2) if $help;
die qq[Illegal on_error value: "$ON_ERROR"]
if $ON_ERROR !~ /^(stop|continue|no-output|0|1|2)$/i;
### Main code ###
*error = $ON_ERROR =~ /^(continue|1)$/i
? sub { warn shift } : sub { die shift };
*process_file = $ON_ERROR =~ /^(no-output|2)$/i
? \&split_and_gather_statements : \&split_and_print_statements;
my $SPLITTER = SQL::SplitStatement->new(
keep_terminators => $keep_terminators,
keep_extra_spaces => $keep_extra_spaces,
keep_comments => $keep_comments,
keep_empty_statements => $keep_empty_statements,
slash_terminates => $slash_terminates
);
my $OUTPUT = '';
{
local $/, $| = 1;
if ( @ARGV ) {
while ( my $filename = shift @ARGV ) {
if ( $filename eq '-' ) {
process_file( \*STDIN )
} elsif ( open my $fh, '<', $filename ) {
process_file( $fh, scalar @ARGV )
} else {
error( qq[Can't open file "$filename": $!] )
}
}
} else {
split_and_print_statements( \*STDIN )
}
print $OUTPUT if $OUTPUT;
print "\n"
}
### Main code End ###
sub split_and_print_statements {
my ($fh, $not_last_file) = @_;
print join $OUTPUT_STATEMENT_SEP, $SPLITTER->split( <$fh> );
print $OUTPUT_FILE_SEP if $not_last_file
}
sub split_and_gather_statements {
my ($fh, $not_last_file) = @_;
$OUTPUT .= join $OUTPUT_STATEMENT_SEP, $SPLITTER->split( <$fh> );
$OUTPUT .= $OUTPUT_FILE_SEP if $not_last_file
}
__END__
=head1 NAME
sql-split - SQL splitting command line utility
=head1 VERSION
version 1.00020
=head1 SYNOPSIS
sql-split [ OPTIONS ] [ FILE(S) ]
sql-split --man
=head1 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 C<-> (dash), the SQL code
is read from STDIN, so that this program can be used as a I<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).
=head1 OPTIONS
=head2 -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:
=over 4
=item * C<;> (the I<semicolon> character);
=item * any string defined by the MySQL C<DELIMITER> command;
=item * an C<;> followed by an C</> (I<forward-slash> character) on its own
line;
=item * an C<;> followed by an C<.> (I<dot> character) on its own line,
followed by an C</> on its own line;
=item * an C</> on its own line regardless of the preceding characters
(only if the C<slash_terminates> option, explained below, is set).
=back
The multi-line terminators above are always treated as a single token, that is
they are discarded (or returned) as a whole (regardless of the
C<--no-slash-terminates> option value).
=head2 -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).
=head2 -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.
=head2 -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 C<--comments> option is
used. Note instead that an empty statement is recognized as such regardless of
the use of the C<--terminators> and C<--extra-spaces> options.
=head2 --no-slash, --no-slash-terminates
By default a C</> (I<forward-slash>) on its own line, even without a preceding
semicolon, is admitted as a candidate terminator.
When this option is used instead, a forward-slash on its own line is treated as
a statement terminator only if preceded by a semicolon or by a dot and a
semicolon.
If you are dealing with Oracle's SQL, you should not use this option, since a
slash (alone, without a preceding semicolon) is often used as a terminator, as
it is permitted by SQL*Plus (on non-I<block> statements).
With SQL dialects other than Oracle, there is the (theoretical) possibility that
a slash on its own line could pass the additional checks and be considered a
terminator (while it shouldn't). This chance should be really tiny (it has never
been observed in real world code indeed). Though negligible, this option will
anyway rule out that risk.
=head2 -s, --oss, --output-statement-separator I<string>
The string which will be printed between every pair of returned atomic
statements. By default, it is a S<C<-->> (I<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 S<C<$'\n--\n'>>).
Note that the last returned statement (for each processed file) will not be
followed by such separator.
=head2 -f, --ofs, --output-file-separator I<string>
The string which will be printed between the groups of statements coming from
different files. By default it is the C<<<< -- >>>*<<< -- >>>> string on its own
line.
Similarly to the statement separator, the file separator will not be printed
after the last file.
=head2 -e, --error, --on-error I<value>
It controls the program behavior in case one of the given files is not
accessible.
It can take the following values:
=over 4
=item *
C<stop> or C<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);
=item *
C<continue> or C<1>, which causes the program, when it encounters a file
error, to just emit a warning (on STDERR) and continue with the next file;
=item *
C<no-output> or C<2>, which, just like C<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.
=back
The above listed string values are case-insensitive.
=head2 -h, -?, --help
It prints a brief help message and exits.
=head2 --man
It shows the full man page.
=head2 --version
It prints the program version and exits.
=head1 SUPPORTED DBMSs
sql-split aims to cover the widest possible range of DBMSs, SQL dialects and
extensions (even proprietary), in a (nearly) fully transparent way for the user.
Currently it has been tested mainly on SQLite, PostgreSQL, MySQL and Oracle.
=head2 Procedural Extensions
Procedural code is by far the most complex to handle.
Currently any block of code which start with C<FUNCTION>, C<PROCEDURE>,
C<DECLARE>, C<CREATE> or C<CALL> is correctly recognized, as well as
I<anonymous> C<BEGIN ... END> blocks, I<dollar quoted> blocks and blocks
delimited by a C<DELIMITER>-defined I<custom terminator>, therefore a wide range
of procedural extensions should be handled correctly. However, only PL/SQL,
PL/PgSQL and MySQL code has been tested so far.
=head1 LIMITATIONS
None currently known (other than the lack of tests on SQL dialects different
from the ones described above).
=head2 Non-limitations
To be split correctly, the given input must, in general, be syntactically valid
SQL. For example, an unbalanced C<BEGIN> or a misspelled keyword could, under
certain circumstances, confuse the parser and make it trip over the next
statement terminator, thus returning non-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!)
=head1 SEE ALSO
=over 4
=item * L<SQL::SplitStatement> (perldoc SQL::SplitStatement)
=back
=head1 COPYRIGHT
Copyright 2011 I<Emanuele Zeppieri> E<lt>emazep@cpan.orgE<gt>.
=head1 LICENSE
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See L<http://www.perl.com/perl/misc/Artistic.html>
=head1 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.
=cut
|