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

svn at pti.co.il svn at pti.co.il
Sun Jul 29 08:36:19 EEST 2007


Author: gabor
Date: 2007-07-29 08:36:19 +0300 (Sun, 29 Jul 2007)
New Revision: 312

Modified:
   trunk/
   trunk/lib/CPAN/Forum/DB/Subscriptions.pm
   trunk/lib/CPAN/Forum/DB/Subscriptions_all.pm
   trunk/lib/CPAN/Forum/DB/Subscriptions_pauseid.pm
   trunk/lib/CPAN/Forum/DBI.pm
   trunk/lib/CPAN/Forum/RM/Subscriptions.pm
   trunk/templates/mypan.tmpl
   trunk/templates/navigation.tmpl
Log:
most of mypan move to use SQL but I think this is change is not final



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

Modified: trunk/lib/CPAN/Forum/DB/Subscriptions.pm
===================================================================
--- trunk/lib/CPAN/Forum/DB/Subscriptions.pm	2007-07-29 05:35:56 UTC (rev 311)
+++ trunk/lib/CPAN/Forum/DB/Subscriptions.pm	2007-07-29 05:36:19 UTC (rev 312)
@@ -37,4 +37,10 @@
     return ($sql, @args{@fields}); 
 }
 
+sub complex_update {
+    my ($self, @args) = @_;
+    $self->_complex_update(@args, 'subscriptions');
+}
+
+
 1;

Modified: trunk/lib/CPAN/Forum/DB/Subscriptions_all.pm
===================================================================
--- trunk/lib/CPAN/Forum/DB/Subscriptions_all.pm	2007-07-29 05:35:56 UTC (rev 311)
+++ trunk/lib/CPAN/Forum/DB/Subscriptions_all.pm	2007-07-29 05:36:19 UTC (rev 312)
@@ -27,15 +27,46 @@
     my ($self, %args) = @_;
 
     # check if keys of args is uid 
-    my @fields = keys %args;
-    my $where = join " AND ", map {"$_=?"} @fields;
+    my ($where, @values) = $self->_prep_where(\%args);
     my $sql = "SELECT id, allposts, starters, followups, announcements
               FROM subscriptions_all";
     if ($where) {
         $sql .= " WHERE $where";
     }
-    return ($sql, @args{@fields}); 
+    return ($sql, @values);
 }
 
+sub _prep_where {
+    my ($self, $args) = @_;
+    #Carp::cluck (Data::Dumper->Dump([$args], ['args']));
 
+    my @fields = keys %$args;
+    my $where = join " AND ", map {"$_=?"} @fields;
+    my %args = %$args;
+    return ($where, @args{@fields}); 
+}
+
+sub _prep_set {
+    my ($self, $args) = @_;
+    my @fields = keys %$args;
+
+    my $where = join ", ", map {"$_=?"} @fields;
+    return ($where, @{ $args->{@fields} }); 
+}
+
+sub _prep_insert {
+    my ($self, $args) = @_;
+
+    my @fields = keys %$args;
+    my $fields = join ", ", @fields;
+    my $placeholders = join ", ", (("?") x scalar @fields);
+    return ($fields, $placeholders, @{ $args->{@fields} }); 
+}
+
+sub complex_update {
+    my ($self, @args) = @_;
+    $self->_complex_update(@args, 'subscriptions_all');
+}
+
+
 1;

Modified: trunk/lib/CPAN/Forum/DB/Subscriptions_pauseid.pm
===================================================================
--- trunk/lib/CPAN/Forum/DB/Subscriptions_pauseid.pm	2007-07-29 05:35:56 UTC (rev 311)
+++ trunk/lib/CPAN/Forum/DB/Subscriptions_pauseid.pm	2007-07-29 05:36:19 UTC (rev 312)
@@ -28,7 +28,7 @@
     # check if keys of args is uid 
     my @fields = keys %args;
     my $where = join " AND ", map {"$_=?"} @fields;
