[siesta-commit] siesta/lib/Siesta/Storage DBI.pm,1.6,1.7

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

From: muttley
Subject: [siesta-commit] siesta/lib/Siesta/Storage DBI.pm,1.6,1.7
Date: 17:13 on 05 Sep 2002
Update of /cvsroot/siesta/siesta/lib/Siesta/Storage
In directory usw-pr-cvs1:/tmp/cvs-serv2037/lib/Siesta/Storage

Modified Files:
	DBI.pm 
Log Message:
Fixes, fixes and more fixes.


Index: DBI.pm
===================================================================
RCS file: /cvsroot/siesta/siesta/lib/Siesta/Storage/DBI.pm,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- DBI.pm	5 Sep 2002 13:50:36 -0000	1.6
+++ DBI.pm	5 Sep 2002 16:13:16 -0000	1.7
@@ -24,10 +24,14 @@
 
     my $sql = join ('', 
                     "SELECT ", join(', ', @columns), " FROM ", $args{table},
-                    " WHERE ", join(' AND ', 
-                                    map { "$_ = '$where{$_}'" } keys %where)
                    );
 
+    $sql = join ('', $sql, 
+			" WHERE ", join(' AND ', 
+                                    map { "$_ = '$where{$_}'" } keys %where)
+		) if %where;
+
+
     return map {
         my %foo;
         @foo{@columns} = @$_;
@@ -36,6 +40,44 @@
 }
 
 
+sub get_lists {
+	my $self = shift;
+	
+	return map { Siesta::List->new_from_hash($_) } 
+		$self->_get_rows(table   => 'lists',
+				 where   => {},
+				 columns => \@Siesta::List::fields);
+}
+
+sub get_users {
+	my $self = shift;
+	
+	return map { Siesta::User->new_from_hash($_) }
+		$self->_get_rows(table   => 'users',
+				 where   => {},
+				 columns => \@Siesta::User::fields);
+}
+
+sub get_lists_of_user
+{
+	my $self    = shift;
+	my $user_id = shift;
+  	# make sure we've got the id
+        $user_id = (ref ($user_id))? $user_id->id() : $user_id;
+
+        my @returns;
+
+
+        foreach my $list ($self->get_lists())
+        {
+                push @returns, $list if 
+			($self->member_of_list($list->id(), $user_id));
+        }
+
+        return @returns;
+
+}
+
 sub load_user {
     my $self = shift;
     my $user = shift;
@@ -46,7 +88,7 @@
     my @results = $self->_get_rows(table   => 'users',
                                    where   => { id => $user },
                                    columns => \@fields);
-    return unless @results == 1;
+    return unless @results;
     return %{ $results[0] };
 }
 
@@ -58,16 +100,33 @@
     return unless defined $user;
 
     my @fields =  @Siesta::User::fields;
+	
+    my $sql;
 
+    # if it already exists then we're updating 
+    if (Siesta->storage->load_user($user->id())) {
 
-    my $sql    = join ( '',
+	$sql = join ( '', 
+			"UPDATE users SET ",
+			join (',', 
+				map { "$_=".$self->dbh->quote($user->$_()) } @fields
+			     ),
+			" WHERE id=", $self->dbh->quote($user->id())
+		    );
+
+    # inserting 
+    } else {
+   
+
+    	$sql    = join ( '',
                         "INSERT INTO users (", join (',', @fields), ") ",
                         "VALUES (", join( ',', map { $self->dbh->quote($user->$_()) } @fields) ,")"
                   ) ;
 
-   return $self->dbh->do($sql)
-	or die $self->dbh->errstr;
+    }
 
+   return $self->dbh->do($sql)
+		or die $self->dbh->errstr;
 
 }
 
@@ -103,13 +162,34 @@
 
     my @fields = @Siesta::List::fields;
 
-    my $sql    =  join ( '',
+    my $sql;
+
+    # if it already exists then we're updating 
+    if (Siesta->storage->load_list($list->id)) {
+
+	$sql = join ( '', 
+			"UPDATE lists SET ",
+			join (',', 
+				map { "$_=".$self->dbh->quote($list->$_()) } @fields
+			     ),
+			" WHERE id=", $self->dbh->quote($list->id())
+		    );
+
+    # inserting 
+    } else {
+   
+
+    	$sql    = join ( '',
                         "INSERT INTO lists (", join (',', @fields), ") ",
-                        "VALUES (", join( ',', 
-		        map { $self->dbh->quote($list->$_()) } 
-			@fields) ,")"
+                        "VALUES (", join( ',', map { $self->dbh->quote($list->$_()) } @fields) ,")"
                   ) ;
-   return $self->dbh->do($sql);
+
+    }
+
+   return $self->dbh->do($sql)
+		or die $self->dbh->errstr;
+
+
 
 
 }
@@ -171,7 +251,8 @@
     my @results = $self->_get_rows(table   => 'list_users',
                                    where   => { list_id => $list },
                                    columns => \@fields);
-    return @results;
+
+    return map { Siesta::User->new_from_hash($_) } @results;
 }
 
 





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