3b4babb3f4e11e80187de20c282e7b780ce725a6
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI / MySQL.pm
1 package SQL::Translator::Parser::DBI::MySQL;
2
3 =head1 NAME
4
5 SQL::Translator::Parser::DBI::MySQL - parser for DBD::mysql
6
7 =head1 SYNOPSIS
8
9 This module will be invoked automatically by SQL::Translator::Parser::DBI,
10 so there is no need to use it directly.
11
12 =head1 DESCRIPTION
13
14 Uses SQL calls to query database directly for schema rather than parsing
15 a create file.  Should be much faster for larger schemas.
16
17 =cut
18
19 use strict;
20 use warnings;
21 use DBI;
22 use Data::Dumper;
23 use SQL::Translator::Schema::Constants;
24 use SQL::Translator::Parser::MySQL;
25
26 our ( $DEBUG, @EXPORT_OK );
27 our $VERSION = '1.61';
28 $DEBUG   = 0 unless defined $DEBUG;
29
30 sub parse {
31     my ( $tr, $dbh ) = @_;
32     my $schema       = $tr->schema;
33     my @table_names  = @{ $dbh->selectcol_arrayref('show tables') };
34     my @skip_tables  = defined $tr->parser_args->{skip}
35                        ? split(/,/, $tr->parser_args->{skip})
36                        : ();
37
38     $dbh->{'FetchHashKeyName'} = 'NAME_lc';
39
40     my $create = q{};
41     for my $table_name ( @table_names ) {
42         next if (grep /^$table_name$/, @skip_tables);
43         my $sth = $dbh->prepare("show create table " . $dbh->quote_identifier($table_name));
44         $sth->execute;
45         my $table = $sth->fetchrow_hashref;
46         $create .= ($table->{'create table'} || $table->{'create view'}) . ";\n\n";
47     }
48
49     SQL::Translator::Parser::MySQL::parse( $tr, $create );
50
51     return 1;
52 }
53
54 1;
55
56 # -------------------------------------------------------------------
57 # Where man is not nature is barren.
58 # William Blake
59 # -------------------------------------------------------------------
60
61 =pod
62
63 =head1 AUTHOR
64
65 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
66
67 =head1 SEE ALSO
68
69 SQL::Translator.
70
71 =cut