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

(-)a/Koha/Report.pm (-1 / +26 lines)
Lines 168-174 for building batch action links in the template Link Here
168
=cut
168
=cut
169
169
170
sub prep_report {
170
sub prep_report {
171
    my ( $self, $param_names, $sql_params ) = @_;
171
    my ( $self, $param_names, $sql_params, $params ) = @_;    # params keys: export
172
    my $sql = $self->savedsql;
172
    my $sql = $self->savedsql;
173
173
174
    # First we split out the placeholders
174
    # First we split out the placeholders
Lines 226-235 sub prep_report { Link Here
226
        $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
226
        $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
227
    }
227
    }
228
228
229
    $sql = $self->_might_add_limit($sql) if $params->{export};
230
229
    $sql = "$sql /* saved_sql.id: ${\( $self->id )} */";
231
    $sql = "$sql /* saved_sql.id: ${\( $self->id )} */";
230
    return $sql, $headers;
232
    return $sql, $headers;
231
}
233
}
232
234
235
=head3 _might_add_limit
236
237
    $sql = $self->_might_add_limit($sql);
238
239
Depending on pref GuidedReportsExportLimit. If there is a limit defined
240
and the report does not contain a limit already, this routine adds
241
a LIMIT to $sql.
242
243
=cut
244
245
sub _might_add_limit {
246
    my ( $self, $sql ) = @_;
247
    if ( C4::Context->preference('GuidedReportsExportLimit')
248
        && $sql !~ /\sLIMIT\s\d+(?![^\(]*\))/i )
249
    {
250
        # Note: regex tries to account for allowed limits in subqueries.
251
        # This assumes that we dont have an INTO option at the end (which does
252
        # not work in current Koha reporting)
253
        $sql .= " LIMIT " . C4::Context->preference('GuidedReportsExportLimit');
254
    }
255
    return $sql;
256
}
257
233
=head3 _type
258
=head3 _type
234
259
235
Returns name of corresponding DBIC resultset
260
Returns name of corresponding DBIC resultset
(-)a/reports/guided_reports.pl (-1 / +1 lines)
Lines 624-630 elsif ($op eq 'export'){ Link Here
624
        $reportname ? "$report_id-$reportname-reportresults.$format" : "$report_id-reportresults.$format";
624
        $reportname ? "$report_id-$reportname-reportresults.$format" : "$report_id-reportresults.$format";
625
    my $scrubber = C4::Scrubber->new();
625
    my $scrubber = C4::Scrubber->new();
626
626
627
    ($sql, undef) = $report->prep_report( \@param_names, \@sql_params );
627
    ($sql, undef) = $report->prep_report( \@param_names, \@sql_params, { export => 1 } );
628
    my ( $sth, $q_errors ) = execute_query( { sql => $sql, report_id => $report_id } );
628
    my ( $sth, $q_errors ) = execute_query( { sql => $sql, report_id => $report_id } );
629
    unless ($q_errors and @$q_errors) {
629
    unless ($q_errors and @$q_errors) {
630
        my ( $type, $content );
630
        my ( $type, $content );
(-)a/t/db_dependent/Koha/Reports.t (-2 / +36 lines)
Lines 17-28 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 7;
20
use Test::More tests => 8;
21
21
22
use Koha::Report;
22
use Koha::Report;
23
use Koha::Reports;
23
use Koha::Reports;
24
use Koha::Database;
24
use Koha::Database;
25
25
26
use t::lib::Mocks;
26
use t::lib::TestBuilder;
27
use t::lib::TestBuilder;
27
28
28
my $schema = Koha::Database->new->schema;
29
my $schema = Koha::Database->new->schema;
Lines 136-139 subtest 'check_columns' => sub { Link Here
136
    );
137
    );
137
};
138
};
138
139
140
subtest '_might_add_limit' => sub {
141
    plan tests => 10;
142
143
    my $sql;
144
145
    t::lib::Mocks::mock_preference( 'GuidedReportsExportLimit', undef );    # i.e. no limit
146
    $sql = "SELECT * FROM biblio WHERE 1";
147
    is( Koha::Report->_might_add_limit($sql), $sql, 'Pref is undefined, no changes' );
148
    t::lib::Mocks::mock_preference( 'GuidedReportsExportLimit', 0 );        # i.e. no limit
149
    is( Koha::Report->_might_add_limit($sql), $sql, 'Pref is zero, no changes' );
150
    t::lib::Mocks::mock_preference( 'GuidedReportsExportLimit', q{} );      # i.e. no limit
151
    is( Koha::Report->_might_add_limit($sql), $sql, 'Pref is empty, no changes' );
152
    t::lib::Mocks::mock_preference( 'GuidedReportsExportLimit', 10 );
153
    like( Koha::Report->_might_add_limit($sql), qr/ LIMIT 10$/, 'Limit 10 found at the end' );
154
    $sql = "SELECT * FROM biblio WHERE 1 LIMIT 1000 ";
155
    is( Koha::Report->_might_add_limit($sql), $sql, 'Already contains a limit' );
156
    $sql = "SELECT * FROM biblio WHERE 1 LIMIT 1000,2000";
157
    is( Koha::Report->_might_add_limit($sql), $sql, 'Variation, also contains a limit' );
158
159
    # trying a subquery having a limit (testing the lookahead in regex)
160
    $sql = "SELECT * FROM biblio WHERE biblionumber IN (SELECT biblionumber FROM reserves LIMIT 2)";
161
    like( Koha::Report->_might_add_limit($sql), qr/ LIMIT 10$/, 'Subquery, limit 10 found at the end' );
162
    $sql = "SELECT * FROM biblio WHERE biblionumber IN (SELECT biblionumber FROM reserves LIMIT 2, 3 ) AND 1";
163
    like( Koha::Report->_might_add_limit($sql), qr/ LIMIT 10$/, 'Subquery variation, limit 10 found at the end' );
164
    $sql = "select * from biblio where biblionumber in (select biblionumber from reserves limiT 3,4) and 1";
165
    like( Koha::Report->_might_add_limit($sql), qr/ LIMIT 10$/, 'Subquery lc variation, limit 10 found at the end' );
166
167
    $sql = "select limit, 22 from mylimits where limit between 1 and 3";
168
    like(
169
        Koha::Report->_might_add_limit($sql), qr/ LIMIT 10$/,
170
        'Query refers to limit field, limit 10 found at the end'
171
    );
172
};
173
139
$schema->storage->txn_rollback;
174
$schema->storage->txn_rollback;
140
- 

Return to bug 23685