ConnectorQuery/Outer.pm untested

filter: Statementstmt 0.0% 0 / 226 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 311 CC 29 SCAR 51.2
CC
29
Cov
0%
db_execute
380.0
term_where
56.0
_init_self
12.0
CRAP 167.7
1 package Bio::ConnectDots::ConnectorQuery::Outer;
2 use vars qw(@ISA @AUTO_ATTRIBUTES @OTHER_ATTRIBUTES %SYNONYMS %DEFAULTS);
3 use strict;
4 use DBI;
5 use Class::AutoClass;
6 use Bio::ConnectDots::Util;
7 use Bio::ConnectDots::ConnectorTable;
8 use Bio::ConnectDots::ConnectorQuery;
9 use Bio::ConnectDots::ConnectorQuery::Alias;
10 use Bio::ConnectDots::ConnectorQuery::Term;
11 use Bio::ConnectDots::ConnectorQuery::Constraint;
12 use Bio::ConnectDots::ConnectorQuery::Join;
13 use Bio::ConnectDots::SimpleGraph;
14 @ISA = qw(Bio::ConnectDots::ConnectorQuery);
15  
16 @AUTO_ATTRIBUTES=qw(cs_sql_aliases ct_alias2sql_alias cs_alias2sql_aliases);
17 %SYNONYMS=();
18 @OTHER_ATTRIBUTES=qw();
19 %DEFAULTS=(cs_sql_aliases=>[],ct_alias2sql_alias=>{},cs_alias2sql_aliases=>{});
20 Class::AutoClass::declare(__PACKAGE__);
21  
22 sub _init_self {
23 my($self,$class,$args)=@_;
24 return unless $class eq __PACKAGE__; # to prevent subclasses from re-running this
25 $self->execute if $args->execute;
26 }
27  
28 sub db_execute {
29 my($self)=@_;
30 my $db=$self->db;
31 $self->throw("Cannot run query: database is not connected") unless $db->is_connected;
32 $self->throw("Cannot run query: database does not exist") unless $db->exists;
33 my $name=$self->name;
34 my $preview = $self->connectortable->preview;
35 my $plimit = $self->connectortable->preview_limit;
36 my $ct_alias2sql_alias=$self->ct_alias2sql_alias;
37 my $cs_alias2sql_aliases=$self->cs_alias2sql_aliases;
38 my %edge2on; # translates the edge bewteen two nodes (concatenated as the key) to the ON clause that links them
39 my %node2subselect; # translates the cs_alias to it's corresponding subselect
40 my %constr_check; # {connectorset id}->{label id}
41 my $i=0;
42 my $SimpleGraph = new Bio::ConnectDots::SimpleGraph(); # graph scaffold for join tree building
43 my(@targets,@from,@where);
44  
45 # initialize cs alias to sql_aliases to empty ARRAYs for each alias
46 for my $cs_alias (@{$self->cs_aliases}) {
47 $cs_alias2sql_aliases->{$cs_alias}=[];
48 }
49  
50 # generate constraint where clauses
51 for my $constraint (@{$self->constraints}) {
52 my $cs_id = $constraint->{_term}->{target_object}->{db_id};
53 my $label_ids = $constraint->{_term}->{label_ids};
54 foreach my $lid (@$label_ids) {
55 if($constr_check{$cs_id}->{$lid}) {
56 my $csname = $constraint->{_term}->{cs_alias}->{alias_name};
57 $self->throw ("Error in Query Syntax: Use IN operator for multiple values from the same ConnectorSet ($csname)");
58 }
59 }
60 foreach my $lid (@$label_ids) { $constr_check{$cs_id}->{$lid}=1 };
61 my $result=$self->constraint_where($constraint,\$i);
62 push(@where,@{$result->{where}}) if $result->{where};
63 my $cd = $result->{cd};
64 my $constrHash = $result->{term_where};
65 $node2subselect{$cd} = "(SELECT * FROM connectdot WHERE ". join(' AND ', @{ $constrHash->{constraints} });
66 $node2subselect{$cd} .= " LIMIT $plimit" if $preview;
67 $node2subselect{$cd} .= ") AS $cd";
68
69 }
70 # generate dot where clauses (cs-label-dot-label-cs)
71 for my $join (@{$self->joins}) {
72 my $constrHash = $self->dot_where($join,\$i);
73
74 # Add ct-cs inner join if there
75 push(@where, @{$constrHash->{term0}->{inner_join}}) if $constrHash->{term0}->{inner_join};
76 push(@where, @{$constrHash->{term1}->{inner_join}}) if $constrHash->{term1}->{inner_join};
77
78 my $cd0 = $constrHash->{term0}->{cd};
79 my $cd1 = $constrHash->{term1}->{cd};
80
81 $node2subselect{$cd0} = "(SELECT * FROM connectdot WHERE ". join(' AND ', @{ $constrHash->{term0}->{constraints} });
82 $node2subselect{$cd0} .= " LIMIT $plimit" if $preview;
83 $node2subselect{$cd0} .= ") AS $cd0";
84 $node2subselect{$cd1} = "(SELECT * FROM connectdot WHERE ". join(' AND ', @{ $constrHash->{term1}->{constraints} });
85 $node2subselect{$cd1} .= " LIMIT $plimit" if $preview;
86 $node2subselect{$cd1} .= ") AS $cd1";
87 my $key = ($cd0 lt $cd1)? $cd0.$cd1 : $cd1.$cd0; # put lexically least cs first
88 $edge2on{$key} = "ON $constrHash->{outer_join_on}";
89 $SimpleGraph->add_edge($cd0, $cd1);
90 }
91
92
93 # generate ON clauses to connect sql_aliases for same cs_alias
94 # ex: LocusLink_cs_0.connector_id=LocusLink_cs_4.connector_id
95 for my $sql_aliases (values %$cs_alias2sql_aliases) {
96 next unless @$sql_aliases >= 2; # no joins necessary unless 2 or more tables
97 for(my $i=0;$i<@$sql_aliases-1;$i++) {
98 my $cd0=$sql_aliases->[$i];
99 my $cd1=$sql_aliases->[$i+1];
100 my $key = ($cd0 lt $cd1)? $cd0.$cd1 : $cd1.$cd0; # put lexically least cs first
101 $edge2on{$key} = "ON $cd0.connector_id=$cd1.connector_id";
102 $SimpleGraph->add_edge($cd0, $cd1);
103 }
104 }
105  
106 # collect targets (columns that are output)
107 # for ct_aliases, need all columns -- col name prefixed with ct_alias
108 for my $ct_alias (@{$self->ct_aliases}) {
109 my $ct=$ct_alias->target_object;
110 my $ct_alias_name=$ct_alias->alias_name;
111 my $sql_alias=$ct_alias2sql_alias->{$ct_alias};
112 for my $column (@{$ct->columns}) {
113 my $column_name=$ct_alias_name.'_'.$column;
114 push(@targets,"$sql_alias.$column AS $column_name");
115 }
116 }
117 # for cs_aliases, need connector_id from one sql_alias -- col name = cs_alias
118 for my $cs_alias (@{$self->cs_aliases}) {
119 my $cs_alias_name=$cs_alias->alias_name;
120 my $sql_aliases=$cs_alias2sql_aliases->{$cs_alias};
121 push(@targets,$sql_aliases->[0].".connector_id AS ".$cs_alias_name);
122 }
123 # assemble 'from' list
124 # each ct_alias refers to its own ConnectorTable
125 for my $ct_alias (@{$self->ct_aliases}) {
126 my $ct=$ct_alias->target_object;
127 my $ct_name=$ct->name;
128 my $sql_alias=$ct_alias2sql_alias->{$ct_alias};
129 my $fromsql = "$ct_name AS $sql_alias";
130 $fromsql = "(SELECT * FROM $ct_name LIMIT $plimit) AS $sql_alias" if $preview;
131 push(@from,$fromsql);
132 }
133  
134 # create full outer joins from DFS of join graph
135 throw("Joins must form a connected graph! Check that all joins connect to each other") unless $SimpleGraph->is_connected;
136 my @edges = $SimpleGraph->edges();
137 my $fojs; # full outer joins
138 my $n=0;
139 my $iterator = $SimpleGraph->edge_traversal($edges[0],'depth first');
140 # interate through edges, removing nodes from the options as we go
141 while($iterator->has_next) {
142 my $edge = $iterator->get_next;
143 if ($edge) {
144 if ($n == 0) {
145 $fojs .= " ". $node2subselect{$edge->[0]} ." FULL OUTER JOIN ". $node2subselect{$edge->[1]};
146 $fojs .= " ". $edge2on{$edge->[0] . $edge->[1]};
147 $node2subselect{$edge->[0]} = undef;
148 $node2subselect{$edge->[1]} = undef;
149 $n++;
150 }
151 else {
152 # choose node that has not been added yet
153 my $cs_to_add = $node2subselect{$edge->[0]}? $edge->[0] : $edge->[1];
154 $fojs .= " FULL OUTER JOIN ". $node2subselect{$cs_to_add};
155 $fojs .= " ". $edge2on{$edge->[0] . $edge->[1]};
156 $node2subselect{$cs_to_add} = undef; # delete it
157 $n++;
158 }
159 }
160 }
161 push(@from, $fojs);
162  
163 my $targets=join(', ',@targets);
164 my $from=join(', ',@from);
165 my $where=joindef(' AND ',@where);
166 my $sql="SELECT DISTINCT $targets FROM $from";
167 $sql .= " WHERE $where" if $where;
168 my @indexes=@{$self->columns};
169 $db->create_table_sql($name,$sql,\@indexes);
170 }
171  
172 sub dot_where {
173 my($self,$join,$i_ref)=@_;
174 my %results;
175 my $term_rslt0 = $self->term_where($join->term0,$i_ref);
176 my $cd0 = $term_rslt0->{cd};
177 $results{term0} = $term_rslt0;
178 my $term_rslt1 = $self->term_where($join->term1,$i_ref);
179 my $cd1 = $term_rslt1->{cd};
180 $results{term1} = $term_rslt1;
181 foreach my $term (keys %results) {
182 my $cd = $results{$term}->{cd};
183 foreach my $constraint (@{ $results{$term}->{constraints} }) {
184 $constraint =~ s/^$cd\.//;
185 }
186 }
187 $results{outer_join_on} = "$cd0.dot_id=$cd1.dot_id";
188 return \%results;
189 }
190  
191 sub constraint_where {
192 my($self,$constraint,$i_ref)=@_;
193 my %results;
194 my $db=$self->db;
195 my @where;
196 my ($hash, $cd) = $self->term_where($constraint->term,$i_ref);
197 my $where = $hash->{constraints};
198 push(@where,@$where);
199 my($op,$constants)=($constraint->op,$constraint->constants);
200 my @constants=map {$db->quote_dot($_)} @$constants;
201 if ($op=~/IN/) { # IN or NOT IN
202 push(@where,"$cd.id $op (".join(',',@constants).")");
203 } elsif ($op ne 'EXISTS') { # EXISTS has no constants -- needs no SQL condition
204 # should only be 1 constant by now -- see Constraint::normalize
205 push(@where,"$cd.id $op ".$db->quote($constants->[0]));
206 }
207 $results{where} = \@where;
208 $results{cd} = $cd;
209 $results{term_where} = $hash;
210 foreach my $constraint (@{ $results{term_where}->{constraints} }) {
211 $constraint =~ s/^$cd\.//;
212 }
213
214 return \%results;
215 }
216 sub term_where {
217 my($self,$term,$i_ref)=@_;
218 my %ret;
219 my $where=[];
220 my $ct_alias=$term->ct_alias;
221 my $cs_alias=$term->cs_alias;
222 my $cs=$term->cs; # always defined, but can be from column or from alias
223 my $cs_id=$cs->db_id;
224 my $cs_sql_alias=($cs_alias? $cs_alias->alias_name: $cs->name).'_cs_'.$$i_ref++;
225 push(@{$self->cs_alias2sql_aliases->{$cs_alias}},$cs_sql_alias) if $cs_alias;
226 push(@{$self->cs_sql_aliases},$cs_sql_alias);
227 if ($ct_alias) {
228 my $ct=$term->ct;
229 my $ct_sql_alias=$self->ct_alias2sql_alias->{$ct_alias} ||
230 ($self->ct_alias2sql_alias->{$ct_alias}=$ct_alias->alias_name.'_ct_'.$$i_ref++);
231 my $column=$term->column;
232 push(@{$ret{inner_join}},"$ct_sql_alias.$column=$cs_sql_alias.connector_id"); # inner join
233 }
234 push(@{$ret{constraints}},"$cs_sql_alias.connectorset_id=$cs_id");
235 # if $label_ids is empty, the label was '*' -- matches all ids
236 my $label_ids=$term->label_ids;
237 if (@$label_ids==1) {
238 push(@{$ret{constraints}},"$cs_sql_alias.label_id=".$label_ids->[0]);
239 } elsif (@$label_ids>1) {
240 push(@{$ret{constraints}},"$cs_sql_alias.label_id IN (".join(',',@$label_ids).")");
241 }
242 $ret{cd} = $cs_sql_alias;
243 wantarray? (\%ret, $cs_sql_alias): \%ret;
244 }
245  
246 sub connector_where {
247 }
248 1;