Removed definedness check for file-based data, for the DBI-based parsers, which will...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI.pm
CommitLineData
6a9f7bae 1package SQL::Translator::Parser::DBI;
2
3# -------------------------------------------------------------------
4# $Id: DBI.pm,v 1.1 2003-10-03 00:21:41 kycl4rk Exp $
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);
87$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
88
89use constant DRIVERS => {
90 sqlite => 'SQLite',
91};
92
93use Exporter;
94use SQL::Translator::Utils qw(debug normalize_name);
95use SQL::Translator::Parser::DBI::SQLite;
96
97use base qw(Exporter);
98@EXPORT = qw(parse);
99
100#
101# Passed a SQL::Translator instance and a string containing the data
102#
103sub parse {
104 my ( $tr, $data ) = @_;
105
106 $tr->debug( "in DBI\n" );
107 my $args = $tr->parser_args;
108 my $dbh = $args->{'dbh'};
109 my $dsn = $args->{'dsn'};
110 my $db_user = $args->{'db_user'};
111 my $db_password = $args->{'db_password'};
112
113 unless ( $dbh ) {
114 die 'No DSN' unless $dsn;
115 $dbh = DBI->connect( $dsn, $db_user, $db_password, {RaiseError=>1} );
116 }
117
118 die 'No database handle' unless defined $dbh;
119
120 my $db_type = $dbh->{'Driver'}{'Name'} or die 'Cannot determine DBI type';
121 my $driver = DRIVERS->{ lc $db_type } or die "$db_type not supported";
122 my $pkg = "SQL::Translator::Parser::DBI::$driver";
123 my $sub = $pkg.'::parse';
124
125 $tr->debug( "Calling sub '$sub'\n" );
126
127# $tr->load( $pkg );
128
129 {
130 no strict 'refs';
131 &{ $sub }( $tr, $dbh ) or die "No result from $pkg";
132 }
133
134 return 1;
135}
136
1371;
138
139# -------------------------------------------------------------------
140=pod
141
142=head1 AUTHOR
143
144Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
145
146=head1 SEE ALSO
147
148DBI.
149
150=cut