[prev] [thread] [next] [lurker] [Date index for 2002/09/10]
Update of /cvsroot/siesta/siesta/lib/Siesta/Storage In directory usw-pr-cvs1:/tmp/cvs-serv19714/Siesta/Storage Modified Files: DBI.pm Volatile.pm Log Message: perltidy Index: DBI.pm =================================================================== RCS file: /cvsroot/siesta/siesta/lib/Siesta/Storage/DBI.pm,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- DBI.pm 10 Sep 2002 16:57:36 -0000 1.18 +++ DBI.pm 10 Sep 2002 18:24:30 -0000 1.19 @@ -5,67 +5,67 @@ use base 'Siesta::Storage'; use DBI; -use Class::MethodMaker get_set => [ '_dbh' ]; +use Class::MethodMaker get_set => ['_dbh']; sub new { my $class = shift; - my $dbh = DBI->connect(@_); + 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 $self = shift; + my %args = @_; + my %where = %{ $args{where} || {} }; my @columns = @{ $args{columns} }; - my $sql = join ('', - "SELECT ", join(', ', @columns), " FROM ", $args{table}, - ); + my $sql = + join ( '', "SELECT ", join ( ', ', @columns ), " FROM ", $args{table}, ); - $sql .= join ('', - " WHERE ", join(' AND ', - map { "$_ = '$where{$_}'" } keys %where) - ) if %where; + $sql .= join ( '', + " WHERE ", + join ( ' AND ', map { "$_ = '$where{$_}'" } keys %where ) ) + if %where; return map { my %foo; @foo{@columns} = @$_; \%foo; - } @{ $self->_dbh->selectall_arrayref($sql) }; + } @{ $self->_dbh->selectall_arrayref($sql) }; } - sub get_lists { my $self = shift; - return map { Siesta::List->new_from_hash($_) } - $self->_get_rows(table => 'lists', - columns => \@Siesta::List::fields); + return map { Siesta::List->new_from_hash($_) } $self->_get_rows( + table => 'lists', + columns => \@Siesta::List::fields + ); } sub get_users { my $self = shift; - return map { Siesta::User->new_from_hash($_) } - $self->_get_rows(table => 'users', - columns => \@Siesta::User::fields); + return map { Siesta::User->new_from_hash($_) } $self->_get_rows( + table => 'users', + 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; + $user_id = ( ref($user_id) ) ? $user_id->id() : $user_id; my @returns; - foreach my $list ($self->get_lists()) { + foreach my $list ( $self->get_lists() ) { push @returns, $list - if $self->member_of_list($list->id(), $user_id); + if $self->member_of_list( $list->id(), $user_id ); } return @returns; @@ -75,54 +75,62 @@ my $self = shift; my $user = shift; - my @fields = @Siesta::User::fields; - my @results = $self->_get_rows(table => 'users', - where => { id => $user }, - columns => \@fields); + my @fields = @Siesta::User::fields; + my @results = $self->_get_rows( + table => 'users', + where => { id => $user }, + columns => \@fields + ); return unless @results; return %{ $results[0] }; } - sub save_user { my $self = shift; my $user = shift; return unless defined $user; - my @fields = @Siesta::User::fields; + my @fields = @Siesta::User::fields; my $sql; + # if it already exists then we're updating - if (Siesta->storage->load_user($user->id())) { - $sql = join ( '', + 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()) - ); + 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) ,")" - ) ; + $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; } - sub delete_user { my $self = shift; my $user = shift; - $user = (ref $user)? $user->id : $user; + $user = ( ref $user ) ? $user->id : $user; - return $self->_dbh->do(qq{DELETE FROM users - WHERE id = '$user'}); + return $self->_dbh->do( + qq{DELETE FROM users + WHERE id = '$user'} ); } sub load_list { @@ -130,14 +138,15 @@ my $id = shift; my @fields = @Siesta::List::fields; - my @results = $self->_get_rows(table => 'lists', - where => { id => $id }, - columns => \@fields); + my @results = $self->_get_rows( + table => 'lists', + where => { id => $id }, + columns => \@fields + ); return unless @results == 1; return %{ $results[0] }; } - sub save_list { my $self = shift; my $list = shift; @@ -148,118 +157,129 @@ 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()) - ); - } + 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) ,")" - ) ; + $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; } - sub delete_list { my $self = shift; my $list = shift; # make sure we've got the id - $list = (ref $list)? $list->id : $list; + $list = ( ref $list ) ? $list->id : $list; - return $self->_dbh->do(qq{DELETE FROM lists - WHERE id = '$list'}); + return $self->_dbh->do( + qq{DELETE FROM lists + WHERE id = '$list'} ); } - sub member_of_list { my $self = shift; 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 }, - columns => \@fields); + 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; } - sub add_member_to_list { my $self = shift; my $list = shift; my $user = shift; - $self->_dbh->do(qq{INSERT INTO list_users (list_id, user_id) - VALUES ('$list', '$user')}) + $self->_dbh->do( + qq{INSERT INTO list_users (list_id, user_id) + VALUES ('$list', '$user')} ) or die $self->_dbh->errstr; return 1; } - sub remove_member_from_list { my $self = shift; my $list = shift; my $user = shift; - return $self->_dbh->do(qq{DELETE FROM list_users - WHERE list_id = '$list' AND user_id = '$user'}); + return $self->_dbh->do( + qq{DELETE FROM list_users + WHERE list_id = '$list' AND user_id = '$user'} ); } - 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); + 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; } - sub list_plugins { my $self = shift; my $list = shift; - my @fields = qw( id list_id plugin_id ); - my @results = $self->_get_rows(table => 'list_plugins', - where => { list_id => $list }, - columns => \@fields); + 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; } - sub user_lists { 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); + my @results = $self->_get_rows( + table => 'list_users', + where => { user_id => $user }, + columns => \@fields + ); - return map { Siesta::List->new($_->{list_id}) } @results; + return map { Siesta::List->new( $_->{list_id} ) } @results; } sub config { my $self = shift; - my ($namespace, $user_id, $list_id, $key, $value) = @_; + 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; @@ -269,39 +289,44 @@ $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) + return $self->get_config( $namespace, $user_id, $list_id, $key ) unless 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 $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 + 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), - ); + $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 + $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) - ); + $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) @@ -320,121 +345,117 @@ # default sub get_config { - my ($self, $namespace, $user_id, $list_id, $key) = @_; + my ( $self, $namespace, $user_id, $list_id, $key ) = @_; $user_id ||= ''; $list_id ||= ''; - - unless ($user_id eq '' || $list_id eq '') { - my $return = $self->get_config_explicitly($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, $user_id, '', $key); - return $return + unless ( $user_id eq '' ) { + my $return = + $self->get_config_explicitly( $namespace, $user_id, '', $key ); + return $return if defined $return; } - unless ($list_id eq '') { - my $return = $self->get_config_explicitly($namespace, '', $list_id, $key); + unless ( $list_id eq '' ) { + my $return = + $self->get_config_explicitly( $namespace, '', $list_id, $key ); return $return if defined $return; } - return $self->get_config_explicitly($namespace, '', '', $key); + 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 ( $self, $namespace, $user_id, $list_id, $key ) = @_; # just to be sure $user_id ||= ''; $list_id ||= ''; - my $where = { user_id => $user_id, + my $where = { + user_id => $user_id, list_id => $list_id, key => $key, - namespace => $namespace }; + namespace => $namespace + }; - my @results = $self->_get_rows(table => 'config', - where => $where, - columns => ['value']); + my @results = $self->_get_rows( + table => 'config', + where => $where, + columns => ['value'] + ); return $results[0]->{value}; } - # 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 # is indexed from 1. Returns 1 on success or undef # on failure. -sub add_plugin_to_list -{ - my ($self, $list, $plugin, $position) = @_; +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) || return undef; - } + # make sure we've got a list object + unless ( ref $list ) { + $list = Siesta->storage->load_list($list) || return undef; + } - my @plugins = $list->plugins(); + my @plugins = $list->plugins(); - # if it looks hacky, then that's because it is - if (defined $position) { - push @plugins, ($plugin, splice (@plugins, $position)); - } else { - push @plugins, $plugin; - } + # if it looks hacky, then that's because it is + if ( defined $position ) { + push @plugins, ( $plugin, splice( @plugins, $position ) ); + } + else { + push @plugins, $plugin; + } - return $self->set_plugins ($list, @plugins); + return $self->set_plugins( $list, @plugins ); } - # sets the order of the plugins for a list -sub set_plugins -{ - my $self = shift; - my $list = shift; - my @plugins = @_; - - # make sure we've got a list object - unless (ref $list) { - $list = Siesta->storage->load_list($list) || return undef; - } +sub set_plugins { + my $self = shift; + my $list = shift; + my @plugins = @_; + # make sure we've got a list object + unless ( ref $list ) { + $list = Siesta->storage->load_list($list) || return undef; + } - # get rid of the exisiting list. - my $sql = sprintf "DELETE from list_plugins where list_id=%s", - $self->_dbh->quote($list->id()); - - $self->_dbh->do($sql) - or die $self->_dbh->errstr; + # get rid of the exisiting list. + my $sql = sprintf "DELETE from list_plugins where list_id=%s", + $self->_dbh->quote( $list->id() ); + $self->_dbh->do($sql) + or die $self->_dbh->errstr; - my $a = 1; - foreach my $plugin (@plugins) { - my $isql = sprintf "INSERT INTO list_plugins + my $a = 1; + foreach my $plugin (@plugins) { + my $isql = sprintf "INSERT INTO list_plugins ( id, plugin_id , list_id ) - VALUES (%d, %s, %s )", - $a++, - $self->_dbh->quote($plugin), - $self->_dbh->quote($list->id()); + VALUES (%d, %s, %s )", $a++, $self->_dbh->quote($plugin), + $self->_dbh->quote( $list->id() ); - $self->_dbh->do($isql) - or die $self->_dbh->errstr; - } + $self->_dbh->do($isql) + or die $self->_dbh->errstr; + } - return 1; + return 1; } - 1; Index: Volatile.pm =================================================================== RCS file: /cvsroot/siesta/siesta/lib/Siesta/Storage/Volatile.pm,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- Volatile.pm 7 Sep 2002 15:18:22 -0000 1.3 +++ Volatile.pm 10 Sep 2002 18:24:32 -0000 1.4 @@ -13,51 +13,44 @@ my $class = shift; my $self = {}; - $self->{users} = []; - $self->{lists} = []; - $self->{list_users} = []; + $self->{users} = []; + $self->{lists} = []; + $self->{list_users} = []; $self->{list_plugins} = []; } - sub load_user { my $self = shift; my $user = shift; } - sub load_list { my $self = shift; my $id = shift; } - sub member_of_list { my $self = shift; my $list = shift; my $user = shift; } - sub add_member_to_list { my $self = shift; my $list = shift; my $user = shift; } - sub remove_member_from_list { my $self = shift; my $list = shift; my $user = shift; } - sub list_members { my $self = shift; my $list = shift; } - sub list_plugins { my $self = shift;
Generated at 13:57 on 01 Jul 2004 by mariachi 0.52