1 package SQL::Translator::Parser::DBI;
3 # -------------------------------------------------------------------
4 # $Id: DBI.pm,v 1.4 2003-10-10 15:52:07 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.
56 The following are acceptable arguments:
62 An open DBI database handle.
66 The DSN to use for connecting to a database.
70 The user name to use for connecting to a database.
74 The password to use for connecting to a database.
78 There is no need to specify which type of database you are querying as
79 this is determined automatically by inspecting $dbh->{'Driver'}{'Name'}.
80 If a parser exists for your database, it will be used automatically;
81 if not, the code will fail automatically (and you can write the parser
82 and contribute it to the project!).
84 Currently parsers exist for the following databases:
94 =item * PostgreSQL (still experimental)
98 Most of these parsers are able to query the database directly for the
99 structure rather than parsing a text file. For large schemas, this is
100 probably orders of magnitude faster than traditional parsing (which
101 uses Parse::RecDescent, an amazing module but really quite slow).
103 Though no Oracle parser currently exists, it would be fairly easy to
104 query an Oracle database directly by using DDL::Oracle to generate a
105 DDL for the schema and then using the normal Oracle parser on this.
106 Perhaps future versions of SQL::Translator will include the ability to
107 query Oracle directly and skip the parsing of a text file, too.
111 # -------------------------------------------------------------------
115 use vars qw($VERSION @EXPORT);
116 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
118 use constant DRIVERS => {
127 use SQL::Translator::Utils qw(debug);
129 use SQL::Translator::Parser::DBI::MySQL;
130 use SQL::Translator::Parser::DBI::SQLite;
131 use SQL::Translator::Parser::DBI::Sybase;
132 use SQL::Translator::Parser::DBI::PostgreSQL;
134 use base qw(Exporter);
138 # Passed a SQL::Translator instance and a string containing the data
141 my ( $tr, $data ) = @_;
143 my $args = $tr->parser_args;
144 my $dbh = $args->{'dbh'};
145 my $dsn = $args->{'dsn'};
146 my $db_user = $args->{'db_user'};
147 my $db_password = $args->{'db_password'};
150 die 'No DSN' unless $dsn;
151 $dbh = DBI->connect( $dsn, $db_user, $db_password,
153 FetchHashKeyName => 'NAME_lc',
161 die 'No database handle' unless defined $dbh;
163 my $db_type = $dbh->{'Driver'}{'Name'} or die 'Cannot determine DBI type';
164 my $driver = DRIVERS->{ lc $db_type } or die "$db_type not supported";
165 my $pkg = "SQL::Translator::Parser::DBI::$driver";
166 my $sub = $pkg.'::parse';
169 # I can't get this to work. I seem to have to have the "use"
176 &{ $sub }( $tr, $dbh ) or die "No result from $pkg";
179 $dbh->disconnect if defined $dbh;
188 # -------------------------------------------------------------------
193 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
197 DBI, SQL::Translator.