ConnectorQuery/Inner.pm untested

filter: Statementstmt 0.0% 0 / 163 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 249 CC 25 SCAR 39.0
CC
25
Cov
0%
db_execute
132.0
term_where
56.0
constraint_where
20.0
CRAP 49.5
1 package Bio::ConnectDots::ConnectorQuery::Inner;
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 @ISA = qw(Bio::ConnectDots::ConnectorQuery);
14  
15 @AUTO_ATTRIBUTES=qw(cs_sql_aliases ct_alias2sql_alias cs_alias2sql_aliases);
16 %SYNONYMS=();
17 @OTHER_ATTRIBUTES=qw();
18 %DEFAULTS=(cs_sql_aliases=>[],ct_alias2sql_alias=>{},cs_alias2sql_aliases=>{});
19 Class::AutoClass::declare(__PACKAGE__);
20  
21 sub _init_self {
22 my($self,$class,$args)=@_;
23 return unless $class eq __PACKAGE__; # to prevent subclasses from re-running this
24 $self->execute if $args->execute;
25 }
26  
27 sub db_execute {
28 my($self)=@_;
29 my $db=$self->db;
30 $self->throw("Cannot run query: database is not connected") unless $db->is_connected;
31 $self->throw("Cannot run query: database does not exist") unless $db->exists;
32 my $name=$self->name;
33 my $preview = $self->connectortable->preview;
34 my $plimit = $self->connectortable->preview_limit;
35 my $ct_alias2sql_alias=$self->ct_alias2sql_alias;
36 my $cs_alias2sql_aliases=$self->cs_alias2sql_aliases;
37 # initialize cs alias to sql_aliases to empty ARRAYs for each alias
38 for my $cs_alias (@{$self->cs_aliases}) {
39 $cs_alias2sql_aliases->{$cs_alias}=[];
40 }
41 my(@targets,@from,@where,@where_cw);
42 # generate dot where clauses (cs-label-dot-label-cs)
43 my $i=0;
44 for my $join (@{$self->joins}) {
45 push(@where,$self->dot_where($join,\$i));
46 }
47
48 # generate constraint where clauses
49 for my $constraint (@{$self->constraints}) {
50 push(@where,$self->constraint_where($constraint,\$i));
51 }
52 # generate clauses to connect sql_aliases for same cs_alias
53 for my $sql_aliases (values %$cs_alias2sql_aliases) {
54 push(@where_cw,$self->connector_where($sql_aliases));
55 }
56 # collect targets (columns that are output)
57 # for ct_aliases, need all columns -- col name prefixed with ct_alias
58 for my $ct_alias (@{$self->ct_aliases}) {
59 my $ct=$ct_alias->target_object;
60 my $ct_alias_name=$ct_alias->alias_name;
61 my $sql_alias=$ct_alias2sql_alias->{$ct_alias};
62 for my $column (@{$ct->columns}) {
63 my $column_name=$ct_alias_name.'_'.$column;
64 push(@targets,"$sql_alias.$column AS $column_name");
65 }
66 }
67 # for cs_aliases, need connector_id from one sql_alias -- col name = cs_alias
68 for my $cs_alias (@{$self->cs_aliases}) {
69 my $cs_alias_name=$cs_alias->alias_name;
70 my $sql_aliases=$cs_alias2sql_aliases->{$cs_alias};
71 push(@targets,$sql_aliases->[0].".connector_id AS ".$cs_alias_name);
72 }
73 # assemble 'from' list
74 # each ct_alias refers to its own ConnectorTable
75 for my $ct_alias (@{$self->ct_aliases}) {
76 my $ct=$ct_alias->target_object;
77 my $ct_name=$ct->name;
78 my $sql_alias=$ct_alias2sql_alias->{$ct_alias};
79 my $fromsql = "$ct_name AS $sql_alias";
80 if ($preview) { #add where clauses for this alias to subselect
81 my @subwhere;
82 foreach my $constr (@where) {
83 if($constr =~ /label_id/ || $constr =~ /connectorset_id/) {
84 my ($constraint) = $constr =~ /$sql_alias\.(.+)/;
85 push @subwhere, $constraint if $constraint;
86 }
87 }
88 my $subwhere = join(' AND ',@subwhere);
89 $fromsql = "(select * from $ct_name WHERE $subwhere LIMIT $plimit) AS $sql_alias" if $preview;
90 }
91 push(@from,$fromsql);
92 }
93 # all cs_aliases refer to connectdot
94 for my $sql_alias (uniq($self->cs_sql_aliases)) {
95 my $fromsql = "connectdot AS $sql_alias";
96 if ($preview) { #add where clauses for this alias to subselect
97 my @subwhere;
98 foreach my $constr (@where) {
99 if($constr =~ /label_id/ || $constr =~ /connectorset_id/) {
100 my ($constraint) = $constr =~ /$sql_alias\.(.+)/;
101 push @subwhere, $constraint if $constraint;
102 }
103 }
104 my $subwhere = join(' AND ',@subwhere);
105 $fromsql = "(select * from connectdot WHERE $subwhere LIMIT $plimit) AS $sql_alias" if $preview;
106 }
107 push(@from,$fromsql);
108 }
109  
110 my $targets=join(', ',@targets);
111 my $from=join(', ',@from);
112 my $where=joindef(' AND ',(@where,@where_cw));
113 my $sql="SELECT DISTINCT $targets FROM $from WHERE $where";
114 my @indexes=@{$self->columns};
115 $db->create_table_sql($name,$sql,\@indexes);
116 }
117  
118 sub dot_where {
119 my($self,$join,$i_ref)=@_;
120 my @where;
121 my($where0,$cd0)=
122 $self->term_where($join->term0,$i_ref);
123 my($where1,$cd1)=
124 $self->term_where($join->term1,$i_ref);
125 push(@where,@$where0,@$where1);
126 push(@where,"$cd0.dot_id=$cd1.dot_id");
127 wantarray? @where: \@where;
128 }
129 sub constraint_where {
130 my($self,$constraint,$i_ref)=@_;
131 my $db=$self->db;
132 my @where;
133 my($where,$cd)=
134 $self->term_where($constraint->term,$i_ref);
135 push(@where,@$where);
136 my($op,$constants)=($constraint->op,$constraint->constants);
137 my @constants=map {$db->quote_dot($_)} @$constants;
138 if ($op=~/IN/) { # IN or NOT IN
139 push(@where,"$cd.id $op (".join(',',@constants).")");
140 } elsif ($op ne 'EXISTS') { # EXISTS has no constants -- needs no SQL condition
141 # should only be 1 constant by now -- see Constraint::normalize
142 push(@where,"$cd.id $op ".$db->quote($constants->[0]));
143 }
144 wantarray? @where: \@where;
145 }
146 sub term_where {
147 my($self,$term,$i_ref)=@_;
148 my $where=[];
149 my $ct_alias=$term->ct_alias;
150 my $cs_alias=$term->cs_alias;
151 my $cs=$term->cs; # always defined, but can be from column or from alias
152 my $cs_id=$cs->db_id;
153 my $cs_sql_alias=($cs_alias? $cs_alias->alias_name: $cs->name).'_cs_'.$$i_ref++;
154 push(@{$self->cs_alias2sql_aliases->{$cs_alias}},$cs_sql_alias) if $cs_alias;
155 push(@{$self->cs_sql_aliases},$cs_sql_alias);
156 if ($ct_alias) {
157 my $ct=$term->ct;
158 my $ct_sql_alias=$self->ct_alias2sql_alias->{$ct_alias} ||
159 ($self->ct_alias2sql_alias->{$ct_alias}=$ct_alias->alias_name.'_ct_'.$$i_ref++);
160 my $column=$term->column;
161 push(@$where,"$ct_sql_alias.$column=$cs_sql_alias.connector_id");
162 }
163 push(@$where,"$cs_sql_alias.connectorset_id=$cs_id");
164 # if $label_ids is empty, the label was '*' -- matches all ids
165 my $label_ids=$term->label_ids;
166 if (@$label_ids==1) {
167 push(@$where,"$cs_sql_alias.label_id=".$label_ids->[0]);
168 } elsif (@$label_ids>1) {
169 push(@$where,"$cs_sql_alias.label_id IN (".join(',',@$label_ids).")");
170 }
171 wantarray? ($where,$cs_sql_alias): $where;
172 }
173 sub connector_where {
174 my($self,$sql_aliases)=@_;
175 return unless @$sql_aliases>=2; # no joins necessary unless 2 or more tables
176 my @where;
177 for(my $i=0;$i<@$sql_aliases-1;$i++) {
178 my $cd0=$sql_aliases->[$i];
179 my $cd1=$sql_aliases->[$i+1];
180 push(@where, "$cd0.connector_id=$cd1.connector_id");
181 }
182 wantarray? @where: \@where;
183 }
184 1;