Downgrade global version - highest version in 9002 on cpan is 1.58 - thus go with...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI / MySQL.pm
1 package SQL::Translator::Parser::DBI::MySQL;
2
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
21 =head1 NAME
22
23 SQL::Translator::Parser::DBI::MySQL - parser for DBD::mysql
24
25 =head1 SYNOPSIS
26
27 This module will be invoked automatically by SQL::Translator::Parser::DBI,
28 so there is no need to use it directly.
29
30 =head1 DESCRIPTION
31
32 Uses SQL calls to query database directly for schema rather than parsing
33 a create file.  Should be much faster for larger schemas.
34
35 =cut
36
37 use strict;
38 use DBI;
39 use Data::Dumper;
40 use SQL::Translator::Schema::Constants;
41 use SQL::Translator::Parser::MySQL;
42
43 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
44 $VERSION = '1.59';
45 $DEBUG   = 0 unless defined $DEBUG;
46
47 # -------------------------------------------------------------------
48 sub parse {
49     my ( $tr, $dbh ) = @_;
50     my $schema       = $tr->schema;
51     my @table_names  = @{ $dbh->selectcol_arrayref('show tables') };
52     my @skip_tables = defined $tr->parser_args->{skip}?split(/,/, $tr->parser_args->{skip}):();
53
54     $dbh->{'FetchHashKeyName'} = 'NAME_lc';
55
56     my $create;
57     for my $table_name ( @table_names ) {
58         next if (grep /^$table_name$/, @skip_tables);
59         my $sth = $dbh->prepare("show create table $table_name");
60         $sth->execute;
61         my $table = $sth->fetchrow_hashref;
62         $create .= $table->{'create table'} . ";\n\n";
63     }
64
65     SQL::Translator::Parser::MySQL::parse( $tr, $create );
66
67     return 1;
68 }
69
70 1;
71
72 # -------------------------------------------------------------------
73 # Where man is not nature is barren.
74 # William Blake
75 # -------------------------------------------------------------------
76
77 =pod
78
79 =head1 AUTHOR
80
81 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
82
83 =head1 SEE ALSO
84
85 SQL::Translator.
86
87 =cut