bugfix Guided Reports - allow user specified limit in sql
authorMichael Hafen <mdhafen@tech.washk12.org>
Wed, 4 Nov 2009 21:31:48 +0000 (14:31 -0700)
committerGalen Charlton <gmcharlt@gmail.com>
Thu, 18 Feb 2010 18:52:18 +0000 (13:52 -0500)
Tweak the regular expression in strip_limit to work.
Tweak execute_query to use the user limit if it's lower than the hard coded one.
Also total is calculated somewhere else now.

This helps most with the csv export of a report so the user can set their own
limit instead of having the hard coded limit of 9999.

Signed-off-by: Galen Charlton <gmcharlt@gmail.com>

C4/Reports/Guided.pm

index d8277b7..9f64cd9 100644 (file)
@@ -427,8 +427,8 @@ sub select_2_select_count ($) {
 sub strip_limit ($) {
     my $sql = shift or return;
     ($sql =~ /\bLIMIT\b/i) or return ($sql, 0, undef);
-    $sql =~ s/\bLIMIT\b\s*\d+(\,\s*\d+)?\s*/ /ig;
-    return ($sql, (defined $1 ? $1 : 0), $2);   # offset can default to 0, LIMIT cannot!
+    $sql =~ s/\bLIMIT\b\s*(\d+)(\s*\,\s*(\d+))?\s*/ /ig;
+    return ($sql, (defined $2 ? $1 : 0), (defined $3 ? $3 : $1));   # offset can default to 0, LIMIT cannot!
 }
 
 sub execute_query ($;$$$) {
@@ -457,13 +457,12 @@ sub execute_query ($;$$$) {
         $useroffset,
         (defined($userlimit ) ? $userlimit  : 'UNDEF');
     $offset += $useroffset;
-    my $total;
     if (defined($userlimit)) {
         if ($offset + $limit > $userlimit ) {
             $limit = $userlimit - $offset;
+        } elsif ( ! $offset && $limit > $userlimit ) {
+            $limit = $userlimit;
         }
-        $total = $userlimit if $userlimit < $total;     # we will never exceed a user defined LIMIT and...
-        $userlimit = $total if $userlimit > $total;     # we will never exceed the total number of records available to satisfy the query
     }
     $sql .= " LIMIT ?, ?";