X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FParser%2FDBI.pm;h=73a73bceff74b8e437fe863d9e12131bde041fff;hb=c0ec0e22d3f0e3852c00daac5ef5763010b410c3;hp=11fe7a10bd37a4e2533c36b88f6f285b44befdb7;hpb=aaac05895e1fb7986ceaaaecb0649a40cb97268f;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Parser/DBI.pm b/lib/SQL/Translator/Parser/DBI.pm index 11fe7a1..73a73bc 100644 --- a/lib/SQL/Translator/Parser/DBI.pm +++ b/lib/SQL/Translator/Parser/DBI.pm @@ -1,25 +1,5 @@ package SQL::Translator::Parser::DBI; -# ------------------------------------------------------------------- -# $Id: DBI.pm,v 1.8 2006-03-09 19:25:47 cahille Exp $ -# ------------------------------------------------------------------- -# Copyright (C) 2002-4 SQLFairy Authors -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; version 2. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA -# ------------------------------------------------------------------- - =head1 NAME SQL::Translator::Parser::DBI - "parser" for DBI handles @@ -29,7 +9,7 @@ SQL::Translator::Parser::DBI - "parser" for DBI handles use DBI; use SQL::Translator; - my $dbh = DBI->connect('dsn', 'user', 'pass', + my $dbh = DBI->connect('dsn', 'user', 'pass', { RaiseError => 1, FetchHashKeyName => 'NAME_lc', @@ -38,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: @@ -56,8 +38,8 @@ Or: =head1 DESCRIPTION -This parser accepts an open database handle (or the arguments to create -one) and queries the database directly for the information. +This parser accepts an open database handle (or the arguments to create +one) and queries the database directly for the information. The following are acceptable arguments: @@ -65,8 +47,8 @@ The following are acceptable arguments: =item * dbh -An open DBI database handle. NB: Be sure to create the database with the -"FetchHashKeyName => 'NAME_lc'" option as all the DBI parsers expect +An open DBI database handle. NB: Be sure to create the database with the +"FetchHashKeyName => 'NAME_lc'" option as all the DBI parsers expect lowercased column names. =item * dsn @@ -87,7 +69,7 @@ There is no need to specify which type of database you are querying as this is determined automatically by inspecting $dbh->{'Driver'}{'Name'}. If a parser exists for your database, it will be used automatically; if not, the code will fail automatically (and you can write the parser -and contribute it to the project!). +and contribute it to the project!). Currently parsers exist for the following databases: @@ -116,12 +98,11 @@ query Oracle directly and skip the parsing of a text file, too. =cut -# ------------------------------------------------------------------- - use strict; +use warnings; use DBI; -use vars qw($VERSION @EXPORT); -$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/; +our @EXPORT; +our $VERSION = '1.59'; use constant DRIVERS => { mysql => 'MySQL', @@ -130,19 +111,13 @@ use constant DRIVERS => { pg => 'PostgreSQL', sqlite => 'SQLite', sybase => 'Sybase', + db2 => 'DB2', }; use Exporter; use SQL::Translator::Utils qw(debug); -use SQL::Translator::Parser::DBI::MySQL; -use SQL::Translator::Parser::DBI::SQLServer; -use SQL::Translator::Parser::DBI::SQLite; -use SQL::Translator::Parser::DBI::Sybase; -use SQL::Translator::Parser::DBI::PostgreSQL; -use SQL::Translator::Parser::DBI::Oracle; - use base qw(Exporter); @EXPORT = qw(parse); @@ -158,16 +133,18 @@ 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, + $dbh = DBI->connect( $dsn, $db_user, $db_password, { FetchHashKeyName => 'NAME_lc', LongReadLen => 3000, LongTruncOk => 1, RaiseError => 1, - } + } ); + $dbh_is_local = 1; } die 'No database handle' unless defined $dbh; @@ -177,27 +154,23 @@ sub parse { my $pkg = "SQL::Translator::Parser::DBI::$driver"; my $sub = $pkg.'::parse'; - # - # I can't get this to work. I seem to have to have the "use" - # statements above. - # -# $tr->load( $pkg ); + 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; -# ------------------------------------------------------------------- =pod =head1 AUTHOR