3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
6 Licensed under the Apache License, Version 2.0 (the
"License");
7 you may not use
this file except in compliance with the License.
8 You may obtain a copy of the License at
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an
"AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License
for the specific language governing permissions and
16 limitations under the License.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
37 Do not use
this class directly. It will automatically be used by the
42 This class extends DBD::mysql::st so that the DESTROY method may be
43 overridden. If the DBConnection::disconnect_when_inactive flag is set
44 this statement handle will cause the database connection to be closed
45 when it goes out of scope and there are no other open statement handles.
60 #use Time::HiRes qw(time);
65 # As DBD::mysql::st is a tied hash can't store things in it,
66 # so have to have parallel hash
77 # without delete key space would grow indefinitely causing mem-leak
78 delete($dbchash{$self});
80 $dbchash{$self} = $dbc;
84 return $dbchash{$self};
93 # without delete key space would grow indefinitely causing mem-leak
94 delete($dbc_sql_hash{$self});
96 $dbc_sql_hash{$self} = $sql;
100 return $dbc_sql_hash{$self};
106 my $dbc = $self->dbc;
108 my $sql = $self->sql;
111 # Re-bless into DBI::st so that superclass destroy method is called if
112 # it exists (it does not exist in all DBI versions).
113 bless( $self,
'DBI::st' );
115 # The count for the number of kids is decremented only after this
116 # function is complete. Disconnect if there is 1 kid (this one)
119 && $dbc->disconnect_when_inactive()
121 && ( $dbc->db_handle->{Kids} == 1 ) )
123 if ( $dbc->disconnect_if_idle() ) {
124 warn(
"Problem disconnect $self around sql = $sql\n");
131 # Comment out this "__END__" for printing out handy debug information
132 # (every query if you want).
136 # To stop caching messing up your timings, try doing the following on
139 # $slice_adaptor->dbc()->db_handle()
140 # ->do("SET SESSION query_cache_type = OFF");
143 # Bio::EnsEMBL::DBSQL::StatementHandle->sql_timing_start();
145 # To display the results:
146 # Bio::EnsEMBL::DBSQL::StatementHandle->sql_timing_print(1);
149 # Bio::EnsEMBL::DBSQL::StatementHandle->sql_timimg_pause();
151 # To resume logging after pause:
152 # Bio::EnsEMBL::DBSQL::StatementHandle->sql_timimg_resume();
154 use Time::HiRes qw(time);
165 sub sql_timing_start {
167 %number_of_times = ();
174 sub sql_timing_pause { $dump = 0 }
175 sub sql_timing_resume { $dump = 1 }
177 sub sql_timing_print {
178 my ( $self, $level, $fh ) = @_;
182 if ( !defined($fh) ) {
186 print( ref($fh),
"\n" );
188 foreach my $key ( keys %total_time ) {
189 $grand_total += $total_time{$key};
191 if ( !( defined($level) and $level ) ) { next }
193 print( $fh $key,
"\n" );
196 "total\t \tnum\tfirst \t\tavg\t \t[min ,max ]\n" );
198 printf( $fh
"%6f\t%d\t%6f\t%6f\t[%6f, %6f]\n\n",
199 $total_time{$key}, $number_of_times{$key},
200 $first_time{$key}, ( $total_time{$key}/$number_of_times{$key} ),
201 $min_time{$key}, $max_time{$key} );
204 printf( $fh
"\ntotal time %6f\n\n", $grand_total );
206 } ## end sub sql_timing_print
209 my ( $self, @args ) = @_;
211 $bind_args[ $args[0] - 1 ] = $args[1];
212 $self->SUPER::bind_param(@args);
216 my ( $self, @args ) = @_;
219 # Skip dumping if !$dump
221 local $self->{RaiseError};
222 $retval = $self->SUPER::execute(@args);
223 if ( !defined($retval) ) {
224 throw(
"Failed to execute SQL statement");
229 my $sql = $self->sql();
234 for ( my $i = 0; $i < @chrs; $i++ ) {
235 if ( $chrs[$i] eq
'?' && defined( $bind_args[$j] ) ) {
236 $chrs[$i] = $bind_args[ $j++ ];
240 my $str = join(
'', @chrs );
242 # Uncomment this line if you want to see sql in order.
243 # print( STDERR "\n\nSQL:\n$str\n\n" );
247 local $self->{RaiseError};
248 $retval = $self->SUPER::execute(@args);
249 if ( !defined($retval) ) {
250 throw(
"Failed to execute SQL statement");
253 # my $res = $self->SUPER::execute(@args);
254 $time = time() - $time;
256 if ( defined( $total_time{$sql} ) ) {
257 $total_time{$sql} += $time;
258 $number_of_times{$sql}++;
260 if ( $min_time{$sql} > $time ) { $min_time{$sql} = $time }
261 if ( $max_time{$sql} < $time ) { $max_time{$sql} = $time }
264 $first_time{$sql} = $time;
265 $max_time{$sql} = $time;
266 $min_time{$sql} = $time;
267 $total_time{$sql} = $time;
268 $number_of_times{$sql} = 1;