BioPerl-DB

 view release on metacpan or  search on metacpan

lib/Bio/DB/BioSQL/BaseDriver.pm  view on Meta::CPAN

	     "rank"           => "rank",
	 },
	 "seqfeature" => {
	     "display_name"   => "display_name",
	     "rank"           => "rank",
	     "primary_tag"    => "type_term_id",
	     "source_tag"     => "source_term_id",
	     "entire_seq"     => "bioentry_id",
	     # these are for context-sensitive FK name resolution
	     "object"         => "object_seqfeature_id",
	     "subject"        => "subject_seqfeature_id",
	     # parent and child are for backwards compatibility
	     "parent"         => "parent_seqfeature_id",
	     "child"          => "child_seqfeature_id",
	 },
	 "seqfeature_dbxref" => {
	     "rank"           => "rank",
	 },
	 "location" => {
	     "start"          => "start_pos",
	     "end"            => "end_pos",
	     "strand"         => "strand",
	     "rank"           => "rank",
	 },
	 "seqfeature_qualifier_value" => {
	     "value"          => "value",
	     "rank"           => "rank",
	 },
	 "seqfeature_relationship" => {
	     "object"         => "object_seqfeature_id",
	     "subject"        => "subject_seqfeature_id",
	     "rank"           => "rank",
	     # parent and child are for backwards compatibility
	     "parent"         => "parent_seqfeature_id",
	     "child"          => "child_seqfeature_id",
	 },
			   );
my %dont_select_attrs = (
	 "biosequence.seq" => 1,
			);

=head2 new

 Title   : new
 Usage   : my $obj = Bio::DB::BioSQL::BaseDriver->new();
 Function: Builds a new Bio::DB::BioSQL::BaseDriver object 
 Returns : an instance of Bio::DB::BioSQL::BaseDriver
 Args    :

=cut

sub new {
    my($class,@args) = @_;

    my $self = $class->SUPER::new(@args);

    # copy the static mapping tables into our private hashs
    # you may then change individual mappings in your derived adaptor driver
    $self->objrel_map(\%object_entity_map);
    $self->slot_attribute_map(\%slot_attribute_map);
    $self->not_select_attrs(\%dont_select_attrs);
    $self->association_entity_map(\%association_entity_map);

    return $self;
}

=head2 prepare_delete_sth

 Title   : prepare_delete_sth
 Usage   :
 Function: Creates a prepared statement with one placeholder variable
           suitable to delete one row from the respective table the
           given class maps to.

           The method may throw an exception, or the database handle
           methods involved may throw an exception.

 Example :
 Returns : A DBI statement handle for a prepared statement with one placeholder
 Args    : The calling adaptor (basically, it needs to implement dbh()).
           Optionally, additional arguments.


=cut

sub prepare_delete_sth{
    my ($self, $adp) = @_;

    # default is a simple DELETE statement
    #
    # we need the table name and the name of the primary key
    my $tbl = $self->table_name($adp);
    my $pkname = $self->primary_key_name($tbl);
    # straightforward SQL:
    my $sql = "DELETE FROM $tbl WHERE $pkname = ?";
    $adp->debug("preparing DELETE statement: $sql\n");
    my $sth = $self->prepare($adp->dbh(),$sql);
    # done
    return $sth;
}

=head2 prepare_findbypk_sth

 Title   : prepare_findbypk_sth
 Usage   :
 Function: Prepares and returns a DBI statement handle with one placeholder for
           the primary key. The statement is expected to return the primary key
           as the first and then as many columns as 
           $adp->get_persistent_slots() returns, and in that order.

 Example :
 Returns : A DBI prepared statement handle with one placeholder
 Args    : The Bio::DB::BioSQL::BasePersistenceAdaptor derived object 
           (basically, it needs to implement dbh() and get_persistent_slots()).
           A reference to an array of foreign key slots (class names).


=cut

