X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FParser%2FDBI.pm;h=eb061174547049e29d7da09de8867ba37f07f1c0;hb=HEAD;hp=20a62b430c816ad5c06af13873a45fc93c56d835;hpb=6a9f7bae6c936d99cc57d57f2a0fb6202860f076;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Parser/DBI.pm b/lib/SQL/Translator/Parser/DBI.pm index 20a62b4..eb06117 100644 --- a/lib/SQL/Translator/Parser/DBI.pm +++ b/lib/SQL/Translator/Parser/DBI.pm @@ -1,26 +1,5 @@ package SQL::Translator::Parser::DBI; -# ------------------------------------------------------------------- -# $Id: DBI.pm,v 1.1 2003-10-03 00:21:41 kycl4rk Exp $ -# ------------------------------------------------------------------- -# Copyright (C) 2003 Ken Y. Clark , -# darren chamberlain -# -# 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 @@ -30,38 +9,47 @@ SQL::Translator::Parser::DBI - "parser" for DBI handles use DBI; use SQL::Translator; - my $dbh = DBI->connect(...); + my $dbh = DBI->connect('dsn', 'user', 'pass', + { + RaiseError => 1, + FetchHashKeyName => 'NAME_lc', + } + ); my $translator = SQL::Translator->new( parser => 'DBI', - dbh => $dbh, + parser_args => { + dbh => $dbh, + }, ); Or: use SQL::Translator; - my $translator = SQL::Translator->new( - parser => 'DBI', - dsn => 'dbi:mysql:FOO', - db_user => 'guest', - db_password => 'password', + my $translator = SQL::Translator->new( + parser => 'DBI', + parser_args => { + dsn => 'dbi:mysql:FOO', + db_user => 'guest', + db_password => 'password', + } ); =head1 DESCRIPTION -This parser accepts an open database handle (or the arguments to create -one) and queries the database directly for the information. The correct -SQL::Translator::Parser::DBI class is determined automatically by -inspecting $dbh->{'Driver'}{'Name'}. +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: -=over +=over 4 =item * dbh -An open DBI database handle. +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 @@ -77,22 +65,58 @@ The password to use for connecting to a database. =back -=cut +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!). + +Currently parsers exist for the following databases: + +=over 4 + +=item * MySQL + +=item * SQLite -# ------------------------------------------------------------------- +=item * Sybase + +=item * PostgreSQL (still experimental) + +=back + +Most of these parsers are able to query the database directly for the +structure rather than parsing a text file. For large schemas, this is +probably orders of magnitude faster than traditional parsing (which +uses Parse::RecDescent, an amazing module but really quite slow). + +Though no Oracle parser currently exists, it would be fairly easy to +query an Oracle database directly by using DDL::Oracle to generate a +DDL for the schema and then using the normal Oracle parser on this. +Perhaps future versions of SQL::Translator will include the ability to +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.1 $ =~ /(\d+)\.(\d+)/; +our @EXPORT; +our $VERSION = '1.62'; use constant DRIVERS => { - sqlite => 'SQLite', + mysql => 'MySQL', + odbc => 'SQLServer', + oracle => 'Oracle', + pg => 'PostgreSQL', + sqlite => 'SQLite', + sybase => 'Sybase', + db2 => 'DB2', }; use Exporter; -use SQL::Translator::Utils qw(debug normalize_name); -use SQL::Translator::Parser::DBI::SQLite; + +use SQL::Translator::Utils qw(debug); use base qw(Exporter); @EXPORT = qw(parse); @@ -103,16 +127,24 @@ use base qw(Exporter); sub parse { my ( $tr, $data ) = @_; - $tr->debug( "in DBI\n" ); my $args = $tr->parser_args; my $dbh = $args->{'dbh'}; my $dsn = $args->{'dsn'}; 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, {RaiseError=>1} ); + $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; @@ -122,21 +154,23 @@ sub parse { my $pkg = "SQL::Translator::Parser::DBI::$driver"; my $sub = $pkg.'::parse'; - $tr->debug( "Calling sub '$sub'\n" ); - -# $tr->load( $pkg ); + SQL::Translator::load( $pkg ); - { + my $s = eval { no strict 'refs'; &{ $sub }( $tr, $dbh ) or die "No result from $pkg"; - } + }; + my $err = $@; + + eval { $dbh->disconnect } if (defined $dbh and $dbh_is_local); + + die $err if $err; - return 1; + return $s; } 1; -# ------------------------------------------------------------------- =pod =head1 AUTHOR @@ -145,6 +179,6 @@ Ken Y. Clark Ekclark@cpan.orgE. =head1 SEE ALSO -DBI. +DBI, SQL::Translator. =cut