| 1 | package Alzabo::RDBMSRules::PostgreSQL; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw($VERSION); | ||
| 5 | |||
| 6 | use Alzabo::Exceptions ( abbr => [ 'recreate_table_exception' ] ); | ||
| 7 | use Alzabo::RDBMSRules; | ||
| 8 | |||
| 9 | use Digest::MD5; | ||
| 10 | |||
| 11 | use Text::Balanced (); | ||
| 12 | |||
| 13 | use base qw(Alzabo::RDBMSRules); | ||
| 14 | |||
| 15 | use Params::Validate qw( validate_pos ); | ||
| 16 | Params::Validate::validation_options( on_fail => sub { Alzabo::Exception::Params->throw( error => join '', @_ ) } ); | ||
| 17 | |||
| 18 | $VERSION = 2.0; | ||
| 19 | |||
| 20 | 1; | ||
| 21 | |||
| 22 | sub new | ||
| 23 | { | ||
| 24 | my $proto = shift; | ||
| 25 | my $class = ref $proto || $proto; | ||
| 26 | |||
| 27 | return bless {}, $class; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub validate_schema_name | ||
| 31 | { | ||
| 32 | my $self = shift; | ||
| 33 | my $name = shift->name; | ||
| 34 | |||
| 35 | $self->_check_name($name, 'schema'); | ||
| 36 | |||
| 37 | Alzabo::Exception::RDBMSRules->throw( error => "Schema name ($name) contains a single quote char (')" ) | ||
| 38 | if index($name, "'") != -1; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub validate_table_name | ||
| 42 | { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | $self->_check_name( shift->name, 'table' ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub validate_column_name | ||
| 49 | { | ||
| 50 | my $self = shift; | ||
| 51 | |||
| 52 | $self->_check_name( shift->name, 'column' ); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _check_name | ||
| 56 | { | ||
| 57 | my $self = shift; | ||
| 58 | my $name = shift; | ||
| 59 | |||
| 60 | Alzabo::Exception::RDBMSRules->throw( error => "Name ($name) must be at least one character long" ) | ||
| 61 | unless length $name; | ||
| 62 | Alzabo::Exception::RDBMSRules->throw( error => "Name ($name) is too long. Names must be 31 characters or less." ) | ||
| 63 | if length $name > 31; | ||
| 64 | Alzabo::Exception::RDBMSRules->throw( error => "Name ($name) must start with an alpha or underscore(_) and must contain only alphanumerics and underscores." ) | ||
| 65 | unless $name =~ /\A[a-zA-Z]\w*\z/; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub validate_column_type | ||
| 69 | { | ||
| 70 | my $self = shift; | ||
| 71 | my $type = uc shift; | ||
| 72 | my $table = shift; | ||
| 73 | |||
| 74 | if ( $table->primary_key_size > 1 ) | ||
| 75 | { | ||
| 76 | return 'INT4' if $type =~ /^SERIAL4?$/; | ||
| 77 | return 'INT8' if $type eq 'BIGSERIAL' or $type eq 'SERIAL8'; | ||
| 78 | } | ||
| 79 | |||
| 80 | my %simple_types = map { $_ => 1 } qw( ABSTIME | ||
| 81 | BIT | ||
| 82 | BIGINT | ||
| 83 | BIGSERIAL | ||
| 84 | BOOL | ||
| 85 | BOOLEAN | ||
| 86 | BOX | ||
| 87 | BYTEA | ||
| 88 | CHAR | ||
| 89 | CHARACTER | ||
| 90 | CIDR | ||
| 91 | CIRCLE | ||
| 92 | DATE | ||
| 93 | DECIMAL | ||
| 94 | FLOAT | ||
| 95 | FLOAT4 | ||
| 96 | FLOAT8 | ||
| 97 | INET | ||
| 98 | SMALLINT | ||
| 99 | INT | ||
| 100 | INTEGER | ||
| 101 | INT2 | ||
| 102 | INT4 | ||
| 103 | INT8 | ||
| 104 | INTERVAL | ||
| 105 | MACADDR | ||
| 106 | MONEY | ||
| 107 | NUMERIC | ||
| 108 | OID | ||
| 109 | RELTIME | ||
| 110 | SERIAL | ||
| 111 | SERIAL4 | ||
| 112 | SERIAL8 | ||
| 113 | TEXT | ||
| 114 | TIME | ||
| 115 | TIMESTAMP | ||
| 116 | TIMESTAMPTZ | ||
| 117 | TIMETZ | ||
| 118 | VARBIT | ||
| 119 | VARCHAR ); | ||
| 120 | |||
| 121 | return 'INTEGER' if $type eq 'INT' || $type eq 'INT4'; | ||
| 122 | return 'SERIAL' if $type eq 'SERIAL4'; | ||
| 123 | return 'INT8' if $type eq 'BIGINT'; | ||
| 124 | |||
| 125 | return $type if $simple_types{$type}; | ||
| 126 | |||
| 127 | return $type if $type =~ /BIT\s+VARYING/; | ||
| 128 | |||
| 129 | return $type if $type =~ /CHARACTER\s+VARYING/; | ||
| 130 | |||
| 131 | return $type if $type =~ /\ABOX|CIRCLE|LINE|LSEG|PATH|POINT|POLYGON/; | ||
| 132 | |||
| 133 | Alzabo::Exception::RDBMSRules->throw( error => "Invalid column type: $type" ); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub validate_column_length | ||
| 137 | { | ||
| 138 | my $self = shift; | ||
| 139 | my $column = shift; | ||
| 140 | |||
| 141 | if ( defined $column->length ) | ||
| 142 | { | ||
| 143 | Alzabo::Exception::RDBMSRules->throw( error => "Length is not supported except for char, varchar, decimal, float, and numeric columns (" . $column->name . " column)" ) | ||
| 144 | unless $column->type =~ /\A(?:(?:VAR)?CHAR|CHARACTER|DECIMAL|FLOAT|NUMERIC|(?:VAR)?BIT|BIT VARYING)\z/i; | ||
| 145 | } | ||
| 146 | |||
| 147 | if ( defined $column->precision ) | ||
| 148 | { | ||
| 149 | Alzabo::Exception::RDBMSRules->throw( error => "Precision is not supported except for decimal, float, and numeric columns" ) | ||
| 150 | unless $column->type =~ /\A(?:DECIMAL|FLOAT|NUMERIC)\z/i; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | # placeholder in case we decide to try to do something better later | ||
| 155 | sub validate_table_attribute { 1 } | ||
| 156 | |||
| 157 | sub validate_column_attribute | ||
| 158 | { | ||
| 159 | my $self = shift; | ||
| 160 | my %p = @_; | ||
| 161 | |||
| 162 | my $column = $p{column}; | ||
| 163 | my $type = $column->type; | ||
| 164 | my $a = uc $p{attribute}; | ||
| 165 | $a =~ s/\A\s//; | ||
| 166 | $a =~ s/\s\z//; | ||
| 167 | |||
| 168 | return if $a =~ /\A(?:UNIQUE\z|CHECK|CONSTRAINT|REFERENCES)/i; | ||
| 169 | |||
| 170 | Alzabo::Exception::RDBMSRules->throw( error => "Only column constraints are supported as column attributes" ) | ||
| 171 | } | ||
| 172 | |||
| 173 | sub validate_primary_key | ||
| 174 | { | ||
| 175 | my $self = shift; | ||
| 176 | my $col = shift; | ||
| 177 | |||
| 178 | my $serial_col = (grep { $_->type =~ /^(?:SERIAL(?:4|8)?|BIGSERIAL)$/ } $col->table->primary_key)[0]; | ||
| 179 | if ( defined $serial_col && | ||
| 180 | $serial_col->name ne $col->name ) | ||
| 181 | { | ||
| 182 | $serial_col->set_type( $serial_col->type =~ /^SERIAL4?$/ | ||
| 183 | ? 'INT4' | ||
| 184 | : 'INT8' ); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | sub validate_sequenced_attribute | ||
| 189 | { | ||
| 190 | my $self = shift; | ||
| 191 | my $col = shift; | ||
| 192 | |||
| 193 | Alzabo::Exception::RDBMSRules->throw( error => 'Non-number columns cannot be sequenced' ) | ||
| 194 | unless $col->is_integer || $col->is_floating_point; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub validate_index | ||
| 198 | { | ||
| 199 | my $self = shift; | ||
| 200 | my $index = shift; | ||
| 201 | |||
| 202 | foreach my $c ( $index->columns ) | ||
| 203 | { | ||
| 204 | Alzabo::Exception::RDBMSRules->throw( error => "PostgreSQL does not support index prefixes" ) | ||
| 205 | if defined $index->prefix($c) | ||
| 206 | } | ||
| 207 | |||
| 208 | Alzabo::Exception::RDBMSRules->throw( error => "PostgreSQL does not support fulltext indexes" ) | ||
| 209 | if $index->fulltext; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub type_is_integer | ||
| 213 | { | ||
| 214 | my $self = shift; | ||
| 215 | my $col = shift; | ||
| 216 | my $type = uc $col->type; | ||
| 217 | |||
| 218 | return 1 if $type =~ /\A(?: | ||
| 219 | INT(?:2|4|8)?| | ||
| 220 | SMALLINT| | ||
| 221 | INTEGER| | ||
| 222 | OID| | ||
| 223 | SERIAL(?:4|8)?| | ||
| 224 | BIGSERIAL | ||
| 225 | ) | ||
| 226 | \z | ||
| 227 | /x; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub type_is_floating_point | ||
| 231 | { | ||
| 232 | my $self = shift; | ||
| 233 | my $col = shift; | ||
| 234 | my $type = uc $col->type; | ||
| 235 | |||
| 236 | return 1 if $type =~ /\A(?: | ||
| 237 | DECIMAL| | ||
| 238 | FLOAT(?:4|8)?| | ||
| 239 | MONEY| | ||
| 240 | NUMERIC | ||
| 241 | ) | ||
| 242 | \z | ||
| 243 | /x; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub type_is_char | ||
| 247 | { | ||
| 248 | my $self = shift; | ||
| 249 | my $col = shift; | ||
| 250 | my $type = uc $col->type; | ||
| 251 | |||
| 252 | return 1 if $type =~ /(?:CHAR|CHARACTER|TEXT)\z/; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub type_is_date | ||
| 256 | { | ||
| 257 | my $self = shift; | ||
| 258 | my $col = shift; | ||
| 259 | my $type = uc $col->type; | ||
| 260 | |||
| 261 | return 1 if $type eq 'DATE' || $self->type_is_datetime($col); | ||
| 262 | } | ||
| 263 | |||
| 264 | sub type_is_datetime | ||
| 265 | { | ||
| 266 | my $self = shift; | ||
| 267 | my $col = shift; | ||
| 268 | my $type = uc $col->type; | ||
| 269 | |||
| 270 | return 1 if $type =~ /^TIMESTAMP/; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub type_is_time | ||
| 274 | { | ||
| 275 | my $self = shift; | ||
| 276 | my $col = shift; | ||
| 277 | my $type = uc $col->type; | ||
| 278 | |||
| 279 | return 1 if $type eq 'TIME'; | ||
| 280 | } | ||
| 281 | |||
| 282 | sub type_is_time_interval | ||
| 283 | { | ||
| 284 | my $self = shift; | ||
| 285 | my $col = shift; | ||
| 286 | my $type = uc $col->type; | ||
| 287 | |||
| 288 | return 1 if $type eq 'INTERVAL'; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub type_is_blob | ||
| 292 | { | ||
| 293 | my $self = shift; | ||
| 294 | my $col = shift; | ||
| 295 | my $type = uc $col->type; | ||
| 296 | |||
| 297 | return 1 if $type =~ /\ABYTEA\z/; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub blob_type { return 'BYTEA' } | ||
| 301 | |||
| 302 | sub column_types | ||
| 303 | { | ||
| 304 | return ( qw( INTEGER | ||
| 305 | INT2 | ||
| 306 | INT8 | ||
| 307 | NUMERIC | ||
| 308 | FLOAT | ||
| 309 | FLOAT4 | ||
| 310 | |||
| 311 | CHAR | ||
| 312 | VARCHAR | ||
| 313 | TEXT | ||
| 314 | |||
| 315 | BYTEA | ||
| 316 | |||
| 317 | DATE | ||
| 318 | TIME | ||
| 319 | TIMESTAMP | ||
| 320 | INTERVAL | ||
| 321 | |||
| 322 | SERIAL | ||
| 323 | BIGSERIAL | ||
| 324 | |||
| 325 | BOOLEAN | ||
| 326 | |||
| 327 | BIT | ||
| 328 | ), | ||
| 329 | 'BIT VARYING', | ||
| 330 | |||
| 331 | qw( INET | ||
| 332 | CIDR | ||
| 333 | MACADDR ) ); | ||
| 334 | } | ||
| 335 | |||
| 336 | my %features = map { $_ => 1 } qw ( extended_column_types | ||
| 337 | constraints | ||
| 338 | functional_indexes | ||
| 339 | allows_raw_default | ||
| 340 | ); | ||
| 341 | sub feature | ||
| 342 | { | ||
| 343 | shift; | ||
| 344 | return $features{+shift}; | ||
| 345 | } | ||
| 346 | |||
| 347 | sub quote_identifiers { 1 } | ||
| 348 | |||
| 349 | sub quote_identifiers_character { '"' } | ||
| 350 | |||
| 351 | sub schema_sql | ||
| 352 | { | ||
| 353 | my $self = shift; | ||
| 354 | |||
| 355 | validate_pos( @_, { isa => 'Alzabo::Schema' } ); | ||
| 356 | |||
| 357 | my $schema = shift; | ||
| 358 | |||
| 359 | my @sql = $self->SUPER::schema_sql($schema); | ||
| 360 | |||
| 361 | # This has to come at the end because we don't know which tables | ||
| 362 | # reference other tables. | ||
| 363 | foreach my $t ( $schema->tables ) | ||
| 364 | { | ||
| 365 | foreach my $con ( grep { /\s*(?:check|constraint)/i } $t->attributes ) | ||
| 366 | { | ||
| 367 | push @sql, $self->table_constraint_sql($t); | ||
| 368 | } | ||
| 369 | |||
| 370 | |||
| 371 | foreach my $fk ( $t->all_foreign_keys ) | ||
| 372 | { | ||
| 373 | push @sql, $self->foreign_key_sql($fk); | ||
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | return @sql; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub table_sql | ||
| 381 | { | ||
| 382 | my $self = shift; | ||
| 383 | my $table = shift; | ||
| 384 | |||
| 385 | my $create_sequence = shift; | ||
| 386 | |||
| 387 | # Create table sequence by default | ||
| 388 | $create_sequence = 1 unless defined $create_sequence; | ||
| 389 | |||
| 390 | my $sql = qq|CREATE TABLE "| . $table->name . qq|" (\n |; | ||
| 391 | |||
| 392 | $sql .= join ",\n ", map { $self->column_sql($_) } $table->columns; | ||
| 393 | |||
| 394 | my @att = $table->attributes; | ||
| 395 | |||
| 396 | if (my @pk = $table->primary_key) | ||
| 397 | { | ||
| 398 | $sql .= ",\n"; | ||
| 399 | $sql .= ' PRIMARY KEY ('; | ||
| 400 | $sql .= join ', ', map { '"' . $_->name . '"' } @pk; | ||
| 401 | $sql .= ")\n"; | ||
| 402 | } | ||
| 403 | |||
| 404 | $sql .= ")\n"; | ||
| 405 | |||
| 406 | my @sql = ($sql); | ||
| 407 | |||
| 408 | foreach my $i ( $table->indexes ) | ||
| 409 | { | ||
| 410 | push @sql, $self->index_sql($i); | ||
| 411 | } | ||
| 412 | |||
| 413 | if ($create_sequence) | ||
| 414 | { | ||
| 415 | foreach my $c ( grep { $_->sequenced } $table->columns ) | ||
| 416 | { | ||
| 417 | push @sql, $self->_sequence_sql($c); | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | if (@att) | ||
| 422 | { | ||
| 423 | $sql .= ' '; | ||
| 424 | $sql .= join ' ', grep { ! /\s*(?:check|constraint)/i } @att; | ||
| 425 | } | ||
| 426 | |||
| 427 | $self->{state}{table_sql}{ $table->name } = 1; | ||
| 428 | |||
| 429 | return @sql; | ||
| 430 | } | ||
| 431 | |||
| 432 | sub _sequence_sql | ||
| 433 | { | ||
| 434 | my $self = shift; | ||
| 435 | my $col = shift; | ||
| 436 | |||
| 437 | return if $col->type =~ /^(?:SERIAL(?:4|8)?|BIGSERIAL)$/; | ||
| 438 | |||
| 439 | my $seq_name = $self->_sequence_name($col); | ||
| 440 | |||
| 441 | return qq|CREATE SEQUENCE "$seq_name";\n|; | ||
| 442 | } | ||
| 443 | |||
| 444 | sub _sequence_name | ||
| 445 | { | ||
| 446 | my $self = shift; | ||
| 447 | my $col = shift; | ||
| 448 | |||
| 449 | return join '___', $col->table->name, $col->name; | ||
| 450 | } | ||
| 451 | |||
| 452 | sub column_sql | ||
| 453 | { | ||
| 454 | my $self = shift; | ||
| 455 | my $col = shift; | ||
| 456 | my $p = shift; # hashref for skip_nullable, skip_default, && skip_name | ||
| 457 | |||
| 458 | my @default; | ||
| 459 | if ( ! $p->{skip_default} && defined $col->default ) | ||
| 460 | { | ||
| 461 | my $def = $self->_default_for_column($col); | ||
| 462 | |||
| 463 | @default = ( "DEFAULT $def" ); | ||
| 464 | } | ||
| 465 | |||
| 466 | my $type = $col->type; | ||
| 467 | my @length; | ||
| 468 | if ( defined $col->length ) | ||
| 469 | { | ||
| 470 | my $length = '(' . $col->length; | ||
| 471 | $length .= ', ' . $col->precision if defined $col->precision; | ||
| 472 | $length .= ')'; | ||
| 473 | $type .= $length; | ||
| 474 | } | ||
| 475 | |||
| 476 | my @nullable; | ||
| 477 | unless ( $p->{skip_nullable} ) | ||
| 478 | { | ||
| 479 | @nullable = $col->nullable ? 'NULL' : 'NOT NULL'; | ||
| 480 | } | ||
| 481 | |||
| 482 | my @name = $p->{skip_name} ? () : '"' . $col->name . '"'; | ||
| 483 | |||
| 484 | my $sql .= join ' ', ( @name, | ||
| 485 | $type, | ||
| 486 | @default, | ||
| 487 | @nullable, | ||
| 488 | $col->attributes ); | ||
| 489 | |||
| 490 | return $sql; | ||
| 491 | } | ||
| 492 | |||
| 493 | sub _default_for_column | ||
| 494 | { | ||
| 495 | my $self = shift; | ||
| 496 | my $col = shift; | ||
| 497 | |||
| 498 | return unless defined $col->default; | ||
| 499 | |||
| 500 | return $col->default if $col->is_numeric || $col->default_is_raw; | ||
| 501 | |||
| 502 | my $d = $col->default; | ||
| 503 | $d =~ s/'/''/g; | ||
| 504 | qq|'$d'|; | ||
| 505 | } | ||
| 506 | |||
| 507 | sub foreign_key_sql | ||
| 508 | { | ||
| 509 | my $self = shift; | ||
| 510 | my $fk = shift; | ||
| 511 | |||
| 512 | if ( grep { $_->is_primary_key } $fk->columns_from ) | ||
| 513 | { | ||
| 514 | return unless $fk->from_is_dependent; | ||
| 515 | } | ||
| 516 | |||
| 517 | return () if $self->{state}{fk_sql}{ $fk->id }; | ||
| 518 | |||
| 519 | my $sql = 'ALTER TABLE "'; | ||
| 520 | $sql .= $fk->table_from->name; | ||
| 521 | $sql .= '" ADD CONSTRAINT '; | ||
| 522 | $sql .= $self->_fk_name($fk); | ||
| 523 | $sql .= ' FOREIGN KEY ( '; | ||
| 524 | $sql .= join ', ', map { '"' . $_->name . '"' } $fk->columns_from; | ||
| 525 | $sql .= ' ) REFERENCES "'; | ||
| 526 | $sql .= $fk->table_to->name; | ||
| 527 | $sql .= '" ('; | ||
| 528 | $sql .= join ', ', map { '"' . $_->name . '"' } $fk->columns_to; | ||
| 529 | $sql .= ')'; | ||
| 530 | $sql .= ' ON DELETE '; | ||
| 531 | |||
| 532 | if ( $fk->from_is_dependent ) | ||
| 533 | { | ||
| 534 | $sql .= 'CASCADE'; | ||
| 535 | } | ||
| 536 | else | ||
| 537 | { | ||
| 538 | my @from = $fk->columns_from; | ||
| 539 | unless ( ( grep { $_->nullable } @from ) == @from ) | ||
| 540 | { | ||
| 541 | $sql .= 'SET DEFAULT'; | ||
| 542 | } | ||
| 543 | else | ||
| 544 | { | ||
| 545 | $sql .= 'SET NULL'; | ||
| 546 | } | ||
| 547 | } | ||
| 548 | |||
| 549 | $self->{state}{fk_sql}{ $fk->id } = 1; | ||
| 550 | |||
| 551 | return $sql; | ||
| 552 | } | ||
| 553 | |||
| 554 | sub _fk_name | ||
| 555 | { | ||
| 556 | my $id = $_[1]->id; | ||
| 557 | |||
| 558 | return | ||
| 559 | ( length $id > 63 | ||
| 560 | ? 'fk_' . Digest::MD5::md5_hex( $_[1]->id ) | ||
| 561 | : $id | ||
| 562 | ); | ||
| 563 | } | ||
| 564 | |||
| 565 | sub table_constraint_sql | ||
| 566 | { | ||
| 567 | my $self = shift; | ||
| 568 | my $table = shift; | ||
| 569 | |||
| 570 | return map { 'ALTER TABLE "' . $table->name . '" ADD ' . $_ } $table->attributes; | ||
| 571 | } | ||
| 572 | |||
| 573 | sub drop_table_sql | ||
| 574 | { | ||
| 575 | my $self = shift; | ||
| 576 | my $table = shift; | ||
| 577 | my $is_recreate = shift; | ||
| 578 | |||
| 579 | my @sql; | ||
| 580 | |||
| 581 | if ($is_recreate) | ||
| 582 | { | ||
| 583 | # We need to drop foreign keys referring to this table before | ||
| 584 | # we drop it. | ||
| 585 | foreach my $fk ( $table->all_foreign_keys ) | ||
| 586 | { | ||
| 587 | push @sql, $self->drop_foreign_key_sql( $fk->reverse ); | ||
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | push @sql, $self->SUPER::drop_table_sql($table); | ||
| 592 | |||
| 593 | unless ($is_recreate) | ||
| 594 | { | ||
| 595 | foreach my $c ( $table->columns ) | ||
| 596 | { | ||
| 597 | push @sql, $self->_drop_sequence_sql($c) if $c->sequenced; | ||
| 598 | } | ||
| 599 | } | ||
| 600 | |||
| 601 | return @sql; | ||
| 602 | } | ||
| 603 | |||
| 604 | sub _drop_sequence_sql | ||
| 605 | { | ||
| 606 | my $self = shift; | ||
| 607 | my $col = shift; | ||
| 608 | |||
| 609 | return if $col->type =~ /^(?:SERIAL(?:4|8)?|BIGSERIAL)$/; | ||
| 610 | |||
| 611 | my $seq_name = $self->_sequence_name($col); | ||
| 612 | |||
| 613 | return qq|DROP SEQUENCE "$seq_name";\n|; | ||
| 614 | } | ||
| 615 | |||
| 616 | sub drop_column_sql | ||
| 617 | { | ||
| 618 | my $self = shift; | ||
| 619 | my %p = @_; | ||
| 620 | |||
| 621 | recreate_table_exception(); | ||
| 622 | } | ||
| 623 | |||
| 624 | sub recreate_table_sql | ||
| 625 | { | ||
| 626 | my $self = shift; | ||
| 627 | my %p = @_; | ||
| 628 | |||
| 629 | # This is a hack to prevent this SQL from being made multiple | ||
| 630 | # times (which would be pointless) | ||
| 631 | return () if $self->{state}{table_sql}{ $p{new}->name }; | ||
| 632 | |||
| 633 | push @{ $self->{state}{deferred_sql} }, | ||
| 634 | $self->_restore_foreign_key_sql( $p{new} ); | ||
| 635 | |||
| 636 | return ( $self->_temp_table_sql( $p{new}, $p{old} ), | ||
| 637 | $self->drop_table_sql( $p{old}, 1 ), | ||
| 638 | # the 0 param indicates that we should not create sequences | ||
| 639 | $self->table_sql( $p{new}, 0 ), | ||
| 640 | $self->_restore_table_data_sql( $p{new}, $p{old} ), | ||
| 641 | $self->_drop_temp_table( $p{new} ), | ||
| 642 | ); | ||
| 643 | |||
| 644 | } | ||
| 645 | |||
| 646 | sub _temp_table_sql | ||
| 647 | { | ||
| 648 | my $self = shift; | ||
| 649 | my $new_table = shift; | ||
| 650 | my $old_table = shift; | ||
| 651 | |||
| 652 | my $temp_name = "TEMP" . $new_table->name; | ||
| 653 | |||
| 654 | my $sql = "SELECT "; | ||
| 655 | $sql .= join ', ', map { '"' . $_->name . '"' } $old_table->columns; | ||
| 656 | $sql .= qq|\n INTO TEMPORARY "$temp_name" FROM "| . $old_table->name . '"'; | ||
| 657 | |||
| 658 | return $sql; | ||
| 659 | } | ||
| 660 | |||
| 661 | sub _restore_table_data_sql | ||
| 662 | { | ||
| 663 | my $self = shift; | ||
| 664 | my $new_table = shift; | ||
| 665 | my $old_table = shift; | ||
| 666 | |||
| 667 | my @cols; | ||
| 668 | foreach my $column ( $new_table->columns ) | ||
| 669 | { | ||
| 670 | my $old_name = | ||
| 671 | defined $column->former_name ? $column->former_name : $column->name; | ||
| 672 | |||
| 673 | push @cols, [ $column->name, $old_name ] | ||
| 674 | if $old_table->has_column($old_name); | ||
| 675 | } | ||
| 676 | |||
| 677 | my $temp_name = "TEMP" . $new_table->name; | ||
| 678 | |||
| 679 | my $sql = 'INSERT INTO "' . $new_table->name . '" ('; | ||
| 680 | $sql .= join ', ', map { qq|"$_->[0]"| } @cols; | ||
| 681 | $sql .= " ) \n SELECT "; | ||
| 682 | $sql .= join ', ', map { qq|"$_->[1]"| } @cols; | ||
| 683 | $sql .= qq| FROM "$temp_name"|; | ||
| 684 | |||
| 685 | return $sql; | ||
| 686 | } | ||
| 687 | |||
| 688 | sub _drop_temp_table | ||
| 689 | { | ||
| 690 | my $self = shift; | ||
| 691 | my $table = shift; | ||
| 692 | |||
| 693 | my $temp_name = "TEMP" . $table->name; | ||
| 694 | |||
| 695 | return qq|DROP TABLE "$temp_name"|; | ||
| 696 | } | ||
| 697 | |||
| 698 | sub _restore_foreign_key_sql | ||
| 699 | { | ||
| 700 | my $self = shift; | ||
| 701 | my $table = shift; | ||
| 702 | |||
| 703 | my @sql; | ||
| 704 | foreach my $fk ( $table->all_foreign_keys ) | ||
| 705 | { | ||
| 706 | push @sql, $self->foreign_key_sql($fk); | ||
| 707 | push @sql, $self->foreign_key_sql( $fk->reverse ); | ||
| 708 | } | ||
| 709 | |||
| 710 | return @sql; | ||
| 711 | } | ||
| 712 | |||
| 713 | sub rename_sequences | ||
| 714 | { | ||
| 715 | my $self = shift; | ||
| 716 | my %p = @_; | ||
| 717 | |||
| 718 | return () if $self->{state}{rename_sequence_sql}{ $p{new}->name }; | ||
| 719 | |||
| 720 | my @sql; | ||
| 721 | |||
| 722 | for my $old_col ( grep { $_->sequenced } $p{old}->columns ) | ||
| 723 | { | ||
| 724 | my $new_col = $p{new}->column( $old_col->name ) | ||
| 725 | or next; | ||
| 726 | |||
| 727 | my $old_seq = $self->_sequence_name($old_col); | ||
| 728 | my $new_seq = $self->_sequence_name($new_col); | ||
| 729 | |||
| 730 | push @sql, | ||
| 731 | qq|ALTER TABLE "$old_seq" RENAME TO "$new_seq";\n|; | ||
| 732 | } | ||
| 733 | |||
| 734 | $self->{state}{rename_sequence_sql}{ $p{new}->name } = 1; | ||
| 735 | |||
| 736 | return @sql; | ||
| 737 | } | ||
| 738 | |||
| 739 | sub drop_foreign_key_sql | ||
| 740 | { | ||
| 741 | my $self = shift; | ||
| 742 | my $fk = shift; | ||
| 743 | |||
| 744 | if ( grep { $_->is_primary_key } $fk->columns_from ) | ||
| 745 | { | ||
| 746 | return unless $fk->from_is_dependent; | ||
| 747 | } | ||
| 748 | |||
| 749 | return () if $self->{state}{drop_fk_sql}{ $fk->id }; | ||
| 750 | |||
| 751 | $self->{state}{drop_fk_sql}{ $fk->id } = 1; | ||
| 752 | |||
| 753 | return 'ALTER TABLE "' . $fk->table_from->name . '" DROP CONSTRAINT ' | ||
| 754 | . $self->_fk_name($fk); | ||
| 755 | } | ||
| 756 | |||
| 757 | sub drop_index_sql | ||
| 758 | { | ||
| 759 | my $self = shift; | ||
| 760 | my $index = shift; | ||
| 761 | |||
| 762 | return 'DROP INDEX "' . $index->id . '"'; | ||
| 763 | } | ||
| 764 | |||
| 765 | sub column_sql_add | ||
| 766 | { | ||
| 767 | my $self = shift; | ||
| 768 | my $col = shift; | ||
| 769 | |||
| 770 | return () if $self->{state}{table_sql}{ $col->table->name }; | ||
| 771 | |||
| 772 | # Skip default and not null while adding column | ||
| 773 | my @sql = 'ALTER TABLE "' . $col->table->name . '" ADD COLUMN ' . $self->column_sql($col, { skip_default => 1, skip_nullable => 1 }); | ||
| 774 | |||
| 775 | my $def = $self->_default_for_column($col); | ||
| 776 | if ($def) | ||
| 777 | { | ||
| 778 | push @sql, | ||
| 779 | ( 'ALTER TABLE "' . $col->table->name . '" ALTER COLUMN "' . | ||
| 780 | $col->name . qq|" SET DEFAULT $def| ); | ||
| 781 | } | ||
| 782 | |||
| 783 | if ( ! $col->nullable ) | ||
| 784 | { | ||
| 785 | push @sql, | ||
| 786 | ( 'UPDATE "' . $col->table->name | ||
| 787 | . '" SET "' . $col->name . qq|" = $def WHERE "| | ||
| 788 | . $col->name . '" IS NULL' | ||
| 789 | ); | ||
| 790 | |||
| 791 | push @sql, | ||
| 792 | ( 'ALTER TABLE "' . $col->table->name | ||
| 793 | . '" ADD CONSTRAINT "' | ||
| 794 | . $col->table->name . '_' . $col->name . '_not_null" CHECK ( "' | ||
| 795 | . $col->name . '" IS NOT NULL )' | ||
| 796 | ); | ||
| 797 | } | ||
| 798 | |||
| 799 | return @sql; | ||
| 800 | } | ||
| 801 | |||
| 802 | sub column_sql_diff | ||
| 803 | { | ||
| 804 | my $self = shift; | ||
| 805 | my %p = @_; | ||
| 806 | |||
| 807 | return $self->drop_column_sql( new_table => $p{new}->table, | ||
| 808 | old => $p{old} ) | ||
| 809 | unless $self->_columns_are_equivalent( $p{new}, $p{old} ); | ||
| 810 | |||
| 811 | return; | ||
| 812 | } | ||
| 813 | |||
| 814 | sub _columns_are_equivalent | ||
| 815 | { | ||
| 816 | my $self = shift; | ||
| 817 | my $new = shift; | ||
| 818 | my $old = shift; | ||
| 819 | |||
| 820 | return 0 unless $self->_types_are_equivalent( $new, $old ); | ||
| 821 | |||
| 822 | return 0 unless $self->_defaults_are_equivalent( $new, $old ); | ||
| 823 | |||
| 824 | return 0 unless $new->sequenced == $old->sequenced; | ||
| 825 | |||
| 826 | my $new_att = join "\0", sort $new->attributes; | ||
| 827 | $new_att ||= ''; | ||
| 828 | |||
| 829 | my $old_att = join "\0", sort $old->attributes; | ||
| 830 | $old_att ||= ''; | ||
| 831 | |||
| 832 | return 0 unless $new_att eq $old_att; | ||
| 833 | |||
| 834 | return 1; | ||
| 835 | } | ||
| 836 | |||
| 837 | { | ||
| 838 | my %CanonicalTypes = | ||
| 839 | ( BOOL => 'BOOLEAN', | ||
| 840 | INT => 'INTEGER', | ||
| 841 | INT4 => 'INTEGER', | ||
| 842 | INT2 => 'SMALLINT', | ||
| 843 | INT8 => 'BIGINT', | ||
| 844 | VARBIT => 'BIT VARYING', | ||
| 845 | VARCHAR => 'CHARACTER VARYING', | ||
| 846 | CHAR => 'CHARACTER', | ||
| 847 | FLOAT => 'DOUBLE PRECISION', | ||
| 848 | FLOAT8 => 'DOUBLE PRECISION', | ||
| 849 | FLOAT4 => 'REAL', | ||
| 850 | DECIMAL => 'NUMERIC', | ||
| 851 | ); | ||
| 852 | |||
| 853 | sub _types_are_equivalent | ||
| 854 | { | ||
| 855 | shift; | ||
| 856 | my $col1 = shift; | ||
| 857 | my $col2 = shift; | ||
| 858 | |||
| 859 | my $type1 = $col1->type; | ||
| 860 | $type1 = $CanonicalTypes{ uc $type1 } if $CanonicalTypes{ uc $type1 }; | ||
| 861 | |||
| 862 | my $type2 = $col2->type; | ||
| 863 | $type2 = $CanonicalTypes{ uc $type2 } if $CanonicalTypes{ uc $type2 }; | ||
| 864 | |||
| 865 | $type1 .= join '-', grep { defined && length } $col1->length, $col1->precision; | ||
| 866 | $type2 .= join '-', grep { defined && length } $col1->length, $col1->precision; | ||
| 867 | |||
| 868 | return 1 if $type1 eq $type2; | ||
| 869 | } | ||
| 870 | } | ||
| 871 | |||
| 872 | sub _defaults_are_equivalent | ||
| 873 | { | ||
| 874 | my $self = shift; | ||
| 875 | my $col1 = shift; | ||
| 876 | my $col2 = shift; | ||
| 877 | |||
| 878 | return 1 if ! defined $col1->default && ! defined $col2->default; | ||
| 879 | return 0 if defined $col1->default && ! defined $col2->default; | ||
| 880 | return 0 if ! defined $col1->default && defined $col2->default; | ||
| 881 | |||
| 882 | if ( $col1->type =~ /^bool/i ) | ||
| 883 | { | ||
| 884 | return 1 | ||
| 885 | if lc substr( $col1->default, 0, 1 ) eq lc substr( $col2->default, 0, 1 ); | ||
| 886 | return 0; | ||
| 887 | } | ||
| 888 | elsif ( $col1->is_date | ||
| 889 | && $col1->default_is_raw | ||
| 890 | && $col2->default_is_raw ) | ||
| 891 | { | ||
| 892 | my $d1 = $col1->default; | ||
| 893 | my $d2 = $col2->default; | ||
| 894 | |||
| 895 | my $re = qr/^(?:current_timestamp|localtime|localtimestamp|now\(\))$/i; | ||
| 896 | return 1 | ||
| 897 | if $col1->default =~ /$re/ | ||
| 898 | && $col2->default =~ /$re/; | ||
| 899 | } | ||
| 900 | |||
| 901 | return 1 if | ||
| 902 | $self->_default_for_column($col1) eq $self->_default_for_column($col2); | ||
| 903 | } | ||
| 904 | |||
| 905 | sub alter_primary_key_sql | ||
| 906 | { | ||
| 907 | my $self = shift; | ||
| 908 | my %p = @_; | ||
| 909 | |||
| 910 | my @sql; | ||
| 911 | push @sql, 'DROP INDEX "' . $p{old}->name . '_pkey"'; | ||
| 912 | |||
| 913 | if ( $p{new}->primary_key ) | ||
| 914 | { | ||
| 915 | push @sql, ( 'CREATE UNIQUE INDEX "' . $p{new}->name . '_pkey" ON "' . | ||
| 916 | $p{new}->name . '" (' . | ||
| 917 | ( join ', ', | ||
| 918 | map { '"' . $_->name . '"' } $p{new}->primary_key ) . ')' ); | ||
| 919 | } | ||
| 920 | |||
| 921 | return @sql; | ||
| 922 | } | ||
| 923 | |||
| 924 | # Actually, Postgres _can_ change table names, but it's inability to | ||
| 925 | # change most aspects of a column definition make it very difficult to | ||
| 926 | # properly change a table name and then change its column definitions, | ||
| 927 | # so its easier just to recreate the table | ||
| 928 | sub can_alter_table_name | ||
| 929 | { | ||
| 930 | 0; | ||
| 931 | } | ||
| 932 | |||
| 933 | # Not sure if this is possible | ||
| 934 | sub alter_table_attributes_sql | ||
| 935 | { | ||
| 936 | my $self = shift; | ||
| 937 | |||
| 938 | recreate_table_exception(); | ||
| 939 | } | ||
| 940 | |||
| 941 | sub alter_column_name_sql | ||
| 942 | { | ||
| 943 | my $self = shift; | ||
| 944 | my $column = shift; | ||
| 945 | |||
| 946 | return | ||
| 947 | ( 'ALTER TABLE "' . $column->table->name . '" RENAME COLUMN ' . | ||
| 948 | $column->former_name . ' TO ' . $column->name | ||
| 949 | ); | ||
| 950 | } | ||
| 951 | |||
| 952 | sub reverse_engineer | ||
| 953 | { | ||
| 954 | my $self = shift; | ||
| 955 | my $schema = shift; | ||
| 956 | |||
| 957 | my $driver = $schema->driver; | ||
| 958 | |||
| 959 | foreach my $table ( $driver->tables ) | ||
| 960 | { | ||
| 961 | $table =~ s/^[^\.]+\.//; | ||
| 962 | $table =~ s/^\"|\"$//g; | ||
| 963 | |||
| 964 | print STDERR "Adding table $table to schema\n" | ||
| 965 | if Alzabo::Debug::REVERSE_ENGINEER; | ||
| 966 | |||
| 967 | my $t = $schema->make_table( name => $table ); | ||
| 968 | |||
| 969 | my $t_oid = $driver->one_row( sql => 'SELECT oid FROM pg_class WHERE relname = ?', | ||
| 970 | bind => $table ); | ||
| 971 | |||
| 972 | my $sql = <<'EOF'; | ||
| 973 | SELECT a.attname, a.attnotnull, t.typname, a.attnum, a.atthasdef, a.atttypmod | ||
| 974 | FROM pg_attribute a, pg_type t | ||
| 975 | WHERE a.attrelid = ? | ||
| 976 | AND a.atttypid = t.oid | ||
| 977 | AND a.attnum > 0 | ||
| 978 | EOF | ||
| 979 | |||
| 980 | $sql .= ' AND NOT a.attisdropped' if $driver->rdbms_version ge '7.3'; | ||
| 981 | |||
| 982 | $sql .= ' ORDER BY attnum'; | ||
| 983 | |||
| 984 | |||
| 985 | my %cols_by_number; | ||
| 986 | foreach my $row ( $driver->rows( sql => $sql, | ||
| 987 | bind => $t_oid ) ) | ||
| 988 | { | ||
| 989 | my %p; | ||
| 990 | |||
| 991 | $p{type} = $row->[2]; | ||
| 992 | |||
| 993 | # has default | ||
| 994 | if ( $row->[4] ) | ||
| 995 | { | ||
| 996 | $p{default} = | ||
| 997 | $driver->one_row | ||
| 998 | ( sql => 'SELECT adsrc FROM pg_attrdef WHERE adrelid = ? AND adnum = ?', | ||
| 999 | bind => [ $t_oid, $row->[3] ] ); | ||
| 1000 | |||
| 1001 | if ( $p{default} =~ /^nextval\(/ ) | ||
| 1002 | { | ||
| 1003 | $p{sequenced} = 1; | ||
| 1004 | $p{type} =~ s/(?:int(?:eger)?|numeric)/serial/; | ||
| 1005 | } | ||
| 1006 | else | ||
| 1007 | { | ||
| 1008 | # strip quotes (and type!) Postgres added | ||
| 1009 | $p{default} =~ s/^'//; #' | ||
| 1010 | if ( $driver->rdbms_version ge '7.4' ) | ||
| 1011 | { | ||
| 1012 | # 'grotesque' becomes 'grotesque'::character | ||
| 1013 | # varying. See | ||
| 1014 | # src/backend/utils/adt/format_type.c | ||
| 1015 | |||
| 1016 | # This is from | ||
| 1017 | # src/backend/util/adt/format_type.c | ||
| 1018 | $p{default} =~ s/'(?:::[^']{3,})?$//; | ||
| 1019 | $p{default} =~ s/\('(\w+)$/$1/; | ||
| 1020 | } | ||
| 1021 | else | ||
| 1022 | { | ||
| 1023 | $p{'default'} =~ s/'$//; | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | if ( $p{default} =~ /\([^\)]*\)/ | ||
| 1027 | || $p{default} =~ /^(?:current_timestamp|localtime|localtimestamp|now)$/i ) | ||
| 1028 | { | ||
| 1029 | $p{default_is_raw} = 1; | ||
| 1030 | } | ||
| 1031 | |||
| 1032 | $p{default} = 'now()' if $p{default} eq 'now'; | ||
| 1033 | } | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | if ( $p{type} =~ /char/i ) | ||
| 1037 | { | ||
| 1038 | # The real length is the value of: a.atttypmod - ((int32) sizeof(int32)) | ||
| 1039 | # | ||
| 1040 | # Sure wish I knew how to figure this out in Perl. | ||
| 1041 | # Its provided as VARHDRSZ in postgres.h but I can't | ||
| 1042 | # really get at it. On my linux machine this is 4. A | ||
| 1043 | # better way of doing this would be welcome. | ||
| 1044 | $p{length} = $row->[5] - 4; | ||
| 1045 | } | ||
| 1046 | if ( lc $p{type} eq 'numeric' ) | ||
| 1047 | { | ||
| 1048 | # see comment above. | ||
| 1049 | my $num = $row->[5] - 4; | ||
| 1050 | $p{length} = ($num >> 16) & 0xffff; | ||
| 1051 | $p{precision} = $num & 0xffff; | ||
| 1052 | } | ||
| 1053 | |||
| 1054 | $p{type} = 'char' if lc $p{type} eq 'bpchar'; | ||
| 1055 | |||
| 1056 | print STDERR "Adding $row->[0] column to $table\n" | ||
| 1057 | if Alzabo::Debug::REVERSE_ENGINEER; | ||
| 1058 | |||
| 1059 | my $col = $t->make_column( name => $row->[0], | ||
| 1060 | nullable => ! $row->[1], | ||
| 1061 | %p | ||
| 1062 | ); | ||
| 1063 | |||
| 1064 | if ( $col->is_integer ) | ||
| 1065 | { | ||
| 1066 | if ( $self->_re_sequence_exists( $driver, $col ) ) | ||
| 1067 | { | ||
| 1068 | $col->set_sequenced(1); | ||
| 1069 | } | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | $cols_by_number{ $row->[3] } = $row->[0]; | ||
| 1073 | } | ||
| 1074 | |||
| 1075 | |||
| 1076 | $sql = <<'EOF'; | ||
| 1077 | SELECT indkey | ||
| 1078 | FROM pg_index | ||
| 1079 | WHERE indisprimary | ||
| 1080 | AND indrelid = ? | ||
| 1081 | EOF | ||
| 1082 | |||
| 1083 | foreach my $cols ( $driver->column( sql => $sql, | ||
| 1084 | bind => $t_oid ) ) | ||
| 1085 | { | ||
| 1086 | my @cols = @cols_by_number{ split ' ', $cols }; | ||
| 1087 | local $" = ", "; | ||
| 1088 | |||
| 1089 | print STDERR "Setting @cols as primary key for $table\n" | ||
| 1090 | if Alzabo::Debug::REVERSE_ENGINEER; | ||
| 1091 | |||
| 1092 | $t->add_primary_key( $_ ) for $t->columns( @cols ); | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | my %i; | ||
| 1096 | if ( $driver->rdbms_version ge '7.4' ) | ||
| 1097 | { | ||
| 1098 | %i = $self->_74_indexes( $driver, $t, $t_oid, \%cols_by_number ); | ||
| 1099 | } | ||
| 1100 | else | ||
| 1101 | { | ||
| 1102 | %i = $self->_pre_74_indexes( $driver, $t, $t_oid, \%cols_by_number ); | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | foreach my $idx (values %i) | ||
| 1106 | { | ||
| 1107 | my @c = map { { column => $_ } } @{ $idx->{cols} }; | ||
| 1108 | |||
| 1109 | print STDERR "Adding index " | ||
| 1110 | . ( defined $idx->{'function'} | ||
| 1111 | ? $idx->{'function'} | ||
| 1112 | : join(', ', map $_->name, @{$idx->{'cols'}} ) ) | ||
| 1113 | . " to $table\n" | ||
| 1114 | if Alzabo::Debug::REVERSE_ENGINEER; | ||
| 1115 | |||
| 1116 | $t->make_index( columns => \@c, | ||
| 1117 | unique => $idx->{unique}, | ||
| 1118 | function => $idx->{function}, | ||
| 1119 | ); | ||
| 1120 | } | ||
| 1121 | |||
| 1122 | $sql = <<'EOF'; | ||
| 1123 | SELECT consrc, array_to_string(conkey,' ') | ||
| 1124 | FROM pg_constraint | ||
| 1125 | WHERE conrelid = ? | ||
| 1126 | AND contype = 'c' | ||
| 1127 | EOF | ||
| 1128 | |||
| 1129 | my @att; | ||
| 1130 | |||
| 1131 | foreach my $row ( $driver->rows( sql => $sql, | ||
| 1132 | bind => $t_oid ) ) | ||
| 1133 | { | ||
| 1134 | my ( $con, $cols ) = @$row; | ||
| 1135 | |||
| 1136 | # this stuff is not needed | ||
| 1137 | $con =~ s/::(\w+)//g; | ||
| 1138 | |||
| 1139 | # If $cols ever covers more than one value then this will fail. | ||
| 1140 | if ( $cols =~ /^(\d+)$/ ) | ||
| 1141 | { | ||
| 1142 | my $column = $cols_by_number{$1}; | ||
| 1143 | |||
| 1144 | print STDERR qq|Adding constraint "$con" to $table.$column\n| | ||
| 1145 | if Alzabo::Debug::REVERSE_ENGINEER; | ||
| 1146 | |||
| 1147 | $t->column($column)->add_attribute("CHECK $con"); | ||
| 1148 | } | ||
| 1149 | else | ||
| 1150 | { | ||
| 1151 | print STDERR qq|Adding constraint "$con" to $table\n| | ||
| 1152 | if Alzabo::Debug::REVERSE_ENGINEER; | ||
| 1153 | |||
| 1154 | $t->add_attribute("CHECK $con"); | ||
| 1155 | } | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | } | ||
| 1159 | |||
| 1160 | # Foreign key info is available in PG 7.3.0 and higher (could fake | ||
| 1161 | # it from pg_triggers with extensive gymnastics in version 7.0 and | ||
| 1162 | # higher, but that's a little iffy) | ||
| 1163 | $self->_foreign_keys_to_relationships($schema) | ||
| 1164 | if $driver->rdbms_version ge '7.3'; | ||
| 1165 | } | ||
| 1166 | |||
| 1167 | sub _re_sequence_exists | ||
| 1168 | { | ||
| 1169 | my $self = shift; | ||
| 1170 | my $driver = shift; | ||
| 1171 | my $col = shift; | ||
| 1172 | |||
| 1173 | my $seq_name = $self->_sequence_name($col); | ||
| 1174 | |||
| 1175 | my $sql = <<'EOF'; | ||
| 1176 | SELECT 1 | ||
| 1177 | FROM pg_class | ||
| 1178 | WHERE relname = ? | ||
| 1179 | AND relkind = ? | ||
| 1180 | EOF | ||
| 1181 | |||
| 1182 | return $driver->one_row( sql => $sql, | ||
| 1183 | bind => [ $seq_name, 'S' ], | ||
| 1184 | ); | ||
| 1185 | } | ||
| 1186 | |||
| 1187 | sub _74_indexes | ||
| 1188 | { | ||
| 1189 | my $self = shift; | ||
| 1190 | my $driver = shift; | ||
| 1191 | my $table = shift; | ||
| 1192 | my $t_oid = shift; | ||
| 1193 | my $cols_by_number = shift; | ||
| 1194 | |||
| 1195 | my $sql = <<'EOF'; | ||
| 1196 | SELECT indexrelid, indisunique, indkey, indnatts | ||
| 1197 | FROM pg_index | ||
| 1198 | WHERE indrelid = ? | ||
| 1199 | AND NOT indisprimary | ||
| 1200 | EOF | ||
| 1201 | |||
| 1202 | my %i; | ||
| 1203 | INDEX: | ||
| 1204 | foreach my $row ( $driver->rows( sql => $sql, | ||
| 1205 | bind => $t_oid ) ) | ||
| 1206 | { | ||
| 1207 | my $function; | ||
| 1208 | my @col_numbers; | ||
| 1209 | |||
| 1210 | my $spi = | ||
| 1211 | $driver->one_row | ||
| 1212 | ( sql => "SELECT COALESCE(indexprs,'') FROM pg_index WHERE indexrelid = ?", | ||
| 1213 | bind => $row->[0] ); | ||
| 1214 | |||
| 1215 | if ( $spi ) | ||
| 1216 | { | ||
| 1217 | SPI_EXPRESSION: | ||
| 1218 | while ( my $spi_expr = | ||
| 1219 | Text::Balanced::extract_bracketed( $spi, '{}', '[^{}]*' ) ) | ||
| 1220 | { | ||
| 1221 | # A wanton lack of respect for boundaries. 'Parse' the | ||
| 1222 | # PostgreSQL internal SPI language to find out what | ||
| 1223 | # columns are being accessed. | ||
| 1224 | push( @col_numbers, | ||
| 1225 | join( ' ', | ||
| 1226 | $spi_expr =~ /:varattno (\d+)/g ) ); | ||
| 1227 | } | ||
| 1228 | } | ||
| 1229 | |||
| 1230 | if ( scalar( @col_numbers ) > 1 ) | ||
| 1231 | { | ||
| 1232 | # Index objects are not prepared to handle functional | ||
| 1233 | # indexes that use more than one function. | ||
| 1234 | die "Alzabo " . Alzabo->VERSION . " does not support functional" | ||
| 1235 | . " indexes that are not strictly a single function." | ||
| 1236 | . " There are multiple functions on an index on the " | ||
| 1237 | . $table->name() . " table.\n"; | ||
| 1238 | } | ||
| 1239 | elsif ( scalar( @col_numbers ) == 1 ) | ||
| 1240 | { | ||
| 1241 | my $func = | ||
| 1242 | $driver->one_row | ||
| 1243 | ( sql => 'SELECT pg_catalog.pg_get_indexdef( ?, 1, true)', | ||
| 1244 | bind => $row->[0] ); | ||
| 1245 | |||
| 1246 | # XXX - not sure if this is a good idea but it makes the | ||
| 1247 | # rev-eng tests pass | ||
| 1248 | $func =~ s/\b(\w+)::\w+\b/$1/g; | ||
| 1249 | my $col_in_func = $1; | ||
| 1250 | |||
| 1251 | my @function; | ||
| 1252 | for my $num ( split / +/, $row->[2] ) | ||
| 1253 | { | ||
| 1254 | if ( $num == 0 ) | ||
| 1255 | { | ||
| 1256 | push @function, $func; | ||
| 1257 | } | ||
| 1258 | else | ||
| 1259 | { | ||
| 1260 | push @function, $cols_by_number->{$num}; | ||
| 1261 | push @col_numbers, $num; | ||
| 1262 | } | ||
| 1263 | } | ||
| 1264 | |||
| 1265 | $function = join ', ', @function; | ||
| 1266 | } | ||
| 1267 | else | ||
| 1268 | { | ||
| 1269 | # A regular index! | ||
| 1270 | @col_numbers = split / +/, $row->[2]; | ||
| 1271 | } | ||
| 1272 | |||
| 1273 | push( @{ $i{ $row->[0] }{cols} }, | ||
| 1274 | $table->columns( @{ $cols_by_number }{ @col_numbers } ) ); | ||
| 1275 | |||
| 1276 | $i{ $row->[0] }{function} = $function; | ||
| 1277 | $i{ $row->[0] }{unique} = $row->[1]; | ||
| 1278 | } | ||
| 1279 | |||
| 1280 | return %i; | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | sub _pre_74_indexes | ||
| 1284 | { | ||
| 1285 | my $self = shift; | ||
| 1286 | my $driver = shift; | ||
| 1287 | my $table = shift; | ||
| 1288 | my $t_oid = shift; | ||
| 1289 | my $cols_by_number = shift; | ||
| 1290 | |||
| 1291 | my $sql = <<'EOF'; | ||
| 1292 | SELECT c.oid, a.attname, i.indisunique, i.indproc, i.indkey | ||
| 1293 | FROM pg_index i, pg_attribute a, pg_class c | ||
| 1294 | WHERE i.indrelid = ? | ||
| 1295 | AND NOT i.indisprimary | ||
| 1296 | AND i.indexrelid = c.oid | ||
| 1297 | AND c.oid = a.attrelid | ||
| 1298 | AND a.attnum > 0 | ||
| 1299 | ORDER BY a.attnum | ||
| 1300 | EOF | ||
| 1301 | |||
| 1302 | my %i; | ||
| 1303 | foreach my $row ( $driver->rows( sql => $sql, | ||
| 1304 | bind => $t_oid ) ) | ||
| 1305 | { | ||
| 1306 | my @col_names = @{ $cols_by_number }{ split ' ', $row->[4] }; | ||
| 1307 | |||
| 1308 | my $function; | ||
| 1309 | if ( $row->[3] && $row->[3] =~ /\w/ && $row->[3] ne '-' ) | ||
| 1310 | { | ||
| 1311 | # some function names come out as "pg_catalog.foo" | ||
| 1312 | $row->[3] =~ s/\w+\.(\w+)/$1/; | ||
| 1313 | $function = uc $row->[3]; | ||
| 1314 | $function .= '('; | ||
| 1315 | |||
| 1316 | $function .= join ', ', @col_names; | ||
| 1317 | |||
| 1318 | $function .= ')'; | ||
| 1319 | } | ||
| 1320 | |||
| 1321 | push( @{ $i{ $row->[0] }{cols} }, | ||
| 1322 | $table->columns( @col_names ) ); | ||
| 1323 | |||
| 1324 | $i{ $row->[0] }{unique} = $row->[2]; | ||
| 1325 | $i{ $row->[0] }{function} = $function; | ||
| 1326 | } | ||
| 1327 | |||
| 1328 | return %i; | ||
| 1329 | } | ||
| 1330 | |||
| 1331 | sub _foreign_keys_to_relationships | ||
| 1332 | { | ||
| 1333 | my ($self, $schema) = @_; | ||
| 1334 | my $driver = $schema->driver; | ||
| 1335 | |||
| 1336 | my $constraint_sql = <<'EOF'; | ||
| 1337 | SELECT conrelid, confrelid, | ||
| 1338 | array_to_string(conkey,' '), | ||
| 1339 | array_to_string(confkey,' ') | ||
| 1340 | FROM pg_constraint | ||
| 1341 | WHERE contype = 'f' | ||
| 1342 | EOF | ||
| 1343 | |||
| 1344 | my $table_sql = <<'EOF'; | ||
| 1345 | SELECT relname | ||
| 1346 | FROM pg_class | ||
| 1347 | WHERE oid = ? | ||
| 1348 | EOF | ||
| 1349 | |||
| 1350 | my $column_sql = <<'EOF'; | ||
| 1351 | SELECT attname | ||
| 1352 | FROM pg_attribute | ||
| 1353 | WHERE attrelid = ? | ||
| 1354 | AND attnum = ? | ||
| 1355 | EOF | ||
| 1356 | |||
| 1357 | foreach my $row ( $driver->rows( sql => $constraint_sql ) ) | ||
| 1358 | { | ||
| 1359 | my $from_table = $driver->one_row( sql => $table_sql, | ||
| 1360 | bind => $row->[0] ); | ||
| 1361 | my $to_table = $driver->one_row( sql => $table_sql, | ||
| 1362 | bind => $row->[1] ); | ||
| 1363 | |||
| 1364 | # Column numbers are given as strings like "3 5" | ||
| 1365 | my @from_cols = split ' ', $row->[2] | ||
| 1366 | or die "Weird column specification $row->[2]"; | ||
| 1367 | |||
| 1368 | my @to_cols = split ' ', $row->[3] | ||
| 1369 | or die "Weird column specification $row->[3]"; | ||
| 1370 | |||
| 1371 | # Convert column numbers to names | ||
| 1372 | foreach (@from_cols) | ||
| 1373 | { | ||
| 1374 | $_ = $driver->one_row( sql => $column_sql, | ||
| 1375 | bind => [$row->[0], $_] ); | ||
| 1376 | } | ||
| 1377 | foreach (@to_cols) | ||
| 1378 | { | ||
| 1379 | $_ = $driver->one_row( sql => $column_sql, | ||
| 1380 | bind => [$row->[1], $_] ); | ||
| 1381 | } | ||
| 1382 | |||
| 1383 | print STDERR "Adding $from_table foreign key to $to_table\n" | ||
| 1384 | if Alzabo::Debug::REVERSE_ENGINEER; | ||
| 1385 | |||
| 1386 | # Convert to Alzabo objects | ||
| 1387 | $from_table = $schema->table($from_table); | ||
| 1388 | $to_table = $schema->table($to_table); | ||
| 1389 | @from_cols = map { $from_table->column($_) } @from_cols; | ||
| 1390 | @to_cols = map { $to_table->column($_) } @to_cols; | ||
| 1391 | |||
| 1392 | # If there's a unique constraint on the "from" columns, treat | ||
| 1393 | # is as 1-to-1. Otherwise treat it as n-to-1. | ||
| 1394 | my $from_unique = 0; | ||
| 1395 | |||
| 1396 | # Only use PK as determination of uniqueness if the FK is from | ||
| 1397 | # the _whole_ PK to something else. If the FK only includes | ||
| 1398 | # _part_ of the PK then it is not unique. | ||
| 1399 | $from_unique = 1 | ||
| 1400 | if ( ( @from_cols == grep { $_->is_primary_key } @from_cols ) | ||
| 1401 | && | ||
| 1402 | ( @from_cols == $from_table->primary_key_size ) ); | ||
| 1403 | |||
| 1404 | $from_unique = 1 | ||
| 1405 | if @from_cols == grep { $_->has_attribute( attribute => 'UNIQUE' ) } @from_cols; | ||
| 1406 | |||
| 1407 | INDEX: | ||
| 1408 | foreach my $i ( grep { $_->unique } $from_table->indexes ) | ||
| 1409 | { | ||
| 1410 | my @i_cols = $i->columns; | ||
| 1411 | |||
| 1412 | next unless @i_cols == @from_cols; | ||
| 1413 | |||
| 1414 | for ( my $x = 0; $x < @i_cols; $x++ ) | ||
| 1415 | { | ||
| 1416 | next INDEX unless $i_cols[$x] eq $from_cols[$x]; | ||
| 1417 | } | ||
| 1418 | |||
| 1419 | $from_unique = 1; | ||
| 1420 | } | ||
| 1421 | |||
| 1422 | my $from_cardinality = $from_unique ? '1' : 'n'; | ||
| 1423 | |||
| 1424 | my $from_is_dependent = | ||
| 1425 | ( grep { $_->nullable || defined $_->default } @from_cols ) ? 0 : 1; | ||
| 1426 | my $to_is_dependent = | ||
| 1427 | ( grep { $_->nullable || $_->is_primary_key } @to_cols ) ? 0 : 1; | ||
| 1428 | |||
| 1429 | $schema->add_relationship( cardinality => [ $from_cardinality, '1' ], | ||
| 1430 | table_from => $from_table, | ||
| 1431 | table_to => $to_table, | ||
| 1432 | columns_from => \@from_cols, | ||
| 1433 | columns_to => \@to_cols, | ||
| 1434 | from_is_dependent => $from_is_dependent, | ||
| 1435 | to_is_dependent => $to_is_dependent, | ||
| 1436 | ); | ||
| 1437 | } | ||
| 1438 | } | ||
| 1439 | |||
| 1440 | sub rules_id | ||
| 1441 | { | ||
| 1442 | return 'PostgreSQL'; | ||
| 1443 | } | ||
| 1444 | |||
| 1445 | __END__ |