| 1 | package Database::Temp::DB; | ||||||||||||||
| 2 | ## no critic (ControlStructures::ProhibitPostfixControls) | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | # ABSTRACT: This class represents the created database to user | ||||||||||||||
| 8 | |||||||||||||||
| 9 | 5 | our $VERSION = '0.003'; # VERSION: generated by DZP::OurPkgVersion | |||||||||||||
| 10 | |||||||||||||||
| 11 | 5 | ▶ | use DBI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 5 | ▶ | use Carp qw( shortmess ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 5 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 5 | ▶ | use Types::Standard qw( Str Int Bool HashRef CodeRef Maybe ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 5 | ▶ | use Log::Any; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 5 | my $ENV_VAR_FOR_KEEPING_DB = 'DATABASE_TEMP_KEEP'; | |||||||||||||
| 19 | |||||||||||||||
| 20 | 5 | has driver => ( | |||||||||||||
| 21 | is => 'ro', | ||||||||||||||
| 22 | isa => Str, | ||||||||||||||
| 23 | required => 1, | ||||||||||||||
| 24 | ); | ||||||||||||||
| 25 | |||||||||||||||
| 26 | 5 | has name => ( | |||||||||||||
| 27 | is => 'ro', | ||||||||||||||
| 28 | isa => Str, | ||||||||||||||
| 29 | required => 1, | ||||||||||||||
| 30 | ); | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 5 | has cleanup => ( | |||||||||||||
| 33 | is => 'rw', | ||||||||||||||
| 34 | isa => Bool, | ||||||||||||||
| 35 | required => 1, | ||||||||||||||
| 36 | ); | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 5 | has init => ( | |||||||||||||
| 39 | is => 'ro', | ||||||||||||||
| 40 | isa => CodeRef, | ||||||||||||||
| 41 | required => 1, | ||||||||||||||
| 42 | ); | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 5 | has deinit => ( | |||||||||||||
| 45 | is => 'rw', | ||||||||||||||
| 46 | isa => CodeRef, | ||||||||||||||
| 47 | required => 1, | ||||||||||||||
| 48 | ); | ||||||||||||||
| 49 | |||||||||||||||
| 50 | # Connection info | ||||||||||||||
| 51 | 5 | has dsn => ( | |||||||||||||
| 52 | is => 'ro', | ||||||||||||||
| 53 | isa => Str, | ||||||||||||||
| 54 | required => 1, | ||||||||||||||
| 55 | ); | ||||||||||||||
| 56 | 5 | has username => ( | |||||||||||||
| 57 | is => 'ro', | ||||||||||||||
| 58 | isa => Maybe [Str], | ||||||||||||||
| 59 | required => 1, | ||||||||||||||
| 60 | ); | ||||||||||||||
| 61 | 5 | has password => ( | |||||||||||||
| 62 | is => 'ro', | ||||||||||||||
| 63 | isa => Maybe [Str], | ||||||||||||||
| 64 | required => 1, | ||||||||||||||
| 65 | ); | ||||||||||||||
| 66 | 5 | has attr => ( | |||||||||||||
| 67 | is => 'ro', | ||||||||||||||
| 68 | isa => HashRef, | ||||||||||||||
| 69 | required => 1, | ||||||||||||||
| 70 | ); | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 5 | has info => ( | |||||||||||||
| 73 | is => 'ro', | ||||||||||||||
| 74 | isa => HashRef, | ||||||||||||||
| 75 | required => 1, | ||||||||||||||
| 76 | ); | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 8 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 79 | 8 | my ($self) = @_; | |||||||||||||
| 80 | 8 | $self->_execute( $self->_start ); | |||||||||||||
| 81 | 8 | $self->_execute( $self->init ); | |||||||||||||
| 82 | 8 | return; | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 8 | ▶ | sub DEMOLISH { | ||||||||||||
|
Subroutine
DEMOLISH: called CC=6 SCAR=19.6
Pod
undocumented
| |||||||||||||||
| 86 | 8 | my ( $self, $in_global_destruction ) = @_; | |||||||||||||
| 87 | 8 | my $_log = Log::Any->get_logger( category => 'Database::Temp' ); | |||||||||||||
| 88 | |||||||||||||||
| 89 | 8 | $self->_execute( $self->deinit ); | |||||||||||||
| 90 | 8 | ▶ | if ( defined $ENV{$ENV_VAR_FOR_KEEPING_DB} ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 91 | 0 | ▶ | $_log->infof( 'Env var %s set. Keeping temp db', $ENV_VAR_FOR_KEEPING_DB ) if ( !$in_global_destruction ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | } | ||||||||||||||
| 93 | elsif ( $self->cleanup ) { | ||||||||||||||
| 94 | 6 | ▶ | $_log->debugf('Cleanup set. Deleting temp db') if ( !$in_global_destruction ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | 6 | $self->_execute( $self->_cleanup ); | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | else { | ||||||||||||||
| 98 | 2 | ▶ | $_log->debugf('Cleanup not set. Keeping temp db') if ( !$in_global_destruction ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | } | ||||||||||||||
| 100 | 8 | return; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 30 | ▶ | sub _execute { | ||||||||||||
|
Subroutine
_execute: called CC=1 SCAR=0.0
| |||||||||||||||
| 104 | 30 | my ( $self, $sub ) = @_; | |||||||||||||
| 105 | 30 | my $dbh = DBI->connect( $self->connection_info ); | |||||||||||||
| 106 | 30 | $sub->( $dbh, $self->name, $self->info, $self->driver ); | |||||||||||||
| 107 | 30 | $dbh->disconnect(); | |||||||||||||
| 108 | 30 | return $self; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 5 | has _start => ( | |||||||||||||
| 112 | is => 'ro', | ||||||||||||||
| 113 | isa => CodeRef, | ||||||||||||||
| 114 | required => 1, | ||||||||||||||
| 115 | ); | ||||||||||||||
| 116 | 5 | has _cleanup => ( | |||||||||||||
| 117 | is => 'ro', | ||||||||||||||
| 118 | isa => CodeRef, | ||||||||||||||
| 119 | required => 1, | ||||||||||||||
| 120 | ); | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 37 | ▶ | sub connection_info { | ||||||||||||
|
Subroutine
connection_info: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 123 | 37 | my ($self) = @_; | |||||||||||||
| 124 | 37 | return ( $self->dsn, $self->username, $self->password, $self->attr ); | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | |||||||||||||||
| 127 | 5 | 1; | |||||||||||||
| 128 | |||||||||||||||
| 129 | __END__ | ||||||||||||||