Improve trigger 'scope' attribute support (RT#119997)
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI.pm
index 05c95df..73a73bc 100644 (file)
@@ -18,7 +18,9 @@ SQL::Translator::Parser::DBI - "parser" for DBI handles
 
   my $translator  =  SQL::Translator->new(
       parser      => 'DBI',
-      dbh         => $dbh,
+      parser_args => {
+          dbh => $dbh,
+      },
   );
 
 Or:
@@ -99,8 +101,8 @@ query Oracle directly and skip the parsing of a text file, too.
 use strict;
 use warnings;
 use DBI;
-use vars qw($VERSION @EXPORT);
-$VERSION = '1.59';
+our @EXPORT;
+our $VERSION = '1.59';
 
 use constant DRIVERS => {
     mysql            => 'MySQL',
@@ -109,7 +111,6 @@ use constant DRIVERS => {
     pg               => 'PostgreSQL',
     sqlite           => 'SQLite',
     sybase           => 'Sybase',
-    pg               => 'PostgreSQL',
     db2              => 'DB2',
 };
 
@@ -132,6 +133,7 @@ sub parse {
     my $db_user       = $args->{'db_user'};
     my $db_password   = $args->{'db_password'};
 
+    my $dbh_is_local;
     unless ( $dbh ) {
         die 'No DSN' unless $dsn;
         $dbh = DBI->connect( $dsn, $db_user, $db_password,
@@ -142,6 +144,7 @@ sub parse {
                 RaiseError       => 1,
             }
         );
+        $dbh_is_local = 1;
     }
 
     die 'No database handle' unless defined $dbh;
@@ -153,16 +156,17 @@ sub parse {
 
     SQL::Translator::load( $pkg );
 
-    eval {
+    my $s = eval {
         no strict 'refs';
         &{ $sub }( $tr, $dbh ) or die "No result from $pkg";
     };
+    my $err = $@;
 
-    $dbh->disconnect if defined $dbh;
+    eval { $dbh->disconnect } if (defined $dbh and $dbh_is_local);
 
-    die $@ if $@;
+    die $err if $err;
 
-    return 1;
+    return $s;
 }
 
 1;