Strip evil svn:keywords
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI.pm
CommitLineData
6a9f7bae 1package SQL::Translator::Parser::DBI;
2
3# -------------------------------------------------------------------
d4f84dd1 4# $Id: DBI.pm 1440 2009-01-17 16:31:57Z jawnsy $
6a9f7bae 5# -------------------------------------------------------------------
478f608d 6# Copyright (C) 2002-2009 SQLFairy Authors
6a9f7bae 7#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License as
10# published by the Free Software Foundation; version 2.
11#
12# This program is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15# General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=head1 NAME
24
25SQL::Translator::Parser::DBI - "parser" for DBI handles
26
27=head1 SYNOPSIS
28
29 use DBI;
30 use SQL::Translator;
31
90e373b0 32 my $dbh = DBI->connect('dsn', 'user', 'pass',
33 {
34 RaiseError => 1,
35 FetchHashKeyName => 'NAME_lc',
36 }
37 );
6a9f7bae 38
39 my $translator = SQL::Translator->new(
40 parser => 'DBI',
41 dbh => $dbh,
42 );
43
44Or:
45
46 use SQL::Translator;
47
90e373b0 48 my $translator = SQL::Translator->new(
49 parser => 'DBI',
50 parser_args => {
51 dsn => 'dbi:mysql:FOO',
52 db_user => 'guest',
53 db_password => 'password',
54 }
6a9f7bae 55 );
56
57=head1 DESCRIPTION
58
59This parser accepts an open database handle (or the arguments to create
05078e2c 60one) and queries the database directly for the information.
6a9f7bae 61
62The following are acceptable arguments:
63
05078e2c 64=over 4
6a9f7bae 65
66=item * dbh
67
90e373b0 68An open DBI database handle. NB: Be sure to create the database with the
69"FetchHashKeyName => 'NAME_lc'" option as all the DBI parsers expect
70lowercased column names.
6a9f7bae 71
72=item * dsn
73
74The DSN to use for connecting to a database.
75
76=item * db_user
77
78The user name to use for connecting to a database.
79
80=item * db_password
81
82The password to use for connecting to a database.
83
84=back
85
05078e2c 86There is no need to specify which type of database you are querying as
87this is determined automatically by inspecting $dbh->{'Driver'}{'Name'}.
88If a parser exists for your database, it will be used automatically;
89if not, the code will fail automatically (and you can write the parser
90and contribute it to the project!).
91
92Currently parsers exist for the following databases:
93
94=over 4
95
96=item * MySQL
97
98=item * SQLite
99
100=item * Sybase
101
102=item * PostgreSQL (still experimental)
103
104=back
105
106Most of these parsers are able to query the database directly for the
107structure rather than parsing a text file. For large schemas, this is
108probably orders of magnitude faster than traditional parsing (which
109uses Parse::RecDescent, an amazing module but really quite slow).
110
111Though no Oracle parser currently exists, it would be fairly easy to
112query an Oracle database directly by using DDL::Oracle to generate a
113DDL for the schema and then using the normal Oracle parser on this.
114Perhaps future versions of SQL::Translator will include the ability to
115query Oracle directly and skip the parsing of a text file, too.
116
6a9f7bae 117=cut
118
119# -------------------------------------------------------------------
120
121use strict;
122use DBI;
478f608d 123use vars qw(@EXPORT);
6a9f7bae 124
125use constant DRIVERS => {
05078e2c 126 mysql => 'MySQL',
461c1a96 127 odbc => 'SQLServer',
aaac0589 128 oracle => 'Oracle',
129 pg => 'PostgreSQL',
05078e2c 130 sqlite => 'SQLite',
131 sybase => 'Sybase',
69dad5b9 132 pg => 'PostgreSQL',
133 db2 => 'DB2',
6a9f7bae 134};
135
136use Exporter;
05078e2c 137
138use SQL::Translator::Utils qw(debug);
139
6a9f7bae 140use base qw(Exporter);
141@EXPORT = qw(parse);
142
143#
144# Passed a SQL::Translator instance and a string containing the data
145#
146sub parse {
147 my ( $tr, $data ) = @_;
148
6a9f7bae 149 my $args = $tr->parser_args;
150 my $dbh = $args->{'dbh'};
151 my $dsn = $args->{'dsn'};
152 my $db_user = $args->{'db_user'};
153 my $db_password = $args->{'db_password'};
154
155 unless ( $dbh ) {
156 die 'No DSN' unless $dsn;
9d53a6a2 157 $dbh = DBI->connect( $dsn, $db_user, $db_password,
158 {
159 FetchHashKeyName => 'NAME_lc',
160 LongReadLen => 3000,
161 LongTruncOk => 1,
162 RaiseError => 1,
163 }
164 );
6a9f7bae 165 }
166
167 die 'No database handle' unless defined $dbh;
168
169 my $db_type = $dbh->{'Driver'}{'Name'} or die 'Cannot determine DBI type';
170 my $driver = DRIVERS->{ lc $db_type } or die "$db_type not supported";
171 my $pkg = "SQL::Translator::Parser::DBI::$driver";
172 my $sub = $pkg.'::parse';
173
4c4636ed 174 $tr->load( $pkg );
05078e2c 175
176 eval {
6a9f7bae 177 no strict 'refs';
178 &{ $sub }( $tr, $dbh ) or die "No result from $pkg";
05078e2c 179 };
180
181 $dbh->disconnect if defined $dbh;
182
183 die $@ if $@;
6a9f7bae 184
185 return 1;
186}
187
1881;
189
190# -------------------------------------------------------------------
191=pod
192
193=head1 AUTHOR
194
195Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
196
197=head1 SEE ALSO
198
05078e2c 199DBI, SQL::Translator.
6a9f7bae 200
201=cut