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