| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||
| 3 | # File: GX/Session/Store/Database.pm | ||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 5 | |||||||||||
| 6 | package GX::Session::Store::Database; | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use GX::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use GX::SQL::Types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 13 | # Class setup | ||||||||||
| 14 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use GX::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 1 | extends 'GX::Session::Store'; | |||||||||
| 19 | |||||||||||
| 20 | has 'database' => ( | ||||||||||
| 21 | isa => 'Object', | ||||||||||
| 22 | 0 | ▶ | preprocessor => sub { eval { $_ = $_->instance } }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 23 | 0 | ▶ | constraint => sub { $_->isa( 'GX::Database' ) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 24 | 1 | required => 1, | |||||||||
| 25 | weaken => 1, | ||||||||||
| 26 | accessor => { type => 'get' } | ||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | 1 | has 'table' => ( | |||||||||
| 30 | isa => 'String', | ||||||||||
| 31 | initialize => 1, | ||||||||||
| 32 | default => 'sessions', | ||||||||||
| 33 | accessor => { type => 'get' } | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | 1 | has '_column_type_map' => ( | |||||||||
| 37 | isa => 'Hash', | ||||||||||
| 38 | initialize => 1, | ||||||||||
| 39 | initializer => '_initialize_column_type_map', | ||||||||||
| 40 | accessors => { | ||||||||||
| 41 | '_get_column_type_map' => { type => 'get_reference' } | ||||||||||
| 42 | } | ||||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | 1 | has '_sql_cache' => ( | |||||||||
| 46 | isa => 'Hash', | ||||||||||
| 47 | initialize => 1, | ||||||||||
| 48 | initializer => '_initialize_sql_cache', | ||||||||||
| 49 | accessors => { | ||||||||||
| 50 | '_get_sql_cache' => { type => 'get_reference' } | ||||||||||
| 51 | } | ||||||||||
| 52 | ); | ||||||||||
| 53 | |||||||||||
| 54 | 1 | build; | |||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 58 | # Public methods | ||||||||||
| 59 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | sub delete { | ||||||||
|
Subroutine
delete: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 62 | |||||||||||
| 63 | 0 | my $self = shift; | |||||||||
| 64 | 0 | my $session_id = shift; | |||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | if ( ! defined $session_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 0 | complain "Missing argument"; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 0 | my ( $sql, $bind ) = @{$self->_get_sql_cache->{'delete'}}; | |||||||||
| 71 | |||||||||||
| 72 | 0 | my $dbh = $self->database->dbh; | |||||||||
| 73 | |||||||||||
| 74 | 0 | my $sth = $dbh->prepare_cached( $sql, { gx_cache_key => __PACKAGE__ . __LINE__ } ); | |||||||||
| 75 | |||||||||||
| 76 | 0 | $sth->bind_param( 1, $session_id, $bind->[0] ); | |||||||||
| 77 | |||||||||||
| 78 | 0 | $sth->execute; | |||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | my $result = $sth->rows > 0 ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | |||||||||||
| 82 | 0 | return $result; | |||||||||
| 83 | |||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | sub load { | ||||||||
|
Subroutine
load: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 87 | |||||||||||
| 88 | 0 | my $self = shift; | |||||||||
| 89 | 0 | my $session_id = shift; | |||||||||
| 90 | |||||||||||
| 91 | 0 | ▶ | if ( ! defined $session_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | complain "Missing argument"; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 0 | my ( $sql, $bind ) = @{$self->_get_sql_cache->{'load'}}; | |||||||||
| 96 | |||||||||||
| 97 | 0 | my $dbh = $self->database->dbh; | |||||||||
| 98 | |||||||||||
| 99 | 0 | my $sth = $dbh->prepare_cached( $sql, { gx_cache_key => __PACKAGE__ . __LINE__ } ); | |||||||||
| 100 | |||||||||||
| 101 | 0 | $sth->bind_param( 1, $session_id, $bind->[0] ); | |||||||||
| 102 | |||||||||||
| 103 | 0 | $sth->execute; | |||||||||
| 104 | |||||||||||
| 105 | 0 | my $row = $sth->fetchrow_arrayref; | |||||||||
| 106 | |||||||||||
| 107 | 0 | $sth->finish; | |||||||||
| 108 | |||||||||||
| 109 | 0 | ▶ | return unless $row; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | |||||||||||
| 111 | 0 | my $session_info = { | |||||||||
| 112 | 'remote_address' => $row->[0], | ||||||||||
| 113 | 'started_at' => $row->[1], | ||||||||||
| 114 | 'updated_at' => $row->[2], | ||||||||||
| 115 | 'expires_at' => $row->[3] | ||||||||||
| 116 | }; | ||||||||||
| 117 | |||||||||||
| 118 | 0 | my $session_data = $self->serializer->unserialize( $row->[4] ); | |||||||||
| 119 | |||||||||||
| 120 | 0 | return $session_info, $session_data; | |||||||||
| 121 | |||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 0 | ▶ | sub save { | ||||||||
|
Subroutine
save: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 125 | |||||||||||
| 126 | 0 | my $self = shift; | |||||||||
| 127 | 0 | my $session_id = shift; | |||||||||
| 128 | 0 | my $session_info = shift; | |||||||||
| 129 | 0 | my $session_data = shift; | |||||||||
| 130 | |||||||||||
| 131 | 0 | ▶ | if ( ! defined $session_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 0 | complain "Missing argument"; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 0 | ▶ | if ( ! defined $session_info ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | complain "Missing argument"; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 0 | ▶ | if ( ref $session_info ne 'HASH' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | 0 | complain "Invalid argument"; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 0 | my $serialized_session_data = $self->serializer->serialize( $session_data ); | |||||||||
| 144 | |||||||||||
| 145 | 0 | my ( $sql, $bind ) = @{$self->_get_sql_cache->{'save'}}; | |||||||||
| 146 | |||||||||||
| 147 | 0 | my $dbh = $self->database->dbh; | |||||||||
| 148 | |||||||||||
| 149 | 0 | my $sth = $dbh->prepare_cached( $sql, { gx_cache_key => __PACKAGE__ . __LINE__ } ); | |||||||||
| 150 | |||||||||||
| 151 | 0 | $sth->bind_param( 1, $session_id, $bind->[0] ); | |||||||||
| 152 | 0 | $sth->bind_param( 2, $session_info->{'remote_address'}, $bind->[1] ); | |||||||||
| 153 | 0 | $sth->bind_param( 3, $session_info->{'started_at'}, $bind->[2] ); | |||||||||
| 154 | 0 | $sth->bind_param( 4, $session_info->{'updated_at'}, $bind->[3] ); | |||||||||
| 155 | 0 | $sth->bind_param( 5, $session_info->{'expires_at'}, $bind->[4] ); | |||||||||
| 156 | 0 | $sth->bind_param( 6, $serialized_session_data, $bind->[5] ); | |||||||||
| 157 | |||||||||||
| 158 | 0 | $sth->execute; | |||||||||
| 159 | |||||||||||
| 160 | 0 | return 1; | |||||||||
| 161 | |||||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 0 | ▶ | sub update { | ||||||||
|
Subroutine
update: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 165 | |||||||||||
| 166 | 0 | my $self = shift; | |||||||||
| 167 | 0 | my $session_id = shift; | |||||||||
| 168 | 0 | my $session_info = shift; | |||||||||
| 169 | 0 | my $session_data = shift; | |||||||||
| 170 | |||||||||||
| 171 | 0 | ▶ | if ( ! defined $session_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 0 | complain "Missing argument"; | |||||||||
| 173 | } | ||||||||||
| 174 | |||||||||||
| 175 | 0 | ▶ | if ( ! defined $session_info ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 0 | complain "Missing argument"; | |||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | 0 | ▶ | if ( ref $session_info ne 'HASH' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | 0 | complain "Invalid argument"; | |||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | 0 | my $serialized_session_data = $self->serializer->serialize( $session_data ); | |||||||||
| 184 | |||||||||||
| 185 | 0 | my ( $sql, $bind ) = @{$self->_get_sql_cache->{'update'}}; | |||||||||
| 186 | |||||||||||
| 187 | 0 | my $dbh = $self->database->dbh; | |||||||||
| 188 | |||||||||||
| 189 | 0 | my $sth = $dbh->prepare_cached( $sql, { gx_cache_key => __PACKAGE__ . __LINE__ } ); | |||||||||
| 190 | |||||||||||
| 191 | 0 | $sth->bind_param( 1, $session_info->{'remote_address'}, $bind->[0] ); | |||||||||
| 192 | 0 | $sth->bind_param( 2, $session_info->{'started_at'}, $bind->[1] ); | |||||||||
| 193 | 0 | $sth->bind_param( 3, $session_info->{'updated_at'}, $bind->[2] ); | |||||||||
| 194 | 0 | $sth->bind_param( 4, $session_info->{'expires_at'}, $bind->[3] ); | |||||||||
| 195 | 0 | $sth->bind_param( 5, $serialized_session_data, $bind->[4] ); | |||||||||
| 196 | 0 | $sth->bind_param( 6, $session_id, $bind->[5] ); | |||||||||
| 197 | |||||||||||
| 198 | 0 | $sth->execute; | |||||||||
| 199 | |||||||||||
| 200 | 0 | ▶ | my $result = $sth->rows > 0 ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 201 | |||||||||||
| 202 | 0 | return $result; | |||||||||
| 203 | |||||||||||
| 204 | } | ||||||||||
| 205 | |||||||||||
| 206 | |||||||||||
| 207 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 208 | # Private methods | ||||||||||
| 209 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 210 | |||||||||||
| 211 | 0 | ▶ | sub _build_sql_delete { | ||||||||
|
Subroutine
_build_sql_delete: not called CC=1 SCAR=6.9
| |||||||||||
| 212 | |||||||||||
| 213 | 0 | my $self = shift; | |||||||||
| 214 | |||||||||||
| 215 | 0 | my ( $sql, $bind ) = $self->database->sql_builder->delete( | |||||||||
| 216 | table => $self->table, | ||||||||||
| 217 | where => [ 'id' => '?' ], | ||||||||||
| 218 | bind => $self->_get_column_type_map | ||||||||||
| 219 | ); | ||||||||||
| 220 | |||||||||||
| 221 | 0 | return [ $sql, [ map { $_->[2] } @$bind ] ]; | |||||||||
| 222 | |||||||||||
| 223 | } | ||||||||||
| 224 | |||||||||||
| 225 | 0 | ▶ | sub _build_sql_load { | ||||||||
|
Subroutine
_build_sql_load: not called CC=1 SCAR=6.9
| |||||||||||
| 226 | |||||||||||
| 227 | 0 | my $self = shift; | |||||||||
| 228 | |||||||||||
| 229 | 0 | my ( $sql, $bind ) = $self->database->sql_builder->select( | |||||||||
| 230 | table => $self->table, | ||||||||||
| 231 | columns => [ qw( remote_address started_at updated_at expires_at data ) ], | ||||||||||
| 232 | where => [ 'id' => '?' ], | ||||||||||
| 233 | bind => $self->_get_column_type_map | ||||||||||
| 234 | ); | ||||||||||
| 235 | |||||||||||
| 236 | 0 | return [ $sql, [ map { $_->[2] } @$bind ] ]; | |||||||||
| 237 | |||||||||||
| 238 | } | ||||||||||
| 239 | |||||||||||
| 240 | 0 | ▶ | sub _build_sql_save { | ||||||||
|
Subroutine
_build_sql_save: not called CC=1 SCAR=6.9
| |||||||||||
| 241 | |||||||||||
| 242 | 0 | my $self = shift; | |||||||||
| 243 | |||||||||||
| 244 | 0 | my ( $sql, $bind ) = $self->database->sql_builder->insert( | |||||||||
| 245 | table => $self->table, | ||||||||||
| 246 | columns => [ qw( id remote_address started_at updated_at expires_at data ) ], | ||||||||||
| 247 | bind => $self->_get_column_type_map | ||||||||||
| 248 | ); | ||||||||||
| 249 | |||||||||||
| 250 | 0 | return [ $sql, [ map { $_->[2] } @$bind ] ]; | |||||||||
| 251 | |||||||||||
| 252 | } | ||||||||||
| 253 | |||||||||||
| 254 | 0 | ▶ | sub _build_sql_update { | ||||||||
|
Subroutine
_build_sql_update: not called CC=1 SCAR=6.9
| |||||||||||
| 255 | |||||||||||
| 256 | 0 | my $self = shift; | |||||||||
| 257 | |||||||||||
| 258 | 0 | my ( $sql, $bind ) = $self->database->sql_builder->update( | |||||||||
| 259 | table => $self->table, | ||||||||||
| 260 | columns => [ qw( remote_address started_at updated_at expires_at data ) ], | ||||||||||
| 261 | where => [ 'id' => '?' ], | ||||||||||
| 262 | bind => $self->_get_column_type_map | ||||||||||
| 263 | ); | ||||||||||
| 264 | |||||||||||
| 265 | 0 | return [ $sql, [ map { $_->[2] } @$bind ] ]; | |||||||||
| 266 | |||||||||||
| 267 | } | ||||||||||
| 268 | |||||||||||
| 269 | 0 | ▶ | sub _initialize_column_type_map { | ||||||||
|
Subroutine
_initialize_column_type_map: not called CC=1 SCAR=6.9
| |||||||||||
| 270 | |||||||||||
| 271 | return { | ||||||||||
| 272 | 0 | 'id' => GX::SQL::Types::VARCHAR, | |||||||||
| 273 | 'remote_address' => GX::SQL::Types::VARCHAR, | ||||||||||
| 274 | 'started_at' => GX::SQL::Types::INTEGER, | ||||||||||
| 275 | 'updated_at' => GX::SQL::Types::INTEGER, | ||||||||||
| 276 | 'expires_at' => GX::SQL::Types::INTEGER, | ||||||||||
| 277 | 'data' => GX::SQL::Types::BINARY | ||||||||||
| 278 | }; | ||||||||||
| 279 | |||||||||||
| 280 | } | ||||||||||
| 281 | |||||||||||
| 282 | 0 | ▶ | sub _initialize_sql_cache { | ||||||||
|
Subroutine
_initialize_sql_cache: not called CC=1 SCAR=6.9
| |||||||||||
| 283 | |||||||||||
| 284 | 0 | my $self = shift; | |||||||||
| 285 | |||||||||||
| 286 | return { | ||||||||||
| 287 | 0 | 'delete' => $self->_build_sql_delete, | |||||||||
| 288 | 'load' => $self->_build_sql_load, | ||||||||||
| 289 | 'save' => $self->_build_sql_save, | ||||||||||
| 290 | 'update' => $self->_build_sql_update | ||||||||||
| 291 | }; | ||||||||||
| 292 | |||||||||||
| 293 | } | ||||||||||
| 294 | |||||||||||
| 295 | |||||||||||
| 296 | 1 | 1; | |||||||||
| 297 | |||||||||||
| 298 | __END__ | ||||||||||