| 1 | package Fey::Loader::mysql; | ||
| 2 | { | ||
| 3 | $Fey::Loader::mysql::VERSION = '0.13'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | |||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use DBD::mysql 4.004; | ||
| 11 | |||
| 12 | use Fey::Literal; | ||
| 13 | use Scalar::Util qw( looks_like_number ); | ||
| 14 | |||
| 15 | extends 'Fey::Loader::DBI'; | ||
| 16 | |||
| 17 | package # hide from PAUSE | ||
| 18 | DBD::mysql::Fixup; | ||
| 19 | |||
| 20 | BEGIN { | ||
| 21 | use B::Deparse; | ||
| 22 | |||
| 23 | # XXX - hack of epic proportions - DBD::mysql::DB defined a totally broken | ||
| 24 | # statistics_info that attempts to call SUPER::statistics_info | ||
| 25 | # internally. The method doesn't exist in the parent class so it | ||
| 26 | # explodes. However, since this broken method is in place, we can't just | ||
| 27 | # check to see if the method exists before monkey patching. | ||
| 28 | my $meth = DBD::mysql::db->can('statistics_info'); | ||
| 29 | if ( $meth ) { | ||
| 30 | my @lines = split /[\r\n]+/, B::Deparse->new()->coderef2text($meth); | ||
| 31 | |||
| 32 | # The broken implementation is just a few lines. If this ever gets | ||
| 33 | # implemented for real it will have to be longer. | ||
| 34 | no warnings 'redefine'; | ||
| 35 | *DBD::mysql::db::statistics_info = \&_statistics_info | ||
| 36 | if @lines < 10; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _statistics_info { | ||
| 41 | my ( $dbh, $catalog, $schema, $table, $unique_only ) = @_; | ||
| 42 | |||
| 43 | if ( DBD::mysql->VERSION >= 4.018 ) { | ||
| 44 | return unless $dbh->func('_async_check'); | ||
| 45 | } | ||
| 46 | |||
| 47 | $dbh->{mysql_server_prepare} ||= 0; | ||
| 48 | my $mysql_server_prepare_save = $dbh->{mysql_server_prepare}; | ||
| 49 | |||
| 50 | my $table_id = $dbh->quote_identifier( $catalog, $schema, $table ); | ||
| 51 | |||
| 52 | my @names = qw( | ||
| 53 | TABLE_CAT TABLE_SCHEM TABLE_NAME NON_UNIQUE INDEX_QUALIFIER | ||
| 54 | INDEX_NAME TYPE ORDINAL_POSITION COLUMN_NAME COLLATION | ||
| 55 | CARDINALITY PAGES FILTER_CONDITION | ||
| 56 | ); | ||
| 57 | my %index_info; | ||
| 58 | |||
| 59 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; | ||
| 60 | my $desc_sth = $dbh->prepare("SHOW KEYS FROM $table_id"); | ||
| 61 | my $desc = $dbh->selectall_arrayref( $desc_sth, { Columns => {} } ); | ||
| 62 | my $ordinal_pos = 0; | ||
| 63 | |||
| 64 | for my $row ( grep { $_->{key_name} ne 'PRIMARY' } @$desc ) { | ||
| 65 | next if $unique_only && $row->{non_unique}; | ||
| 66 | |||
| 67 | $index_info{ $row->{key_name} } = { | ||
| 68 | TABLE_CAT => $catalog, | ||
| 69 | TABLE_SCHEM => $schema, | ||
| 70 | TABLE_NAME => $table, | ||
| 71 | NON_UNIQUE => $row->{non_unique}, | ||
| 72 | INDEX_NAME => $row->{key_name}, | ||
| 73 | TYPE => lc $row->{index_type}, | ||
| 74 | ORDINAL_POSITION => $row->{seq_in_index}, | ||
| 75 | COLUMN_NAME => $row->{column_name}, | ||
| 76 | COLLATION => $row->{collation}, | ||
| 77 | CARDINALITY => $row->{cardinality}, | ||
| 78 | mysql_nullable => ( $row->{nullable} ? 1 : 0 ), | ||
| 79 | mysql_comment => $row->{comment}, | ||
| 80 | }; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $sponge = DBI->connect( "DBI:Sponge:", '', '' ) | ||
| 84 | or ( $dbh->{mysql_server_prepare} = $mysql_server_prepare_save | ||
| 85 | && return $dbh->DBI::set_err( $DBI::err, "DBI::Sponge: $DBI::errstr" ) | ||
| 86 | ); | ||
| 87 | |||
| 88 | my $sth = $sponge->prepare( | ||
| 89 | "statistics_info $table", { | ||
| 90 | rows => [ map { [ @{$_}{@names} ] } values %index_info ], | ||
| 91 | NUM_OF_FIELDS => scalar @names, | ||
| 92 | NAME => \@names, | ||
| 93 | } | ||
| 94 | ) | ||
| 95 | or ( $dbh->{mysql_server_prepare} = $mysql_server_prepare_save | ||
| 96 | && return $dbh->DBI::set_err( $sponge->err(), $sponge->errstr() ) ); | ||
| 97 | |||
| 98 | $dbh->{mysql_server_prepare} = $mysql_server_prepare_save; | ||
| 99 | |||
| 100 | return $sth; | ||
| 101 | } | ||
| 102 | |||
| 103 | package Fey::Loader::mysql; | ||
| 104 | |||
| 105 | sub _build_dbh_name { | ||
| 106 | my $self = shift; | ||
| 107 | |||
| 108 | return $self->dbh()->selectrow_arrayref('SELECT DATABASE()')->[0]; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _column_params { | ||
| 112 | my $self = shift; | ||
| 113 | my $table = shift; | ||
| 114 | my $col_info = shift; | ||
| 115 | |||
| 116 | my %col = $self->SUPER::_column_params( $table, $col_info ); | ||
| 117 | |||
| 118 | # DBD::mysql adds the max length for some data types to the column | ||
| 119 | # info, but we only care about user-specified lengths. | ||
| 120 | # | ||
| 121 | # Unfortunately, DBD::mysql itself adds a length to some types | ||
| 122 | # (notably integer types) that isn't really useful, but it's | ||
| 123 | # impossible to distinguish between a length specified by the user | ||
| 124 | # and one specified by DBD::mysql. | ||
| 125 | delete $col{length} | ||
| 126 | if ( | ||
| 127 | $col{type} =~ /(?:text|blob)$/i | ||
| 128 | || $col{type} =~ /^(?:float|double)/i | ||
| 129 | || $col{type} =~ /^(?:enum|set)/i | ||
| 130 | || ( $col{type} =~ /^(?:date|time)/i | ||
| 131 | && lc $col{type} ne 'timestamp' ) | ||
| 132 | ); | ||
| 133 | |||
| 134 | delete $col{precision} | ||
| 135 | if $col{type} =~ /date|time/o; | ||
| 136 | |||
| 137 | delete $col{default} | ||
| 138 | if ( exists $col{default} | ||
| 139 | && $col_info->{COLUMN_DEF} eq '' | ||
| 140 | && $col_info->{TYPE_NAME} =~ /int|float|double/i ); | ||
| 141 | |||
| 142 | return %col; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _is_auto_increment { | ||
| 146 | my $self = shift; | ||
| 147 | my $table = shift; | ||
| 148 | my $col_info = shift; | ||
| 149 | |||
| 150 | return $col_info->{mysql_is_auto_increment} ? 1 : 0; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub _default { | ||
| 154 | my $self = shift; | ||
| 155 | my $default = shift; | ||
| 156 | my $col_info = shift; | ||
| 157 | |||
| 158 | if ( $default =~ /^NULL$/i ) { | ||
| 159 | return Fey::Literal::Null->new(); | ||
| 160 | } | ||
| 161 | elsif ( $default =~ /^CURRENT_TIMESTAMP$/i ) { | ||
| 162 | return Fey::Literal::Term->new($default); | ||
| 163 | } | ||
| 164 | elsif ( looks_like_number($default) ) { | ||
| 165 | return Fey::Literal::Number->new($default); | ||
| 166 | } | ||
| 167 | else { | ||
| 168 | return Fey::Literal::String->new($default); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | { | ||
| 173 | my $quoted_name = qr/["`](.*?)["`]/; | ||
| 174 | my $fk = qr/CONSTRAINT $quoted_name FOREIGN KEY \($quoted_name\) REFERENCES $quoted_name \($quoted_name\)/; | ||
| 175 | |||
| 176 | sub _fk_info_sth { | ||
| 177 | my $self = shift; | ||
| 178 | my $name = shift; | ||
| 179 | |||
| 180 | my ( $pk_tbl, $ddl ) = eval { | ||
| 181 | $self->dbh()->selectrow_array("SHOW CREATE TABLE `$name`"); | ||
| 182 | }; | ||
| 183 | |||
| 184 | return unless defined $ddl; | ||
| 185 | |||
| 186 | my @fk_info; | ||
| 187 | |||
| 188 | while ( $ddl =~ /$fk/g ) { | ||
| 189 | my $fk_name = $1; | ||
| 190 | my $pk_cols = $2; | ||
| 191 | my $fk_tbl = $3; | ||
| 192 | my $fk_cols = $4; | ||
| 193 | |||
| 194 | my @pk_col = split /\s*,\s*/, $pk_cols; | ||
| 195 | my @fk_col = split /\s*,\s*/, $fk_cols; | ||
| 196 | |||
| 197 | push @fk_info, | ||
| 198 | [ $fk_tbl, $fk_col[$_], $pk_tbl, $pk_col[$_], $fk_name ] | ||
| 199 | for 0 .. $#pk_col; | ||
| 200 | } | ||
| 201 | |||
| 202 | return unless @fk_info; | ||
| 203 | |||
| 204 | return DBI->connect( 'dbi:Sponge:', '', '', { RaiseError => 1 } ) | ||
| 205 | ->prepare( | ||
| 206 | "foreign_key_info $name", { | ||
| 207 | NAME => [ | ||
| 208 | qw( PKTABLE_NAME PKCOLUMN_NAME FKTABLE_NAME FKCOLUMN_NAME FK_NAME ) | ||
| 209 | ], | ||
| 210 | rows => \@fk_info, | ||
| 211 | } | ||
| 212 | ); | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | __PACKAGE__->meta()->make_immutable(); | ||
| 217 | |||
| 218 | 1; | ||
| 219 | |||
| 220 | =head1 SYNOPSIS | ||
| 221 | |||
| 222 | my $loader = Fey::Loader->new( dbh => $dbh ); | ||
| 223 | |||
| 224 | my $schema = $loader->make_schema( name => $name ); | ||
| 225 | |||
| 226 | =head1 DESCRIPTION | ||
| 227 | |||
| 228 | C<Fey::Loader::mysql> implements some MySQL-specific loader | ||
| 229 | behavior. | ||
| 230 | |||
| 231 | =head1 METHODS | ||
| 232 | |||
| 233 | This class provides the same public methods as L<Fey::Loader::DBI>. | ||
| 234 | |||
| 235 | =head1 BUGS | ||
| 236 | |||
| 237 | Please report any bugs or feature requests to | ||
| 238 | C<bug-fey-loader@rt.cpan.org>, or through the web interface at | ||
| 239 | L<http://rt.cpan.org>. I will be notified, and then you'll | ||
| 240 | automatically be notified of progress on your bug as I make changes. | ||
| 241 | |||
| 242 | =cut |