From 9507d838668846666d2c790a0a1e6e51eb8670de Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Fri, 4 Jan 2008 16:50:15 +0000 Subject: more refactoring (not limited to attribute handling) * completed separation of attributes and standard (column) values * improved tests a lot That work is still not finished, so expect things to fail horribly. git-svn-id: http://svn.openslx.org/svn/openslx/trunk@1443 95ad53e4-c205-0410-b2fa-d234c58c8868 --- config-db/OpenSLX/ConfigDB.pm | 301 ++++++++++++++++++++++-------------------- 1 file changed, 157 insertions(+), 144 deletions(-) (limited to 'config-db/OpenSLX/ConfigDB.pm') diff --git a/config-db/OpenSLX/ConfigDB.pm b/config-db/OpenSLX/ConfigDB.pm index 95183c95..289f6f7f 100644 --- a/config-db/OpenSLX/ConfigDB.pm +++ b/config-db/OpenSLX/ConfigDB.pm @@ -561,6 +561,15 @@ sub fetchSystemByFilter $filter, $resultCols, $attrFilter ); + # unless specific result cols have been given, we mix in the attributes + # of each system, too: + if (!defined $resultCols) { + foreach my $system (@systems) { + $system->{attrs} + = $self->{'meta-db'}->fetchSystemAttrs($system->{id}); + } + } + return wantarray() ? @systems : shift @systems; } @@ -596,91 +605,16 @@ sub fetchSystemByID # unless specific result cols have been given, we mix in the attributes # of each system, too: -# if (!defined $resultCols) { -# foreach my $system (@systems) { -# $system->{attrs} -# = $self->{'meta-db'}->fetchSystemAttrs($system->{id}); -# } -# } + if (!defined $resultCols) { + foreach my $system (@systems) { + $system->{attrs} + = $self->{'meta-db'}->fetchSystemAttrs($system->{id}); + } + } return wantarray() ? @systems : shift @systems; } -=item C - -Fetches and returns the information about the attributes of the system -with the given ID. - -=over - -=item Param C<$systemID> - -The ID of the system whose attributes you are interested in. - -=item Param C<$attrNames> [Optional] - -A comma-separated list of attribute names that shall be returned. If not -defined,all available attributes will be returned. - -=item Return Value - -An array of hash-refs containing the resulting data rows (or a single hash-ref -mapping all attribute names to the respective value). - -=back - -=cut - -sub fetchSystemAttrs -{ - my $self = shift; - my $systemID = shift; - my $attrNames = shift; - - my @attrs = $self->{'meta-db'}->fetchSystemAttrs($systemID, $attrNames); - - return wantarray() ? @attrs : shift @attrs; -} - -=item C - -Fetches and returns the information about the attributes of the system -with the given ID. - -=over - -=item Param C<$systemID> - -The ID of the system whose attributes you are interested in. - -=item Param C<$attrNames> [Optional] - -A comma-separated list of attribute names that shall be returned. If not -defined,all available attributes will be returned. - -=item Return Value - -A single hash-ref mapping all attribute names to their respective value. - -=back - -=cut - -sub fetchSystemAttrsAsHash -{ - my $self = shift; - my $systemID = shift; - my $attrNames = shift; - - my @attrs = $self->{'meta-db'}->fetchSystemAttrs($systemID, $attrNames); - - my $Result = {}; - foreach my $attr (@attrs) { - $Result->{$attr->{name}} = $attr->{value}; - } - return $Result; -} - =item C Fetches the IDs of all systems that make use of the export with the given ID. @@ -787,10 +721,23 @@ An array of hash-refs containing the resulting data rows. sub fetchClientByFilter { - my $self = shift; - my $filter = shift; + my $self = shift; + my $filter = shift; + my $resultCols = shift; + my $attrFilter = shift; - my @clients = $self->{'meta-db'}->fetchClientByFilter($filter); + my @clients = $self->{'meta-db'}->fetchClientByFilter( + $filter, $resultCols, $attrFilter + ); + + # unless specific result cols have been given, we mix in the attributes + # of each client, too: + if (!defined $resultCols) { + foreach my $client (@clients) { + $client->{attrs} + = $self->{'meta-db'}->fetchClientAttrs($client->{id}); + } + } return wantarray() ? @clients : shift @clients; } @@ -825,6 +772,15 @@ sub fetchClientByID my @clients = $self->{'meta-db'}->fetchClientByID($ids, $resultCols); + # unless specific result cols have been given, we mix in the attributes + # of each client, too: + if (!defined $resultCols) { + foreach my $client (@clients) { + $client->{attrs} + = $self->{'meta-db'}->fetchClientAttrs($client->{id}); + } + } + return wantarray() ? @clients : shift @clients; } @@ -911,8 +867,20 @@ sub fetchGroupByFilter my $self = shift; my $filter = shift; my $resultCols = shift; + my $attrFilter = shift; - my @groups = $self->{'meta-db'}->fetchGroupByFilter($filter, $resultCols); + my @groups = $self->{'meta-db'}->fetchGroupByFilter( + $filter, $resultCols, $attrFilter + ); + + # unless specific result cols have been given, we mix in the attributes + # of each group, too: + if (!defined $resultCols) { + foreach my $group (@groups) { + $group->{attrs} + = $self->{'meta-db'}->fetchGroupAttrs($group->{id}); + } + } return wantarray() ? @groups : shift @groups; } @@ -947,6 +915,15 @@ sub fetchGroupByID my @groups = $self->{'meta-db'}->fetchGroupByID($ids, $resultCols); + # unless specific result cols have been given, we mix in the attributes + # of each group, too: + if (!defined $resultCols) { + foreach my $group (@groups) { + $group->{attrs} + = $self->{'meta-db'}->fetchGroupAttrs($group->{id}); + } + } + return wantarray() ? @groups : shift @groups; } @@ -1707,6 +1684,43 @@ sub changeClient return $self->{'meta-db'}->changeClient($clientIDs, $valRows); } +=item C + +Sets a value for an attribute of the given client. If the client already +has a value for this attribute, it will be overwritten. + +=over + +=item Param C + +The ID of the client whose attribute shall be changed. + +=item Param C + +The name of the attribute to change. + +=item Param C + +The new value for the attribute. + +=item Return Value + +C<1> if the attribute could be set, C if not. + +=back + +=cut + +sub setClientAttr +{ + my $self = shift; + my $clientID = shift; + my $attrName = shift; + my $attrValue = shift; + + return $self->{'meta-db'}->setClientAttr($clientID, $attrName, $attrValue); +} + =item C Specifies all systems that should be offered for booting by the given client. @@ -1987,6 +2001,43 @@ sub removeGroup return $self->{'meta-db'}->removeGroup($groupIDs); } +=item C + +Sets a value for an attribute of the given group. If the group already +has a value for this attribute, it will be overwritten. + +=over + +=item Param C + +The ID of the group whose attribute shall be changed. + +=item Param C + +The name of the attribute to change. + +=item Param C + +The new value for the attribute. + +=item Return Value + +C<1> if the attribute could be set, C if not. + +=back + +=cut + +sub setGroupAttr +{ + my $self = shift; + my $groupID = shift; + my $attrName = shift; + my $attrValue = shift; + + return $self->{'meta-db'}->setGroupAttr($groupID, $attrName, $attrValue); +} + =item C Changes the data of one or more groups. @@ -2588,13 +2639,17 @@ sub mergeAttributes my $target = shift; my $source = shift; - foreach my $key (keys %$source) { - next if !isAttribute($key); - my $sourceVal = $source->{$key} || ''; - my $targetVal = $target->{$key} || ''; - if (length($sourceVal) && !length($targetVal)) { - vlog(3, _tr("merging %s (val=%s)", $key, $sourceVal)); - $target->{$key} = $sourceVal; + my $sourceAttrs = $source->{attrs} || {}; + + $target->{attrs} ||= {}; + my $targetAttrs = $target->{attrs}; + + foreach my $key (keys %$sourceAttrs) { + my $sourceVal = $sourceAttrs->{$key}; + my $targetVal = $targetAttrs->{$key}; + if (defined $sourceVal && !defined $targetVal) { + vlog(0, _tr("merging %s (val=%s)", $key, $sourceVal)); + $targetAttrs->{$key} = $sourceVal; } } @@ -2628,11 +2683,16 @@ sub pushAttributes my $target = shift; my $source = shift; - foreach my $key (grep { isAttribute($_) } keys %$source) { - my $sourceVal = $source->{$key} || ''; - if (length($sourceVal)) { + my $sourceAttrs = $source->{attrs} || {}; + + $target->{attrs} ||= {}; + my $targetAttrs = $target->{attrs}; + + foreach my $key (keys %$sourceAttrs) { + my $sourceVal = $sourceAttrs->{$key}; + if (defined $sourceVal) { vlog(3, _tr("pushing %s (val=%s)", $key, $sourceVal)); - $target->{$key} = $sourceVal; + $targetAttrs->{$key} = $sourceVal; } } @@ -2826,54 +2886,7 @@ sub _checkAndUpgradeDBSchemaIfNecessary $DbSchema->{version}, $currVersion ) ); - foreach my $v (sort { $a <=> $b } keys %DbSchemaHistory) { - next if $v <= $currVersion; - my $changeSet = $DbSchemaHistory{$v}; - foreach my $c (0 .. scalar(@$changeSet) - 1) { - my $changeDescr = @{$changeSet}[$c]; - my $cmd = $changeDescr->{cmd}; - if ($cmd eq 'add-table') { - $metaDB->schemaAddTable( - $changeDescr->{'table'}, - $changeDescr->{'cols'}, - $changeDescr->{'vals'} - ); - } elsif ($cmd eq 'drop-table') { - $metaDB->schemaDropTable($changeDescr->{'table'}); - } elsif ($cmd eq 'rename-table') { - $metaDB->schemaRenameTable( - $changeDescr->{'old-table'}, - $changeDescr->{'new-table'}, - $changeDescr->{'cols'} - ); - } elsif ($cmd eq 'add-columns') { - $metaDB->schemaAddColumns( - $changeDescr->{'table'}, - $changeDescr->{'new-cols'}, - $changeDescr->{'new-default-vals'}, - $changeDescr->{'cols'} - ); - } elsif ($cmd eq 'drop-columns') { - $metaDB->schemaDropColumns( - $changeDescr->{'table'}, - $changeDescr->{'drop-cols'}, - $changeDescr->{'cols'} - ); - } elsif ($cmd eq 'rename-columns') { - $metaDB->schemaRenameColumns( - $changeDescr->{'table'}, - $changeDescr->{'col-renames'}, - $changeDescr->{'cols'} - ); - } elsif ($cmd eq 'perl-func') { - if (!$changeDescr->{code}->($self)) { - croak _tr('problem in perl-func cmd!'); - } - } else { - croak _tr('UnknownDbSchemaCommand', $cmd); - } - } - } + $metaDB->schemaUpgradeDBFrom($currVersion); $metaDB->schemaSetDBVersion($DbSchema->{version}); vlog(1, _tr('upgrade done')); } else { -- cgit v1.2.3-55-g7522