From: <pau...@us...> - 2007-03-18 13:30:31
|
Revision: 938 http://svn.sourceforge.net/everydevel/?rev=938&view=rev Author: paul_the_nomad Date: 2007-03-16 17:26:21 -0700 (Fri, 16 Mar 2007) Log Message: ----------- Add parse_sql_file and tests. A utility method for Nodeball.pm which will go as soon as Nodeballs have a different format. Modified Paths: -------------- trunk/ebase/lib/Everything/DB.pm trunk/ebase/lib/Everything/Test/DB.pm Property Changed: ---------------- trunk/ebase/ Property changes on: trunk/ebase ___________________________________________________________________ Name: svk:merge - 16c2b9cb-492b-4d64-9535-64d4e875048d:/wip/ebase:944 a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17930 + 16c2b9cb-492b-4d64-9535-64d4e875048d:/wip/ebase:945 a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17930 Modified: trunk/ebase/lib/Everything/DB.pm =================================================================== --- trunk/ebase/lib/Everything/DB.pm 2007-03-17 00:25:59 UTC (rev 937) +++ trunk/ebase/lib/Everything/DB.pm 2007-03-17 00:26:21 UTC (rev 938) @@ -1092,4 +1092,39 @@ # override this to fix odd column names sub fix_node_keys {} + +=head2 C<parse_sql_file> + +This is a utility method for Nodeball.pm. It takes an open filehandle +that should be open on a file of some raw sql perhaps dumped from a +database. The method, strips out comments and blank lines and splits the string +into seperate sql statements that can then be passed individually to +DBI.pm. + +=over 4 + +=item * $fh + +Takes an open filehandle. + +=back + +Returns a list of strings, that are SQL statements. + +=cut + +sub parse_sql_file { + my ($self, $fh) = @_; + my $sql = ''; + foreach (<$fh>) { + next if /^\//; + next if /^\s*--/; + next if /^\s*$/; + $sql .= "$_"; + } + my @statements = split /;\s*/, $sql; + return @statements; + +} + 1; Modified: trunk/ebase/lib/Everything/Test/DB.pm =================================================================== --- trunk/ebase/lib/Everything/Test/DB.pm 2007-03-17 00:25:59 UTC (rev 937) +++ trunk/ebase/lib/Everything/Test/DB.pm 2007-03-17 00:26:21 UTC (rev 938) @@ -808,4 +808,74 @@ ['node'], '... but adding node if addNode flag is true' ); } +sub test_parse_sql_file :Test(2){ + my $self = shift; + my $instance = $self->{instance}; + use File::Temp qw/tempfile/; + my $fh = tempfile(UNLINK => 1); + + my $sql = <<SQL; +BEGIN TRANSACTION; + + +-- +-- Table: mail +-- +DROP TABLE IF EXISTS mail; +CREATE TABLE mail ( +-- Comments: +-- Created by SQL::Translator::Producer::PostgreSQL +-- Created on Mon Jun 14 12:06:35 2004 +-- Table: mail +-- + + mail_id INTEGER PRIMARY KEY NOT NULL DEFAULT '0', + from_address char(80) NOT NULL DEFAULT '' +); + + +-- +-- Table: image +-- +DROP TABLE IF EXISTS image; +CREATE TABLE image ( +-- Comments: +-- Created by SQL::Translator::Producer::PostgreSQL +-- Created on Mon Jun 14 12:06:09 2004 +-- Table: image +-- + + image_id INTEGER PRIMARY KEY NOT NULL, + src varchar(255), + alt varchar(255), + thumbsrc varchar(255), + description text +); + +SQL + +my @expected= ('BEGIN TRANSACTION', +'DROP TABLE IF EXISTS mail', +q{CREATE TABLE mail ( + mail_id INTEGER PRIMARY KEY NOT NULL DEFAULT '0', + from_address char(80) NOT NULL DEFAULT '' +)}, +'DROP TABLE IF EXISTS image', +'CREATE TABLE image ( + image_id INTEGER PRIMARY KEY NOT NULL, + src varchar(255), + alt varchar(255), + thumbsrc varchar(255), + description text +)'); + + +print $fh $sql; +$fh->seek(0,0); +ok(my @rv = $instance->parse_sql_file($fh), '...should parse OK.'); +is_deeply(\@rv, \@expected, '...splits the sql into manageable portions.'); + + +} + 1; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |