Fix PK info if available.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI.pm
CommitLineData
6a9f7bae 1package SQL::Translator::Parser::DBI;
2
3# -------------------------------------------------------------------
525905b9 4# $Id: DBI.pm,v 1.3 2003-10-03 20:58:18 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);
525905b9 87$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/;
6a9f7bae 88
89use constant DRIVERS => {
9d53a6a2 90 mysql => 'MySQL',
6a9f7bae 91 sqlite => 'SQLite',
525905b9 92 sybase => 'Sybase',
6a9f7bae 93};
94
95use Exporter;
96use SQL::Translator::Utils qw(debug normalize_name);
9d53a6a2 97use SQL::Translator::Parser::DBI::MySQL;
6a9f7bae 98use SQL::Translator::Parser::DBI::SQLite;
525905b9 99use SQL::Translator::Parser::DBI::Sybase;
6a9f7bae 100
101use base qw(Exporter);
102@EXPORT = qw(parse);
103
104#
105# Passed a SQL::Translator instance and a string containing the data
106#
107sub parse {
108 my ( $tr, $data ) = @_;
109
6a9f7bae 110 my $args = $tr->parser_args;
111 my $dbh = $args->{'dbh'};
112 my $dsn = $args->{'dsn'};
113 my $db_user = $args->{'db_user'};
114 my $db_password = $args->{'db_password'};
115
116 unless ( $dbh ) {
117 die 'No DSN' unless $dsn;
9d53a6a2 118 $dbh = DBI->connect( $dsn, $db_user, $db_password,
119 {
120 FetchHashKeyName => 'NAME_lc',
121 LongReadLen => 3000,
122 LongTruncOk => 1,
123 RaiseError => 1,
124 }
125 );
6a9f7bae 126 }
127
128 die 'No database handle' unless defined $dbh;
129
130 my $db_type = $dbh->{'Driver'}{'Name'} or die 'Cannot determine DBI type';
131 my $driver = DRIVERS->{ lc $db_type } or die "$db_type not supported";
132 my $pkg = "SQL::Translator::Parser::DBI::$driver";
133 my $sub = $pkg.'::parse';
134
6a9f7bae 135 {
136 no strict 'refs';
137 &{ $sub }( $tr, $dbh ) or die "No result from $pkg";
138 }
139
140 return 1;
141}
142
1431;
144
145# -------------------------------------------------------------------
146=pod
147
148=head1 AUTHOR
149
150Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
151
152=head1 SEE ALSO
153
154DBI.
155
156=cut