From 5dea7ec50434d0d9bd1449c5a458d7f2b48e1ffc Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Fri, 4 Jan 2008 00:28:45 +0000 Subject: more work at refactoring of the way attributes are handled: * finished and integrated support for declaring known attributes from within plugins (they need to install a AttrInfo module) * implemented support for checking which attributes are applicable to systems and clients respectively * adjusted slxconfig to new attribute handling (systems only at the moment) git-svn-id: http://svn.openslx.org/svn/openslx/trunk@1442 95ad53e4-c205-0410-b2fa-d234c58c8868 --- config-db/OpenSLX/ConfigDB.pm | 168 +++++++++++++++++++++++++++++++++++------- 1 file changed, 142 insertions(+), 26 deletions(-) (limited to 'config-db/OpenSLX/ConfigDB.pm') diff --git a/config-db/OpenSLX/ConfigDB.pm b/config-db/OpenSLX/ConfigDB.pm index debbef9c..95183c95 100644 --- a/config-db/OpenSLX/ConfigDB.pm +++ b/config-db/OpenSLX/ConfigDB.pm @@ -104,8 +104,6 @@ use OpenSLX::Utils; =item C Returns an object representing a database handle to the config database. -N.B. this class is implemented as a singleton, so several successive calls to -new will return the *same* object =cut @@ -283,11 +281,57 @@ sub getColumnsOfTable my $self = shift; my $tableName = shift; - return + return map { (/^(\w+)\W/) ? $1 : $_; } @{$DbSchema->{tables}->{$tableName}->{cols}}; } +=item C + +Returns the attribute names that apply to systems. + +=over + +=item Return Value + +An array of attribute names. + +=back + +=cut + +sub getKnownSystemAttrs +{ + my $self = shift; + + return + grep { $AttributeInfo{$_}->{"applies_to_systems"} } + keys %AttributeInfo +} + +=item C + +Returns the attribute names that apply to clients. + +=over + +=item Return Value + +An array of attribute names. + +=back + +=cut + +sub getKnownClientAttrs +{ + my $self = shift; + + return + grep { $AttributeInfo{$_}->{"applies_to_clients"} } + keys %AttributeInfo +} + =item C Fetches and returns information about all vendor-OSes that match the given @@ -318,8 +362,8 @@ sub fetchVendorOSByFilter my $filter = shift; my $resultCols = shift; - my @vendorOS = - $self->{'meta-db'}->fetchVendorOSByFilter($filter, $resultCols); + my @vendorOS + = $self->{'meta-db'}->fetchVendorOSByFilter($filter, $resultCols); return wantarray() ? @vendorOS : shift @vendorOS; } @@ -493,6 +537,11 @@ is no filtering. See L for more info. A comma-separated list of colunm names that shall be returned. If not defined, all available data must be returned. +=item Param C<$attrFilter> [Optional] + +A hash-ref containing the filter criteria that shall be applied against +attributes. + =item Return Value An array of hash-refs containing the resulting data rows. @@ -506,19 +555,11 @@ sub fetchSystemByFilter my $self = shift; my $filter = shift; my $resultCols = shift; + my $attrFilter = shift; - my @systems = $self->{'meta-db'}->fetchSystemByFilter($filter, $resultCols); - - # unless specific result cols have been given, we mix in the attributes - # of each system, too: - if (!defined $resultCols) { - foreach my $system (@systems) { - my @attrs = $self->{'meta-db'}->fetchSystemAttrs($system->{id}); - foreach my $attr (@attrs) { - $system->{"attr_$attr->{name}"} = $attr->{value}; - } - } - } + my @systems = $self->{'meta-db'}->fetchSystemByFilter( + $filter, $resultCols, $attrFilter + ); return wantarray() ? @systems : shift @systems; } @@ -555,18 +596,91 @@ 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) { - my @attrs = $self->{'meta-db'}->fetchSystemAttrs($system->{id}); - foreach my $attr (@attrs) { - $system->{"attr_$attr->{name}"} = $attr->{value}; - } - } - } +# 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. @@ -2702,6 +2816,7 @@ sub _checkAndUpgradeDBSchemaIfNecessary $DbSchema->{tables}->{$tableName}->{vals} ); } + $metaDB->schemaSetDBVersion($DbSchema->{version}); vlog(1, _tr('DB has been created successfully')); } elsif ($currVersion < $DbSchema->{version}) { vlog( @@ -2759,6 +2874,7 @@ sub _checkAndUpgradeDBSchemaIfNecessary } } } + $metaDB->schemaSetDBVersion($DbSchema->{version}); vlog(1, _tr('upgrade done')); } else { vlog(1, _tr('DB matches current schema version (%s)', $currVersion)); @@ -2770,6 +2886,7 @@ sub _checkAndUpgradeDBSchemaIfNecessary sub _aref { # transparently converts the given reference to an array-ref my $ref = shift; + return [] unless defined $ref; $ref = [$ref] unless ref($ref) eq 'ARRAY'; @@ -2779,7 +2896,6 @@ sub _aref sub _unique { # return given array filtered to unique elements my %seenIDs; - return grep { !$seenIDs{$_}++; } @_; } -- cgit v1.2.3-55-g7522