[Bio] / Sprout / ERDB.pm Repository:
ViewVC logotype

Diff of /Sprout/ERDB.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.65, Tue Jul 11 06:10:41 2006 UTC revision 1.69, Fri Sep 29 15:06:17 2006 UTC
# Line 1436  Line 1436 
1436  sub GetCount {  sub GetCount {
1437      # Get the parameters.      # Get the parameters.
1438      my ($self, $objectNames, $filter, $params) = @_;      my ($self, $objectNames, $filter, $params) = @_;
1439        # Insure the params argument is an array reference if the caller left it off.
1440        if (! defined($params)) {
1441            $params = [];
1442        }
1443      # Declare the return variable.      # Declare the return variable.
1444      my $retVal;      my $retVal;
1445      # Find out if we're counting an entity or a relationship.      # Find out if we're counting an entity or a relationship.
# Line 1939  Line 1943 
1943      return $retVal;      return $retVal;
1944  }  }
1945    
1946    =head3 GetChoices
1947    
1948    C<< my @values = $erdb->GetChoices($entityName, $fieldName); >>
1949    
1950    Return a list of all the values for the specified field that are represented in the
1951    specified entity.
1952    
1953    Note that if the field is not indexed, then this will be a very slow operation.
1954    
1955    =over 4
1956    
1957    =item entityName
1958    
1959    Name of an entity in the database.
1960    
1961    =item fieldName
1962    
1963    Name of a field belonging to the entity. This is a raw field name without
1964    the standard parenthesized notation used in most calls.
1965    
1966    =item RETURN
1967    
1968    Returns a list of the distinct values for the specified field in the database.
1969    
1970    =back
1971    
1972    =cut
1973    
1974    sub GetChoices {
1975        # Get the parameters.
1976        my ($self, $entityName, $fieldName) = @_;
1977        # Declare the return variable.
1978        my @retVal;
1979        # Get the entity data structure.
1980        my $entityData = $self->_GetStructure($entityName);
1981        # Get the field.
1982        my $fieldHash = $entityData->{Fields};
1983        if (! exists $fieldHash->{$fieldName}) {
1984            Confess("$fieldName not found in $entityName.");
1985        } else {
1986            # Get the name of the relation containing the field.
1987            my $relation = $fieldHash->{$fieldName}->{relation};
1988            # Fix up the field name.
1989            my $realName = _FixName($fieldName);
1990            # Get the database handle.
1991            my $dbh = $self->{_dbh};
1992            # Query the database.
1993            my $results = $dbh->SQL("SELECT DISTINCT $realName FROM $relation");
1994            # Clean the results. They are stored as a list of lists, and we just want the one list.
1995            @retVal = sort map { $_->[0] } @{$results};
1996        }
1997        # Return the result.
1998        return @retVal;
1999    }
2000    
2001  =head3 GetEntityValues  =head3 GetEntityValues
2002    
2003  C<< my @values = $erdb->GetEntityValues($entityType, $ID, \@fields); >>  C<< my @values = $erdb->GetEntityValues($entityType, $ID, \@fields); >>
2004    
2005  Return a list of values from a specified entity instance.  Return a list of values from a specified entity instance. If the entity instance
2006    does not exist, an empty list is returned.
2007    
2008  =over 4  =over 4
2009    
# Line 3246  Line 3306 
3306  relation when creating the load file.  relation when creating the load file.
3307    
3308  For example, if the load file should be sorted ascending by the first  For example, if the load file should be sorted ascending by the first
3309  field with duplicates removed, this method would return  field, this method would return
3310    
3311      sort -k 1 -u -t "\t"      sort -k1 -t"\t"
3312    
3313  If the first field is numeric and duplicates are okay, the method would  If the first field is numeric, the method would return
 return  
3314    
3315      sort -k 1n -t "\t"      sort -k 1n -t "\t"
3316    
3317    Unfortunately, due to a bug in the C<sort> command, we cannot eliminate duplicate
3318    keys using a sort.
3319    
3320  =over 4  =over 4
3321    
3322  =item relationName  =item relationName
# Line 3274  Line 3336 
3336      my ($self, $relationName) = @_;      my ($self, $relationName) = @_;
3337      # Declare a descriptor to hold the names of the key fields.      # Declare a descriptor to hold the names of the key fields.
3338      my @keyNames = ();      my @keyNames = ();
     # Declare a flag for indicating uniqueness.  
     my $unique;  
3339      # Get the relation structure.      # Get the relation structure.
3340      my $relationData = $self->_FindRelation($relationName);      my $relationData = $self->_FindRelation($relationName);
3341      # Find out if the relation is a primary entity relation,      # Find out if the relation is a primary entity relation,
# Line 3283  Line 3343 
3343      my $entityTable = $self->{_metaData}->{Entities};      my $entityTable = $self->{_metaData}->{Entities};
3344      my $relationshipTable = $self->{_metaData}->{Relationships};      my $relationshipTable = $self->{_metaData}->{Relationships};
3345      if (exists $entityTable->{$relationName}) {      if (exists $entityTable->{$relationName}) {
3346          # Here we have a primary entity relation, so we have a unique sort on the          # Here we have a primary entity relation.
         # ID field.  
         $unique = "-u ";  
3347          push @keyNames, "id";          push @keyNames, "id";
3348      } elsif (exists $relationshipTable->{$relationName}) {      } elsif (exists $relationshipTable->{$relationName}) {
3349          # Here we have a relationship. We sort using the FROM index.          # Here we have a relationship. We sort using the FROM index.
         $unique = "";  
3350          my $relationshipData = $relationshipTable->{$relationName};          my $relationshipData = $relationshipTable->{$relationName};
3351          my $index = $relationData->{Indexes}->{"idx${relationName}From"};          my $index = $relationData->{Indexes}->{"idx${relationName}From"};
3352          push @keyNames, @{$index->{IndexFields}};          push @keyNames, @{$index->{IndexFields}};
3353      } else {      } else {
3354          # Here we have a secondary entity relation, so we have a non-unique sort on          # Here we have a secondary entity relation, so we have a sort on the ID field.
         # the ID field.  
         $unique = "";  
3355          push @keyNames, "id";          push @keyNames, "id";
3356      }      }
3357      # Now we parse the key names into sort parameters. First, we prime the return      # Now we parse the key names into sort parameters. First, we prime the return
3358      # string.      # string.
3359      my $retVal = "sort -t \"\t\" $unique";      my $retVal = "sort -t\"\t\" ";
3360      # Get the relation's field list.      # Get the relation's field list.
3361      my @fields = @{$relationData->{Fields}};      my @fields = @{$relationData->{Fields}};
3362      # Loop through the keys.      # Loop through the keys.

Legend:
Removed from v.1.65  
changed lines
  Added in v.1.69

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3