[Cpan-forum-commit] rev 344 - in trunk: . lib/CPAN lib/CPAN/Forum lib/CPAN/Forum/DB

svn at pti.co.il svn at pti.co.il
Tue Aug 14 11:34:51 EEST 2007


Author: gabor
Date: 2007-08-14 11:34:51 +0300 (Tue, 14 Aug 2007)
New Revision: 344

Modified:
   trunk/
   trunk/lib/CPAN/Forum.pm
   trunk/lib/CPAN/Forum/DB/Posts.pm
   trunk/lib/CPAN/Forum/DBI.pm
Log:
improve _prep_where to be able to handle LIKE parameters as well



Property changes on: trunk
___________________________________________________________________
Name: svk:merge
   - 7bc34947-122d-0410-bc5a-f898d2bb5f81:/local/cpan-forum:4430
8c4c90e1-83eb-0310-96eb-e7cb62807872:/local/cpan-forum:12752
   + 7bc34947-122d-0410-bc5a-f898d2bb5f81:/local/cpan-forum:4431
8c4c90e1-83eb-0310-96eb-e7cb62807872:/local/cpan-forum:12752

Modified: trunk/lib/CPAN/Forum/DB/Posts.pm
===================================================================
--- trunk/lib/CPAN/Forum/DB/Posts.pm	2007-08-14 08:34:35 UTC (rev 343)
+++ trunk/lib/CPAN/Forum/DB/Posts.pm	2007-08-14 08:34:51 UTC (rev 344)
@@ -127,14 +127,12 @@
     #%where = (1 => 1) if not %where;
     $CPAN::Forum::logger->debug(Data::Dumper->Dump([\%where], ['where']));
 
-    my $pager = __PACKAGE__->pager(
+    my $pager = __PACKAGE__->mypager(
         where         => \%where,
         per_page      => $params->{per_page} || 10,
         page          => $params->{page}     || 1,
         order_by      => $params->{order_by} || "id DESC",
     );
-
-    #my $sql = "SELECT COUNT(*) FROM posts";
 }
 
 sub list_counted_posts {

Modified: trunk/lib/CPAN/Forum/DBI.pm
===================================================================
--- trunk/lib/CPAN/Forum/DBI.pm	2007-08-14 08:34:35 UTC (rev 343)
+++ trunk/lib/CPAN/Forum/DBI.pm	2007-08-14 08:34:51 UTC (rev 344)
@@ -202,9 +202,29 @@
     #Carp::cluck (Data::Dumper->Dump([$args], ['args']));
 
     my @fields = keys %$args;
-    my $where = join " AND ", map {"$_=?"} @fields;
+    my @FIELDS;
+    my @values;
+    foreach my $f (@fields) {
+        if (not ref $args->{$f}) {
+            push @FIELDS, "$f=?";
+            push @values, $args->{$f};
+        } elsif ('HASH' eq ref $args->{$f}) {
+            my @k = keys %{ $args->{$f} };
+            Carp::croak("don't know how to handle more than one keys in $f") if @k != 1;
+            if ($k[0] eq 'LIKE') {
+                push @FIELDS, "$f LIKE ?";
+                push @values, $args->{$f}{$k[0]};
+            } else {
+                Carp::croak("don't know how to handle $k[0] in $f");
+            }
+        } else {
+            Carp::croak("don't know how to handle $args->{$f}");
+        }
+    }
+
+    my $where = join " AND ", @FIELDS;
     my %args = %$args;
-    return ($where, @args{@fields}); 
+    return ($where, @values); 
 }
 
 sub _prep_set {
@@ -237,6 +257,36 @@
     #return ($fields, $placeholders, @{ $args->{@fields} }); 
 }
 
+sub mypager {
+    my ($self, %args) = @_;
+    my ($where, @values) = $self->_prep_where($args{where});
+    $CPAN::Forum::logger->debug("where='$where'");
 
+    my $fetch_sql = "";
+    my $count_sql = "SELECT COUNT(*) FROM posts";
+    my @fetch_values;
+
+    if ($where) {
+        $fetch_sql .= " WHERE $where";
+        $count_sql .= " WHERE $where";
+    }
+
+    $fetch_sql .= " LIMIT ?";
+    my $limit = $args{per_page} || 10;
+    push @fetch_values, $limit;
+
+    my $page = $args{page} || 1;
+    if ($page > 1) {
+        $fetch_sql .= " OFFSET ?";
+        push @fetch_values, $limit*($page-1);
+    }
+
+    $fetch_sql .= "ORDER BY $args{order_by}";
+    $CPAN::Forum::logger->debug("count_sql='$count_sql' " . Data::Dumper->Dump([\@values], ['values']));
+    my $total = $self->_fetch_single_value($count_sql, @values);
+    $CPAN::Forum::logger->debug("total='$total'");
+
+    return {};
+}
 1;
 

Modified: trunk/lib/CPAN/Forum.pm
===================================================================
--- trunk/lib/CPAN/Forum.pm	2007-08-14 08:34:35 UTC (rev 343)
+++ trunk/lib/CPAN/Forum.pm	2007-08-14 08:34:51 UTC (rev 344)
@@ -758,7 +758,7 @@
     
     foreach my $post (@$it) {
         #$self->log->debug(Data::Dumper->Dump([$post], ['post']));
-        $self->log->debug("id=" . $post->id);
+        #$self->log->debug("id=" . $post->id);
 #warn "called for each post";
         my $thread = $post->thread;
         my $thread_count = ($thread and $threads->{$thread}) ? $threads->{$thread}{cnt} : 0;



More information about the Cpan-forum-commit mailing list