1 package SQL::Translator::Parser::DBI;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
23 SQL::Translator::Parser::DBI - "parser" for DBI handles
30 my $dbh = DBI->connect('dsn', 'user', 'pass',
33 FetchHashKeyName => 'NAME_lc',
37 my $translator = SQL::Translator->new(
46 my $translator = SQL::Translator->new(
49 dsn => 'dbi:mysql:FOO',
51 db_password => 'password',
57 This parser accepts an open database handle (or the arguments to create
58 one) and queries the database directly for the information.
60 The following are acceptable arguments:
66 An open DBI database handle. NB: Be sure to create the database with the
67 "FetchHashKeyName => 'NAME_lc'" option as all the DBI parsers expect
68 lowercased column names.
72 The DSN to use for connecting to a database.
76 The user name to use for connecting to a database.
80 The password to use for connecting to a database.
84 There is no need to specify which type of database you are querying as
85 this is determined automatically by inspecting $dbh->{'Driver'}{'Name'}.
86 If a parser exists for your database, it will be used automatically;
87 if not, the code will fail automatically (and you can write the parser
88 and contribute it to the project!).
90 Currently parsers exist for the following databases:
100 =item * PostgreSQL (still experimental)
104 Most of these parsers are able to query the database directly for the
105 structure rather than parsing a text file. For large schemas, this is
106 probably orders of magnitude faster than traditional parsing (which
107 uses Parse::RecDescent, an amazing module but really quite slow).
109 Though no Oracle parser currently exists, it would be fairly easy to
110 query an Oracle database directly by using DDL::Oracle to generate a
111 DDL for the schema and then using the normal Oracle parser on this.
112 Perhaps future versions of SQL::Translator will include the ability to
113 query Oracle directly and skip the parsing of a text file, too.
117 # -------------------------------------------------------------------
121 use vars qw($VERSION @EXPORT);
124 use constant DRIVERS => {
137 use SQL::Translator::Utils qw(debug);
139 use base qw(Exporter);
143 # Passed a SQL::Translator instance and a string containing the data
146 my ( $tr, $data ) = @_;
148 my $args = $tr->parser_args;
149 my $dbh = $args->{'dbh'};
150 my $dsn = $args->{'dsn'};
151 my $db_user = $args->{'db_user'};
152 my $db_password = $args->{'db_password'};
155 die 'No DSN' unless $dsn;
156 $dbh = DBI->connect( $dsn, $db_user, $db_password,
158 FetchHashKeyName => 'NAME_lc',
166 die 'No database handle' unless defined $dbh;
168 my $db_type = $dbh->{'Driver'}{'Name'} or die 'Cannot determine DBI type';
169 my $driver = DRIVERS->{ lc $db_type } or die "$db_type not supported";
170 my $pkg = "SQL::Translator::Parser::DBI::$driver";
171 my $sub = $pkg.'::parse';
173 SQL::Translator::load( $pkg );
177 &{ $sub }( $tr, $dbh ) or die "No result from $pkg";
180 $dbh->disconnect if defined $dbh;
189 # -------------------------------------------------------------------
194 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
198 DBI, SQL::Translator.