%
my $conf = new FS::Conf;
my $maxrecords = $conf->config('maxsearchrecordsperpage');
my $limit = '';
$limit .= "LIMIT $maxrecords" if $maxrecords;
my $offset = $cgi->param('offset') || 0;
$limit .= " OFFSET $offset" if $offset;
my $total;
my $sql = $cgi->param('sql');
$sql =~ s/^\s*SELECT//i;
my $count_sql = $sql;
$count_sql =~ s/^(.*)\s+FROM\s/COUNT(*) FROM /i;
my $sth = dbh->prepare("SELECT $count_sql")
  or eidiot dbh->errstr. " doing $count_sql\n";
$sth->execute or eidiot "Error executing \"$count_sql\": ". $sth->errstr;
$total = $sth->fetchrow_arrayref->[0];
my $sth = dbh->prepare("SELECT $sql $limit")
  or eidiot dbh->errstr. " doing $sql\n";
$sth->execute or eidiot "Error executing \"$sql\": ". $sth->errstr;
my $rows = $sth->fetchall_arrayref;
%>
<%
  #begin pager
  my $pager = '';
  if ( $total != scalar(@$rows) && $maxrecords ) {
    unless ( $offset == 0 ) {
      $cgi->param('offset', $offset - $maxrecords);
      $pager .= 'Previous ';
    }
    my $poff;
    my $page;
    for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
      $page++;
      if ( $offset == $poff ) {
        $pager .= qq!$page !;
      } else {
        $cgi->param('offset', $poff);
        $pager .= qq!$page !;
      }
    }
    unless ( $offset + $maxrecords > $total ) {
      $cgi->param('offset', $offset + $maxrecords);
      $pager .= 'Next ';
    }
  }
  #end pager
  print header('Query Results', menubar('Main Menu'=>$p) ).
        "$total total rows
$pager". table().
        "