[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.46, Thu Jun 8 19:42:06 2006 UTC revision 1.52, Fri Jun 23 23:03:40 2006 UTC
# Line 1391  Line 1391 
1391  would return the number of genomes for the genus I<homo>. It is conceivable, however,  would return the number of genomes for the genus I<homo>. It is conceivable, however,
1392  to use it to return records based on a join. For example,  to use it to return records based on a join. For example,
1393    
1394      my $count = $erdb->GetCount(['Feature', 'Genome'], 'Genome(genus-species) LIKE ?',      my $count = $erdb->GetCount(['HasFeature', 'Genome'], 'Genome(genus-species) LIKE ?',
1395                                  ['homo %']);                                  ['homo %']);
1396    
1397  would return the number of features for genomes in the genus I<homo>. Note that  would return the number of features for genomes in the genus I<homo>. Note that
# Line 1433  Line 1433 
1433      my ($self, $objectNames, $filter, $params) = @_;      my ($self, $objectNames, $filter, $params) = @_;
1434      # Declare the return variable.      # Declare the return variable.
1435      my $retVal;      my $retVal;
1436        # Find out if we're counting an entity or a relationship.
1437        my $countedField;
1438        if ($self->IsEntity($objectNames->[0])) {
1439            $countedField = "id";
1440        } else {
1441            # For a relationship we count the to-link because it's usually more
1442            # numerous. Note we're automatically converting to the SQL form
1443            # of the field name (to_link vs. to-link).
1444            $countedField = "to_link";
1445        }
1446      # Create the SQL command suffix to get the desired records.      # Create the SQL command suffix to get the desired records.
1447      my ($suffix, $mappedNameListRef, $mappedNameHashRef) = $self->_SetupSQL($objectNames,      my ($suffix, $mappedNameListRef, $mappedNameHashRef) = $self->_SetupSQL($objectNames,
1448                                                                              $filter);                                                                              $filter);
1449      # Prefix it with text telling it we want a record count.      # Prefix it with text telling it we want a record count.
1450      my $firstObject = $mappedNameListRef->[0];      my $firstObject = $mappedNameListRef->[0];
1451      my $command = "SELECT COUNT($firstObject.id) $suffix";      my $command = "SELECT COUNT($firstObject.$countedField) $suffix";
1452      # Prepare and execute the command.      # Prepare and execute the command.
1453      my $sth = $self->_GetStatementHandle($command, $params);      my $sth = $self->_GetStatementHandle($command, $params);
1454      # Get the count value.      # Get the count value.
# Line 1534  Line 1544 
1544      }      }
1545  }  }
1546    
1547    =head3 InsertValue
1548    
1549    C<< $erdb->InsertValue($entityID, $fieldName, $value); >>
1550    
1551    This method will insert a new value into the database. The value must be one
1552    associated with a secondary relation, since primary values cannot be inserted:
1553    they occur exactly once. Secondary values, on the other hand, can be missing
1554    or multiply-occurring.
1555    
1556    =over 4
1557    
1558    =item entityID
1559    
1560    ID of the object that is to receive the new value.
1561    
1562    =item fieldName
1563    
1564    Field name for the new value-- this includes the entity name, since
1565    field names are of the format I<objectName>C<(>I<fieldName>C<)>.
1566    
1567    =item value
1568    
1569    New value to be put in the field.
1570    
1571    =back
1572    
1573    =cut
1574    
1575    sub InsertValue {
1576        # Get the parameters.
1577        my ($self, $entityID, $fieldName, $value) = @_;
1578        # Parse the entity name and the real field name.
1579        if ($fieldName =~ /^([^(]+)\(([^)]+)\)/) {
1580            my $entityName = $1;
1581            my $fieldTitle = $2;
1582            # Get its descriptor.
1583            if (!$self->IsEntity($entityName)) {
1584                Confess("$entityName is not a valid entity.");
1585            } else {
1586                my $entityData = $self->{_metaData}->{Entities}->{$entityName};
1587                # Find the relation containing this field.
1588                my $fieldHash = $entityData->{Fields};
1589                if (! exists $fieldHash->{$fieldTitle}) {
1590                    Confess("$fieldTitle not found in $entityName.");
1591                } else {
1592                    my $relation = $fieldHash->{$fieldTitle}->{relation};
1593                    if ($relation eq $entityName) {
1594                        Confess("Cannot do InsertValue on primary field $fieldTitle of $entityName.");
1595                    } else {
1596                        # Now we can create an INSERT statement.
1597                        my $dbh = $self->{_dbh};
1598                        my $fixedName = _FixName($fieldTitle);
1599                        my $statement = "INSERT INTO $relation ($fixedName) VALUES(?, ?)";
1600                        # Execute the command.
1601                        $dbh->SQL($statement, 0, $entityID, $value);
1602                    }
1603                }
1604            }
1605        } else {
1606            Confess("$fieldName is not a valid field name.");
1607        }
1608    }
1609    
1610  =head3 InsertObject  =head3 InsertObject
1611    
1612  C<< my $ok = $erdb->InsertObject($objectType, \%fieldHash); >>  C<< my $ok = $erdb->InsertObject($objectType, \%fieldHash); >>
# Line 2062  Line 2135 
2135      return $objectData->{Fields};      return $objectData->{Fields};
2136  }  }
2137    
2138    =head2 Data Mining Methods
2139    
2140  =head3 GetUsefulCrossValues  =head3 GetUsefulCrossValues
2141    
2142  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>
# Line 2123  Line 2198 
2198      return @retVal;      return @retVal;
2199  }  }
2200    
2201    =head3 FindColumn
2202    
2203    C<< my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); >>
2204    
2205    Return the location a desired column in a data mining header line. The data
2206    mining header line is a tab-separated list of column names. The column
2207    identifier is either the numerical index of a column or the actual column
2208    name.
2209    
2210    =over 4
2211    
2212    =item headerLine
2213    
2214    The header line from a data mining command, which consists of a tab-separated
2215    list of column names.
2216    
2217    =item columnIdentifier
2218    
2219    Either the ordinal number of the desired column (1-based), or the name of the
2220    desired column.
2221    
2222    =item RETURN
2223    
2224    Returns the array index (0-based) of the desired column.
2225    
2226    =back
2227    
2228    =cut
2229    
2230    sub FindColumn {
2231        # Get the parameters.
2232        my ($headerLine, $columnIdentifier) = @_;
2233        # Declare the return variable.
2234        my $retVal;
2235        # Split the header line into column names.
2236        my @headers = ParseColumns($headerLine);
2237        # Determine whether we have a number or a name.
2238        if ($columnIdentifier =~ /^\d+$/) {
2239            # Here we have a number. Subtract 1 and validate the result.
2240            $retVal = $columnIdentifier - 1;
2241            if ($retVal < 0 || $retVal > $#headers) {
2242                Confess("Invalid column identifer \"$columnIdentifier\": value out of range.");
2243            }
2244        } else {
2245            # Here we have a name. We need to find it in the list.
2246            for (my $i = 0; $i <= $#headers && ! defined($retVal); $i++) {
2247                if ($headers[$i] eq $columnIdentifier) {
2248                    $retVal = $i;
2249                }
2250            }
2251            if (! defined($retVal)) {
2252                Confess("Invalid column identifier \"$columnIdentifier\": value not found.");
2253            }
2254        }
2255        # Return the result.
2256        return $retVal;
2257    }
2258    
2259    =head3 ParseColumns
2260    
2261    C<< my @columns = ERDB::ParseColumns($line); >>
2262    
2263    Convert the specified data line to a list of columns.
2264    
2265    =over 4
2266    
2267    =item line
2268    
2269    A data mining input, consisting of a tab-separated list of columns terminated by a
2270    new-line.
2271    
2272    =item RETURN
2273    
2274    Returns a list consisting of the column values.
2275    
2276    =back
2277    
2278    =cut
2279    
2280    sub ParseColumns {
2281        # Get the parameters.
2282        my ($line) = @_;
2283        # Chop off the line-end.
2284        chomp $line;
2285        # Split it into a list.
2286        my @retVal = split(/\t/, $line);
2287        # Return the result.
2288        return @retVal;
2289    }
2290    
2291  =head2 Internal Utility Methods  =head2 Internal Utility Methods
2292    
2293  =head3 SetupSQL  =head3 SetupSQL

Legend:
Removed from v.1.46  
changed lines
  Added in v.1.52

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3