Fixed syntax error.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI.pm
CommitLineData
6a9f7bae 1package SQL::Translator::Parser::DBI;
2
3# -------------------------------------------------------------------
9d53a6a2 4# $Id: DBI.pm,v 1.2 2003-10-03 20:39:53 kycl4rk Exp $
6a9f7bae 5# -------------------------------------------------------------------
6# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7# darren chamberlain <darren@cpan.org>
8#
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.
12#
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.
17#
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
21# 02111-1307 USA
22# -------------------------------------------------------------------
23
24=head1 NAME
25
26SQL::Translator::Parser::DBI - "parser" for DBI handles
27
28=head1 SYNOPSIS
29
30 use DBI;
31 use SQL::Translator;
32
33 my $dbh = DBI->connect(...);
34
35 my $translator = SQL::Translator->new(
36 parser => 'DBI',
37 dbh => $dbh,
38 );
39
40Or:
41
42 use SQL::Translator;
43
44 my $translator = SQL::Translator->new(
45 parser => 'DBI',
46 dsn => 'dbi:mysql:FOO',
47 db_user => 'guest',
48 db_password => 'password',
49 );
50
51=head1 DESCRIPTION
52
53This parser accepts an open database handle (or the arguments to create
54one) and queries the database directly for the information. The correct
55SQL::Translator::Parser::DBI class is determined automatically by
56inspecting $dbh->{'Driver'}{'Name'}.
57
58The following are acceptable arguments:
59
60=over
61
62=item * dbh
63
64An open DBI database handle.
65
66=item * dsn
67
68The DSN to use for connecting to a database.
69
70=item * db_user
71
72The user name to use for connecting to a database.
73
74=item * db_password
75
76The password to use for connecting to a database.
77
78=back
79
80=cut
81
82# -------------------------------------------------------------------
83
84use strict;
85use DBI;
86use vars qw($VERSION @EXPORT);
9d53a6a2 87$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
6a9f7bae 88
89use constant DRIVERS => {
9d53a6a2 90 mysql => 'MySQL',
6a9f7bae 91 sqlite => 'SQLite',
92};
93
94use Exporter;
95use SQL::Translator::Utils qw(debug normalize_name);
9d53a6a2 96use SQL::Translator::Parser::DBI::MySQL;
6a9f7bae 97use SQL::Translator::Parser::DBI::SQLite;
98
99use base qw(Exporter);
100@EXPORT = qw(parse);
101
102#
103# Passed a SQL::Translator instance and a string containing the data
104#
105sub parse {
106 my ( $tr, $data ) = @_;
107
6a9f7bae 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'};
113
114 unless ( $dbh ) {
115 die 'No DSN' unless $dsn;
9d53a6a2 116 $dbh = DBI->connect( $dsn, $db_user, $db_password,
117 {
118 FetchHashKeyName => 'NAME_lc',
119 LongReadLen => 3000,
120 LongTruncOk => 1,
121 RaiseError => 1,
122 }
123 );
6a9f7bae 124 }
125
126 die 'No database handle' unless defined $dbh;
127
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';
132
6a9f7bae 133 {
134 no strict 'refs';
135 &{ $sub }( $tr, $dbh ) or die "No result from $pkg";
136 }
137
138 return 1;
139}
140
1411;
142
143# -------------------------------------------------------------------
144=pod
145
146=head1 AUTHOR
147
148Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
149
150=head1 SEE ALSO
151
152DBI.
153
154=cut