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 (-18 / +32 lines)
Lines 233-256 sub prep_report { Link Here
233
233
234
    $sql = "$sql /* saved_sql.id: ${\( $self->id )} */";
234
    $sql = "$sql /* saved_sql.id: ${\( $self->id )} */";
235
235
236
    # https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=39164
237
    my $report_sql_max_statement_time_seconds = C4::Context->config('report_sql_max_statement_time_seconds') // 0;
238
    if ($report_sql_max_statement_time_seconds) {
239
        my %versions = C4::Context::get_versions();
240
        if ( $versions{'mysqlVersion'} =~ /MariaDB/i ) {
241
            $sql = sprintf(
242
                'SET STATEMENT max_statement_time=%d FOR %s',
243
                $report_sql_max_statement_time_seconds,
244
                $sql
245
            );
246
        } else {
247
248
            # mysql timing is in milliseconds
249
            $report_sql_max_statement_time_seconds *= 1000;
250
            $sql =~ s#select#select /*+ MAX_EXECUTION_TIME($report_sql_max_statement_time_seconds) */#i;
251
        }
252
    }
253
254
    return $sql, $headers;
236
    return $sql, $headers;
255
}
237
}
256
238
Lines 277-282 sub _might_add_limit { Link Here
277
    return $sql;
259
    return $sql;
278
}
260
}
279
261
262
=head3 apply_execution_time_limit
263
264
    $sql = Koha::Report->apply_execution_time_limit($sql);
265
266
Apply SQL execution time limits based on report_sql_max_statement_time_seconds.
267
268
=cut
269
270
sub apply_execution_time_limit {
271
    my ( $class, $sql ) = @_;
272
273
    # https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=39164
274
    my $report_sql_max_statement_time_seconds = C4::Context->config('report_sql_max_statement_time_seconds') // 0;
275
    if ($report_sql_max_statement_time_seconds) {
276
        my %versions = C4::Context::get_versions();
277
        if ( $versions{'mysqlVersion'} =~ /MariaDB/i ) {
278
            $sql = sprintf(
279
                'SET STATEMENT max_statement_time=%f FOR %s',
280
                $report_sql_max_statement_time_seconds,
281
                $sql
282
            );
283
        } else {
284
285
            # mysql timing is in milliseconds
286
            $report_sql_max_statement_time_seconds *= 1000;
287
            $sql =~ s#select#select /*+ MAX_EXECUTION_TIME($report_sql_max_statement_time_seconds) */#i;
288
        }
289
    }
290
291
    return $sql;
292
}
293
280
=head3 _type
294
=head3 _type
281
295
282
Returns name of corresponding DBIC resultset
296
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