[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.47, Sun Jun 18 05:14:56 2006 UTC revision 1.59, Fri Jul 7 00:24:50 2006 UTC
# Line 1544  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 1560  Line 1623 
1623  The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and  The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and
1624  property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.  property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.
1625    
1626  C<< $erdb->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence = 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>  C<< $erdb->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>
1627    
1628  =over 4  =over 4
1629    
# Line 1771  Line 1834 
1834      }      }
1835      # Analyze the table to improve performance.      # Analyze the table to improve performance.
1836      $dbh->vacuum_it($relationName);      $dbh->vacuum_it($relationName);
1837        # Flush the database cache.
1838        $dbh->flush_tables();
1839      # Return the statistics.      # Return the statistics.
1840      return $retVal;      return $retVal;
1841  }  }
# Line 2001  Line 2066 
2066          push @retVal, \@rowData;          push @retVal, \@rowData;
2067          $fetched++;          $fetched++;
2068      }      }
2069        Trace("$fetched rows returned in GetAll.") if T(SQL => 4);
2070      # Return the resulting list.      # Return the resulting list.
2071      return @retVal;      return @retVal;
2072  }  }
2073    
2074    =head3 Exists
2075    
2076    C<< my $found = $sprout->Exists($entityName, $entityID); >>
2077    
2078    Return TRUE if an entity exists, else FALSE.
2079    
2080    =over 4
2081    
2082    =item entityName
2083    
2084    Name of the entity type (e.g. C<Feature>) relevant to the existence check.
2085    
2086    =item entityID
2087    
2088    ID of the entity instance whose existence is to be checked.
2089    
2090    =item RETURN
2091    
2092    Returns TRUE if the entity instance exists, else FALSE.
2093    
2094    =back
2095    
2096    =cut
2097    #: Return Type $;
2098    sub Exists {
2099        # Get the parameters.
2100        my ($self, $entityName, $entityID) = @_;
2101        # Check for the entity instance.
2102        Trace("Checking existence of $entityName with ID=$entityID.") if T(4);
2103        my $testInstance = $self->GetEntity($entityName, $entityID);
2104        # Return an existence indicator.
2105        my $retVal = ($testInstance ? 1 : 0);
2106        return $retVal;
2107    }
2108    
2109  =head3 EstimateRowSize  =head3 EstimateRowSize
2110    
2111  C<< my $rowSize = $erdb->EstimateRowSize($relName); >>  C<< my $rowSize = $erdb->EstimateRowSize($relName); >>
# Line 2072  Line 2173 
2173      return $objectData->{Fields};      return $objectData->{Fields};
2174  }  }
2175    
2176    =head2 Data Mining Methods
2177    
2178  =head3 GetUsefulCrossValues  =head3 GetUsefulCrossValues
2179    
2180  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>
# Line 2133  Line 2236 
2236      return @retVal;      return @retVal;
2237  }  }
2238    
2239    =head3 FindColumn
2240    
2241    C<< my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); >>
2242    
2243    Return the location a desired column in a data mining header line. The data
2244    mining header line is a tab-separated list of column names. The column
2245    identifier is either the numerical index of a column or the actual column
2246    name.
2247    
2248    =over 4
2249    
2250    =item headerLine
2251    
2252    The header line from a data mining command, which consists of a tab-separated
2253    list of column names.
2254    
2255    =item columnIdentifier
2256    
2257    Either the ordinal number of the desired column (1-based), or the name of the
2258    desired column.
2259    
2260    =item RETURN
2261    
2262    Returns the array index (0-based) of the desired column.
2263    
2264    =back
2265    
2266    =cut
2267    
2268    sub FindColumn {
2269        # Get the parameters.
2270        my ($headerLine, $columnIdentifier) = @_;
2271        # Declare the return variable.
2272        my $retVal;
2273        # Split the header line into column names.
2274        my @headers = ParseColumns($headerLine);
2275        # Determine whether we have a number or a name.
2276        if ($columnIdentifier =~ /^\d+$/) {
2277            # Here we have a number. Subtract 1 and validate the result.
2278            $retVal = $columnIdentifier - 1;
2279            if ($retVal < 0 || $retVal > $#headers) {
2280                Confess("Invalid column identifer \"$columnIdentifier\": value out of range.");
2281            }
2282        } else {
2283            # Here we have a name. We need to find it in the list.
2284            for (my $i = 0; $i <= $#headers && ! defined($retVal); $i++) {
2285                if ($headers[$i] eq $columnIdentifier) {
2286                    $retVal = $i;
2287                }
2288            }
2289            if (! defined($retVal)) {
2290                Confess("Invalid column identifier \"$columnIdentifier\": value not found.");
2291            }
2292        }
2293        # Return the result.
2294        return $retVal;
2295    }
2296    
2297    =head3 ParseColumns
2298    
2299    C<< my @columns = ERDB::ParseColumns($line); >>
2300    
2301    Convert the specified data line to a list of columns.
2302    
2303    =over 4
2304    
2305    =item line
2306    
2307    A data mining input, consisting of a tab-separated list of columns terminated by a
2308    new-line.
2309    
2310    =item RETURN
2311    
2312    Returns a list consisting of the column values.
2313    
2314    =back
2315    
2316    =cut
2317    
2318    sub ParseColumns {
2319        # Get the parameters.
2320        my ($line) = @_;
2321        # Chop off the line-end.
2322        chomp $line;
2323        # Split it into a list.
2324        my @retVal = split(/\t/, $line);
2325        # Return the result.
2326        return @retVal;
2327    }
2328    
2329  =head2 Internal Utility Methods  =head2 Internal Utility Methods
2330    
2331  =head3 SetupSQL  =head3 SetupSQL
# Line 3069  Line 3262 
3262      # Find out if the relation is a primary entity relation.      # Find out if the relation is a primary entity relation.
3263      my $entityTable = $self->{_metaData}->{Entities};      my $entityTable = $self->{_metaData}->{Entities};
3264      if (exists $entityTable->{$relationName}) {      if (exists $entityTable->{$relationName}) {
         my $keyType = $entityTable->{$relationName}->{keyType};  
         Trace("Relation $relationName found in entity table with key type $keyType.") if T(3);  
         # If the key is not a hash string, we must do the sort.  
         if ($keyType ne 'hash-string') {  
3265              $retVal = 1;              $retVal = 1;
3266          }          }
     }  
3267      # Return the result.      # Return the result.
3268      return $retVal;      return $retVal;
3269  }  }

Legend:
Removed from v.1.47  
changed lines
  Added in v.1.59

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3