[siesta-commit] siesta/lib/Siesta/Storage DBI.pm,1.35,1.36

[prev] [thread] [next] [lurker] [Date index for 2003/03/15]

From: clampr
Subject: [siesta-commit] siesta/lib/Siesta/Storage DBI.pm,1.35,1.36
Date: 20:16 on 15 Mar 2003
Update of /cvsroot/siesta/siesta/lib/Siesta/Storage
In directory sc8-pr-cvs1:/tmp/cvs-serv10805/lib/Siesta/Storage

Modified Files:
	DBI.pm 
Log Message:
A metric ton of changes.

* All tables now have abstract id columns, so a users email address 
  is now $user->email and a lists name is $list->name

* Storage methods take ids, not objects

* User/List methods try to be a bit less magical/vaired about 
  what they take as parameters

Since modules and tests have been changed at the same time I expect 
there to be many bugs, but those'll be squished RSN


Index: DBI.pm
===================================================================
RCS file: /cvsroot/siesta/siesta/lib/Siesta/Storage/DBI.pm,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -d -r1.35 -r1.36
--- DBI.pm	17 Oct 2002 12:57:29 -0000	1.35
+++ DBI.pm	15 Mar 2003 20:15:50 -0000	1.36
@@ -4,6 +4,7 @@
 use Siesta::Storage;
 use base 'Siesta::Storage';
 
+use Data::Dumper;
 use DBI;
 use Class::MethodMaker get_set => ['_dbh'];
 
@@ -43,11 +44,14 @@
                map { "$_ = " . $dbh->quote( $where{$_} ) } keys %where )
       if %where;
 
-    return map {
+    warn $sql if $args{debug};
+    my @ret = map {
         my %foo;
         @foo{@columns} = @$_;
         \%foo;
     } @{ $dbh->selectall_arrayref($sql) };
+    warn Dumper \@ret if $args{debug};
+    return @ret;
 }
 
 sub _update_or_insert {
@@ -76,8 +80,9 @@
           . join ( ', ', map { $dbh->quote( $row{$_} ) } keys %row ) . " )";
     }
 
+    warn $sql if $args{debug};
     $dbh->do($sql)
-      or die $dbh->errstr;
+      or die $dbh->errstr." ".$sql;
 }
 
 sub _delete {
@@ -123,6 +128,19 @@
     grep { $self->member_of_list( $_->id, $user_id ) } $self->get_lists;
 }
 
+sub user_id_from_email {
+    my $self = shift;
+    my $email = shift;
+
+    my @results = $self->_get_rows(
+                                    table   => 'users',
+                                    where   => { email => $email },
+                                    columns => [ 'id' ]
+                                    );
+    return unless @results;
+    return $results[0]{id};
+}
+
 sub load_user {
     my $self = shift;
     my $user = shift;
@@ -149,7 +167,8 @@
     $self->_update_or_insert(
                               table => 'users',
                               where => { id => $user->id },
-                              row   => { map { $_ => $user->$_() } @fields }
+                              row   => { map { $_ => $user->$_() } @fields },
+                             #debug => 1,
                               );
 }
 
@@ -164,14 +183,13 @@
 
 sub load_list {
     my $self = shift;
-    my $id   = shift;
+    my $name = shift;
 
-    my @fields = @Siesta::List::fields;
     my @results = $self->_get_rows(
-                                    table   => 'lists',
-                                    where   => { id => $id },
-                                    columns => \@fields
-                                    );
+                                   table   => 'lists',
+                                   where   => { name => $name },
+                                   columns => \@Siesta::List::fields
+                                  );
     return unless @results == 1;
     return %{ $results[0] };
 }
@@ -204,14 +222,13 @@
     my $list = shift;
     my $user = shift;
 
-    my @fields = qw( id list_id user_id );
     my @results = $self->_get_rows(
                                     table => 'list_users',
                                     where => {
-                                               list_id => $list,
-                                               user_id => $user
+                                              list_id => $list,
+                                              user_id => $user
                                     },
-                                    columns => \@fields
+                                    columns => [ 'id' ],
                                     );
     return scalar @results;
 }
@@ -238,18 +255,32 @@
            WHERE list_id = '$list' AND user_id = '$user'} );
 }
 
