| 1 | package Dancer2::Session::DBIC; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 2 | ▶ | use Dancer2::Core::Types; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 2 | ▶ | use DBIx::Class; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 2 | ▶ | use DBICx::Sugar; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 2 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 2 | ▶ | use Module::Runtime 'use_module'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 2 | our $_schema; | |||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | 2 | ▶ | use Moo; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | 2 | with 'Dancer2::Core::Role::SessionFactory'; | |||||||||||||||||
| 14 | 2 | ▶ | use namespace::clean; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 2 | our $VERSION = '0.120'; | |||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | Dancer2::Session::DBIC - DBIx::Class session engine for Dancer2 | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | =head1 VERSION | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | 0.120 | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | This module implements a session engine for Dancer2 by serializing the session, | ||||||||||||||||||
| 29 | and storing it in a database via L<DBIx::Class>. | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | JSON was chosen as the default serialization format, as it is fast, terse, | ||||||||||||||||||
| 32 | and portable. | ||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | Example configuration: | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | session: "DBIC" | ||||||||||||||||||
| 39 | engines: | ||||||||||||||||||
| 40 | session: | ||||||||||||||||||
| 41 | DBIC: | ||||||||||||||||||
| 42 | dsn: "DBI:mysql:database=testing;host=127.0.0.1;port=3306" # DBI Data Source Name | ||||||||||||||||||
| 43 | schema_class: "Interchange6::Schema" # DBIx::Class schema | ||||||||||||||||||
| 44 | user: "user" # Username used to connect to the database | ||||||||||||||||||
| 45 | password: "password" # Password to connect to the database | ||||||||||||||||||
| 46 | resultset: "MySession" # DBIx::Class resultset, defaults to Session | ||||||||||||||||||
| 47 | id_column: "my_session_id" # defaults to sessions_id | ||||||||||||||||||
| 48 | data_column: "my_session_data" # defaults to session_data | ||||||||||||||||||
| 49 | serializer: "YAML" # defaults to JSON | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | Or if you are already using L<Dancer2::Plugin::DBIC> and want to use its | ||||||||||||||||||
| 52 | existing configuration for a database section named 'default' with all else | ||||||||||||||||||
| 53 | set to default in this module then you could simply use: | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | session: "DBIC" | ||||||||||||||||||
| 56 | engines: | ||||||||||||||||||
| 57 | session: | ||||||||||||||||||
| 58 | DBIC: | ||||||||||||||||||
| 59 | db_connection_name: default | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | =head1 SESSION EXPIRATION | ||||||||||||||||||
| 62 | |||||||||||||||||||
| 63 | A timestamp field that updates when a session is updated is recommended, so you can expire sessions server-side as well as client-side. | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | This session engine will not automagically remove expired sessions on the server, but with a timestamp field as above, you should be able to to do this manually. | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | =cut | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | =head1 ATTRIBUTES | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | =head2 schema_class | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | DBIx::Class schema class, e.g. L<Interchange6::Schema>. | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | =cut | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | 2 | has schema_class => ( | |||||||||||||||||
| 78 | is => 'ro', | ||||||||||||||||||
| 79 | isa => Str, | ||||||||||||||||||
| 80 | ); | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | =head2 db_connection_name | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | The L<Dancer2::Plugin::DBIC> database connection name. | ||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | If this option is provided then L</schema_class>, L</dsn>, L</user> and | ||||||||||||||||||
| 87 | L</password> are all ignored. | ||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | =cut | ||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | 2 | has db_connection_name => ( | |||||||||||||||||
| 92 | is => 'ro', | ||||||||||||||||||
| 93 | isa => Str, | ||||||||||||||||||
| 94 | ); | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | =head2 resultset | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | DBIx::Class resultset, defaults to C<Session>. | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | =cut | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | 2 | has resultset => ( | |||||||||||||||||
| 103 | is => 'ro', | ||||||||||||||||||
| 104 | isa => Str, | ||||||||||||||||||
| 105 | default => 'Session', | ||||||||||||||||||
| 106 | ); | ||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | =head2 id_column | ||||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | Column for session id, defaults to C<sessions_id>. | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | If this column is not the primary key of the table, it should have | ||||||||||||||||||
| 113 | a unique constraint added to it. See L<DBIx::Class::ResultSource/add_unique_constraint>. | ||||||||||||||||||
| 114 | |||||||||||||||||||
| 115 | =cut | ||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | 2 | has id_column => ( | |||||||||||||||||
| 118 | is => 'ro', | ||||||||||||||||||
| 119 | isa => Str, | ||||||||||||||||||
| 120 | default => 'sessions_id', | ||||||||||||||||||
| 121 | ); | ||||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | =head2 data_column | ||||||||||||||||||
| 124 | |||||||||||||||||||
| 125 | Column for session data, defaults to C<session_data>. | ||||||||||||||||||
| 126 | |||||||||||||||||||
| 127 | =cut | ||||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | 2 | has data_column => ( | |||||||||||||||||
| 130 | is => 'ro', | ||||||||||||||||||
| 131 | isa => Str, | ||||||||||||||||||
| 132 | default => 'session_data', | ||||||||||||||||||
| 133 | ); | ||||||||||||||||||
| 134 | |||||||||||||||||||
| 135 | =head2 dsn | ||||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | L<DBI> dsn to connect to the database. | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | =cut | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | 2 | has dsn => ( | |||||||||||||||||
| 142 | is => 'ro', | ||||||||||||||||||
| 143 | isa => Str, | ||||||||||||||||||
| 144 | ); | ||||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | =head2 user | ||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | Database username. | ||||||||||||||||||
| 149 | |||||||||||||||||||
| 150 | =cut | ||||||||||||||||||
| 151 | |||||||||||||||||||
| 152 | 2 | has user => ( | |||||||||||||||||
| 153 | is => 'ro', | ||||||||||||||||||
| 154 | ); | ||||||||||||||||||
| 155 | |||||||||||||||||||
| 156 | =head2 password | ||||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | Database password. | ||||||||||||||||||
| 159 | |||||||||||||||||||
| 160 | =cut | ||||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | 2 | has password => ( | |||||||||||||||||
| 163 | is => 'ro', | ||||||||||||||||||
| 164 | ); | ||||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | =head2 schema | ||||||||||||||||||
| 167 | |||||||||||||||||||
| 168 | L<DBIx::Class> schema. | ||||||||||||||||||
| 169 | |||||||||||||||||||
| 170 | =cut | ||||||||||||||||||
| 171 | |||||||||||||||||||
| 172 | 2 | has schema => ( | |||||||||||||||||
| 173 | is => 'ro', | ||||||||||||||||||
| 174 | ); | ||||||||||||||||||
| 175 | |||||||||||||||||||
| 176 | =head2 serializer | ||||||||||||||||||
| 177 | |||||||||||||||||||
| 178 | Serializer to use, defaults to JSON. | ||||||||||||||||||
| 179 | |||||||||||||||||||
| 180 | L<Dancer2::Session::DBIC> provides the following serializer classes: | ||||||||||||||||||
| 181 | |||||||||||||||||||
| 182 | =over | ||||||||||||||||||
| 183 | |||||||||||||||||||
| 184 | =item JSON - L<Dancer2::Session::DBIC::Serializer::JSON> | ||||||||||||||||||
| 185 | |||||||||||||||||||
| 186 | =item Sereal - L<Dancer2::Session::DBIC::Serializer::Sereal> | ||||||||||||||||||
| 187 | |||||||||||||||||||
| 188 | =item YAML - L<Dancer2::Session::DBIC::Serializer::YAML> | ||||||||||||||||||
| 189 | |||||||||||||||||||
| 190 | =back | ||||||||||||||||||
| 191 | |||||||||||||||||||
| 192 | If you do not use the default JSON serializer then you might need to install | ||||||||||||||||||
| 193 | additional modules - see the specific serializer class for details. | ||||||||||||||||||
| 194 | |||||||||||||||||||
| 195 | You can also use your own serializer class by passing the fully-qualified class | ||||||||||||||||||
| 196 | name as argument to this option, e.g.: MyApp::Session::Serializer | ||||||||||||||||||
| 197 | |||||||||||||||||||
| 198 | =cut | ||||||||||||||||||
| 199 | |||||||||||||||||||
| 200 | 2 | has serializer => ( | |||||||||||||||||
| 201 | is => 'ro', | ||||||||||||||||||
| 202 | isa => Str, | ||||||||||||||||||
| 203 | default => 'JSON', | ||||||||||||||||||
| 204 | ); | ||||||||||||||||||
| 205 | |||||||||||||||||||
| 206 | =head2 serializer_object | ||||||||||||||||||
| 207 | |||||||||||||||||||
| 208 | Vivified L</serializer> object. | ||||||||||||||||||
| 209 | |||||||||||||||||||
| 210 | =cut | ||||||||||||||||||
| 211 | |||||||||||||||||||
| 212 | 2 | has serializer_object => ( | |||||||||||||||||
| 213 | is => 'lazy', | ||||||||||||||||||
| 214 | isa => Object, | ||||||||||||||||||
| 215 | ); | ||||||||||||||||||
| 216 | |||||||||||||||||||
| 217 | 9 | ▶ | sub _build_serializer_object { | ||||||||||||||||
|
Subroutine
_build_serializer_object: called CC=4 SCAR=14.2
| |||||||||||||||||||
| 218 | 9 | my $self = shift; | |||||||||||||||||
| 219 | 9 | my $class = $self->serializer; | |||||||||||||||||
| 220 | 9 | ▶ | if ( $class !~ /::/ ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 221 | 9 | $class = __PACKAGE__ . "::Serializer::$class"; | |||||||||||||||||
| 222 | } | ||||||||||||||||||
| 223 | |||||||||||||||||||
| 224 | 9 | my %args; | |||||||||||||||||
| 225 | |||||||||||||||||||
| 226 | 9 | ▶ | $args{serialize_options} = $self->serialize_options | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 227 | if $self->serialize_options; | ||||||||||||||||||
| 228 | |||||||||||||||||||
| 229 | 9 | ▶ | $args{deserialize_options} = $self->deserialize_options | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 230 | if $self->deserialize_options; | ||||||||||||||||||
| 231 | |||||||||||||||||||
| 232 | 9 | use_module($class)->new(%args); | |||||||||||||||||
| 233 | } | ||||||||||||||||||
| 234 | |||||||||||||||||||
| 235 | =head2 serialize_options | ||||||||||||||||||
| 236 | |||||||||||||||||||
| 237 | Options to be passed to the constructor of the C<serializer> class | ||||||||||||||||||
| 238 | as a hash reference. | ||||||||||||||||||
| 239 | |||||||||||||||||||
| 240 | =cut | ||||||||||||||||||
| 241 | |||||||||||||||||||
| 242 | has serialize_options => ( | ||||||||||||||||||
| 243 | is => 'ro', | ||||||||||||||||||
| 244 | isa => HashRef, | ||||||||||||||||||
| 245 | 12 | ▶ | default => sub { {} }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 246 | 2 | ); | |||||||||||||||||
| 247 | |||||||||||||||||||
| 248 | =head2 deserialize_options | ||||||||||||||||||
| 249 | |||||||||||||||||||
| 250 | Options to be passed to the constructor of the C<deserializer> class | ||||||||||||||||||
| 251 | as a hash reference. | ||||||||||||||||||
| 252 | |||||||||||||||||||
| 253 | =cut | ||||||||||||||||||
| 254 | |||||||||||||||||||
| 255 | has deserialize_options => ( | ||||||||||||||||||
| 256 | is => 'ro', | ||||||||||||||||||
| 257 | isa => HashRef, | ||||||||||||||||||
| 258 | 12 | ▶ | default => sub { {} }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 259 | 2 | ); | |||||||||||||||||
| 260 | |||||||||||||||||||
| 261 | =head1 METHODS | ||||||||||||||||||
| 262 | |||||||||||||||||||
| 263 | =cut | ||||||||||||||||||
| 264 | |||||||||||||||||||
| 265 | 0 | ▶ | sub _sessions { return [] }; | ||||||||||||||||
|
Subroutine
_sessions: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 266 | |||||||||||||||||||
| 267 | =head2 _flush | ||||||||||||||||||
| 268 | |||||||||||||||||||
| 269 | Write the session to the database. Returns the session object. | ||||||||||||||||||
| 270 | |||||||||||||||||||
| 271 | =cut | ||||||||||||||||||
| 272 | |||||||||||||||||||
| 273 | 30 | ▶ | sub _flush { | ||||||||||||||||
|
Subroutine
_flush: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 274 | 30 | my ($self, $id, $session) = @_; | |||||||||||||||||
| 275 | |||||||||||||||||||
| 276 | 30 | my %session_data = ($self->id_column => $id, | |||||||||||||||||
| 277 | $self->data_column => $self->serializer_object->serialize($session), | ||||||||||||||||||
| 278 | ); | ||||||||||||||||||
| 279 | |||||||||||||||||||
| 280 | 30 | $self->_rset->update_or_create(\%session_data); | |||||||||||||||||
| 281 | |||||||||||||||||||
| 282 | 30 | return $self; | |||||||||||||||||
| 283 | } | ||||||||||||||||||
| 284 | |||||||||||||||||||
| 285 | =head2 _retrieve($id) | ||||||||||||||||||
| 286 | |||||||||||||||||||
| 287 | Look for a session with the given id. | ||||||||||||||||||
| 288 | |||||||||||||||||||
| 289 | Returns the session object if found, C<undef> if not. | ||||||||||||||||||
| 290 | Dies if the session was found, but could not be deserialized. | ||||||||||||||||||
| 291 | |||||||||||||||||||
| 292 | =cut | ||||||||||||||||||
| 293 | |||||||||||||||||||
| 294 | 78 | ▶ | sub _retrieve { | ||||||||||||||||
|
Subroutine
_retrieve: called CC=2 SCAR=7.1
| |||||||||||||||||||
| 295 | 78 | my ($self, $session_id) = @_; | |||||||||||||||||
| 296 | 78 | my $session_object; | |||||||||||||||||
| 297 | |||||||||||||||||||
| 298 | 78 | $session_object = $self->_rset->find({ $self->id_column => $session_id }); | |||||||||||||||||
| 299 | |||||||||||||||||||
| 300 | # Bail early if we know we have no session data at all | ||||||||||||||||||
| 301 | 78 | ▶ | if (!defined $session_object) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 302 | 12 | die "Could not retrieve session ID: $session_id"; | |||||||||||||||||
| 303 | 0 | return; | |||||||||||||||||
| 304 | } | ||||||||||||||||||
| 305 | |||||||||||||||||||
| 306 | 66 | my $data_column = $self->data_column; | |||||||||||||||||
| 307 | 66 | my $session_data = $session_object->$data_column; | |||||||||||||||||
| 308 | |||||||||||||||||||
| 309 | # No way to check that it's valid JSON other than trying to deserialize it | ||||||||||||||||||
| 310 | 66 | ▶ | my $session = try { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 311 | 66 | $self->serializer_object->deserialize($session_data); | |||||||||||||||||
| 312 | 0 | ▶ | } catch { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 313 | 0 | die "Could not deserialize session ID: $session_id - $_"; | |||||||||||||||||
| 314 | 0 | return; | |||||||||||||||||
| 315 | 66 | }; | |||||||||||||||||
| 316 | |||||||||||||||||||
| 317 | 66 | return $session; | |||||||||||||||||
| 318 | } | ||||||||||||||||||
| 319 | |||||||||||||||||||
| 320 | =head2 _change_id( $old_id, $new_id ) | ||||||||||||||||||
| 321 | |||||||||||||||||||
| 322 | Change ID of session with C<$old_id> to <$new_id>. | ||||||||||||||||||
| 323 | |||||||||||||||||||
| 324 | =cut | ||||||||||||||||||
| 325 | |||||||||||||||||||
| 326 | 6 | ▶ | sub _change_id { | ||||||||||||||||
|
Subroutine
_change_id: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 327 | 6 | my ( $self, $old_id, $new_id ) = @_; | |||||||||||||||||
| 328 | |||||||||||||||||||
| 329 | 6 | $self->_rset->search( { $self->id_column => $old_id } ) | |||||||||||||||||
| 330 | ->update( { $self->id_column => $new_id } ); | ||||||||||||||||||
| 331 | } | ||||||||||||||||||
| 332 | |||||||||||||||||||
| 333 | =head2 _destroy() | ||||||||||||||||||
| 334 | |||||||||||||||||||
| 335 | Remove the current session object from the database. | ||||||||||||||||||
| 336 | |||||||||||||||||||
| 337 | =cut | ||||||||||||||||||
| 338 | |||||||||||||||||||
| 339 | # as per doc: The _destroy method must be implemented. It must take | ||||||||||||||||||
| 340 | # $id as a single argument and destroy the underlying data. | ||||||||||||||||||
| 341 | |||||||||||||||||||
| 342 | 6 | ▶ | sub _destroy { | ||||||||||||||||
|
Subroutine
_destroy: called CC=2 SCAR=8.4
| |||||||||||||||||||
| 343 | 6 | my ($self, $id) = @_; | |||||||||||||||||
| 344 | |||||||||||||||||||
| 345 | 6 | ▶ | if (!defined $id) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 346 | 0 | die "No session ID passed to destroy method"; | |||||||||||||||||
| 347 | 0 | return; | |||||||||||||||||
| 348 | } | ||||||||||||||||||
| 349 | |||||||||||||||||||
| 350 | 6 | $self->_rset->find({ $self->id_column => $id})->delete; | |||||||||||||||||
| 351 | } | ||||||||||||||||||
| 352 | |||||||||||||||||||
| 353 | # Creates and connects schema | ||||||||||||||||||
| 354 | |||||||||||||||||||
| 355 | 120 | ▶ | sub _dbic { | ||||||||||||||||
|
Subroutine
_dbic: called CC=6 SCAR=22.9
| |||||||||||||||||||
| 356 | 120 | my $self = shift; | |||||||||||||||||
| 357 | |||||||||||||||||||
| 358 | 120 | ▶ | return $_schema if $_schema; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 359 | |||||||||||||||||||
| 360 | # Prefer an active schema over a schema class. | ||||||||||||||||||
| 361 | 6 | my $schema = $self->schema; | |||||||||||||||||
| 362 | |||||||||||||||||||
| 363 | 6 | ▶ | if (defined $schema) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 364 | 6 | ▶ | if (blessed $schema) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 365 | 0 | $_schema = $schema; | |||||||||||||||||
| 366 | } | ||||||||||||||||||
| 367 | else { | ||||||||||||||||||
| 368 | 6 | $_schema = $schema->(); | |||||||||||||||||
| 369 | } | ||||||||||||||||||
| 370 | } | ||||||||||||||||||
| 371 | elsif ( $self->db_connection_name ) { | ||||||||||||||||||
| 372 | 0 | $_schema = DBICx::Sugar::schema($self->db_connection_name); | |||||||||||||||||
| 373 | } | ||||||||||||||||||
| 374 | elsif (! defined $self->schema_class) { | ||||||||||||||||||
| 375 | 0 | die "No schema class defined."; | |||||||||||||||||
| 376 | } | ||||||||||||||||||
| 377 | else { | ||||||||||||||||||
| 378 | 0 | $_schema = $self->_load_schema_class($self->schema_class, | |||||||||||||||||
| 379 | $self->dsn, | ||||||||||||||||||
| 380 | $self->user, | ||||||||||||||||||
| 381 | $self->password); | ||||||||||||||||||
| 382 | } | ||||||||||||||||||
| 383 | |||||||||||||||||||
| 384 | 6 | return $_schema; | |||||||||||||||||
| 385 | } | ||||||||||||||||||
| 386 | |||||||||||||||||||
| 387 | # Returns specific resultset | ||||||||||||||||||
| 388 | 120 | ▶ | sub _rset { | ||||||||||||||||
|
Subroutine
_rset: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 389 | 120 | my ($self) = @_; | |||||||||||||||||
| 390 | |||||||||||||||||||
| 391 | 120 | return $self->_dbic->resultset($self->resultset); | |||||||||||||||||
| 392 | } | ||||||||||||||||||
| 393 | |||||||||||||||||||
| 394 | # Loads schema class | ||||||||||||||||||
| 395 | 0 | ▶ | sub _load_schema_class { | ||||||||||||||||
|
Subroutine
_load_schema_class: not called CC=2 SCAR=17.9
| |||||||||||||||||||
| 396 | 0 | my ($self, $schema_class, @conn_info) = @_; | |||||||||||||||||
| 397 | 0 | my ($schema_object); | |||||||||||||||||
| 398 | |||||||||||||||||||
| 399 | 0 | ▶ | if ($schema_class) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 400 | 0 | $schema_class =~ s/-/::/g; | |||||||||||||||||
| 401 | 0 | ▶ | try { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 402 | 0 | use_module($schema_class); | |||||||||||||||||
| 403 | } | ||||||||||||||||||
| 404 | 0 | ▶ | catch { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 405 | 0 | die "Could not load schema_class $schema_class: $_"; | |||||||||||||||||
| 406 | 0 | }; | |||||||||||||||||
| 407 | 0 | $schema_object = $schema_class->connect(@conn_info); | |||||||||||||||||
| 408 | } else { | ||||||||||||||||||
| 409 | 0 | my $dbic_loader = 'DBIx::Class::Schema::Loader'; | |||||||||||||||||
| 410 | 0 | ▶ | try { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 411 | 0 | use_module($dbic_loader); | |||||||||||||||||
| 412 | } | ||||||||||||||||||
| 413 | 0 | ▶ | catch { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 414 | 0 | die | |||||||||||||||||
| 415 | "You must provide a schema_class option or install $dbic_loader."; | ||||||||||||||||||
| 416 | 0 | }; | |||||||||||||||||
| 417 | 0 | $dbic_loader->naming('v7'); | |||||||||||||||||
| 418 | 0 | $schema_object = DBIx::Class::Schema::Loader->connect(@conn_info); | |||||||||||||||||
| 419 | } | ||||||||||||||||||
| 420 | |||||||||||||||||||
| 421 | 0 | return $schema_object; | |||||||||||||||||
| 422 | } | ||||||||||||||||||
| 423 | |||||||||||||||||||
| 424 | =head1 SEE ALSO | ||||||||||||||||||
| 425 | |||||||||||||||||||
| 426 | L<Dancer2>, L<Dancer2::Session> | ||||||||||||||||||
| 427 | |||||||||||||||||||
| 428 | =head1 AUTHOR | ||||||||||||||||||
| 429 | |||||||||||||||||||
| 430 | Stefan Hornburg (Racke) <racke@linuxia.de> | ||||||||||||||||||
| 431 | |||||||||||||||||||
| 432 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||
| 433 | |||||||||||||||||||
| 434 | Based on code from L<Dancer::Session::DBI> written by James Aitken | ||||||||||||||||||
| 435 | and code from L<Dancer::Plugin::DBIC> written by Naveed Massjouni. | ||||||||||||||||||
| 436 | |||||||||||||||||||
| 437 | Peter Mottram, support for JSON, YAML, Sereal and custom | ||||||||||||||||||
| 438 | serializers, GH #8, #9, #11, #12. Also for adding _change_id | ||||||||||||||||||
| 439 | method and accompanying tests. | ||||||||||||||||||
| 440 | |||||||||||||||||||
| 441 | Rory Zweistra, GH #9. | ||||||||||||||||||
| 442 | |||||||||||||||||||
| 443 | Andy Jack, GH #2. | ||||||||||||||||||
| 444 | |||||||||||||||||||
| 445 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||
| 446 | |||||||||||||||||||
| 447 | This software is copyright (c) Stefan Hornburg. | ||||||||||||||||||
| 448 | |||||||||||||||||||
| 449 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||
| 450 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||
| 451 | |||||||||||||||||||
| 452 | =cut | ||||||||||||||||||
| 453 | |||||||||||||||||||
| 454 | |||||||||||||||||||
| 455 | 2 | 1; | |||||||||||||||||