we don't actually need the results ordered, and i'm sure it doesn't help the planner...
[freeside.git] / FS / FS / Cron / bill.pm
index 899b117..225b372 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use vars qw( @ISA @EXPORT_OK );
 use Exporter;
 use Date::Parse;
+use DBI 1.33; #The "clone" method was added in DBI 1.33. 
 use FS::UID qw(dbh);
 use FS::Record qw(qsearchs);
 use FS::cust_main;
@@ -95,24 +96,23 @@ END
 
   push @search, "( $where_pkg OR $where_event )";
 
-  my $prev_custnum = 0;
-  while ( 1 ) {
+  ###
+  # get a list of custnums
+  ###
 
-    ###
-    # get a list of custnums
-    ###
+  warn "searching for customers:\n". join("\n", @search). "\n"
+    if $opt{'v'} || $opt{'l'};
 
-    warn "searching for customers:\n".
-           join("\n", @search).
-           "custnum > $prev_custnum\n"
-      if $opt{'v'} || $opt{'l'};
+  my $cursor_dbh = dbh->clone;
 
-    my $sth = dbh->prepare(
-      "SELECT custnum FROM cust_main".
-      " WHERE ". join(' AND ', @search).
-      " AND custnum > $prev_custnum ".
-      " ORDER BY custnum LIMIT 1000 "
-    ) or die dbh->errstr;
+  $cursor_dbh->do(
+    "DECLARE cron_bill_cursor CURSOR FOR ".
+    "  SELECT custnum FROM cust_main WHERE ". join(' AND ', @search)
+  ) or die $cursor_dbh->errstr;
+
+  while ( 1 ) {
+
+    my $sth = $cursor_dbh->prepare('FETCH 100 FROM cron_bill_cursor'); #mysql?
 
     $sth->execute or die $sth->errstr;
 
@@ -120,8 +120,6 @@ END
 
     last unless scalar(@custnums);
 
-    $prev_custnum = $custnums[-1];
-
     ###
     # for each custnum, queue or make one customer object and bill
     # (one at a time, to reduce memory footprint with large #s of customers)
@@ -159,6 +157,8 @@ END
 
   }
 
+  $cursor_dbh->commit or die $cursor_dbh->errstr;
+
 }
 
 1;