+sub list_name_from_id {
+    my $self = shift;
+    my $id = shift;
+
+    my @results = $self->_get_rows
+      (
+       table   => 'lists',
+       where   => { id => $id },
+       columns => [ 'name' ],
+       #debug => 1,
+      );
+    return unless @results;
+    return $results[0]{name};
+}
+
 sub list_members {
     my $self = shift;
     my $list = shift;
 
-    my @fields = qw( id list_id user_id );
-    my @results = $self->_get_rows(
-                                    table   => 'list_users',
-                                    where   => { list_id => $list },
-                                    columns => \@fields
-                                    );
-
-    return map { Siesta::User->new_from_hash(%$_) } @results;
+    my @results = $self->_get_rows
+      (
+       table   => 'list_users',
+       where   => { list_id => $list },
+       columns => [ 'user_id' ],
+      );
+   return map { $_->{user_id} } @results;
 }
 
 sub list_plugins {
@@ -269,30 +300,22 @@
     my $self = shift;
     my $user = shift;
 
-    my @fields = qw( list_id );
     my @results = $self->_get_rows(
-                                    table   => 'list_users',
-                                    where   => { user_id => $user },
-                                    columns => \@fields
-                                    );
-
-    return map { Siesta::List->new( $_->{list_id} ) } @results;
+                                   table   => 'list_users',
+                                   where   => { user_id => $user },
+                                   columns => [ 'list_id' ],
+                                  );
+    return map { $_->{list_id} } @results;
 }
 
 # add plugin into the processing queue for a list
 # providing a number will insert at that position.
-# otherwise it will be added at the end. Position 
+# otherwise it will be added at the end. Position
 # is indexed from 1. Returns true on sucess
 sub add_plugin_to_list {
     my ( $self, $list, $plugin, $position ) = @_;
 
-    # make sure we've got a list object
-    unless ( ref $list ) {
-        $list = Siesta->storage->load_list($list)
-          or return;
-    }
-
-    my @plugins = $list->plugins();
+    my @plugins = $self->list_plugins( $list );
 
     # if it looks hacky, then that's because it is
     if ( defined $position ) {
@@ -311,25 +334,19 @@
     my $list    = shift;
     my @plugins = @_;
 
-    # make sure we've got a list object
-    unless ( ref $list ) {
-        $list = Siesta->storage->load_list($list)
-          or return;
-    }
-
     # get rid of the exisiting list.
     my $sql = sprintf "DELETE from list_plugins where list_id=%s",
-      $self->_dbh->quote( $list->id() );
+      $self->_dbh->quote( $list );
 
     $self->_dbh->do($sql)
       or die $self->_dbh->errstr;
 
     my $a = 1;
     foreach my $plugin (@plugins) {
-        my $isql = sprintf "INSERT INTO list_plugins 
-                                    ( id, plugin_id , list_id )
+        my $isql = sprintf "INSERT INTO list_plugins
+                                    ( rank, plugin_id , list_id )
                                     VALUES (%d, %s, %s )", $a++,
-          $self->_dbh->quote($plugin), $self->_dbh->quote( $list->id() );
+          $self->_dbh->quote($plugin), $self->_dbh->quote( $list );
 
         $self->_dbh->do($isql)
           or die $self->_dbh->errstr;
@@ -338,7 +355,7 @@
     return 1;
 }
 
-# sets a preference value 
+# sets a preference value
 sub set_preference {
     my ( $self, $namespace, $user_id, $list_id, $key, $value ) = @_;
 
@@ -415,7 +432,8 @@
 
 __DATA__
 CREATE TABLE users (
-        id              VARCHAR(30),
+        id              INTEGER PRIMARY KEY,
+        email           VARCHAR(30),
         forename        VARCHAR(20),
         surname         VARCHAR(20),
         password        VARCHAR(13),
@@ -424,23 +442,26 @@
         created         DATETIME
 );
 CREATE TABLE lists (
-        id              VARCHAR(20),
+        id              INTEGER PRIMARY KEY,
+        name            VARCHAR(20),
         owner           VARCHAR(20),
         post_address    VARCHAR(255),
         return_path     VARCHAR(255),
         created         DATETIME
 );
 CREATE TABLE list_users (
-        id              INT,
-        list_id         VARCHAR(20),
-        user_id         VARCHAR(30)
+        id              INTEGER PRIMARY KEY,
+        list_id         INT,
+        user_id         INT
 );
 CREATE TABLE list_plugins (
-        id              INT,
+        id              INTEGER PRIMARY KEY,
+        rank            INT,
         plugin_id       VARCHAR(20),
         list_id         VARCHAR(20)
 );
 CREATE TABLE config (
+        id              INTEGER PRIMARY KEY,
         namespace       VARCHAR(255),
         list_id         VARCHAR(20),
         user_id         VARCHAR(30),



Generated at 13:57 on 01 Jul 2004 by mariachi 0.52