View | Details | Raw Unified | Return to bug 39164
Collapse All | Expand All

(-)a/C4/Reports/Guided.pm (+2 lines)
Lines 54-59 use Koha::Logger; Link Here
54
use Koha::Notice::Templates;
54
use Koha::Notice::Templates;
55
use Koha::Patron::Categories;
55
use Koha::Patron::Categories;
56
use Koha::Patrons;
56
use Koha::Patrons;
57
use Koha::Report;
57
use Koha::Reports;
58
use Koha::Reports;
58
use Koha::SharedContent;
59
use Koha::SharedContent;
59
use Koha::TemplateUtils qw( process_tt );
60
use Koha::TemplateUtils qw( process_tt );
Lines 604-609 sub execute_query { Link Here
604
605
605
    my ( $is_sql_valid, $errors ) = Koha::Report->new( { savedsql => $sql } )->is_sql_valid;
606
    my ( $is_sql_valid, $errors ) = Koha::Report->new( { savedsql => $sql } )->is_sql_valid;
606
    return ( undef, @{$errors}[0] ) unless $is_sql_valid;
607
    return ( undef, @{$errors}[0] ) unless $is_sql_valid;
608
    $sql = Koha::Report->apply_execution_time_limit($sql);
607
609
608
    foreach my $sql_param (@$sql_params) {
610
    foreach my $sql_param (@$sql_params) {
609
        if ( $sql_param =~ m/\n/ ) {
611
        if ( $sql_param =~ m/\n/ ) {
(-)a/Koha/Report.pm (+32 lines)
Lines 258-263 sub _might_add_limit { Link Here
258
    return $sql;
258
    return $sql;
259
}
259
}
260
260
261
=head3 apply_execution_time_limit
262
263
    $sql = Koha::Report->apply_execution_time_limit($sql);
264
265
Apply SQL execution time limits based on report_sql_max_statement_time_seconds.
266
267
=cut
268
269
sub apply_execution_time_limit {
270
    my ( $class, $sql ) = @_;
271
272
    # https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=39164
273
    my $report_sql_max_statement_time_seconds = C4::Context->config('report_sql_max_statement_time_seconds') // 0;
274
    if ($report_sql_max_statement_time_seconds) {
275
        my %versions = C4::Context::get_versions();
276
        if ( $versions{'mysqlVersion'} =~ /MariaDB/i ) {
277
            $sql = sprintf(
278
                'SET STATEMENT max_statement_time=%f FOR %s',
279
                $report_sql_max_statement_time_seconds,
280
                $sql
281
            );
282
        } else {
283
284
            # mysql timing is in milliseconds
285
            $report_sql_max_statement_time_seconds *= 1000;
286
            $sql =~ s#select#select /*+ MAX_EXECUTION_TIME($report_sql_max_statement_time_seconds) */#i;
287
        }
288
    }
289
290
    return $sql;
291
}
292
261
=head3 _type
293
=head3 _type
262
294
263
Returns name of corresponding DBIC resultset
295
Returns name of corresponding DBIC resultset
(-)a/t/db_dependent/Koha/Reports.t (-2 / +26 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::NoWarnings;
20
use Test::NoWarnings;
21
use Test::More tests => 9;
21
use Test::More tests => 10;
22
22
23
use Koha::Report;
23
use Koha::Report;
24
use Koha::Reports;
24
use Koha::Reports;
Lines 185-188 subtest '_might_add_limit' => sub { Link Here
185
    );
185
    );
186
};
186
};
187
187
188
subtest 'apply_execution_time_limit' => sub {
189
    plan tests => 3;
190
191
    my $sql = "SELECT * FROM biblio";
192
193
    t::lib::Mocks::mock_config( 'report_sql_max_statement_time_seconds', 0 );
194
    is( Koha::Report->apply_execution_time_limit($sql), $sql, 'No execution time limit configured' );
195
196
    t::lib::Mocks::mock_config( 'report_sql_max_statement_time_seconds', 1.5 );
197
    my $context = Test::MockModule->new('C4::Context');
198
    $context->mock( 'get_versions', sub { return ( mysqlVersion => '10.6.0-MariaDB' ); } );
199
    is(
200
        Koha::Report->apply_execution_time_limit($sql),
201
        'SET STATEMENT max_statement_time=1.500000 FOR SELECT * FROM biblio',
202
        'MariaDB max statement time applied'
203
    );
204
205
    $context->mock( 'get_versions', sub { return ( mysqlVersion => '8.0.36' ); } );
206
    like(
207
        Koha::Report->apply_execution_time_limit($sql),
208
        qr!^(?i:select) /\*\+ MAX_EXECUTION_TIME\(1500\) \*/ \* FROM biblio$!,
209
        'MySQL max execution time hint applied'
210
    );
211
};
212
188
$schema->storage->txn_rollback;
213
$schema->storage->txn_rollback;
189
- 

Return to bug 39164