[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.54, Sat Jun 24 15:55:37 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 2001  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  }  }
# Line 2072  Line 2136 
2136      return $objectData->{Fields};      return $objectData->{Fields};
2137  }  }
2138    
2139    =head2 Data Mining Methods
2140    
2141  =head3 GetUsefulCrossValues  =head3 GetUsefulCrossValues
2142    
2143  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>
# Line 2133  Line 2199 
2199      return @retVal;      return @retVal;
2200  }  }
2201    
2202    =head3 FindColumn
2203    
2204    C<< my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); >>
2205    
2206    Return the location a desired column in a data mining header line. The data
2207    mining header line is a tab-separated list of column names. The column
2208    identifier is either the numerical index of a column or the actual column
2209    name.
2210    
2211    =over 4
2212    
2213    =item headerLine
2214    
2215    The header line from a data mining command, which consists of a tab-separated
2216    list of column names.
2217    
2218    =item columnIdentifier
2219    
2220    Either the ordinal number of the desired column (1-based), or the name of the
2221    desired column.
2222    
2223    =item RETURN
2224    
2225    Returns the array index (0-based) of the desired column.
2226    
2227    =back
2228    
2229    =cut
2230    
2231    sub FindColumn {
2232        # Get the parameters.
2233        my ($headerLine, $columnIdentifier) = @_;
2234        # Declare the return variable.
2235        my $retVal;
2236        # Split the header line into column names.
2237        my @headers = ParseColumns($headerLine);
2238        # Determine whether we have a number or a name.
2239        if ($columnIdentifier =~ /^\d+$/) {
2240            # Here we have a number. Subtract 1 and validate the result.
2241            $retVal = $columnIdentifier - 1;
2242            if ($retVal < 0 || $retVal > $#headers) {
2243                Confess("Invalid column identifer \"$columnIdentifier\": value out of range.");
2244            }
2245        } else {
2246            # Here we have a name. We need to find it in the list.
2247            for (my $i = 0; $i <= $#headers && ! defined($retVal); $i++) {
2248                if ($headers[$i] eq $columnIdentifier) {
2249                    $retVal = $i;
2250                }
2251            }
2252            if (! defined($retVal)) {
2253                Confess("Invalid column identifier \"$columnIdentifier\": value not found.");
2254            }
2255        }
2256        # Return the result.
2257        return $retVal;
2258    }
2259    
2260    =head3 ParseColumns
2261    
2262    C<< my @columns = ERDB::ParseColumns($line); >>
2263    
2264    Convert the specified data line to a list of columns.
2265    
2266    =over 4
2267    
2268    =item line
2269    
2270    A data mining input, consisting of a tab-separated list of columns terminated by a
2271    new-line.
2272    
2273    =item RETURN
2274    
2275    Returns a list consisting of the column values.
2276    
2277    =back
2278    
2279    =cut
2280    
2281    sub ParseColumns {
2282        # Get the parameters.
2283        my ($line) = @_;
2284        # Chop off the line-end.
2285        chomp $line;
2286        # Split it into a list.
2287        my @retVal = split(/\t/, $line);
2288        # Return the result.
2289        return @retVal;
2290    }
2291    
2292  =head2 Internal Utility Methods  =head2 Internal Utility Methods
2293    
2294  =head3 SetupSQL  =head3 SetupSQL

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3