From: Yanick Champoux Date: Tue, 29 Nov 2011 20:10:11 +0000 (-0500) Subject: refactor the parsing of SQL files X-Git-Tag: v0.001006~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class-DeploymentHandler.git;a=commitdiff_plain;h=115c68ce0fc0e56265d201a8f7bbcf912efa86db refactor the parsing of SQL files The same logic was in two different places. Agglomerated them into a single function. --- diff --git a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm index 167e455..5fb6918 100644 --- a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm +++ b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm @@ -89,9 +89,9 @@ has schema_version => ( # this will probably never get called as the DBICDH # will be passing down a schema_version normally, which # is built the same way, but we leave this in place -sub _build_schema_version { +sub _build_schema_version { my $self = shift; - $self->schema->schema_version + $self->schema->schema_version } sub __ddl_consume_with_prefix { @@ -243,13 +243,7 @@ sub _run_sql_array { my ($self, $sql) = @_; my $storage = $self->storage; - $sql = [grep { - $_ && # remove blank lines - !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's - } map { - s/^\s+//; s/\s+$//; # trim whitespace - join '', grep { !/^--/ } split /\n/ # remove comments - } @$sql]; + $sql = [ _split_sql_chunk( @$sql ) ]; Dlog_trace { "Running SQL $_" } $sql; foreach my $line (@{$sql}) { @@ -266,6 +260,30 @@ sub _run_sql_array { return join "\n", @$sql } +# split a chunk o' SQL into statements +sub _split_sql_chunk { + my @sql = map { split /;\n/, $_ } @_; + + for ( @sql ) { + # strip transactions + s/^(?:BEGIN|BEGIN TRANSACTION|COMMIT).*//mgi; + + # trim whitespaces + s/^\s+|\s+$//mg; + + # remove comments + s/^--.*//gm; + + # remove blank lines + s/^\n//mg; + + # put on single line + s/\n/ /g; + } + + return @sql; +} + sub _run_sql { my ($self, $filename) = @_; log_debug { "Running SQL from $filename" }; @@ -613,19 +631,10 @@ sub _read_sql_file { my ($self, $file) = @_; return unless $file; + local $/ = undef; #sluuuuuurp + open my $fh, '<', $file; - my @data = split /;\n/, join '', <$fh>; - close $fh; - - @data = grep { - $_ && # remove blank lines - !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's - } map { - s/^\s+//; s/\s+$//; # trim whitespace - join '', grep { !/^--/ } split /\n/ # remove comments - } @data; - - return \@data; + return [ _split_sql_chunk( <$fh> ) ]; } sub downgrade_single_step { diff --git a/t/10-split-sql-chunk.t b/t/10-split-sql-chunk.t new file mode 100644 index 0000000..025e9fc --- /dev/null +++ b/t/10-split-sql-chunk.t @@ -0,0 +1,17 @@ +use strict; +use warnings; + +use Test::More tests => 1; + +use DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator; + +*split_sql_chunk = +*DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator::_split_sql_chunk; + +is_deeply [ split_sql_chunk( <<'END' ) ], [ 'SELECT * FROM YADAH END' ]; +BEGIN + -- stuff + SELECT * FROM YADAH +END; +END +