[siesta-commit] siesta/lib/Siesta/Storage DBI.pm,1.3,1.4 Table.pm,1.6,NONE

[prev] [thread] [next] [lurker] [Date index for 2002/09/03]

From: clampr
Subject: [siesta-commit] siesta/lib/Siesta/Storage DBI.pm,1.3,1.4 Table.pm,1.6,NONE
Date: 22:36 on 03 Sep 2002
Update of /cvsroot/siesta/siesta/lib/Siesta/Storage
In directory usw-pr-cvs1:/tmp/cvs-serv1981/lib/Siesta/Storage

Modified Files:
	DBI.pm 
Removed Files:
	Table.pm 
Log Message:
refactor Table::* into Storage::DBI

Index: DBI.pm
===================================================================
RCS file: /cvsroot/siesta/siesta/lib/Siesta/Storage/DBI.pm,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- DBI.pm	28 Aug 2002 10:06:12 -0000	1.3
+++ DBI.pm	3 Sep 2002 21:36:10 -0000	1.4
@@ -4,40 +4,60 @@
 use Siesta::Storage;
 use base 'Siesta::Storage';
 
-use Siesta::Storage::Table::Users;
-use Siesta::Storage::Table::Lists;
-use Siesta::Storage::Table::List_Users;
-use Siesta::Storage::Table::List_Plugins;
+use DBI;
+use Class::MethodMaker get_set => [ 'dbh' ];
 
 sub new {
     my $class = shift;
-    my ($db, $user, $pass) = @_;
-    Siesta::Storage::Table->set_db('Main', $db, $user, $pass,
-                                   { RaiseError => 1 } );
-    bless {}, $class;
+    my $dbh = DBI->connect(@_);
+    return unless $dbh;
+    bless { dbh => $dbh }, $class;
 }
 
+
+# yes kids, it's yet another cheesy query builder
+sub _get_rows {
+    my $self = shift;
+    my %args = @_;
+    my %where   = %{ $args{where} };
+    my @columns = @{ $args{columns} };
+
+    my $sql = join ('', 
+                    "SELECT ", join(', ', @columns), " FROM ", $args{table},
+                    " WHERE ", join(' AND ', 
+                                    map { "$_ = '$where{$_}'" } keys %where)
+                   );
+
+    return map {
+        my %foo;
+        @foo{@columns} = @$_;
+        \%foo;
+    }  @{ $self->dbh->selectall_arrayref($sql) };
+}
+
+
 sub load_user {
     my $self = shift;
     my $user = shift;
 
-    my $t_lists = Siesta::Storage::Table::Users->new();
-    my @results = $t_lists->search(id => $user);
+    my @fields =  qw(id forename surname password bouncing lastbounce created);
+    my @results = $self->_get_rows(table   => 'users',
+                                   where   => { id => $user },
+                                   columns => \@fields);
     return unless @results == 1;
-    delete $results[0]->{__Changed};
-
     return %{ $results[0] };
 }
 
+
 sub load_list {
     my $self = shift;
-    my $id = shift;
+    my $id   = shift;
 
-    my $t_lists = Siesta::Storage::Table::Lists->new();
-    my @results = $t_lists->search(id => $id);
+    my @fields =  qw(id post_address return_path owner created );
+    my @results = $self->_get_rows(table   => 'lists',
+                                   where   => { id => $id },
+                                   columns => \@fields);
     return unless @results == 1;
-
-    delete $results[0]->{__Changed} ;
     return %{ $results[0] };
 }
 
@@ -47,8 +67,11 @@
     my $list = shift;
     my $user = shift;
 
-    my $t_list_users = Siesta::Storage::Table::List_Users->new();
-    my @results = $t_list_users->search2(list_id => $list, user_id => $user);
+    my @fields =  qw( id list_id user_id );
+    my @results = $self->_get_rows(table   => 'list_users',
+                                   where   => { list_id => $list,
+                                                user_id => $user },
+                                   columns => \@fields);
     return scalar @results;
 }
 
@@ -58,11 +81,9 @@
     my $list = shift;
     my $user = shift;
 
-    # this is hacky, but I'm too lazy to learn Class::DBI today -- richardc
-    my $table = Siesta::Storage::Table::List_Users->new();
-    $table->dbh->do(qq{INSERT INTO list_users (list_id, user_id)
-                       VALUES ('$list', '$user')})
-      or die $table->dbh->errstr;
+    $self->dbh->do(qq{INSERT INTO list_users (list_id, user_id)
+                      VALUES ('$list', '$user')})
+      or die $self->dbh->errstr;
     return 1;
 }
 
@@ -72,9 +93,8 @@
     my $list = shift;
     my $user = shift;
 
-    my $t_list_users = Siesta::Storage::Table::List_Users->new();
-    my @results = $t_list_users->search2(list_id => $list, user_id => $user);
-    return $results[0]->delete();
+    return $self->dbh->do(qq{DELETE FROM list_users
+                             WHERE list_id = '$list' AND user_id = '$user'});
 }
 
 
@@ -82,8 +102,11 @@
     my $self = shift;
     my $list = shift;
 
-    my $t_list_users = Siesta::Storage::Table::List_Users->new();
-    return $t_list_users->search(list_id => $list);
+    my @fields =  qw( id list_id user_id );
+    my @results = $self->_get_rows(table   => 'list_users',
+                                   where   => { list_id => $list },
+                                   columns => \@fields);
+    return @results;
 }
 
 
@@ -91,9 +114,11 @@
     my $self = shift;
     my $list = shift;
 
-    my $t_list_plugins = Siesta::Storage::Table::List_Plugins->new();
-    my @results = $t_list_plugins->search(list_id => $list);
-    return map { $_->plugin_id } @results;
+    my @fields =  qw( id list_id plugin_id );
+    my @results = $self->_get_rows(table   => 'list_plugins',
+                                   where   => { list_id => $list },
+                                   columns => \@fields);
+    return map { $_->{plugin_id} } @results;
 }
 
 1;

--- Table.pm DELETED ---



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