Parent Directory
|
Revision Log
fixed new implementations of "get" -- they need to return a single item, not a list of items!
1 ################################################################################ 2 # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project 3 # $Id$ 4 ################################################################################ 5 6 package WeBWorK::DB::Schema::Auth1Hash; 7 use base qw(WeBWorK::DB::Schema); 8 9 =head1 NAME 10 11 WeBWorK::DB::Schema::Auth1Hash - support access to the password, permission, 12 and key tables with a WWDBv1 hash-style backend. 13 14 =cut 15 16 use strict; 17 use warnings; 18 19 use constant TABLES => qw(password permission key); 20 use constant STYLE => "hash"; 21 22 ################################################################################ 23 # table access functions 24 # Auth1Hash provides access to three tables, so it checks the $self->{table} 25 # field to know what data its dealing with. 26 ################################################################################ 27 28 sub list { 29 my ($self, @keyparts) = @_; 30 my ($matchUserID) = @keyparts; 31 $self->{driver}->connect("ro"); 32 my @keys = keys %{ $self->{driver}->hash() }; 33 $self->{driver}->disconnect(); 34 if (defined $matchUserID) { 35 @keys = grep { $_ eq $matchUserID } @keys; 36 } 37 return map { [$_] } @keys; 38 } 39 40 sub exists { 41 my ($self, $userID) = @_; 42 $self->{driver}->connect("ro"); 43 my $result; 44 if (defined $userID) { 45 $result = exists $self->{driver}->hash()->{$userID}; 46 } else { 47 $result = keys %{$self->{driver}->hash()} ? 1 : 0; 48 } 49 $self->{driver}->disconnect(); 50 return $result; 51 } 52 53 sub add { 54 my ($self, $Record) = @_; 55 my $valueName = $self->{table}; 56 $self->{driver}->connect("rw"); 57 my $hash = $self->{driver}->hash(); 58 die $Record->user_id, ": $valueName exists" 59 if exists $hash->{$Record->user_id}; 60 if ($self->{table} eq "key") { 61 # key's value contains two fields 62 $hash->{$Record->user_id} = $Record->key() . " " . $Record->timestamp(); 63 } else { 64 $hash->{$Record->user_id} = $Record->$valueName(); 65 } 66 $self->{driver}->disconnect(); 67 } 68 69 sub get { 70 my ($self, $userID) = @_; 71 # $self->{driver}->connect("ro"); 72 # my $value = $self->{driver}->hash()->{$userID}; 73 # $self->{driver}->disconnect(); 74 # return undef unless defined $value; 75 # if ($self->{table} eq "key") { 76 # # key's value contains two fields 77 # my ($key, $timestamp) = $value =~ m/^(\S+)\s+(.*)$/; 78 # return $self->{record}->new( 79 # user_id => $userID, 80 # key => $key, 81 # timestamp => $timestamp, 82 # ); 83 # } else { 84 # return $self->{record}->new( 85 # user_id => $userID, 86 # $self->{table} => $value, 87 # ); 88 # } 89 my @results = 90 return ($self->gets($userID))[0]; 91 } 92 93 sub gets { 94 my ($self, @userIDs) = @_; 95 $self->{driver}->connect("ro"); 96 my @records; 97 foreach my $userID (@userIDs) { 98 my $string = $self->{driver}->hash()->{$userID}; 99 if (defined $string) { 100 if ($self->{table} eq "key") { 101 # key's value contains two fields 102 my ($key, $timestamp) = $string =~ m/^(\S+)\s+(.*)$/; 103 push @records, $self->{record}->new( 104 user_id => $userID, 105 key => $key, 106 timestamp => $timestamp, 107 ); 108 } else { 109 push @records, $self->{record}->new( 110 user_id => $userID, 111 $self->{table} => $string, 112 ); 113 } 114 } else { 115 push @records, undef; 116 } 117 } 118 $self->{driver}->disconnect(); 119 return @records; 120 } 121 122 sub put { 123 my ($self, $Record) = @_; 124 my $valueName = $self->{table}; 125 $self->{driver}->connect("rw"); 126 my $hash = $self->{driver}->hash(); 127 die $Record->user_id, ": $valueName not found" 128 unless exists $hash->{$Record->user_id}; 129 if ($self->{table} eq "key") { 130 # key's value contains two fields 131 $hash->{$Record->user_id} = $Record->key() . " " . $Record->timestamp(); 132 } else { 133 $hash->{$Record->user_id} = $Record->$valueName(); 134 } 135 $self->{driver}->disconnect(); 136 } 137 138 sub delete { 139 my ($self, $userID) = @_; 140 my $valueName = $self->{table}; 141 return 0 unless $self->{driver}->connect("rw"); 142 my $hash = $self->{driver}->hash(); 143 if (defined $userID) { 144 delete $hash->{$userID}; 145 } else { 146 # delete all elements 147 delete @$hash{keys %$hash}; 148 } 149 $self->{driver}->disconnect(); 150 return 1; 151 } 152 153 1;
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |