[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.55, Sat Jun 24 23:49:10 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 (id, $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 1991  Line 2064 
2064          push @retVal, \@rowData;          push @retVal, \@rowData;
2065          $fetched++;          $fetched++;
2066      }      }
2067        Trace("$fetched rows returned in GetAll.") if T(SQL => 4);
2068      # Return the resulting list.      # Return the resulting list.
2069      return @retVal;      return @retVal;
2070  }  }
2071    
2072    =head3 Exists
2073    
2074    C<< my $found = $sprout->Exists($entityName, $entityID); >>
2075    
2076    Return TRUE if an entity exists, else FALSE.
2077    
2078    =over 4
2079    
2080    =item entityName
2081    
2082    Name of the entity type (e.g. C<Feature>) relevant to the existence check.
2083    
2084    =item entityID
2085    
2086    ID of the entity instance whose existence is to be checked.
2087    
2088    =item RETURN
2089    
2090    Returns TRUE if the entity instance exists, else FALSE.
2091    
2092    =back
2093    
2094    =cut
2095    #: Return Type $;
2096    sub Exists {
2097        # Get the parameters.
2098        my ($self, $entityName, $entityID) = @_;
2099        # Check for the entity instance.
2100        Trace("Checking existence of $entityName with ID=$entityID.") if T(4);
2101        my $testInstance = $self->GetEntity($entityName, $entityID);
2102        # Return an existence indicator.
2103        my $retVal = ($testInstance ? 1 : 0);
2104        return $retVal;
2105    }
2106    
2107  =head3 EstimateRowSize  =head3 EstimateRowSize
2108    
2109  C<< my $rowSize = $erdb->EstimateRowSize($relName); >>  C<< my $rowSize = $erdb->EstimateRowSize($relName); >>
# Line 2062  Line 2171 
2171      return $objectData->{Fields};      return $objectData->{Fields};
2172  }  }
2173    
2174    =head2 Data Mining Methods
2175    
2176  =head3 GetUsefulCrossValues  =head3 GetUsefulCrossValues
2177    
2178  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>
# Line 2123  Line 2234 
2234      return @retVal;      return @retVal;
2235  }  }
2236    
2237    =head3 FindColumn
2238    
2239    C<< my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); >>
2240    
2241    Return the location a desired column in a data mining header line. The data
2242    mining header line is a tab-separated list of column names. The column
2243    identifier is either the numerical index of a column or the actual column
2244    name.
2245    
2246    =over 4
2247    
2248    =item headerLine
2249    
2250    The header line from a data mining command, which consists of a tab-separated
2251    list of column names.
2252    
2253    =item columnIdentifier
2254    
2255    Either the ordinal number of the desired column (1-based), or the name of the
2256    desired column.
2257    
2258    =item RETURN
2259    
2260    Returns the array index (0-based) of the desired column.
2261    
2262    =back
2263    
2264    =cut
2265    
2266    sub FindColumn {
2267        # Get the parameters.
2268        my ($headerLine, $columnIdentifier) = @_;
2269        # Declare the return variable.
2270        my $retVal;
2271        # Split the header line into column names.
2272        my @headers = ParseColumns($headerLine);
2273        # Determine whether we have a number or a name.
2274        if ($columnIdentifier =~ /^\d+$/) {
2275            # Here we have a number. Subtract 1 and validate the result.
2276            $retVal = $columnIdentifier - 1;
2277            if ($retVal < 0 || $retVal > $#headers) {
2278                Confess("Invalid column identifer \"$columnIdentifier\": value out of range.");
2279            }
2280        } else {
2281            # Here we have a name. We need to find it in the list.
2282            for (my $i = 0; $i <= $#headers && ! defined($retVal); $i++) {
2283                if ($headers[$i] eq $columnIdentifier) {
2284                    $retVal = $i;
2285                }
2286            }
2287            if (! defined($retVal)) {
2288                Confess("Invalid column identifier \"$columnIdentifier\": value not found.");
2289            }
2290        }
2291        # Return the result.
2292        return $retVal;
2293    }
2294    
2295    =head3 ParseColumns
2296    
2297    C<< my @columns = ERDB::ParseColumns($line); >>
2298    
2299    Convert the specified data line to a list of columns.
2300    
2301    =over 4
2302    
2303    =item line
2304    
2305    A data mining input, consisting of a tab-separated list of columns terminated by a
2306    new-line.
2307    
2308    =item RETURN
2309    
2310    Returns a list consisting of the column values.
2311    
2312    =back
2313    
2314    =cut
2315    
2316    sub ParseColumns {
2317        # Get the parameters.
2318        my ($line) = @_;
2319        # Chop off the line-end.
2320        chomp $line;
2321        # Split it into a list.
2322        my @retVal = split(/\t/, $line);
2323        # Return the result.
2324        return @retVal;
2325    }
2326    
2327  =head2 Internal Utility Methods  =head2 Internal Utility Methods
2328    
2329  =head3 SetupSQL  =head3 SetupSQL

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3