| 1 | package Rose::DB::Object::Metadata::Auto::MySQL; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Carp(); | ||
| 6 | |||
| 7 | use Rose::DB::Object::Metadata::ForeignKey; | ||
| 8 | use Rose::DB::Object::Metadata::UniqueKey; | ||
| 9 | |||
| 10 | use Rose::DB::Object::Metadata::Auto; | ||
| 11 | our @ISA = qw(Rose::DB::Object::Metadata::Auto); | ||
| 12 | |||
| 13 | our $VERSION = '0.784'; | ||
| 14 | |||
| 15 | sub auto_init_primary_key_columns | ||
| 16 | { | ||
| 17 | my($self) = shift; | ||
| 18 | |||
| 19 | $self->SUPER::auto_init_primary_key_columns(@_); | ||
| 20 | |||
| 21 | # Wipe pk defaults because stupid MySQL adds them implicitly | ||
| 22 | foreach my $name ($self->primary_key_columns) | ||
| 23 | { | ||
| 24 | my $column = $self->column($name) or next; | ||
| 25 | $column->default(undef); | ||
| 26 | } | ||
| 27 | |||
| 28 | return; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub auto_generate_unique_keys | ||
| 32 | { | ||
| 33 | my($self) = shift; | ||
| 34 | |||
| 35 | unless(defined wantarray) | ||
| 36 | { | ||
| 37 | Carp::croak "Useless call to auto_generate_unique_keys() in void context"; | ||
| 38 | } | ||
| 39 | |||
| 40 | my($class, %unique_keys, $error); | ||
| 41 | |||
| 42 | TRY: | ||
| 43 | { | ||
| 44 | local $@; | ||
| 45 | |||
| 46 | eval | ||
| 47 | { | ||
| 48 | $class = $self->class or die "Missing class!"; | ||
| 49 | |||
| 50 | my $db = $self->db; | ||
| 51 | my $dbh = $db->dbh or die $db->error; | ||
| 52 | |||
| 53 | local $dbh->{'FetchHashKeyName'} = 'NAME'; | ||
| 54 | |||
| 55 | my $sth = $dbh->prepare('SHOW INDEX FROM ' . $self->fqq_table_sql($db)); | ||
| 56 | $sth->execute; | ||
| 57 | |||
| 58 | while(my $row = $sth->fetchrow_hashref) | ||
| 59 | { | ||
| 60 | next if($row->{'Non_unique'} || $row->{'Key_name'} eq 'PRIMARY'); | ||
| 61 | |||
| 62 | my $uk = $unique_keys{$row->{'Key_name'}} ||= | ||
| 63 | Rose::DB::Object::Metadata::UniqueKey->new(name => $row->{'Key_name'}, | ||
| 64 | parent => $self); | ||
| 65 | |||
| 66 | $uk->add_column($row->{'Column_name'}); | ||
| 67 | } | ||
| 68 | }; | ||
| 69 | |||
| 70 | $error = $@; | ||
| 71 | } | ||
| 72 | |||
| 73 | if($error) | ||
| 74 | { | ||
| 75 | Carp::croak "Could not auto-retrieve unique keys for class $class - $error"; | ||
| 76 | } | ||
| 77 | |||
| 78 | # This sort order is part of the API, and is essential to make the | ||
| 79 | # test suite work. | ||
| 80 | no warnings 'uninitialized'; | ||
| 81 | my @uk = map { $unique_keys{$_} } sort { lc $a cmp lc $b } keys(%unique_keys); | ||
| 82 | |||
| 83 | return wantarray ? @uk : \@uk; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub auto_generate_foreign_keys | ||
| 87 | { | ||
| 88 | my($self, %args) = @_; | ||
| 89 | |||
| 90 | unless(defined wantarray) | ||
| 91 | { | ||
| 92 | Carp::croak "Useless call to auto_generate_foreign_keys() in void context"; | ||
| 93 | } | ||
| 94 | |||
| 95 | my $no_warnings = $args{'no_warnings'}; | ||
| 96 | |||
| 97 | my($class, @foreign_keys, $total_fks, $error); | ||
| 98 | |||
| 99 | TRY: | ||
| 100 | { | ||
| 101 | local $@; | ||
| 102 | |||
| 103 | eval | ||
| 104 | { | ||
| 105 | $class = $self->class or die "Missing class!"; | ||
| 106 | |||
| 107 | my $db = $self->db; | ||
| 108 | my $dbh = $db->dbh or die $db->error; | ||
| 109 | my $db_name = $db->database; | ||
| 110 | |||
| 111 | local $dbh->{'FetchHashKeyName'} = 'NAME'; | ||
| 112 | |||
| 113 | my $cm = $self->convention_manager; | ||
| 114 | |||
| 115 | my $information_schema_ok = 0; | ||
| 116 | |||
| 117 | # Try information_schema if using MySQL >= 5.0.6 | ||
| 118 | if($db->database_version >= 5_000_006) | ||
| 119 | { | ||
| 120 | local $@; | ||
| 121 | |||
| 122 | eval | ||
| 123 | { | ||
| 124 | local $dbh->{'PrintError'} = 0; | ||
| 125 | |||
| 126 | my $sth = $dbh->prepare(<<"EOF"); | ||
| 127 | SELECT | ||
| 128 | CONSTRAINT_CATALOG, | ||
| 129 | CONSTRAINT_SCHEMA, | ||
| 130 | CONSTRAINT_NAME, | ||
| 131 | TABLE_CATALOG, | ||
| 132 | TABLE_SCHEMA, | ||
| 133 | TABLE_NAME, | ||
| 134 | COLUMN_NAME, | ||
| 135 | REFERENCED_TABLE_SCHEMA, | ||
| 136 | REFERENCED_TABLE_NAME, | ||
| 137 | REFERENCED_COLUMN_NAME | ||
| 138 | FROM | ||
| 139 | INFORMATION_SCHEMA.KEY_COLUMN_USAGE | ||
| 140 | WHERE | ||
| 141 | REFERENCED_TABLE_NAME IS NOT NULL AND | ||
| 142 | TABLE_SCHEMA = ? AND TABLE_NAME = ? | ||
| 143 | ORDER BY | ||
| 144 | REFERENCED_TABLE_SCHEMA, REFERENCED_TABLE_NAME, CONSTRAINT_NAME | ||
| 145 | EOF | ||
| 146 | $sth->execute($db_name, $self->table); | ||
| 147 | |||
| 148 | my($constraint_catalog, $constraint_schema, $constraint_name, | ||
| 149 | $table_catalog, $table_schema, $table_name, $local_column, | ||
| 150 | $foreign_schema, $foreign_table, $foreign_column); | ||
| 151 | |||
| 152 | $sth->bind_columns(\($constraint_catalog, $constraint_schema, $constraint_name, | ||
| 153 | $table_catalog, $table_schema, $table_name, $local_column, | ||
| 154 | $foreign_schema, $foreign_table, $foreign_column)); | ||
| 155 | my($current_constraint, %fk_info); | ||
| 156 | |||
| 157 | while($sth->fetch) | ||
| 158 | { | ||
| 159 | # No cross-database foreign keys in MySQL for now... | ||
| 160 | next unless(!defined $foreign_schema || lc $foreign_schema eq lc $db_name); | ||
| 161 | |||
| 162 | my $info = $fk_info{$constraint_name} ||= { foreign_table => $foreign_table }; | ||
| 163 | |||
| 164 | push(@{$info->{'local_columns'}}, $local_column); | ||
| 165 | push(@{$info->{'foreign_columns'}}, $foreign_column); | ||
| 166 | } | ||
| 167 | |||
| 168 | my $cm = $self->convention_manager; | ||
| 169 | |||
| 170 | FK: while(my($constraint_name, $info) = each %fk_info) | ||
| 171 | { | ||
| 172 | my $foreign_table = $info->{'foreign_table'}; | ||
| 173 | my @local_columns = @{$info->{'local_columns'}}; | ||
| 174 | my @foreign_columns = @{$info->{'foreign_columns'}}; | ||
| 175 | |||
| 176 | unless(@local_columns > 0 && @local_columns == @foreign_columns) | ||
| 177 | { | ||
| 178 | die "Failed to extrat foreign key information from ", | ||
| 179 | "information_schema for table '", $self->table, "' ", | ||
| 180 | " in database '$db_name'"; | ||
| 181 | } | ||
| 182 | |||
| 183 | my $foreign_class = $self->class_for(table => $foreign_table, schema => $db_name); | ||
| 184 | |||
| 185 | unless($foreign_class) | ||
| 186 | { | ||
| 187 | # Add deferred task | ||
| 188 | $self->add_deferred_task( | ||
| 189 | { | ||
| 190 | class => $self->class, | ||
| 191 | method => 'auto_init_foreign_keys', | ||
| 192 | args => \%args, | ||
| 193 | |||
| 194 | code => sub | ||
| 195 | { | ||
| 196 | $self->auto_init_foreign_keys(%args); | ||
| 197 | $self->make_foreign_key_methods(%args, preserve_existing => 1); | ||
| 198 | }, | ||
| 199 | |||
| 200 | check => sub | ||
| 201 | { | ||
| 202 | my $fks = $self->foreign_keys; | ||
| 203 | return @$fks == $total_fks ? 1 : 0; | ||
| 204 | } | ||
| 205 | }); | ||
| 206 | |||
| 207 | unless($no_warnings || $self->allow_auto_initialization) | ||
| 208 | { | ||
| 209 | no warnings; # Allow undef coercion to empty string | ||
| 210 | warn "No Rose::DB::Object-derived class found for table ", | ||
| 211 | "'$foreign_table'"; | ||
| 212 | } | ||
| 213 | |||
| 214 | $total_fks++; | ||
| 215 | next FK; | ||
| 216 | } | ||
| 217 | |||
| 218 | my %key_columns; | ||
| 219 | @key_columns{@local_columns} = @foreign_columns; | ||
| 220 | |||
| 221 | my $fk = | ||
| 222 | Rose::DB::Object::Metadata::ForeignKey->new( | ||
| 223 | name => $constraint_name, | ||
| 224 | class => $foreign_class, | ||
| 225 | key_columns => \%key_columns); | ||
| 226 | |||
| 227 | push(@foreign_keys, $fk); | ||
| 228 | $total_fks++; | ||
| 229 | } | ||
| 230 | |||
| 231 | $information_schema_ok = 1; | ||
| 232 | }; | ||
| 233 | } | ||
| 234 | |||
| 235 | # Fall back to the crappy method... | ||
| 236 | unless($information_schema_ok) | ||
| 237 | { | ||
| 238 | #my $q = $dbh->get_info(29); # quote character | ||
| 239 | |||
| 240 | my $sth = $dbh->prepare("SHOW CREATE TABLE `$db_name`.`" . $self->table . '`'); | ||
| 241 | $sth->execute; | ||
| 242 | |||
| 243 | # This happens when the table has no foreign keys | ||
| 244 | return unless(defined $sth); | ||
| 245 | |||
| 246 | FK: while(my $row = $sth->fetchrow_hashref) | ||
| 247 | { | ||
| 248 | # The Create Table column contains a text description of foreign keys | ||
| 249 | # that we have to parse. See, this is why people hate MySQL. | ||
| 250 | # | ||
| 251 | # The value looks like this (s/\n/ /g): | ||
| 252 | # | ||
| 253 | # CONSTRAINT `products_ibfk_1` FOREIGN KEY (`vendor_id`) | ||
| 254 | # REFERENCES `vendors` (`id`) ON DELETE NO ACTION ON UPDATE SET NULL, | ||
| 255 | # CONSTRAINT `products_ibfk_1` FOREIGN KEY (`vendor_id`) | ||
| 256 | # REFERENCES `dbname`.`vendors` (`id`) ON DELETE NO ACTION ON UPDATE SET NULL, | ||
| 257 | # CONSTRAINT `rose_db_object_test_ibfk_4` FOREIGN KEY (`fk1`, `fk2`, `fk3`) | ||
| 258 | # REFERENCES `rose_db_object_other` (`k1`, `k2`, `k3`) | ||
| 259 | |||
| 260 | for(my $sql = $row->{'Create Table'}) | ||
| 261 | { | ||
| 262 | s/^.+?,\n\s*(?=CONSTRAINT)//si; | ||
| 263 | |||
| 264 | # XXX: This is not bullet-proof | ||
| 265 | FK: while(s{^CONSTRAINT \s+ | ||
| 266 | `((?:[^`]|``)+)` \s+ # constraint name | ||
| 267 | FOREIGN \s+ KEY \s+ | ||
| 268 | \( ((?:`(?:[^`]|``)+`,? \s*)+) \) \s+ # local columns | ||
| 269 | REFERENCES \s* | ||
| 270 | (?: `((?:[^`]|``)+)` \. )? # foreign db | ||
| 271 | `((?:[^`]|``)+)` \s+ # foreign table | ||
| 272 | \( ((?:`(?:[^`]|``)+`,? \s*)+) \) # foreign columns | ||
| 273 | (?: \s+ ON \s+ (?: DELETE | UPDATE) \s+ | ||
| 274 | (?: RESTRICT | CASCADE | SET \s+ NULL | NO \s+ ACTION) | ||
| 275 | )* (?:, \s* | \s* \))}{}six) | ||
| 276 | { | ||
| 277 | my $constraint_name = $1; | ||
| 278 | my $local_columns = $2; | ||
| 279 | my $foreign_db = $3; | ||
| 280 | my $foreign_table = $4; | ||
| 281 | my $foreign_columns = $5; | ||
| 282 | |||
| 283 | # No cross-database foreign keys in MySQL for now... | ||
| 284 | next unless(!defined $foreign_db || lc $foreign_db eq $db_name); | ||
| 285 | |||
| 286 | # XXX: This is not bullet-proof | ||
| 287 | my @local_columns = map { s/^`//; s/`$//; s/``/`/g; $_ } split(/,? /, $local_columns); | ||
| 288 | my @foreign_columns = map { s/^`//; s/`$//; s/``/`/g; $_ } split(/,? /, $foreign_columns); | ||
| 289 | |||
| 290 | unless(@local_columns > 0 && @local_columns == @foreign_columns) | ||
| 291 | { | ||
| 292 | die "Failed to parse MySQL table definition ", | ||
| 293 | "'$row->{'Create Table'}' returned by the query '", | ||
| 294 | "SHOW CREATE TABLE `$db_name`.`" . $self->table . '`'; | ||
| 295 | } | ||
| 296 | |||
| 297 | my $foreign_class = $self->class_for(table => $foreign_table); | ||
| 298 | |||
| 299 | unless($foreign_class) | ||
| 300 | { | ||
| 301 | # Add deferred task | ||
| 302 | $self->add_deferred_task( | ||
| 303 | { | ||
| 304 | class => $self->class, | ||
| 305 | method => 'auto_init_foreign_keys', | ||
| 306 | args => \%args, | ||
| 307 | |||
| 308 | code => sub | ||
| 309 | { | ||
| 310 | $self->auto_init_foreign_keys(%args); | ||
| 311 | $self->make_foreign_key_methods(%args, preserve_existing => 1); | ||
| 312 | }, | ||
| 313 | |||
| 314 | check => sub | ||
| 315 | { | ||
| 316 | my $fks = $self->foreign_keys; | ||
| 317 | return @$fks == $total_fks ? 1 : 0; | ||
| 318 | } | ||
| 319 | }); | ||
| 320 | |||
| 321 | unless($no_warnings || $self->allow_auto_initialization) | ||
| 322 | { | ||
| 323 | no warnings; # Allow undef coercion to empty string | ||
| 324 | warn "No Rose::DB::Object-derived class found for table ", | ||
| 325 | "'$foreign_table'"; | ||
| 326 | } | ||
| 327 | |||
| 328 | $total_fks++; | ||
| 329 | next FK; | ||
| 330 | } | ||
| 331 | |||
| 332 | my %key_columns; | ||
| 333 | @key_columns{@local_columns} = @foreign_columns; | ||
| 334 | |||
| 335 | my $fk = | ||
| 336 | Rose::DB::Object::Metadata::ForeignKey->new( | ||
| 337 | name => $constraint_name, | ||
| 338 | class => $foreign_class, | ||
| 339 | key_columns => \%key_columns); | ||
| 340 | |||
| 341 | push(@foreign_keys, $fk); | ||
| 342 | $total_fks++; | ||
| 343 | } | ||
| 344 | } | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | # This step is important! It ensures that foreign keys will be created | ||
| 349 | # in a deterministic order, which in turn allows the "auto-naming" of | ||
| 350 | # foreign keys to work in a predictable manner. This exact sort order | ||
| 351 | # (lowercase table name comparisons) is part of the API for foreign | ||
| 352 | # key auto generation. | ||
| 353 | @foreign_keys = | ||
| 354 | sort { lc $a->class->meta->table cmp lc $b->class->meta->table } | ||
| 355 | @foreign_keys; | ||
| 356 | |||
| 357 | my %used_names; | ||
| 358 | |||
| 359 | foreach my $fk (@foreign_keys) | ||
| 360 | { | ||
| 361 | my $name = | ||
| 362 | $cm->auto_foreign_key_name($fk->class, $fk->name, scalar $fk->key_columns, \%used_names); | ||
| 363 | |||
| 364 | unless(defined $name) | ||
| 365 | { | ||
| 366 | $fk->name($name = $self->foreign_key_name_generator->($self, $fk)); | ||
| 367 | } | ||
| 368 | |||
| 369 | unless(defined $name && $name =~ /^\w+$/) | ||
| 370 | { | ||
| 371 | die "Missing or invalid key name '$name' for foreign key ", | ||
| 372 | "generated in $class for ", $fk->class; | ||
| 373 | } | ||
| 374 | |||
| 375 | $used_names{$name}++; | ||
| 376 | |||
| 377 | $fk->name($name); | ||
| 378 | } | ||
| 379 | }; | ||
| 380 | |||
| 381 | $error = $@; | ||
| 382 | } | ||
| 383 | |||
| 384 | if($error) | ||
| 385 | { | ||
| 386 | Carp::croak "Could not auto-generate foreign keys for class $class - $error"; | ||
| 387 | } | ||
| 388 | |||
| 389 | @foreign_keys = sort { lc $a->name cmp lc $b->name } @foreign_keys; | ||
| 390 | |||
| 391 | return wantarray ? @foreign_keys : \@foreign_keys; | ||
| 392 | } | ||
| 393 | |||
| 394 | 1; |