From 77691d57c478404d33235d45cb94156efd3a95f2 Mon Sep 17 00:00:00 2001 From: "mkanat%kerio.com" <> Date: Sun, 17 Apr 2005 14:22:41 +0000 Subject: Bug 290402: Functions to support reading-in a Schema object from the database Patch by Max Kanat-Alexander r=Tomas.Kopal, a=justdave --- Bugzilla/DB/Schema.pm | 84 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 5 deletions(-) (limited to 'Bugzilla/DB/Schema.pm') diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index b151edf91..c65d1f0f2 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -897,7 +897,8 @@ use constant ABSTRACT_SCHEMA => { whine_queries => { FIELDS => [ - id => {TYPE => 'MEDIUMSERIAL', PRIMARYKEY => 1}, + id => {TYPE => 'MEDIUMSERIAL', PRIMARYKEY => 1, + NOTNULL => 1}, eventid => {TYPE => 'INT3', NOTNULL => 1}, query_name => {TYPE => 'varchar(64)', NOTNULL => 1, DEFAULT => "''"}, @@ -914,7 +915,8 @@ use constant ABSTRACT_SCHEMA => { whine_schedules => { FIELDS => [ - id => {TYPE => 'MEDIUMSERIAL', PRIMARYKEY => 1}, + id => {TYPE => 'MEDIUMSERIAL', PRIMARYKEY => 1, + NOTNULL => 1}, eventid => {TYPE => 'INT3', NOTNULL => 1}, run_day => {TYPE => 'varchar(32)'}, run_time => {TYPE => 'varchar(32)'}, @@ -930,7 +932,8 @@ use constant ABSTRACT_SCHEMA => { whine_events => { FIELDS => [ - id => {TYPE => 'MEDIUMSERIAL', PRIMARYKEY => 1}, + id => {TYPE => 'MEDIUMSERIAL', PRIMARYKEY => 1, + NOTNULL => 1}, owner_userid => {TYPE => 'INT3', NOTNULL => 1}, subject => {TYPE => 'varchar(128)'}, body => {TYPE => 'MEDIUMTEXT'}, @@ -1211,7 +1214,7 @@ sub get_column { } return undef; } #eosub--get_column -#-------------------------------------------------------------------------- + sub get_table_list { =item C @@ -1584,7 +1587,7 @@ sub get_column_abstract { # Prevent a possible dereferencing of an undef hash, if the # table doesn't exist. - if (exists $self->{abstract_schema}->{$table}) { + if ($self->get_table_abstract($table)) { my %fields = (@{ $self->{abstract_schema}{$table}{FIELDS} }); return $fields{$column}; } @@ -1616,6 +1619,47 @@ sub get_index_abstract { return undef; } +=item C + + Description: Gets the abstract definition for a table in this Schema + object. + Params: $table - The name of the table you want a definition for. + Returns: An abstract table definition, or undef if the table doesn't + exist. + +=cut + +sub get_table_abstract { + my ($self, $table) = @_; + return $self->{abstract_schema}->{$table}; +} + +=item C + + Description: Creates a new table in this Schema object. + If you do not specify a definition, we will + simply create an empty table. + Params: $name - The name for the new table. + \%definition (optional) - An abstract definition for + the new table. + Returns: nothing + +=cut +sub add_table { + my ($self, $name, $definition) = @_; + (die "Table already exists: $name") + if exists $self->{abstract_schema}->{$name}; + if ($definition) { + $self->{abstract_schema}->{$name} = dclone($definition); + $self->{schema} = dclone($self->{abstract_schema}); + $self->_adjust_schema(); + } + else { + $self->{abstract_schema}->{$name} = {FIELDS => []}; + $self->{schema}->{$name} = {FIELDS => []}; + } +} + sub delete_column { =item C @@ -1705,6 +1749,11 @@ sub set_index { my ($self, $table, $name, $definition) = @_; + if ( exists $self->{abstract_schema}{$table} + && !exists $self->{abstract_schema}{$table}{INDEXES} ) { + $self->{abstract_schema}{$table}{INDEXES} = []; + } + my $indexes = $self->{abstract_schema}{$table}{INDEXES}; $self->_set_object($table, $name, $definition, $indexes); } @@ -1839,6 +1888,31 @@ sub deserialize_abstract { return $class->new(undef, $thawed_hash); } +##################################################################### +# Class Methods +##################################################################### + +=back + +=head1 CLASS METHODS + +These methods are generally called on the class instead of on a specific +object. + +=item C + + Description: Returns a Schema that has no tables. In effect, this + Schema is totally "empty." + Params: none + Returns: A "empty" Schema object. + +=cut + +sub get_empty_schema { + my ($class) = @_; + return $class->deserialize_abstract(freeze({})); +} + 1; __END__ -- cgit v1.2.3-24-g4f1b