sub prepare_findbypk_sth{
    my ($self,$adp,$fkslots) = @_;

lib/Bio/DB/BioSQL/BaseDriver.pm  view on Meta::CPAN

 Title   : translate_query
 Usage   :
 Function: Translates the given query as represented by the query object
           from objects and class names and slot names to tables and column
           names.
 Example :
 Returns : An object of the same class as the input query, but representing
           the translated query, and also with the SELECT fields properly set
           to facilitate object construction.
 Args    : The calling adaptor.
           The query as a Bio::DB::Query::BioQuery or derived object.
           A reference to an array of foreign key objects.


=cut

sub translate_query{
    my ($self,$adp,$query,$fkobjs) = @_;

    # the query object can itself translate the datacollections and
    # slot names to column names (all it needs is a obj-rel mapper, which
    # is us)
    my %entitymap = ();
    my $tquery = $query->translate_query($self, \%entitymap);
    # build the SELECT list
    my @selattrs = $self->_build_select_list($adp,$fkobjs,\%entitymap);
    # set as the SELECT elements of the query
    $tquery->selectelts(\@selattrs);
    # done
    return $tquery;
}

=head2 _build_select_list

 Title   : _build_select_list
 Usage   :
 Function: Builds and returns the select list for an object query. The list
           contains those columns, in the right order, that are necessary to
           populate the object.
 Example :
 Returns : An array of strings (column names, not prefixed)
 Args    : The calling persistence adaptor.
           A reference to an array of foreign key entities (objects, class
           names, or adaptors) the object must attach.
           A reference to a hash table mapping entity names to aliases (if
           omitted, aliases will not be used, and SELECT columns can only be
           from one table)


=cut

sub _build_select_list{
    my ($self,$adp,$fkobjs,$entitymap) = @_;

    # get the persistent slots
    my @slots = $adp->get_persistent_slots();
    # get the slot/attribute map
    my $table = $self->table_name($adp);
    my $slotmap = $self->slot_attribute_map();
    # get the map of columns excluded from SELECTs
    my $dont_select_attrs = $self->not_select_attrs();
    # default the entity-alias map if not provided
    if(! $entitymap) {
	$entitymap = {};
	$entitymap->{$table} = [$table];
    }
    # Alias for the table. We'll use the first one if the table is in the
    # FROM list with different aliases. Also note that the alias may come
    # with context, which we need to strip off.
    my ($alias) = split(/::/, $entitymap->{$table}->[0]);
    # get the primary key name
    my $pkname = $self->primary_key_name($table);
    # SELECT columns
    my @attrs = ($alias .".". $pkname);
    foreach (@slots) {
	$self->throw("no mapping for slot $_ in slot-attribute map")
	    if ! exists($slotmap->{$table}->{$_});
	my $attr = $slotmap->{$table}->{$_};
	my $tbl = $table;
	# is this attribute actually mapped to one or more other tables?
	if($attr && (substr($attr,0,2) eq '=>')) {
	    # yes, figure out to which attribute
	    ($tbl,$attr) = split(/\./, substr($attr,2));
	    # is this mapped to multiple tables?
	    if($tbl =~ /^\{(.*)\}$/) {
		# yes, figure out which one we have in the entity map
		foreach (split(/[,\s]+/, $1)) {
		    # we just grab the first one
		    if($entitymap->{$_}) {
			$tbl = $_;
			last;
		    }
		}
	    }
	    $attr = $slotmap->{$tbl}->{$attr};
	}
	if((! $attr) || (! $entitymap->{$tbl}) ||
	   $dont_select_attrs->{$tbl .".". $attr}) {
	    push(@attrs, "NULL");
	} else {
	    # same caveats as for the alias of the 'main' table
	    my ($tblalias) = split(/::/, $entitymap->{$tbl}->[0]);
	    push(@attrs, $tblalias .".". $attr);
	}
    }
    # add foreign key attributes
    if($fkobjs) {
	foreach (@$fkobjs) {
	    my $fkattr = $self->foreign_key_name($_);
	    $self->throw("no mapping for foreign key to $_") unless $fkattr;
	    push(@attrs, $alias .".". $fkattr);
	}
    }
    return @attrs;
}

=head2 table_name

 Title   : table_name
 Usage   :
 Function: Obtain the name of the table in the relational schema

lib/Bio/DB/BioSQL/BaseDriver.pm  view on Meta::CPAN

    if($value) {
	$self->{'_objrel_map'} = $value;
    }
    return $self->{'_objrel_map'};
}

