sub execute_query {
- my ( $sql, $offset, $limit, $sql_params ) = @_;
+ my ( $sql, $offset, $limit, $sql_params, $report_id ) = @_;
$sql_params = [] unless defined $sql_params;
}
$sql .= " LIMIT ?, ?";
- my $sth = C4::Context->dbh->prepare($sql);
+ my $dbh = C4::Context->dbh;
+
+ $dbh->do( 'UPDATE saved_sql SET last_run = NOW() WHERE id = ?', undef, $report_id ) if $report_id;
+
+ my $sth = $dbh->prepare($sql);
$sth->execute(@$sql_params, $offset, $limit);
+
return ( $sth, { queryerr => $sth->errstr } ) if ($sth->err);
return ( $sth );
}
$subject = 'Koha Saved Report';
}
}
- # my $results = execute_query($sql, undef, 0, 99999, $format, $report_id);
- my ($sth) = execute_query($sql);
+ my ($sth) = execute_query( $sql, undef, undef, undef, $report_id );
my $count = scalar($sth->rows);
unless ($count) {
print "NO OUTPUT: 0 results from execute_query\n";
$sql =~ s/(<<.*?>>)/\?/g;
my ( $sth, $errors ) =
- execute_query( $sql, $offset, $limit, \@sql_params );
+ execute_query( $sql, $offset, $limit, \@sql_params, $report_id );
if ($sth) {
my $lines;
if ($report_annotation) {
$quoted = C4::Context->dbh->quote($quoted);
$sql =~ s/<<$split[$i*2+1]>>/$quoted/;
}
- my ($sth, $errors) = execute_query($sql, $offset, $limit);
+ my ( $sth, $errors ) = execute_query( $sql, $offset, $limit, undef, $report_id );
my $total = nb_rows($sql) || 0;
unless ($sth) {
die "execute_query failed to return sth for report $report_id: $sql";
# convert SQL parameters to placeholders
$sql =~ s/(<<.*?>>)/\?/g;
- my ( $sth, $errors ) = execute_query( $sql, $offset, $limit, \@sql_params );
+ my ( $sth, $errors ) = execute_query( $sql, $offset, $limit, \@sql_params, $report_id );
if ($sth) {
my $lines;
if ($report_annotation) {
use Modern::Perl;
-use Test::More tests => 7;
+use Test::More tests => 8;
use Test::Warn;
use t::lib::TestBuilder;
"get_report_areas returns the correct array of report areas");
};
+subtest 'Ensure last_run is populated' => sub {
+ plan tests => 3;
+
+ my $rs = Koha::Database->new()->schema()->resultset('SavedSql');
+
+ my $report = $rs->new(
+ {
+ report_name => 'Test Report',
+ savedsql => 'SELECT * FROM branches',
+ notes => undef,
+ }
+ )->insert();
+
+ is( $report->last_run, undef, 'Newly created report has null last_run ' );
+
+ execute_query( $report->savedsql, undef, undef, undef, $report->id );
+ $report->discard_changes();
+
+ isnt( $report->last_run, undef, 'First run of report populates last_run' );
+
+ my $previous_last_run = $report->last_run;
+ sleep(1); # last_run is stored to the second, so we need to ensure at least one second has passed between runs
+ execute_query( $report->savedsql, undef, undef, undef, $report->id );
+ $report->discard_changes();
+
+ isnt( $report->last_run, $previous_last_run, 'Second run of report updates last_run' );
+};
+
$schema->storage->txn_rollback;
sub trim {