[siesta-commit] siesta/lib/Siesta/Storage DBI.pm,1.27,1.28

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

From: clampr
Subject: [siesta-commit] siesta/lib/Siesta/Storage DBI.pm,1.27,1.28
Date: 13:13 on 15 Sep 2002
Update of /cvsroot/siesta/siesta/lib/Siesta/Storage
In directory usw-pr-cvs1:/tmp/cvs-serv26557

Modified Files:
	DBI.pm 
Log Message:
remove a crapload of cookie-cutter code


Index: DBI.pm
===================================================================
RCS file: /cvsroot/siesta/siesta/lib/Siesta/Storage/DBI.pm,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -d -r1.27 -r1.28
--- DBI.pm	12 Sep 2002 14:14:51 -0000	1.27
+++ DBI.pm	15 Sep 2002 12:13:15 -0000	1.28
@@ -11,7 +11,7 @@
     my $class = shift;
     my $dbh   = DBI->connect(@_);
 
-    unless ($dbh) {    # busy wait for SQLite
+    unless ($dbh && $_[0] =~ /:SQLite:/) {    # busy wait for SQLite
         for ( 1 .. 10 ) {
             sleep 2;
             $dbh = DBI->connect(@_)
@@ -49,6 +49,35 @@
     } @{ $self->_dbh->selectall_arrayref($sql) };
 }
 
+sub _update_or_insert {
+    my $self = shift;
+    my %args = @_;
+    my %where   = %{ $args{where} || {} };
+    my %row = %{ $args{row} };
+    my $dbh = $self->_dbh;
+
+    my $sql;
+    if ( $self->_get_rows( %args, columns => [ keys %row ]) ) {
+        $sql = "UPDATE $args{table} SET " . join ( ', ', map {
+            "$_ = " . $dbh->quote( $row{$_} )
+        } keys %row );
+
+        $sql .= " WHERE ". join ( ' AND ', map {
+            "$_ = " . $dbh->quote( $where{$_} )
+        } keys %where )
+          if %where;
+    }
+    else {
+        $sql = "INSERT INTO $args{table} (" . join(', ', keys %row) .
+          ") VALUES ( " . join ( ', ', map { 
+              $dbh->quote( $row{$_} )
+          } keys %row ) . " )";
+    }
+
+    $dbh->do($sql)
+      or die $dbh->errstr;
+}
+
 sub get_lists {
     my $self = shift;
 
@@ -99,32 +128,9 @@
     my @fields = @Siesta::User::fields;
     my $sql;
 
-    # if it already exists then we're updating 
-    if ( Siesta->storage->load_user( $user->id() ) ) {
-        $sql = join (
-                      '',
-                      "UPDATE users SET ",
-                      join ( ',',
-                             map { "$_=" . $self->_dbh->quote( $user->$_() ) }
-                               @fields ),
-                      " WHERE id=",
-                      $self->_dbh->quote( $user->id() )
-                      );
-    }
-    else {
-
-        # inserting
-        $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;
+    $self->_update_or_insert( table => 'users',
+                              where => { id => $user->id },
+                              row   => { map { $_ => $user->$_() } @fields } );
 }
 
 sub delete_user {
@@ -134,8 +140,7 @@
     $user = ( ref $user ) ? $user->id : $user;
 
     return $self->_dbh->do(
-        qq{DELETE FROM users
-                              WHERE id = '$user'} );
+        qq{DELETE FROM users WHERE id = '$user'} );
 }
 
 sub load_list {
@@ -159,34 +164,9 @@
     return unless defined $list;
 
     my @fields = @Siesta::List::fields;
-    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() )
-                      );
-    }
-    else {
-
-        # inserting 
-        $sql = join ( '',
-                "INSERT INTO lists (",
-                join ( ',', @fields ),
-                ") ",
-                "VALUES (",
-                join ( ',', map { $self->_dbh->quote( $list->$_() ) } @fields ),
-                ")" );
-    }
-
-    return $self->_dbh->do($sql)
-      or die $self->_dbh->errstr;
+    $self->_update_or_insert( table => 'lists',
+                              where => { id => $list->id },
+                              row   => { map { $_ => $list->$_() } @fields } );
 }
 
 sub delete_list {
@@ -197,8 +177,7 @@
     $list = ( ref $list ) ? $list->id : $list;
 
     return $self->_dbh->do(
-        qq{DELETE FROM lists
-                              WHERE id = '$list'} );
+        qq{DELETE FROM lists WHERE id = '$list'} );
 }
 
 sub member_of_list {
@@ -224,7 +203,7 @@
     my $user = shift;
 
     $self->_dbh->do(
-        qq{INSERT INTO list_users (list_id, user_id)
+                    qq{INSERT INTO list_users (list_id, user_id)
                        VALUES ('$list', '$user')} )
       or die $self->_dbh->errstr;
     return 1;
@@ -237,7 +216,7 @@
 
     return $self->_dbh->do(
         qq{DELETE FROM list_users
-                              WHERE list_id = '$list' AND user_id = '$user'} );
+           WHERE list_id = '$list' AND user_id = '$user'} );
 }
 
 sub list_members {
@@ -286,8 +265,8 @@
 
     my ( $namespace, $user_id, $list_id, $key, $value ) = @_;
 
-    $user_id = ref $user_id ? $user_id->id : $user_id;
-    $list_id = ref $list_id ? $list_id->id : $list_id;
+    $user_id = UNIVERSAL::can($user_id, 'id') ? $user_id->id : $user_id;
+    $list_id = UNIVERSAL::can($list_id, 'id') ? $list_id->id : $list_id;
 
     # make suyre they're not undef
     $user_id ||= '';
@@ -304,38 +283,18 @@
     my $return =
       $self->get_config_explicitly( $namespace, $user_id, $list_id, $key );
 
-    my $sql;
-
-    # are we updating
-    if ( defined $return ) {
-        $sql = sprintf(
-            "UPDATE config set value = %s where
-                        namespace = %s AND user_id = %s AND
-                        list_id = %s AND key = %s",
-            $self->_dbh->quote($value),
-            $self->_dbh->quote($namespace),
-            $self->_dbh->quote($user_id),
-            $self->_dbh->quote($list_id),
-            $self->_dbh->quote($key),
-            );
-    }
-    else {
-
-        # ... or inserting
-        $sql = sprintf(
-            "INSERT INTO config 
-                        (namespace, user_id, list_id, key, value) 
-                        VALUES (%s, %s, %s, %s, %s)",
-            $self->_dbh->quote($namespace),
-            $self->_dbh->quote($user_id),
-            $self->_dbh->quote($list_id),
-            $self->_dbh->quote($key),
-            $self->_dbh->quote($value)
-            );
-    }
-
-    return $self->_dbh->do($sql)
-      or die $self->_dbh->errstr;
+    $self->_update_or_insert( table => 'config',
+                              where => { namespace => $namespace,
+                                         user_id => $user_id,
+                                         list_id => $list_id,
+                                         key => $key },
+                              row =>   { namespace => $namespace,
+                                         user_id => $user_id,
+                                         list_id => $list_id,
+                                         key => $key,
+                                         value => $value
+                                       },
+                              );
 }
 
 # work out the first valid config value



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