=head2 slot_attribute_map

 Title   : slot_attribute_map
 Usage   :
 Function: Get/set the mapping for each entity from object slot names
           to column names.

 Example :
 Returns : A reference to a hash map with entity names being the keys,
           if no key (entity name, object, or adaptor) was
           provided. Otherwise, a hash reference with the slot names
           being keys to their corresponding column names.

 Args    : Optionally, the object, adaptor, or entity for which to
           obtain the map.

           Optionally, on set a reference to a hash map satisfying the
           features of the returned value.


=cut

sub slot_attribute_map{
    my ($self,$tablekey,$map) = @_;

    if($tablekey) {
	# this might actually be the overall map on set
	if((ref($tablekey) eq "HASH") && (! $map)) {
	    $map = $tablekey;
	    $tablekey = undef;
	    $self->{'_slot_attr_map'} = $map;
	} else {
	    # make sure the hash exists before we query it with a key
	    if(! exists($self->{'_slot_attr_map'})) {
		$self->{'_slot_attr_map'} = {};
	    }
	    # see whether we need to transform it into an entity name
	    if(ref($tablekey)) {
		$tablekey = $self->table_name($tablekey);
	    }
	    # set/get the individual map
	    if($map) {
		$self->{'_slot_attr_map'}->{$tablekey} = $map;
	    } else {
		$map = $self->{'_slot_attr_map'}->{$tablekey};
	    }
	}
    } else {
	# return the overall map
	$map = $self->{'_slot_attr_map'};
    }
    return $map;
}

=head2 not_select_attrs

 Title   : not_select_attrs
 Usage   : $obj->not_select_attrs($newval)
 Function: Get/set a map of all columns that should not be included in
           SELECT lists.
 Example : 
 Returns : value of not_select_attrs (a reference to a hash map)
 Args    : new value (a reference to a hash map, optional)


=cut

sub not_select_attrs{
    my ($self,$value) = @_;
    if( defined $value) {
	$self->{'not_select_attrs'} = $value;
    }
    return $self->{'not_select_attrs'};
}

=head2 association_entity_map

 Title   : association_entity_map
 Usage   : $obj->association_entity_map($newval)
 Function: Get/set the association entity map. The map is an anonymous
           hash with entities that participate in associations being
           keys. The values are hash refs themselves, with the other
           participating entity being the key, and the value being
           either the name of the respective association entity, or
           another hash ref with the same structure if more entities
           participate in the association.

           The hash map must be commutative. I.e., the association
           entity must be locatable irregardless with which of the
           participating entities one starts.

 Example : 
 Returns : value of association_entity_map (a hash ref of hash refs)
 Args    : new value (a hash ref of hash refs, optional)


=cut

sub association_entity_map{
    my ($self,$value) = @_;
    if( defined $value) {
	$self->{'association_entity_map'} = $value;
    }
    return $self->{'association_entity_map'};
}

=head1 DBI calls for possible interception

These will usually delegate straightforward DBI calls on the supplied
handle, but can also be used by an inheriting adaptor driver to
intercept the call and add additional parameters, for example a hash
reference with named parameters.

=cut

=head2 commit

 Title   : commit
 Usage   :
 Function: Commits the current transaction, if the underlying driver
           supports transactions.
 Example :
 Returns : TRUE
 Args    : The database connection handle for which to commit.


=cut

sub commit{
    my ($self, $dbh) = @_;
    return $dbh->commit();
}



( run in 0.492 second using v1.01-cache-2.11-cpan-39bf76dae61 )