sub bz_alter_column {
my ($self, $table, $name, $new_def) = @_;
+ # You can't change a column to be NOT NULL if you have no DEFAULT,
+ # if there are any NULL values in that column.
+ if ($new_def->{NOTNULL} && !exists $new_def->{DEFAULT}) {
+ # Check for NULLs
+ my $any_nulls = $self->selectrow_array(
+ "SELECT 1 FROM $table WHERE $name IS NULL");
+ if ($any_nulls) {
+ die "You cannot alter the ${table}.${name} column to be NOT NULL"
+ . " without\nspecifying a default, because there are NULL"
+ . " values currently in it.";
+ }
+ }
+
my $current_def = $self->bz_column_info($table, $name);
if (!$self->_bz_schema->columns_equal($current_def, $new_def)) {
}
}
+sub bz_drop_column {
+ my ($self, $table, $column) = @_;
+
+ my $current_def = $self->bz_column_info($table, $column);
+
+ if ($current_def) {
+ my @statements = $self->_bz_real_schema->get_drop_column_ddl(
+ $table, $column);
+ print "Deleting unused column $column from table $table ...\n";
+ foreach my $sql (@statements) {
+ $self->do($sql);
+ }
+ $self->_bz_real_schema->delete_column($table, $column);
+ $self->_bz_store_real_schema;
+ }
+}
+
# XXX - Need to make this cross-db compatible
# XXX - This shouldn't print stuff to stdout
sub bz_drop_field ($$) {
}
}
+sub bz_rename_column {
+ my ($self, $table, $old_name, $new_name) = @_;
+
+ my $old_col_exists = $self->bz_column_info($table, $old_name);
+
+ if ($old_col_exists) {
+ my $already_renamed = $self->bz_column_info($table, $new_name);
+ die "Name conflict: Cannot rename ${table}.${old_name} to"
+ . " ${table}.${new_name},\nbecause ${table}.${new_name}"
+ . " already exists." if $already_renamed;
+ my @statements = $self->_bz_real_schema->get_rename_column_ddl(
+ $table, $old_name, $new_name);
+ print "Changing column $old_name in table $table to"
+ . " be named $new_name...\n";
+ foreach my $sql (@statements) {
+ $self->do($sql);
+ }
+ $self->_bz_real_schema->rename_column($table, $old_name, $new_name);
+ $self->_bz_store_real_schema;
+ }
+}
+
# XXX - Needs to be made cross-db compatible
sub bz_rename_field ($$$) {
my ($self, $table, $field, $newname) = @_;
$dbh->bz_add_column($table, $name, \%definition);
$dbh->bz_add_index($table, $name, $definition);
$dbh->bz_drop_index($table, $name);
+ $dbh->bz_alter_column($table, $name, \%new_def);
+ $dbh->bz_drop_column($table, $column);
+ $dbh->bz_rename_column($table, $old_name, $new_name);
# Schema Modification (DEPRECATED)
$dbh->bz_add_field($table, $column, $definition);
$name - The name of the index that you want to drop.
Returns: nothing
+=item C<bz_alter_column($table, $name, \%new_def)>
+
+ Description: Changes the data type of a column in a table. Prints out
+ the changes being made to stdout. If the new type is the
+ same as the old type, the function returns without changing
+ anything.
+ Params: $table = the table where the column is
+ $name = the name of the column you want to change
+ $new_def = An abstract column definition for the new
+ data type of the columm
+ Returns: nothing
+
+=item C<bz_drop_column($table, $column)>
+
+ Description: Removes a column from a database table. If the column
+ doesn't exist, we return without doing anything. If we do
+ anything, we print a short message to stdout about the change.
+ Params: $table = The table where the column is
+ $column = The name of the column you want to drop
+ Returns: none
+
+=item C<bz_rename_column($table, $old_name, $new_name)>
+
+ Description: Renames a column in a database table. If the C<$old_name>
+ column doesn't exist, we return without doing anything.
+ If C<$old_name> and C<$new_name> both already exist in the
+ table specified, we fail.
+ Params: $table = The table containing the column
+ that you want to rename
+ $old_name = The current name of the column that
+ you want to rename
+ $new_name = The new name of the column
+ Returns: nothing
+
=back
} #eosub--get_type_ddl
#--------------------------------------------------------------------------
-sub get_column_ddl {
+sub get_column {
+=item C<get_column($table, $column)>
-=item C<get_column_ddl>
-
- Description: Public method to generate a DDL segment of a "create table"
- SQL statement for a given table and field.
+ Description: Public method to get the abstract definition of a column.
Parameters: $table - the table name
$column - a column in the table
- Returns: a hash containing information about the column including its
+ Returns: a hashref containing information about the column, including its
type (C<TYPE>), whether or not it can be null (C<NOTNULL>),
- its default value if it has one (C<DEFAULT), whether it is
- a etc. The hash will be empty if either the specified
- table or column does not exist in the database schema.
+ its default value if it has one (C<DEFAULT), etc.
+ Returns undef if the table or column does not exist.
=cut
my($self, $table, $column) = @_;
- my $thash = $self->{schema}{$table};
- return() unless ($thash);
-
- my %fields = @{ $thash->{FIELDS} };
- return() unless ($fields{$column});
- return %{ $fields{$column} };
-
-} #eosub--get_column_ddl
+ # Prevent a possible dereferencing of an undef hash, if the
+ # table doesn't exist.
+ if (exists $self->{schema}->{$table}) {
+ my %fields = (@{ $self->{schema}{$table}{FIELDS} });
+ return $fields{$column};
+ }
+ return undef;
+} #eosub--get_column
#--------------------------------------------------------------------------
sub get_table_list {
sub get_add_column_ddl {
-=item C<get_alter_ddl($table, $column, \%definition)>
+=item C<get_add_column_ddl($table, $column, \%definition)>
Description: Generate SQL to add a column to a table.
Params: $table - The table containing the column.
sub get_alter_column_ddl {
-=item C<get_alter_ddl($table, $column, \%definition)>
+=item C<get_alter_column_ddl($table, $column, \%definition)>
Description: Generate SQL to alter a column in a table.
The column that you are altering must exist,
# OR if we changed the type and we are NOT NULL
if ( (!$old_def->{NOTNULL} && $new_def->{NOTNULL}) ||
($typechange && $new_def->{NOTNULL}) ) {
+ if (exists $new_def->{DEFAULT}) {
+ # Handle any fields that were NULL before, if we have a default.
+ push(@statements, "UPDATE $table SET $column = $default"
+ . " WHERE $column IS NULL");
+ }
push(@statements, "ALTER TABLE $table ALTER COLUMN $column"
- . " SET NOT NULL");
+ . " SET NOT NULL");
}
# If we went from NOT NULL to NULL
elsif ($old_def->{NOTNULL} && !$new_def->{NOTNULL}) {
return ("DROP INDEX $name");
}
+sub get_drop_column_ddl {
+
+=item C<get_drop_column_ddl($table, $column)>
+
+ Description: Generate SQL to drop a column from a table.
+ Params: $table - The table containing the column.
+ $column - The name of the column being dropped.
+ Returns: An array of SQL statements.
+
+=cut
+
+ my ($self, $table, $column) = @_;
+ return ("ALTER TABLE $table DROP COLUMN $column");
+}
+
+sub get_rename_column_ddl {
+
+=item C<get_rename_column_ddl($table, $old_name, $new_name)>
+
+ Description: Generate SQL to change the name of a column in a table.
+ NOTE: ANSI SQL contains no simple way to rename a column,
+ so this function is ABSTRACT and must be implemented
+ by subclasses.
+ Params: $table - The table containing the column to be renamed.
+ $old_name - The name of the column being renamed.
+ $new_name - The name the column is changing to.
+ Returns: An array of SQL statements.
+
+=cut
+
+ die "ANSI SQL has no way to rename a column, and your database driver\n"
+ . " has not implemented a method.";
+}
+
sub get_column_abstract {
=item C<get_column_abstract($table, $column)>
return undef;
}
+sub delete_column {
+
+=item C<delete_column($table, $column)>
+
+ Description: Deletes a column from this Schema object.
+ Params: $table - Name of the table that the column is in.
+ The table must exist, or we will fail.
+ $column - Name of the column to delete.
+ Returns: nothing
+
+=cut
+
+ my ($self, $table, $column) = @_;
+
+ my $abstract_fields = $self->{abstract_schema}{$table}{FIELDS};
+ my $name_position = lsearch($abstract_fields, $column);
+ die "Attempted to delete nonexistent column ${table}.${column}"
+ if $name_position == -1;
+ # Delete the key/value pair from the array.
+ splice(@$abstract_fields, $name_position, 2);
+
+ $self->{schema} = dclone($self->{abstract_schema});
+ $self->_adjust_schema();
+}
+
+sub rename_column {
+
+=item C<rename_column($table, $old_name, $new_name>
+
+ Description: Renames a column on a table in the Schema object.
+ The column that you are renaming must exist.
+ Params: $table - The table the column is on.
+ $old_name - The current name of the column.
+ $new_name - The new name of hte column.
+ Returns: nothing
+
+=cut
+
+ my ($self, $table, $old_name, $new_name) = @_;
+ my $def = $self->get_column_abstract($table, $old_name);
+ die "Renaming a column that doesn't exist" if !$def;
+ $self->delete_column($table, $old_name);
+ $self->set_column($table, $new_name, $def);
+}
+
sub set_column {
=item C<set_column($table, $column, \%new_def)>
my ($self, $table, $column, $new_def) = @_;
- my $fields = \@{ $self->{abstract_schema}{$table}{FIELDS} };
+ my $fields = $self->{abstract_schema}{$table}{FIELDS};
$self->_set_object($table, $column, $new_def, $fields);
}
my ($self, $table, $name, $definition) = @_;
- my $indexes = \@{ $self->{abstract_schema}{$table}{INDEXES} };
+ my $indexes = $self->{abstract_schema}{$table}{INDEXES};
$self->_set_object($table, $name, $definition, $indexes);
}