Changeset 2881
- Timestamp:
- 08/01/08 21:38:08 (4 months ago)
- Files:
-
- branches/release-42/lib/MT/ObjectDriver/DDL.pm (modified) (6 diffs)
- branches/release-42/lib/MT/ObjectDriver/DDL/SQLite.pm (modified) (5 diffs)
- branches/release-42/lib/MT/ObjectDriver/DDL/mysql.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/release-42/lib/MT/ObjectDriver/DDL.pm
r2187 r2881 28 28 sub create_sequence {} 29 29 sub drop_sequence {} 30 sub unique_constraint_sql { '' } 30 31 sub unique_constraint_sql { 32 my $ddl = shift; 33 my ($class) = @_; 34 35 my $pk = $class->properties->{primary_key}; 36 return q{} if !ref $pk || 'ARRAY' ne ref $pk; 37 38 my $driver = $class->driver; 39 my $dbd = $driver->dbd; 40 my $table = $class->table_name; 41 42 my @key_fields = map { $dbd->db_column_name($table, $_) } @$pk; 43 return ', PRIMARY KEY (' . join(q{, }, @key_fields) . ')'; 44 } 31 45 32 46 sub table_exists { … … 258 272 my @stmts; 259 273 if ($indexes) { 260 # FIXME: Handle possible future primary key tuple case261 274 my $pk = $props->{primary_key}; 275 undef $pk if ref $pk; # ignore complex key 262 276 foreach my $name (keys %$indexes) { 263 277 next if $pk && $name eq $pk; … … 283 297 my $pk = $props->{primary_key}; 284 298 if (!ref $indexes->{$name}) { 285 if (! ($pk && $name eq $pk)) {299 if (!$pk || ref $pk || $name ne $pk) { 286 300 push @stmts, "CREATE INDEX ${table_name}_$name ON $table_name (${field_prefix}_$name)"; 287 301 } … … 362 376 $default = ' DEFAULT ' . $value; 363 377 } 364 my $key = ''; 365 $key = ' PRIMARY KEY' if $def->{key}; 378 my $key = !$def->{key} ? q{} 379 : ref $class->properties->{primary_key} ? q{} 380 : ' PRIMARY KEY' 381 ; 366 382 return $field_prefix . '_' . $name . ' ' . $type . $nullable . $default . $key; 367 383 } … … 430 446 =head1 NAME 431 447 432 MT::ObjectDriver::DDL 433 434 =head1 DESCRIPTION 448 MT::ObjectDriver::DDL - Data Definition Language driver 435 449 436 450 =head1 SYNOPSIS … … 443 457 my @ddl = $ddl->as_ddl; 444 458 459 =head1 DESCRIPTION 460 461 The Data Definition Language (DDL) drivers provide compatible SQL for creating 462 and changing the database tables that contain Movable Type's data. The DDL 463 drivers are mainly used by MT::Upgrade, the automatic upgrader. 464 445 465 =head1 METHODS 446 466 467 =head2 $ddl->column_defs($class) 468 469 Returns a description of the current column definitions in the table holding 470 records for C<$class>, an C<MT::Object> subclass. These descriptions are 471 comparable to the results of C<MT::Object::column_defs()>; that is, the 472 description for a class is a hashref containing a column definition for each 473 column of the class, keyed on the name of the column. Each column definition is 474 itself a hashref with the possible keys: 475 476 =over 4 477 478 =item * C<type> 479 480 The type of data contained in the column. See C<db2type()> for the possible 481 values of this member. 482 483 =item * C<auto> 484 485 If true, indicates the column is an auto-increment column (that is, a column 486 automatically filled by the database when no value is provided for new 487 records). 488 489 =item * C<key> 490 491 If true, indicates the column is or is part of the table's primary key. The 492 combined value of all key columns in a record must be unique, and should 493 constitute the identity of the record. 494 495 =item * C<size> 496 497 For string columns, the maximum possible length of values in the column. 498 499 =item * C<not_null> 500 501 If true, indicates the column is not allowed to contain a C<NULL> value. 502 503 =item * C<default> 504 505 The default value used if a record is sent to be saved with no value for that 506 column. 507 508 =back 509 510 If the table does not exist, no value is returned. 511 512 Subclasses B<must> themselves implement C<column_defs()>. No default 513 implementation is provided. 514 515 =head2 $ddl->index_defs($class) 516 517 Returns a description of all the index definitions for the table storing 518 records for C<$class>, an C<MT::Object> subclass. These descriptions are 519 hashrefs containing individual index definitions, keyed on the names of the 520 indexes. 521 522 Each index definition is either C<1>, meaning the index is on the single column 523 named the same as the index, or a hashref with the possible keys: 524 525 =over 4 526 527 =item * C<columns> 528 529 An arrayref listing the columns that compose the index. 530 531 =item * C<unique> 532 533 If true, indicates a record's values for the indexes are required to be unique 534 across the table. 535 536 =back 537 538 If no indexes exist for the table, either no value or an empty hashref is 539 returned. 540 541 =head2 $ddl->table_exists($class) 542 543 Returns whether the table for C<$class>, an C<MT::Object> subclass, exists in 544 the database. 545 546 =head2 $ddl->unique_constraint_sql($class) 547 548 Returns the SQL describing the uniqueness constraints specified in the 549 properties of C<$class>, an C<MT::Object> subclass, suitable for insertion at 550 the end of a C<CREATE TABLE> statement. 551 552 The default implementation returns a standard multi-column C<PRIMARY KEY> 553 declaration if the primary key of C<$class> is multiple columns, or the empty 554 string otherwise. 555 556 =head2 $ddl->can_add_column() 557 558 Returns whether the database can add columns to a table that already exists. 559 560 The default implementation returns false. Subclasses should override this 561 method to return true if they implement C<add_column_sql()>. 562 563 =head2 $ddl->can_drop_column() 564 565 Returns whether the database can drop (remove) columns from a table that 566 already exists. 567 568 The default implementation returns false. Subclasses should override this 569 method to return true if they implement C<drop_column_sql()>. 570 571 =head2 $ddl->can_alter_column() 572 573 Returns whether the database can change the definition of a column that already 574 exists. 575 576 The default implementation returns false. Subclasses should override this 577 method to return true if they implement C<alter_column_sql()>. 578 579 =head2 $ddl->can_add_constraint() 580 581 Returns whether the database can add a constraint on the table that already 582 exists. 583 584 The default implementation returns B<true>. Subclasses should override this 585 method to return B<false> if the database does not support the C<ALTER TABLE 586 ... ADD CONSTRAINT> statement. 587 588 =head2 $ddl->fix_class() 589 590 =head2 $ddl->insert_from_sql() 591 592 =head2 $ddl->drop_table_sql() 593 594 =head2 $ddl->drop_index_sql() 595 596 =head2 $ddl->create_table_sql() 597 598 =head2 $ddl->create_table_as_sql() 599 600 =head2 $ddl->index_table_sql() 601 602 =head2 $ddl->index_column_sql() 603 604 =head2 $ddl->add_column_sql() 605 606 =head2 $ddl->alter_column_sql() 607 608 =head2 $ddl->drop_column_sql() 609 610 =head2 $ddl->column_sql() 611 612 =head2 $ddl->cast_column_sql() 613 614 =head2 $ddl->db2type() 615 616 =head2 $ddl->type2db() 617 618 TODO 619 447 620 =head2 $ddl->add_column() 448 621 622 =head2 $ddl->alter_column() 623 624 =head2 $ddl->drop_column() 625 626 =head2 $ddl->index_column() 627 628 B<Deprecated.> These methods return the results of the corresponding C<_sql> 629 methods. 630 449 631 =head2 $ddl->create_table() 450 632 451 633 =head2 $ddl->drop_table() 452 634 453 =head2 $ddl->drop_column() 454 455 =head2 $ddl->create_index() 456 457 =head2 $ddl->drop_index() 635 =head2 $ddl->index_table() 636 637 =head2 $ddl->create_sequence() 638 639 =head2 $ddl->drop_sequence() 640 641 B<Deprecated.> These methods return no value. 458 642 459 643 =head1 AUTHOR & COPYRIGHT branches/release-42/lib/MT/ObjectDriver/DDL/SQLite.pm
r2705 r2881 101 101 $sth->execute or return undef; 102 102 my $defs = {}; 103 my @pks; 103 104 while (my $row = $sth->fetchrow_hashref) { 104 105 my $colname = lc $row->{name}; … … 109 110 } 110 111 $defs->{$colname}{type} = $coltype; 112 # TODO: isn't key for pks, not foreign keys? 111 113 if ($colname =~ m/_id$/) { 112 114 $defs->{$colname}{key} = 1; 113 115 } 114 if ( ($coltype eq 'integer') && $row->{pk} ) { 115 # with sqlite, integer primary keys auto increment. always. 116 if ($row->{pk}) { 116 117 $defs->{$colname}{key} = 1; 117 $defs->{$colname}{auto} = 1;118 push @pks, $colname; 118 119 } 119 120 $defs->{$colname}{not_null} = 1 … … 124 125 $sth->finish; 125 126 return undef unless %$defs; 127 128 if (@pks && 1 == scalar @pks) { 129 my ($colname) = @pks; 130 if ($defs->{$colname}{type} eq 'integer') { 131 # with sqlite, simple integer primary keys auto increment. always. 132 $defs->{$colname}{auto} = 1; 133 } 134 } 135 126 136 return $defs; 127 137 } … … 173 183 my ($class) = @_; 174 184 175 my $table_name = $class->table_name;176 my $props = $class->properties;185 my $table_name = $class->table_name; 186 my $props = $class->properties; 177 187 my $field_prefix = $class->datasource; 178 my $indexes = $props->{indexes}; 188 my $indexes = $props->{indexes}; 189 my $pk = $props->{primary_key}; 179 190 180 191 my @stmts; 181 192 if ($indexes) { 182 # FIXME: Handle possible future primary key tuple case183 my $pk = $props->{primary_key};184 193 foreach my $name (keys %$indexes) { 185 194 next if $pk && $name eq $pk; … … 199 208 } 200 209 } 210 if ($pk && 'ARRAY' eq ref $pk) { 211 my @columns = map { join q{_}, $field_prefix, $_ } @$pk; 212 my $columns = join q{, }, @columns; 213 push @stmts, "PRIMARY KEY ($columns)"; 214 } 201 215 if (@stmts) { 202 216 return ',' . join("\n", @stmts); branches/release-42/lib/MT/ObjectDriver/DDL/mysql.pm
r1927 r2881 97 97 $defs->{$colname}{type} = $coltype; 98 98 $defs->{$colname}{auto} = ($row->{Extra} =~ m/auto_increment/i) ? 1 : 0; 99 $defs->{$colname}{key} = $row->{Key} eq 'PRI' ? 1 : 0; 99 100 if (($coltype eq 'string') && $size) { 100 101 $defs->{$colname}{size} = $size;
