/
BaseDriver.pm
1968 lines (1652 loc) · 63.2 KB
/
BaseDriver.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# BioPerl module for Bio::DB::BioSQL::BaseDriver
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Hilmar Lapp <hlapp at gmx.net>
#
# Copyright Hilmar Lapp
#
# You may distribute this module under the same terms as perl itself
#
# (c) Hilmar Lapp, hlapp at gmx.net, 2002.
# (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
#
# You may distribute this module under the same terms as perl itself.
# Refer to the Perl Artistic License (see the license accompanying this
# software package, or see http://www.perl.com/language/misc/Artistic.html)
# for the terms under which you may use, modify, and redistribute this module.
#
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# POD documentation - main docs before the code
=head1 NAME
Bio::DB::BioSQL::BaseDriver - DESCRIPTION of Object
=head1 DESCRIPTION
This object contains the mapping of BioPerl classes to entities, as well as
the private methods that construct the INSERT, DELETE, UPDATE, and SELECT
SQL statements.
=head1 FEEDBACK
=head2 Mailing Lists
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list. Your participation is much appreciated.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
=head2 Support
Please direct usage questions or support issues to the mailing list:
I<bioperl-l@bioperl.org>
rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
=head2 Reporting Bugs
Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via
the web:
http://redmine.open-bio.org/projects/bioperl/
=head1 AUTHOR - Hilmar Lapp
Email hlapp at gmx.net
=head1 CONTRIBUTORS
Additional contributors names and emails here
=head1 APPENDIX
The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _
=cut
# Let the code begin...
package Bio::DB::BioSQL::BaseDriver;
use vars qw(@ISA);
use strict;
# Object preamble - inherits from Bio::Root::Root
use Bio::Root::Root;
use Bio::DB::DBD;
@ISA = qw(Bio::Root::Root Bio::DB::DBD);
#
# here goes our entire object-relational mapping
#
my %object_entity_map = (
"Bio::BioEntry" => "bioentry",
"Bio::PrimarySeqI" => "bioentry",
"Bio::DB::BioSQL::PrimarySeqAdaptor" => "bioentry",
"Bio::SeqI" => "bioentry",
"Bio::DB::BioSQL::SeqAdaptor" => "bioentry",
"Bio::IdentifiableI" => "bioentry",
"Bio::ClusterI" => "bioentry",
"Bio::DB::BioSQL::ClusterAdaptor" => "bioentry",
"Bio::DB::BioSQL::BiosequenceAdaptor" => "biosequence",
"Bio::SeqFeatureI" => "seqfeature",
"Bio::DB::BioSQL::SeqFeatureAdaptor" => "seqfeature",
"Bio::Species" => "taxon_name",
"Bio::DB::BioSQL::SpeciesAdaptor" => "taxon_name",
# TaxonNode is a hack: there is no such object, but we need it
# to distinguish between the node and the name table
"TaxonNode" => "taxon",
"Bio::LocationI" => "location",
"Bio::DB::BioSQL::LocationAdaptor" => "location",
"Bio::DB::BioSQL::BioNamespaceAdaptor" => "biodatabase",
"Bio::DB::Persistent::BioNamespace" => "biodatabase",
"Bio::Annotation::DBLink" => "dbxref",
"Bio::DB::BioSQL::DBLinkAdaptor" => "dbxref",
"Bio::Annotation::Comment" => "comment",
"Bio::DB::BioSQL::CommentAdaptor" => "comment",
"Bio::Annotation::Reference" => "reference",
"Bio::DB::BioSQL::ReferenceAdaptor" => "reference",
"Bio::Annotation::SimpleValue" => "term",
"Bio::DB::BioSQL::SimpleValueAdaptor" => "term",
"Bio::Annotation::OntologyTerm" => "term",
"Bio::Ontology::TermI" => "term",
"Bio::DB::BioSQL::TermAdaptor" => "term",
"Bio::Ontology::RelationshipI" => "term_relationship",
"Bio::DB::BioSQL::RelationshipAdaptor" => "term_relationship",
"Bio::Ontology::PathI" => "term_path",
"Bio::Ontology::Path" => "term_path",
"Bio::DB::BioSQL::PathAdaptor" => "term_path",
"Bio::Ontology::OntologyI" => "ontology",
"Bio::DB::BioSQL::OntologyAdaptor" => "ontology",
# TermSynonym is a hack - there is no such object
"TermSynonym" => "term_synonym",
);
my %association_entity_map = (
"bioentry" => {
"dbxref" => "bioentry_dbxref",
"reference" => "bioentry_reference",
"term" => "bioentry_qualifier_value",
"bioentry" => { "term" => "bioentry_relationship", }
},
"ontology" =>
{ "term" => { "term" => { "term" => "term_relationship", }, }, },
"seqfeature" => {
"term" => "seqfeature_qualifier_value",
"dbxref" => "seqfeature_dbxref",
"reference" => undef,
"seqfeature" => { "term" => "seqfeature_relationship", }
},
"dbxref" => {
"bioentry" => "bioentry_dbxref",
"seqfeature" => "seqfeature_dbxref",
"term" => "dbxref_qualifier_value",
},
"reference" => {
"bioentry" => "bioentry_reference",
"seqfeature" => undef,
},
"term" => {
"bioentry" => "bioentry_qualifier_value",
"dbxref" => "term_dbxref",
"seqfeature" => "seqfeature_qualifier_value",
"term" => {
"term" => { "ontology" => "term_relationship", },
"ontology" => { "term" => "term_relationship", }
},
"ontology" => { "term" => { "term" => "term_relationship", } }
},
);
my %slot_attribute_map = (
"biodatabase" => {
"name" => "name",
"namespace" => "name",
"authority" => "authority",
},
"taxon_name" => {
"classification" => undef,
"common_name" => undef,
"ncbi_taxid" => "ncbi_taxon_id",
"binomial" => "name",
"variant" => undef,
# the following are hacks: there is no such thing on
# the object model. The sole reason they are here is so that you
# can set the physical column name of your taxon_name table.
# You MUST have these columns on the taxon node table, NOT the
# taxon name table.
"name_class" => "name_class",
"node_rank" => "node_rank",
"parent_taxon" => "parent_taxon_id",
},
"taxon" => {
"ncbi_taxid" => "ncbi_taxon_id",
# the following are hacks, see taxon_name mapping
"name_class" => "name_class",
"node_rank" => "node_rank",
"parent_taxon" => "parent_taxon_id",
},
"bioentry" => {
"display_id" => "name",
"primary_id" => "identifier",
"accession_number" => "accession",
"desc" => "description",
"description" => "description",
"version" => "version",
"division" => "division",
"bionamespace" => "biodatabase_id",
"namespace" => "biodatabase_id",
# these are for context-sensitive FK name resolution
"object" => "object_bioentry_id",
"subject" => "subject_bioentry_id",
# parent and child are for backwards compatibility
"parent" => "object_bioentry_id",
"child" => "subject_bioentry_id",
},
"bioentry_relationship" => {
"object" => "object_bioentry_id",
"subject" => "subject_bioentry_id",
"rank" => "rank",
# parent and child are for backwards compatibility
"parent" => "object_bioentry_id",
"child" => "subject_bioentry_id",
},
"biosequence" => {
"seq_version" => "version",
"length" => "length",
"seq" => "seq",
"alphabet" => "alphabet",
"primary_seq" => "bioentry_id",
# NOTE: change undef to the name of the CRC column to
# enable having CRC64s computed for sequences automatically,
# or set to undef to disable
"crc" => undef,
},
"dbxref" => {
"database" => "dbname",
"primary_id" => "accession",
"version" => "version",
"rank" => "=>{bioentry_dbxref,seqfeature_dbxref,term_dbxref}.rank",
},
"bioentry_dbxref" => { "rank" => "rank", },
"term_dbxref" => { "rank" => "rank", },
"reference" => {
"authors" => "authors",
"title" => "title",
"location" => "location",
"medline" => "dbxref_id",
"pubmed" => "dbxref_id",
"doc_id" => "crc",
"start" => "=>bioentry_reference.start",
"end" => "=>bioentry_reference.end",
"rank" => "=>bioentry_reference.rank",
},
"bioentry_reference" => {
"start" => "start_pos",
"end" => "end_pos",
"rank" => "rank",
},
"comment" => {
"text" => "comment_text",
"rank" => "rank",
"Bio::DB::BioSQL::SeqFeatureAdaptor" => undef,
},
"term" => {
"identifier" => "identifier",
"name" => "name",
"tagname" => "name",
"is_obsolete" => "is_obsolete",
"definition" => "definition",
"value" =>
"=>{bioentry_qualifier_value,seqfeature_qualifier_value}.value",
"rank" =>
"=>{bioentry_qualifier_value,seqfeature_qualifier_value}.rank",
"ontology" => "ontology_id",
# these are for context-sensitive FK name resolution
# term relationships:
"subject" => "subject_term_id",
"predicate" => "predicate_term_id",
"object" => "object_term_id",
# seqfeatures:
"primary_tag" => "type_term_id",
"source_tag" => "source_term_id",
},
# term_synonym is more a hack - it doesn't correspond to an object
# in bioperl, but this does let you specify your column naming
"term_synonym" => {
"synonym" => "synonym",
"term" => "term_id"
},
"term_relationship" => {
"subject" => "subject_term_id",
"predicate" => "predicate_term_id",
"object" => "object_term_id",
"ontology" => "ontology_id",
},
"term_path" => {
"distance" => "distance",
"subject" => "subject_term_id",
"predicate" => "predicate_term_id",
"object" => "object_term_id",
"ontology" => "ontology_id",
},
"ontology" => {
"name" => "name",
"definition" => "definition",
},
"bioentry_qualifier_value" => {
"value" => "value",
"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 ) = @_;
# get table name and the primary key name
my $table = $self->table_name($adp);
my $pkname = $self->primary_key_name($table);
# gather attributes
my @attrs = $self->_build_select_list( $adp, $fkslots );
# create the sql statement
my $sql
= "SELECT " . join( ", ", @attrs ) . " FROM $table WHERE $pkname = ?";
$adp->debug("preparing PK select statement: $sql\n");
# prepare statement and return
return $self->prepare( $adp->dbh(), $sql );
}
=head2 prepare_findbyuk_sth
Title : prepare_findbyuk_sth
Usage :
Function: Prepares and returns a DBI SELECT statement handle with as many
placeholders as necessary for the given unique 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 as many placeholders as
necessary for the given unique key
Args : The calling Bio::DB::BioSQL::BasePersistenceAdaptor derived object
(basically, it needs to implement dbh() and get_persistent_slots()).
A reference to a hash with the names of the object''s slots in the
unique key as keys and their values as values.
A reference to an array of foreign key objects or slots
(class names if slot).
=cut
sub prepare_findbyuk_sth {
my ( $self, $adp, $ukval_h, $fkslots ) = @_;
# get the slots for which we need columns
my @slots = $adp->get_persistent_slots();
# get the slot/attribute map
my $table = $self->table_name($adp);
my $slotmap = $self->slot_attribute_map($table);
# SELECT columns
my @attrs = $self->_build_select_list( $adp, $fkslots );
# WHERE clause constraints
my @cattrs = ();
foreach ( sort keys %$ukval_h ) {
my $col;
if ( exists( $slotmap->{$_} ) ) {
$col = $slotmap->{$_};
} else {
# try it as a foreign key
$col = $self->foreign_key_name($_);
}
push( @cattrs, $col || "NULL" );
if ( ! $col ) {
$self->warn( "slot $_ is in unique key, but can't be mapped to "
. "an entity column: you won't find anything" );
}
}
# create the sql statement
my $sql
= "SELECT "
. join( ", ", @attrs )
. " FROM $table WHERE "
. join( " AND ", map { "$_ = ?"; } @cattrs );
$adp->debug("preparing UK select statement: $sql\n");
# prepare statement and return
return $self->prepare( $adp->dbh(), $sql );
}
=head2 prepare_insert_association_sth
Title : prepare_insert_association_sth
Usage :
Function: Prepares a DBI statement handle suitable for inserting the
association between the two entities that correspond to the
given objects.
Example :
Returns : the DBI statement handle
Args : The calling adaptor.
Named parameters. Currently recognized are:
-objs a reference to an array of objects to be
associated with each other
-values a reference to a hash the keys of which are
column names and the values are values of
columns other than the ones for foreign keys to
the entities to be associated
-contexts optional; if given it denotes a reference
to an array of context keys (strings), which
allow the foreign key name to be determined
through the association map rather than through
foreign_key_name(). This may be necessary if
more than one object of the same type takes
part in the association. The array must be in
the same order as -objs, and have the same
number of elements. Put undef for objects
for which there are no multiple contexts.
Caveats: Make sure you *always* give the objects to be associated in the
same order.
=cut
sub prepare_insert_association_sth {
my ( $self, $adp, @args ) = @_;
my ($i);
# get arguments
my ( $objs, $values, $contexts )
= $self->_rearrange( [qw(OBJS VALUES CONTEXTS)], @args );
# obtain column map for non-fk columns
my $table = $self->association_table_name($objs);
if ( ! $table ) {
$self->throw(
"no object-relational map for association between "
. "classes ("
. join(
",",
map {
$_->isa("Bio::DB::PersistentObjectI")
? ref( $_->obj() )
: ref($_);
} @$objs
)
. ")"
);
}
my $columnmap = $self->slot_attribute_map($table);
my $attr;
my @attrs = ();
my @plchldrs = ();
# first, gather the foreign key names
$i = 0;
while ( $i < @$objs ) {
my $fktable = $self->table_name( $objs->[$i] );
if ( ! $fktable ) {
$self->throw(
"no object-relational map for class " . ref( $objs->[$i] ) );
}
if ( $contexts && $contexts->[$i] ) {
$attr = $columnmap->{ $contexts->[$i] };
} else {
$attr = $self->foreign_key_name( $objs->[$i] );
}
if ( ! $attr ) {
$self->throw( "unable to determine column for FK to class "
. ref( $objs->[$i] ) );
}
push( @attrs, $attr );
push( @plchldrs, "?" );
$i++;
}
# now add the columns for values if any
if ($values) {
foreach my $colkey ( keys %$values ) {
$self->throw("unmapped association column $colkey")
unless exists( $columnmap->{$colkey} );
$attr = $columnmap->{$colkey};
if ($attr) {
push( @attrs, $attr );
push( @plchldrs, "?" );
}
}
}
# construct SQL straightforwardly
my $sql
= "INSERT INTO $table ("
. join( ", ", @attrs )
. ") VALUES ("
. join( ", ", @plchldrs ) . ")";
$adp->debug("preparing INSERT statement: $sql\n");
# prepare sth and return
return $self->prepare( $adp->dbh(), $sql );
}
=head2 prepare_delete_association_sth
Title : prepare_delete_association_sth
Usage :
Function: Prepares a DBI statement handle suitable for deleting the
association between the two entities that correspond to the
given objects.
Example :
Returns : the DBI statement handle
Args : The calling adaptor.
Named parameters. Currently recognized are:
-objs a reference to an array of objects the association
between which is to be deleted
-values a reference to a hash the keys of which are
column names and the values are values of
columns other than the ones for foreign keys to
the entities to be associated
-contexts optional; if given it denotes a reference
to an array of context keys (strings), which
allow the foreign key name to be determined
through the association map rather than through
foreign_key_name(). This may be necessary if
more than one object of the same type takes
part in the association. The array must be in
the same order as -objs, and have the same
number of elements. Put undef for objects
for which there are no multiple contexts.
Caveats: Make sure you *always* give the objects to be associated in the
same order.
=cut
sub prepare_delete_association_sth {
my ( $self, $adp, @args ) = @_;
my ($i);
# get arguments
my ( $objs, $values, $contexts )
= $self->_rearrange( [qw(OBJS VALUES CONTEXTS)], @args );
# obtain column map for non-fk columns
my $table = $self->association_table_name($objs);
if ( ! $table ) {
$self->throw(
"no object-relational map for association between "
. "classes ("
. join(
",",
map {
ref($_)
? ( $_->isa("Bio::DB::PersistentObjectI")
? ref( $_->obj() )
: ref($_) )
: $_;
} @$objs
)
. ")"
);
}
my $columnmap = $self->slot_attribute_map($table);
my $attr;
my @attrs = ();
# first, gather the foreign key names
$i = 0;
while ( $i < @$objs ) {
my $obj = $objs->[$i];
if ( ref($obj) && $obj->isa("Bio::DB::PersistentObjectI") ) {
my $fktable = $self->table_name($obj);
if ( ! $fktable ) {
$self->throw(
"no object-relational map for class " . ref($obj) );
}
if ( $contexts && $contexts->[$i] ) {
$attr = $columnmap->{ $contexts->[$i] };
} else {
$attr = $self->foreign_key_name($obj);
}
if ( ! $attr ) {
$self->throw( "unable to determine column for FK to class "
. ref($obj) );
}
push( @attrs, $attr );
}
$i++;
}
# now add the columns for values if any
if ($values) {
foreach my $colkey ( keys %$values ) {
$self->throw("unmapped association column $colkey")
unless exists( $columnmap->{$colkey} );
$attr = $columnmap->{$colkey};
push( @attrs, $attr ) if $attr;
}
}
# construct SQL straightforwardly
my $sql = "DELETE FROM $table WHERE "
. join( " AND ", map { $_ . " = ?"; } @attrs );
$adp->debug("preparing DELETE ASSOC statement: $sql\n");
# prepare sth and return
return $self->prepare( $adp->dbh(), $sql );
}
=head2 prepare_delete_query_sth
Title : prepare_delete_query_sth
Usage :
Function: Prepares a DBI statement handle suitable for deleting rows
from a table that match a number of attributes.
Example :
Returns : the DBI statement handle
Args : The calling adaptor.
Named parameters. Currently recognized are:
-fkobjs optional; a reference to an array of foreign
key objects by which to constrain; this is
complementary to -values
-contexts optional; if given it denotes a reference
to an array of context keys (strings), which
allow the foreign key name to be determined
through the association map rather than through
foreign_key_name(). This may be necessary if
an entity has more than one foreign key to the
same entity. The array must be in the same
order as -fkobjs, and have the same number of
elements. Put undef for objects for which there
are no multiple contexts.
-values optional; a reference to a hash the keys of
which are attribute names by which to constrain
the query
=cut
sub prepare_delete_query_sth {
my ( $self, $adp, @args ) = @_;
my ($i);
# get arguments
my ( $fkobjs, $values, $contexts )
= $self->_rearrange( [qw(FKOBJS VALUES CONTEXTS)], @args );
# obtain column map for attributes
my $table = $self->table_name($adp);
my $columnmap = $self->slot_attribute_map($table);
my @attrs = ();
my $attr;
# add the query constraint columns for foreign key columns if any
if ( $fkobjs && @$fkobjs ) {
foreach my $obj (@$fkobjs) {
my $fktable = $self->table_name($obj);
if ( ! $fktable ) {
$self->throw( "no object-relational map for class "
. ( ref($obj) ? ref($obj) : $obj ) );
}
if ( $contexts && $contexts->[$i] ) {
$attr = $columnmap->{ $contexts->[$i] };
} else {
$attr = $self->foreign_key_name($obj);
}
if ( ! $attr ) {
$self->throw( "unable to determine column for FK to class "
. ( ref($obj) ? ref($obj) : $obj ) );
}
push( @attrs, $attr );
}
}
# add any other query constraint columns
if ($values) {
foreach my $colkey ( keys %$values ) {
$self->throw("unmapped association column $colkey")
unless exists( $columnmap->{$colkey} );
$attr = $columnmap->{$colkey};
push( @attrs, $attr ) if $attr;
}
}
# construct SQL straightforwardly
my $sql = "DELETE FROM $table";
if (@attrs) {
$sql .= " WHERE " . join( " AND ", map { $_ . " = ?"; } @attrs );
}
$adp->debug("preparing DELETE QUERY statement: $sql\n");
# prepare sth and return
return $self->prepare( $adp->dbh(), $sql );
}
=head2 prepare_insert_sth
Title : prepare_insert_sth
Usage :
Function: Prepares a DBI statement handles suitable for inserting
a row (as values of the slots of an object) into a table.
Example :
Returns : the DBI statement handle
Args : the calling adaptor (a Bio::DB::PersistenceAdaptorI object)
a reference to an array of object slot names
a reference to an array of foreign key objects (optional)
=cut
sub prepare_insert_sth {
my ( $self, $adp, $slots, $fkobjs ) = @_;
# obtain table and object slot map
my $table = $self->table_name($adp);
my $slotmap = $self->slot_attribute_map($table);
$self->throw("no slot/attribute map for table $table") unless $slotmap;
# construct INSERT statement as straightforward SQL with placeholders
my @attrs = ();
my @plchlds = ();
foreach my $slot (@$slots) {
if ( ! exists( $slotmap->{$slot} ) ) {
$self->throw("no mapping for slot $slot in slot-attribute map");
}
# we don't add a column nor a placeholder for unmapped slots
if ( $slotmap->{$slot}
&& ( substr( $slotmap->{$slot}, 0, 2 ) ne '=>' ) ) {
push( @attrs, $slotmap->{$slot} );
push( @plchlds, "?" );
}
}
# foreign keys
if ($fkobjs) {
foreach (@$fkobjs) {
my $fkattr = $self->foreign_key_name($_);
push( @attrs, $fkattr );
push( @plchlds, "?" );
}
}
my $sql
= "INSERT INTO "
. $table . " ("
. join( ", ", @attrs )
. ") VALUES ("
. join( ", ", @plchlds ) . ")";
$adp->debug("preparing INSERT statement: $sql\n");
return $self->prepare( $adp->dbh, $sql );
}
=head2 prepare_update_sth
Title : prepare_update_sth
Usage :
Function: Prepares a DBI statement handle suitable for updating
a row in a table where the row is identified by its
primary key.
Example :
Returns : the DBI statement handle
Args : the calling adaptor (a Bio::DB::PersistenceAdaptorI object)
a reference to an array of object slot names
a reference to an array of foreign key objects (optional)
=cut
sub prepare_update_sth {
my ( $self, $adp, $slots, $fkobjs ) = @_;
# obtain the table name and corresponding slot map
my $table = $self->table_name($adp);
my $slotmap = $self->slot_attribute_map($table);
$self->throw("no slot/attribute map for table $table") unless $slotmap;
# construct UPDATE statement as straightforward SQL
my @attrs = ();
foreach my $slot (@$slots) {
if ( ! exists( $slotmap->{$slot} ) ) {
$self->throw("no mapping for slot $_ in slot-attribute map");
}
# we don't add a column nor a placeholder for unmapped slots
if ( $slotmap->{$slot}
&& ( substr( $slotmap->{$slot}, 0, 2 ) ne '=>' ) ) {
push( @attrs, $slotmap->{$slot} );
}
}
# foreign keys
if ($fkobjs) {
foreach (@$fkobjs) {
my $fkattr = $self->foreign_key_name($_);
push( @attrs, $fkattr );
}
}
my $ifnull = $adp->dbcontext->dbi->ifnull_sqlfunc();
my $sql
= "UPDATE $table SET "
. join( ", ", map { "$_ = $ifnull\(?,$_\)"; } @attrs )
. " WHERE "
. $self->primary_key_name($table) . " = ?";
$adp->debug("preparing UPDATE statement: $sql\n");
return $self->prepare( $adp->dbh(), $sql );
}
=head2 cascade_delete
Title : cascade_delete
Usage :
Function: Removes all persistent objects dependent from the given persistent
object from the database (foreign key integrity).
This implementation assumes that the underlying schema and RDBMS
support cascading deletes, and hence does nothing other than
returning TRUE.
Example :
Returns : TRUE on success, and FALSE otherwise
Args : The DBContextI implementing object for the database.
The object for which the dependent rows shall be deleted.
Optionally, additional (named) arguments.
=cut
sub cascade_delete {
# our default assumption is that the RDBMS does support cascading deletes
return 1;
}
=head2 insert_object
Title : insert_object
Usage :
Function:
Example :
Returns : The primary key of the newly inserted record.
Args : A Bio::DB::BioSQL::BasePersistenceAdaptor derived object
(basically, it needs to implement dbh(), sth($key, $sth),
dbcontext(), and get_persistent_slots()).
The object to be inserted.
A reference to an array of foreign key objects; if any of those
foreign key values is NULL (some foreign keys may be nullable),
then give the class name.
=cut
sub insert_object {
my ( $self, $adp, $obj, $fkobjs ) = @_;
# obtain the object's slots to be serialized
my @slots = $adp->get_persistent_slots($obj);
# get the INSERT statement
# is it cached?
my $cache_key = 'INSERT ' . ref($obj) . ' ' . join( ';', @slots );
my $sth
= $self->get_sth( $adp, $obj, $fkobjs, $cache_key, 'insert_object' );
# we need the slot map regardless of whether we need to construct the
# SQL or not, because we need to know which slots do not map to a column
# (indicated by them being mapped to undef)
my $table = $self->table_name($adp);
my $slotmap = $self->slot_attribute_map($table);
$self->throw("no slot/attribute map for table $table") unless $slotmap;
# we'll need the db handle in any case
my $dbh = $adp->dbh();
# if not cached, create SQL and prepare statement
if ( ! $sth ) {
$sth = $self->prepare_insert_sth( $adp, \@slots, $fkobjs );
# and cache
$adp->sth( $cache_key, $sth );
# and give interceptors a chance to do their work
$sth = $self->get_sth( $adp, $obj, $fkobjs, $cache_key,
'insert_object' );