Fixed bug that wasn't maintaining table order.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI.pm
1 package SQL::Translator::Parser::DBI;
2
3 # -------------------------------------------------------------------
4 # $Id: DBI.pm,v 1.3 2003-10-03 20:58:18 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
26 SQL::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
40 Or:
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
53 This parser accepts an open database handle (or the arguments to create 
54 one) and queries the database directly for the information.  The correct
55 SQL::Translator::Parser::DBI class is determined automatically by 
56 inspecting $dbh->{'Driver'}{'Name'}.
57
58 The following are acceptable arguments:
59
60 =over
61
62 =item * dbh
63
64 An open DBI database handle.
65
66 =item * dsn
67
68 The DSN to use for connecting to a database.
69
70 =item * db_user
71
72 The user name to use for connecting to a database.
73
74 =item * db_password
75
76 The password to use for connecting to a database.
77
78 =back
79
80 =cut
81
82 # -------------------------------------------------------------------
83
84 use strict;
85 use DBI;
86 use vars qw($VERSION @EXPORT);
87 $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/;
88
89 use constant DRIVERS => {
90     mysql  => 'MySQL',
91     sqlite => 'SQLite',
92     sybase => 'Sybase',
93 };
94
95 use Exporter;
96 use SQL::Translator::Utils qw(debug normalize_name);
97 use SQL::Translator::Parser::DBI::MySQL;
98 use SQL::Translator::Parser::DBI::SQLite;
99 use SQL::Translator::Parser::DBI::Sybase;
100
101 use base qw(Exporter);
102 @EXPORT = qw(parse);
103
104 #
105 # Passed a SQL::Translator instance and a string containing the data
106 #
107 sub parse {
108     my ( $tr, $data ) = @_;
109
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;
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         );
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
135     {
136         no strict 'refs';
137         &{ $sub }( $tr, $dbh ) or die "No result from $pkg";
138     }
139
140     return 1;
141 }
142
143 1;
144
145 # -------------------------------------------------------------------
146 =pod
147
148 =head1 AUTHOR
149
150 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
151
152 =head1 SEE ALSO
153
154 DBI.
155
156 =cut