| 1 | package DBIx::Class::Schema::Loader::DBI::MariaDB; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base 'DBIx::Class::Schema::Loader::DBI'; | ||
| 6 | use mro 'c3'; | ||
| 7 | use Carp::Clan qw/^DBIx::Class/; | ||
| 8 | use List::Util qw/any first/; | ||
| 9 | use Try::Tiny; | ||
| 10 | use Scalar::Util 'blessed'; | ||
| 11 | use DBIx::Class::Schema::Loader::Utils qw/sigwarn_silencer/; | ||
| 12 | use namespace::clean; | ||
| 13 | use DBIx::Class::Schema::Loader::Table (); | ||
| 14 | |||
| 15 | our $VERSION = '1.0.1'; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | DBIx::Class::Schema::Loader::DBI::MariaDB - DBIx::Class::Schema::Loader::DBI MariaDB Implementation. | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | MariaDB support for L<DBIx::Class::Schema::Loader>. Be sure to connect to your | ||
| 24 | database using L<DBD::MariaDB> (not L<DBD::mysql>). | ||
| 25 | |||
| 26 | Forked from L<DBIx::Class::Schema::Loader::DBI::mysql> to better support | ||
| 27 | MariaDB. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | sub _setup { | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | $self->schema->storage->sql_maker->quote_char("`"); | ||
| 35 | $self->schema->storage->sql_maker->name_sep("."); | ||
| 36 | |||
| 37 | $self->next::method(@_); | ||
| 38 | |||
| 39 | if (not defined $self->preserve_case) { | ||
| 40 | $self->preserve_case(0); | ||
| 41 | } | ||
| 42 | |||
| 43 | if ($self->db_schema && $self->db_schema->[0] eq '%') { | ||
| 44 | my @schemas = try { | ||
| 45 | $self->_show_databases; | ||
| 46 | } | ||
| 47 | catch { | ||
| 48 | croak "no SHOW DATABASES privileges: $_"; | ||
| 49 | }; | ||
| 50 | |||
| 51 | @schemas = grep { | ||
| 52 | my $schema = $_; | ||
| 53 | not any { lc($schema) eq lc($_) } $self->_system_schemas | ||
| 54 | } @schemas; | ||
| 55 | |||
| 56 | $self->db_schema(\@schemas); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _show_databases { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | return map $_->[0], @{ $self->dbh->selectall_arrayref('SHOW DATABASES') }; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _system_schemas { | ||
| 67 | my $self = shift; | ||
| 68 | |||
| 69 | # Schema still called "mysql", https://mariadb.com/kb/en/the-mysql-database-tables/ | ||
| 70 | return ($self->next::method(@_), 'mysql'); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _table_fk_info { | ||
| 74 | my ($self, $table) = @_; | ||
| 75 | |||
| 76 | my $table_def_ref = eval { $self->dbh->selectrow_arrayref("SHOW CREATE TABLE ".$table->sql_name) }; | ||
| 77 | my $table_def = $table_def_ref->[1]; | ||
| 78 | |||
| 79 | return [] if not $table_def; | ||
| 80 | |||
| 81 | my $qt = qr/["`]/; | ||
| 82 | my $nqt = qr/[^"`]/; | ||
| 83 | |||
| 84 | my (@reldata) = ($table_def =~ | ||
| 85 | /CONSTRAINT ${qt}${nqt}+${qt} FOREIGN KEY \($qt(.*)$qt\) REFERENCES (?:$qt($nqt+)$qt\.)?$qt($nqt+)$qt \($qt(.+)$qt\)\s*(.*)/ig | ||
| 86 | ); | ||
| 87 | |||
| 88 | my @rels; | ||
| 89 | while (scalar @reldata > 0) { | ||
| 90 | my ($cols, $f_schema, $f_table, $f_cols, $rest) = splice @reldata, 0, 5; | ||
| 91 | |||
| 92 | my @cols = map { s/$qt//g; $self->_lc($_) } | ||
| 93 | split(/$qt?\s*$qt?,$qt?\s*$qt?/, $cols); | ||
| 94 | |||
| 95 | my @f_cols = map { s/$qt//g; $self->_lc($_) } | ||
| 96 | split(/$qt?\s*$qt?,$qt?\s*$qt?/, $f_cols); | ||
| 97 | |||
| 98 | # Match case of remote schema to that in SHOW DATABASES, if it's there | ||
| 99 | # and we have permissions to run SHOW DATABASES. | ||
| 100 | if ($f_schema) { | ||
| 101 | my $matched = first { | ||
| 102 | lc($_) eq lc($f_schema) | ||
| 103 | } try { $self->_show_databases }; | ||
| 104 | |||
| 105 | $f_schema = $matched if $matched; | ||
| 106 | } | ||
| 107 | |||
| 108 | my $remote_table = do { | ||
| 109 | # Get ->tables_list to return tables from the remote schema, in case it is not in the db_schema list. | ||
| 110 | local $self->{db_schema} = [ $f_schema ] if $f_schema; | ||
| 111 | |||
| 112 | first { | ||
| 113 | lc($_->name) eq lc($f_table) | ||
| 114 | && ((not $f_schema) || lc($_->schema) eq lc($f_schema)) | ||
| 115 | } $self->_tables_list; | ||
| 116 | }; | ||
| 117 | |||
| 118 | # The table may not be in any database, or it may not have been found by the previous code block for whatever reason. | ||
| 119 | if (not $remote_table) { | ||
| 120 | my $remote_schema = $f_schema || $self->db_schema && @{ $self->db_schema } == 1 && $self->db_schema->[0]; | ||
| 121 | |||
| 122 | $remote_table = DBIx::Class::Schema::Loader::Table->new( | ||
| 123 | loader => $self, | ||
| 124 | name => $f_table, | ||
| 125 | ($remote_schema ? ( | ||
| 126 | schema => $remote_schema, | ||
| 127 | ) : ()), | ||
| 128 | ); | ||
| 129 | } | ||
| 130 | |||
| 131 | my %attrs; | ||
| 132 | |||
| 133 | if ($rest) { | ||
| 134 | my @on_clauses = $rest =~ /(ON DELETE|ON UPDATE) (RESTRICT|CASCADE|SET NULL|NO ACTION) ?/ig; | ||
| 135 | |||
| 136 | while (my ($clause, $value) = splice @on_clauses, 0, 2) { | ||
| 137 | $clause = lc $clause; | ||
| 138 | $clause =~ s/ /_/; | ||
| 139 | |||
| 140 | $value = uc $value; | ||
| 141 | |||
| 142 | $attrs{$clause} = $value; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | # The default behavior is RESTRICT. Specifying RESTRICT explicitly just removes | ||
| 147 | # that ON clause from the SHOW CREATE TABLE output. For this reason, even | ||
| 148 | # though the default for these clauses everywhere else in Schema::Loader is | ||
| 149 | # CASCADE, we change the default here to RESTRICT in order to reproduce the | ||
| 150 | # schema faithfully. | ||
| 151 | $attrs{on_delete} ||= 'RESTRICT'; | ||
| 152 | $attrs{on_update} ||= 'RESTRICT'; | ||
| 153 | |||
| 154 | # MySQL does not have a DEFERRABLE attribute, but there is a way to defer FKs. | ||
| 155 | $attrs{is_deferrable} = 1; | ||
| 156 | |||
| 157 | push(@rels, { | ||
| 158 | local_columns => \@cols, | ||
| 159 | remote_columns => \@f_cols, | ||
| 160 | remote_table => $remote_table, | ||
| 161 | attrs => \%attrs, | ||
| 162 | }); | ||
| 163 | } | ||
| 164 | |||
| 165 | return \@rels; | ||
| 166 | } | ||
| 167 | |||
| 168 | # primary and unique info comes from the same sql statement, | ||
| 169 | # so cache it here for both routines to use | ||
| 170 | sub _mysql_table_get_keys { | ||
| 171 | my ($self, $table) = @_; | ||
| 172 | |||
| 173 | if(!exists($self->{_cache}->{_mysql_keys}->{$table->sql_name})) { | ||
| 174 | my %keydata; | ||
| 175 | my $sth = $self->dbh->prepare('SHOW INDEX FROM '.$table->sql_name); | ||
| 176 | $sth->execute; | ||
| 177 | while(my $row = $sth->fetchrow_hashref) { | ||
| 178 | next if $row->{Non_unique}; | ||
| 179 | push(@{$keydata{$row->{Key_name}}}, | ||
| 180 | [ $row->{Seq_in_index}, $self->_lc($row->{Column_name}) ] | ||
| 181 | ); | ||
| 182 | } | ||
| 183 | foreach my $keyname (keys %keydata) { | ||
| 184 | my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] } | ||
| 185 | @{$keydata{$keyname}}; | ||
| 186 | $keydata{$keyname} = \@ordered_cols; | ||
| 187 | } | ||
| 188 | $self->{_cache}->{_mysql_keys}->{$table->sql_name} = \%keydata; | ||
| 189 | } | ||
| 190 | |||
| 191 | return $self->{_cache}->{_mysql_keys}->{$table->sql_name}; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub _table_pk_info { | ||
| 195 | my ( $self, $table ) = @_; | ||
| 196 | |||
| 197 | return $self->_mysql_table_get_keys($table)->{PRIMARY}; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub _table_uniq_info { | ||
| 201 | my ( $self, $table ) = @_; | ||
| 202 | |||
| 203 | my @uniqs; | ||
| 204 | my $keydata = $self->_mysql_table_get_keys($table); | ||
| 205 | foreach my $keyname (sort keys %$keydata) { | ||
| 206 | next if $keyname eq 'PRIMARY'; | ||
| 207 | push(@uniqs, [ $keyname => $keydata->{$keyname} ]); | ||
| 208 | } | ||
| 209 | |||
| 210 | return \@uniqs; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub _columns_info_for { | ||
| 214 | my $self = shift; | ||
| 215 | my ($table) = @_; | ||
| 216 | |||
| 217 | my $result = $self->next::method(@_); | ||
| 218 | |||
| 219 | while (my ($col, $info) = each %$result) { | ||
| 220 | if ($info->{data_type} eq 'int') { | ||
| 221 | $info->{data_type} = 'integer'; | ||
| 222 | } | ||
| 223 | elsif ($info->{data_type} eq 'double') { | ||
| 224 | $info->{data_type} = 'double precision'; | ||
| 225 | } | ||
| 226 | my $data_type = $info->{data_type}; | ||
| 227 | |||
| 228 | delete $info->{size} if $data_type !~ /^(?: (?:var)?(?:char(?:acter)?|binary) | bit | year)\z/ix; | ||
| 229 | |||
| 230 | # information_schema is available in 5.0+ | ||
| 231 | my ($precision, $scale, $column_type, $default) = eval { $self->dbh->selectrow_array(<<'EOF', {}, $table->name, lc($col)) }; | ||
| 232 | SELECT numeric_precision, numeric_scale, column_type, column_default | ||
| 233 | FROM information_schema.columns | ||
| 234 | WHERE table_schema = schema() AND table_name = ? AND lower(column_name) = ? | ||
| 235 | EOF | ||
| 236 | my $has_information_schema = not $@; | ||
| 237 | |||
| 238 | $column_type = '' if not defined $column_type; | ||
| 239 | |||
| 240 | if ($data_type eq 'bit' && (not exists $info->{size})) { | ||
| 241 | $info->{size} = $precision if defined $precision; | ||
| 242 | } | ||
| 243 | elsif ($data_type =~ /^(?:float|double precision|decimal)\z/i) { | ||
| 244 | if (defined $precision && defined $scale) { | ||
| 245 | if ($precision == 10 && $scale == 0) { | ||
| 246 | delete $info->{size}; | ||
| 247 | } | ||
| 248 | else { | ||
| 249 | $info->{size} = [$precision,$scale]; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | elsif ($data_type eq 'year') { | ||
| 254 | if ($column_type =~ /\(2\)/) { | ||
| 255 | $info->{size} = 2; | ||
| 256 | } | ||
| 257 | elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) { | ||
| 258 | delete $info->{size}; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | elsif ($data_type =~ /^(?:date(?:time)?|timestamp)\z/) { | ||
| 262 | if (not (defined $self->datetime_undef_if_invalid && $self->datetime_undef_if_invalid == 0)) { | ||
| 263 | $info->{datetime_undef_if_invalid} = 1; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | elsif ($data_type =~ /^(?:enum|set)\z/ && $has_information_schema | ||
| 267 | && $column_type =~ /^(?:enum|set)\(/) { | ||
| 268 | |||
| 269 | delete $info->{extra}{list}; | ||
| 270 | |||
| 271 | while ($column_type =~ /'((?:[^']* (?:''|\\')* [^']*)* [^\\']?)',?/xg) { | ||
| 272 | my $el = $1; | ||
| 273 | $el =~ s/''/'/g; | ||
| 274 | push @{ $info->{extra}{list} }, $el; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | # Sometimes apparently there's a bug where default_value gets set to '' | ||
| 279 | # for things that don't actually have or support that default (like ints.) | ||
| 280 | if (exists $info->{default_value} && $info->{default_value} eq '') { | ||
| 281 | if ($has_information_schema) { | ||
| 282 | if (not defined $default) { | ||
| 283 | delete $info->{default_value}; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | else { # just check if it's a char/text type, otherwise remove | ||
| 287 | delete $info->{default_value} unless $data_type =~ /char|text/i; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | return $result; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub _extra_column_info { | ||
| 296 | no warnings 'uninitialized'; | ||
| 297 | my ($self, $table, $col, $info, $dbi_info) = @_; | ||
| 298 | my %extra_info; | ||
| 299 | |||
| 300 | if ($dbi_info->{mariadb_is_auto_increment}) { | ||
| 301 | $extra_info{is_auto_increment} = 1 | ||
| 302 | } | ||
| 303 | if ($dbi_info->{mariadb_type_name} =~ /\bunsigned\b/i) { | ||
| 304 | $extra_info{extra}{unsigned} = 1; | ||
| 305 | } | ||
| 306 | |||
| 307 | # XXX Not currently documented by DBD::MariaDB | ||
| 308 | if ($dbi_info->{mariadb_values}) { | ||
| 309 | $extra_info{extra}{list} = $dbi_info->{mariadb_values}; | ||
| 310 | } | ||
| 311 | |||
| 312 | # TODO MariaDB docs say that the precision may be specified inside the | ||
| 313 | # brackets. | ||
| 314 | if ((not blessed $dbi_info) # isa $sth | ||
| 315 | && $dbi_info->{COLUMN_DEF} =~ m/^current_timestamp(?:\(\))?$/i | ||
| 316 | && $dbi_info->{mariadb_type_name} =~ m/^(?:timestamp|datetime)$/i) { | ||
| 317 | |||
| 318 | my $current_timestamp = 'current_timestamp'; | ||
| 319 | $extra_info{default_value} = \$current_timestamp; | ||
| 320 | } | ||
| 321 | |||
| 322 | return \%extra_info; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub _dbh_column_info { | ||
| 326 | my $self = shift; | ||
| 327 | |||
| 328 | local $SIG{__WARN__} = sigwarn_silencer( | ||
| 329 | qr/^column_info: unrecognized column type/ | ||
| 330 | ); | ||
| 331 | |||
| 332 | $self->next::method(@_); | ||
| 333 | } | ||
| 334 | |||
| 335 | sub _table_comment { | ||
| 336 | my ( $self, $table ) = @_; | ||
| 337 | my $comment = $self->next::method($table); | ||
| 338 | if (not $comment) { | ||
| 339 | ($comment) = try { $self->schema->storage->dbh->selectrow_array( | ||
| 340 | qq{SELECT table_comment | ||
| 341 | FROM information_schema.tables | ||
| 342 | WHERE table_schema = schema() | ||
| 343 | AND table_name = ? | ||
| 344 | }, undef, $table->name); | ||
| 345 | }; | ||
| 346 | # InnoDB likes to auto-append crap. | ||
| 347 | if (not $comment) { | ||
| 348 | # Do nothing. | ||
| 349 | } | ||
| 350 | elsif ($comment =~ /^InnoDB free:/) { | ||
| 351 | $comment = undef; | ||
| 352 | } | ||
| 353 | else { | ||
| 354 | $comment =~ s/; InnoDB.*//; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | return $comment; | ||
| 358 | } | ||
| 359 | |||
| 360 | sub _column_comment { | ||
| 361 | my ( $self, $table, $column_number, $column_name ) = @_; | ||
| 362 | my $comment = $self->next::method($table, $column_number, $column_name); | ||
| 363 | if (not $comment) { | ||
| 364 | ($comment) = try { $self->schema->storage->dbh->selectrow_array( | ||
| 365 | qq{SELECT column_comment | ||
| 366 | FROM information_schema.columns | ||
| 367 | WHERE table_schema = schema() | ||
| 368 | AND table_name = ? | ||
| 369 | AND lower(column_name) = ? | ||
| 370 | }, undef, $table->name, lc($column_name)); | ||
| 371 | }; | ||
| 372 | } | ||
| 373 | return $comment; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub _view_definition { | ||
| 377 | my ($self, $view) = @_; | ||
| 378 | |||
| 379 | return scalar $self->schema->storage->dbh->selectrow_array( | ||
| 380 | q{SELECT view_definition | ||
| 381 | FROM information_schema.views | ||
| 382 | WHERE table_schema = schema() | ||
| 383 | AND table_name = ? | ||
| 384 | }, undef, $view->name, | ||
| 385 | ); | ||
| 386 | } | ||
| 387 | |||
| 388 | =head1 SEE ALSO | ||
| 389 | |||
| 390 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, | ||
| 391 | L<DBIx::Class::Schema::Loader::DBI>, L<DBIx::Class::Schema::Loader::DBI::mysql> | ||
| 392 | |||
| 393 | =head1 AUTHORS | ||
| 394 | |||
| 395 | Dave Lambley <dave@lambley.me.uk> | ||
| 396 | |||
| 397 | Martin Renvoize <martin.renvoize@ptfs-europe.com> | ||
| 398 | |||
| 399 | See L<DBIx::Class::Schema::Loader/AUTHORS>. | ||
| 400 | |||
| 401 | =head1 LICENSE | ||
| 402 | |||
| 403 | This library is free software; you can redistribute it and/or modify it under | ||
| 404 | the same terms as Perl itself. | ||
| 405 | |||
| 406 | =cut | ||
| 407 | |||
| 408 | 1; | ||
| 409 | # vim:et sw=4 sts=4 tw=0: |