Session/Store/Sybase.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 109 CC 6 SCAR 19.3
CC
6
Cov
0%
connection
12.0
DESTROY
12.0
materialize
6.0
CRAP 6.9
1 #############################################################################
2 #
3 # Apache::Session::Store::Sybase
4 # Apache persistent user sessions in a DBI::Sybase database
5 #
6 # Copyright(c) 2000, 2004 Jeffrey William Baker (jwbaker@acm.org), Mark Landry (mdlandry@lincoln.midcoast.com), and Chris Winters (chris@cwinters.com)
7 #
8 # With modifications from earlier version of Apache::Session::DBI::Sybase
9 # from Mark Landry (mdlandry@lincoln.midcoast.com)
10 #
11 # Modified to work with Apache::Session v 1.5+ by Chris Winters (chris@cwinters.com)
12 #
13 # Distribute under the Perl License
14 #
15 ############################################################################
16
17 package Apache::Session::Store::Sybase;
18
19 use strict;
20 use vars qw( @ISA $VERSION );
21
22 use Apache::Session::Store::DBI;
23
24 @ISA = qw( Apache::Session::Store::DBI );
25 $VERSION = '1.01';
26
27 $Apache::Session::Store::Sybase::DataSource = undef;
28 $Apache::Session::Store::Sybase::UserName = undef;
29 $Apache::Session::Store::Sybase::Password = undef;
30
31 sub connection {
32 my $self = shift;
33 my $session = shift;
34
35 return if ( defined $self->{dbh} );
36
37 if ( exists $session->{args}->{Handle} ) {
38 $self->{dbh} = $session->{args}->{Handle};
39 $self->{commit} = $session->{args}->{Commit};
40 }
41 else {
42 my $datasource = $session->{args}->{DataSource} ||
43 $Apache::Session::Store::Sybase::DataSource;
44 my $username = $session->{args}->{UserName} ||
45 $Apache::Session::Store::Sybase::UserName;
46 my $password = $session->{args}->{Password} ||
47 $Apache::Session::Store::Sybase::Password;
48
49 $self->{dbh} = DBI->connect(
50 $datasource,
51 $username,
52 $password,
53 { RaiseError => 1, AutoCommit => 0 }
54 ) || die $DBI::errstr;
55
56
57 # If we open the connection, we close the connection
58 $self->{disconnect} = 1;
59 }
60
61 # the programmer has to tell us what commit policy to use;
62 # note that this should take effect even if the programmer
63 # passes us a handle
64 $self->{commit} = $session->{args}->{Commit};
65
66 # sets the variable @@textsize to the default, which
67 # should be 32K; to test, do this from a isql/sqsh session:
68 #
69 # > set textsize 0
70 # > go
71 # > select @@textsize
72 # > go
73 #
74 # You should see something like:
75 # :
76 # : -----------
77 # : 32768
78 #
79 # Note that you can also pass an argument ('textsize') for a
80 # larger/smaller text size
81 my $textsize = $session->{args}->{textsize} || '0';
82 $self->{dbh}->do( "set textsize $textsize" );
83
84 }
85
86 # Both insert() and update() are modifications to
87 # Apache::Session::Store::DBI.
88
89 # Sybase cannot use placeholders for IMAGE/TEXT field types so you
90 # must pass the data directly in the SQL rather than using bound
91 # parameters. Naturally, this negates any usefulness of the
92 # 'prepare_cached' method used in Apache::Session::Store::DBI, so we
93 # use a more straightforward sequence to prepare/execute here.
94
95 # Also, if you use this storage mechanism, you must also use the
96 # serializer that puts the data structure into a format that you can
97 # put directly into the SQL statement (e.g., '0xblahblahblah')
98
99 sub insert {
100 my $self = shift;
101 my $session = shift;
102
103 $self->connection( $session );
104
105 local $self->{dbh}->{RaiseError} = 1;
106
107 my $sth = $self->{dbh}->prepare( qq{
108 INSERT INTO sessions (id, a_session) VALUES ( }.$self->{dbh}->quote($session->{data}->{_session_id}).qq{, }.$self->{dbh}->quote($session->{serialized}).qq{ ) } );
109
110 $sth->execute( );
111 }
112
113
114 sub update {
115 my $self = shift;
116 my $session = shift;
117
118 $self->connection( $session );
119
120 local $self->{dbh}->{RaiseError} = 1;
121
122 my $sth = $self->{dbh}->prepare( qq{
123 UPDATE sessions SET a_session = }.$self->{dbh}->quote($session->{serialized}).qq{ WHERE id = }.$self->{dbh}->quote($session->{data}->{_session_id}) );
124
125 $sth->execute( );
126 }
127
128 sub materialize {
129 my $self = shift;
130 my $session = shift;
131
132 $self->connection($session);
133
134 local $self->{dbh}->{RaiseError} = 1;
135
136 $self->{materialize_sth} =
137 $self->{dbh}->prepare(qq{
138 SELECT a_session FROM sessions WHERE id = }.$self->{dbh}->quote(
139 $session->{data}->{_session_id}));
140
141 $self->{materialize_sth}->execute;
142
143 my $results = $self->{materialize_sth}->fetchrow_arrayref;
144
145 if (!(defined $results)) {
146 die "Object does not exist in the data store";
147 }
148
149 $self->{materialize_sth}->finish;
150
151 $session->{serialized} = $results->[0];
152 }
153
154 sub remove {
155 my $self = shift;
156 my $session = shift;
157
158 $self->connection($session);
159
160 local $self->{dbh}->{RaiseError} = 1;
161
162 $self->{remove_sth} =
163 $self->{dbh}->prepare_cached(qq{
164 DELETE FROM sessions WHERE id = }.$self->{dbh}->quote($session->{data}->{_session_id}));
165
166 $self->{remove_sth}->execute;
167 $self->{remove_sth}->finish;
168 }
169
170 sub DESTROY {
171 my $self = shift;
172
173 if ( $self->{commit} ) {
174 $self->{dbh}->commit;
175 }
176
177 if ( $self->{disconnect} ) {
178 $self->{dbh}->disconnect;
179 }
180 }
181
182 1;
183
184 =pod
185
186 =head1 NAME
187
188 Apache::Session::Store::Sybase - Store persistent data in a Sybase database
189
190 =head1 SYNOPSIS
191
192 use Apache::Session::Store::Sybase;
193
194 my $store = new Apache::Session::Store::MySQL;
195
196 $store->insert( $ref );
197 $store->update( $ref );
198 $store->materialize( $ref );
199 $store->remove( $ref );
200
201 =head1 DESCRIPTION
202
203 Apache::Session::Store::Sybase fulfills the storage interface of
204 Apache::Session. Session data is stored in a Sybase database.
205
206 =head1 SCHEMA
207
208 To use this module, you will need at least these columns in a table
209 called 'sessions':
210
211 id CHAR(32) # or however long your session IDs are.
212 a_session IMAGE
213
214 To create this schema, you can execute this command using the isql or
215 sqsh programs:
216
217 CREATE TABLE sessions (
218 id CHAR(32) not null primary key,
219 a_session TEXT
220 )
221 go
222
223 If you use some other command, ensure that there is a unique index on the
224 id column of the table
225
226 =head1 CONFIGURATION
227
228 The module must know what datasource, username, and password to use when
229 connecting to the database. These values can be set using the options hash
230 (see Apache::Session documentation). The options are:
231
232 =over 4
233
234 =item DataSource
235
236 =item UserName
237
238 =item Password
239
240 =back
241
242 Example:
243
244 tie %hash, 'Apache::Session::Sybase', $id, {
245 DataSource => 'dbi:Sybase:database=db;server=server',
246 UserName => 'database_user',
247 Password => 'K00l',
248 Commit => 1,
249 };
250
251 Instead, you may pass in an already-opened DBI handle to your database.
252
253 tie %hash, 'Apache::Session::Sybase', $id, {
254 Handle => $dbh
255 };
256
257 Additional arguments you can pass to the backing store are:
258
259 =over 4
260
261 =item Commit - whether we should commit any changes; if you pass in
262 an already-open database handle that has AutoCommit set to a true
263 value, you do not need to set this. If you let
264 Apache::Session::Store::Sybase create your database, handle, you must
265 set this to a true value, otherwise, your changes will not be saved
266
267 =item textsize - the value we should pass to the 'set textsize '
268 command that sets the max size of the IMAGE field. Default is 32K (at
269 least in Sybase ASE 11.9.2).
270
271 =back
272
273 =head1 AUTHOR
274
275 This module was based on L<Apache::Session::Store::Oracle> which was
276 written by Jeffrey William Baker <jwbaker@acm.org>; it was modified by
277 Chris Winters <chris@cwinters.com> to work with Apache::Session 1.5+
278 with changes from earlier version of Apache::Session::DBI::Sybase from
279 Mark Landry <mdlandry@lincoln.midcoast.com>.
280
281 =head1 SEE ALSO
282
283 L<Apache::Session>
284
285 =cut