-    my $sql = "SELECT subscriptions_pauseid.id, uid, allposts, starters, followups, announcements,
+    my $sql = "SELECT subscriptions_pauseid.id pasuseid, uid, allposts, starters, followups, announcements,
         authors.pauseid pauseid_name FROM subscriptions_pauseid, authors WHERE authors.id=subscriptions_pauseid.pauseid";
     if ($where) {
         $sql .= " AND $where";
@@ -36,5 +36,10 @@
     return ($sql, @args{@fields}); 
 }
 
+sub complex_update {
+    my ($self, @args) = @_;
+    $self->_complex_update(@args, 'subscriptions');
+}
 
+
 1;

Modified: trunk/lib/CPAN/Forum/DBI.pm
===================================================================
--- trunk/lib/CPAN/Forum/DBI.pm	2007-07-29 05:35:56 UTC (rev 311)
+++ trunk/lib/CPAN/Forum/DBI.pm	2007-07-29 05:36:19 UTC (rev 312)
@@ -133,5 +133,56 @@
     return \%h;
 }
 
+
+# code for the Subscription* tables
+sub _complex_update {
+    my ($self, $where, $on, $data, $table) = @_;
+    if ($on) {
+        my $s = $self->find_one($table, %$where);
+        if ($s) {
+            $self->update($table, $where, $data);
+        } else {
+            $self->add($table, {%$data, %$where} );
+        }
+        
+    } else {
+        $self->delete($table, $where);
+    }
+    return;
+}
+
+sub add {
+    my ($self, $table, $args) = @_;
+    my ($fields, $placeholders, @values) = $self->_prep_insert($args);
+    my $sql = "INSERT INTO subscriptions_all ($fields) VALUES($placeholders)";
+    my $dbh = CPAN::Forum::DBI::db_Main();
+    $dbh->do($sql, undef, @values);
+    return;
+}    
+
+
+sub update {
+    my ($self, $table, $args, $data) = @_;
+    my ($where, @values)   = $self->_prep_where($args);
+    Carp::croak("") if not $where;
+    my ($set, @new_values) = $self->_prep_set($data);
+    my $sql = "UPDATE subscriptions_all $set $where";
+    my $dbh = CPAN::Forum::DBI::db_Main();
+    $dbh->do($sql, undef, @new_values, @values);
+    return;
+}
+
+sub delete {
+    my ($self, $table, $args) = @_;
+    my ($where, @values) = $self->_prep_where($args);
+    my $sql = "DELETE FROM $table";
+    if ($where) {
+        $sql .= " WHERE $where";
+    }
+    my $dbh = CPAN::Forum::DBI::db_Main();
+    $dbh->do($sql, undef, @values);
+    return;
+}
+
 1;
 

Modified: trunk/lib/CPAN/Forum/RM/Subscriptions.pm
===================================================================
--- trunk/lib/CPAN/Forum/RM/Subscriptions.pm	2007-07-29 05:35:56 UTC (rev 311)
+++ trunk/lib/CPAN/Forum/RM/Subscriptions.pm	2007-07-29 05:36:19 UTC (rev 312)
@@ -83,7 +83,7 @@
 
     my $it = CPAN::Forum::DB::Subscriptions_pauseid->find(uid => $user->{id}); # SQL
     foreach my $s (@$it) {
-        $gids .= ($gids ? ",_" : "_") . $s->{pauseid}; 
+        $gids .= ",_" . $s->{pauseid}; 
         push @subscriptions, {
             gid       => "_" . $s->{pauseid},
             group     => $s->{pauseid_name},
@@ -154,39 +154,32 @@
     my $uid = $user->{id};
 
     foreach my $gid (@gids) {
+        my ($on, $data) = $self->_get_subs($gid);
+
         if ($gid eq "_all") {
-            my ($s) = CPAN::Forum::DB::Subscriptions_all->search(uid => $uid);
-            if (not $s) {
-                $s = CPAN::Forum::DB::Subscriptions_all->create({
-                    uid       => $uid,
-                });
-            }
-            $self->_update_subs($s, $gid);
+            CPAN::Forum::DB::Subscriptions_all->complex_update({uid => $uid}, $on, $data);
         } elsif ($gid =~ /^_(\d+)$/) {
             my $pauseid = $1;
-            my ($s) = CPAN::Forum::DB::Subscriptions_pauseid->search(pauseid => $pauseid, uid => $uid);
-            if (not $s) {
-                $s = CPAN::Forum::DB::Subscriptions->create({
-                    uid       => $uid,
-                    pauseid   => $pauseid,
-                });
-            }
-            $self->_update_subs($s, $gid);
+            CPAN::Forum::DB::Subscriptions_pauseid->complex_update({pauseid => $pauseid, uid => $uid}, $on, $data); # SQL
         } elsif ($gid =~ /^(\d+)$/) {
-            my ($s) = CPAN::Forum::DB::Subscriptions->search(gid => $gid, uid => $uid);
-            if (not $s) {
-                $s = CPAN::Forum::DB::Subscriptions->create({
-                    uid       => $uid,
-                    gid       => $gid,
-                });
-            }
-            $self->_update_subs($s, $gid);
+            CPAN::Forum::DB::Subscriptions->complex_update({gid => $gid, uid => $uid}, $on, $data); #SQL 
         } else {
             $self->log->error("Invalid gid: '$gid' provided in the gids entry of mypan");
             # shall we show an error page here?
         }
     }
     
+    return $self->notes("mypanok");
+
+=pod
+    # I think we don't need to provide this
+    # A user either comes from the page of a Module ( /dist/Module-Name) 
+    # and then we already have the value.
+    # There should be also a similar link on the /author/PAUSEID  page
+    # to link to mypan/author/PAUSEID
+    
+
+    # name is the empty textbox to hold an arbitrary values 
     # if there is not name, no need for further processing
     if (not $q->param("name")) {
         return $self->notes("mypanok");
@@ -232,6 +225,8 @@
     }
 
     return $self->notes("mypanok");
+=cut
+
 }
 
 =head2 _update_subs
@@ -262,5 +257,23 @@
     }
 }
 
+sub _get_subs {
+    my ($self, $gid) = @_;
+    my $q = $self->query;
+
+    my %data;
+    my $on = 0;
+    foreach my $type (qw(allposts starters followups)) {
+        if (defined $q->param($type ."_$gid") and $q->param($type . "_$gid") eq "on") {
+            $data{$type} = 1;
+            $on++;
+        } else {
+            $data{$type} = 0;
+        }
+    }
+    return ($on, \%data);
+}
+
+
 1;
 

Modified: trunk/templates/mypan.tmpl
===================================================================
--- trunk/templates/mypan.tmpl	2007-07-29 05:35:56 UTC (rev 311)
+++ trunk/templates/mypan.tmpl	2007-07-29 05:36:19 UTC (rev 312)
@@ -35,6 +35,7 @@
   <td><input type="checkbox" name="followups_<TMPL_VAR gid>" <TMPL_IF followups>CHECKED</TMPL_IF> /></td>
 </tr>
 </TMPL_LOOP>
+<!--
 <tr>
   <td><input name="name" size="20" />
       <select name="type">
@@ -47,6 +48,7 @@
   <td><input type="checkbox" name="starters__new"  <TMPL_IF starters>CHECKED</TMPL_IF> /></td>
   <td><input type="checkbox" name="followups__new" <TMPL_IF followups>CHECKED</TMPL_IF> /></td>
 </tr>
+-->
 </table>
 
 </div>

Modified: trunk/templates/navigation.tmpl
===================================================================
--- trunk/templates/navigation.tmpl	2007-07-29 05:35:56 UTC (rev 311)
+++ trunk/templates/navigation.tmpl	2007-07-29 05:36:19 UTC (rev 312)
@@ -20,3 +20,6 @@
     | <a href="/tags/">tags</a>
 ]
 </div>
+<div class="navigation">
+<a href="http://perlsurvey.org/">Take the Perl Survey Now!</a>
+</div>



More information about the Cpan-forum-commit mailing list