1 package SQL::Translator::Parser::DBI;
3 # -------------------------------------------------------------------
4 # $Id: DBI.pm,v 1.2 2003-10-03 20:39:53 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 # darren chamberlain <darren@cpan.org>
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; version 2.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # -------------------------------------------------------------------
26 SQL::Translator::Parser::DBI - "parser" for DBI handles
33 my $dbh = DBI->connect(...);
35 my $translator = SQL::Translator->new(
44 my $translator = SQL::Translator->new(
46 dsn => 'dbi:mysql:FOO',
48 db_password => 'password',
53 This parser accepts an open database handle (or the arguments to create
54 one) and queries the database directly for the information. The correct
55 SQL::Translator::Parser::DBI class is determined automatically by
56 inspecting $dbh->{'Driver'}{'Name'}.
58 The following are acceptable arguments:
64 An open DBI database handle.
68 The DSN to use for connecting to a database.
72 The user name to use for connecting to a database.
76 The password to use for connecting to a database.
82 # -------------------------------------------------------------------
86 use vars qw($VERSION @EXPORT);
87 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
89 use constant DRIVERS => {
95 use SQL::Translator::Utils qw(debug normalize_name);
96 use SQL::Translator::Parser::DBI::MySQL;
97 use SQL::Translator::Parser::DBI::SQLite;
99 use base qw(Exporter);
103 # Passed a SQL::Translator instance and a string containing the data
106 my ( $tr, $data ) = @_;
108 my $args = $tr->parser_args;
109 my $dbh = $args->{'dbh'};
110 my $dsn = $args->{'dsn'};
111 my $db_user = $args->{'db_user'};
112 my $db_password = $args->{'db_password'};
115 die 'No DSN' unless $dsn;
116 $dbh = DBI->connect( $dsn, $db_user, $db_password,
118 FetchHashKeyName => 'NAME_lc',
126 die 'No database handle' unless defined $dbh;
128 my $db_type = $dbh->{'Driver'}{'Name'} or die 'Cannot determine DBI type';
129 my $driver = DRIVERS->{ lc $db_type } or die "$db_type not supported";
130 my $pkg = "SQL::Translator::Parser::DBI::$driver";
131 my $sub = $pkg.'::parse';
135 &{ $sub }( $tr, $dbh ) or die "No result from $pkg";
143 # -------------------------------------------------------------------
148 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.