[siesta-commit] siesta/lib/Siesta/Storage DBI.pm,1.9,1.10

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

From: muttley
Subject: [siesta-commit] siesta/lib/Siesta/Storage DBI.pm,1.9,1.10
Date: 12:00 on 08 Sep 2002
Update of /cvsroot/siesta/siesta/lib/Siesta/Storage
In directory usw-pr-cvs1:/tmp/cvs-serv24340/lib/Siesta/Storage

Modified Files:
	DBI.pm 
Log Message:
Config stuff.


Index: DBI.pm
===================================================================
RCS file: /cvsroot/siesta/siesta/lib/Siesta/Storage/DBI.pm,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- DBI.pm	7 Sep 2002 15:19:52 -0000	1.9
+++ DBI.pm	8 Sep 2002 11:00:24 -0000	1.10
@@ -265,4 +265,133 @@
     return map { Siesta::List->new($_->{list_id}) } @results;
 }
 
+sub config
+{
+	my $self = shift;
+
+
+
+	my ($namespace, $user_id, $list_id, $key, $value) = @_;
+
+	# make sure that we get the id and not the object
+	if (defined $user_id) {
+		$user_id = (ref $user_id)?$user_id->id():$user_id;
+	}
+
+
+	# make sure that we get the id and not the object
+	if (defined $list_id) {
+		$list_id = (ref $list_id)?$list_id->id():$list_id;
+	}
+
+	# make suyre they're not undef
+	$user_id ||= '';
+	$list_id ||= '';
+
+
+	# if we haven't been passed a value then fetch and return it
+	return $self->_get_config($namespace, $user_id, $list_id, $key) 
+				if (!defined $value);	
+
+
+	
+
+
+	# otherwise we need to check to see if it's been defined 
+	# explicitly for this particular tuple and update or insert
+	# appropriately.
+
+	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);
+
+	# ... or inserting
+	} else {
+		$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;
+
+	
+	
+
+}
+
+# work out the first valid config value
+# the preferred order is
+#
+#         per user, per list
+#                 |
+#              per user        
+#                 |
+#              per listÅ
+#                 |
+#           system default
+#
+sub _get_config
+{
+	my ($self, $namespace, $user_id, $list_id, $key) = @_;
+
+	unless (($user_id eq '')  || ($list_id eq ''))
+	{
+		my $return = $self->_get_config_explicitly($namespace, $user_id, $list_id, $key);
+		return $return if defined $return;
+	}	
+
+	unless ($user_id eq '')
+	{
+		my $return = $self->_get_config_explicitly($namespace, '', $list_id, $key);
+		return $return if defined $return;
+	}	
+
+	unless ($list_id eq '')
+	{
+		my $return = $self->_get_config_explicitly($namespace, $user_id, '', $key);
+		return $return if defined $return;
+	}	
+
+	return $self->_get_config_explicitly($namespace, '', '', $key);
+
+	
+}
+
+
+# explicitly look up a config value for a given
+# namespace, user, list, key tuple. 
+sub _get_config_explicitly
+{
+	my ($self, $namespace, $user_id, $list_id, $key) = @_;
+
+	
+	my @results = $self->_get_rows(table  => 'config',
+				 	where  => { user_id   => $user_id,
+					            list_id   => $list_id,
+				                    key       => $key,
+					            namespace => $namespace },
+				        columns => ['value']);
+
+	return $results[0]->{value};		
+
+
+}
+
 1;



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