| 1 | # ************************************************************************* | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2014-2015, SUSE LLC | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # All rights reserved. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Redistribution and use in source and binary forms, with or without | ||||||||||||||||||||||||||||
| 7 | # modification, are permitted provided that the following conditions are met: | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # 1. Redistributions of source code must retain the above copyright notice, | ||||||||||||||||||||||||||||
| 10 | # this list of conditions and the following disclaimer. | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # 2. Redistributions in binary form must reproduce the above copyright | ||||||||||||||||||||||||||||
| 13 | # notice, this list of conditions and the following disclaimer in the | ||||||||||||||||||||||||||||
| 14 | # documentation and/or other materials provided with the distribution. | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # 3. Neither the name of SUSE LLC nor the names of its contributors may be | ||||||||||||||||||||||||||||
| 17 | # used to endorse or promote products derived from this software without | ||||||||||||||||||||||||||||
| 18 | # specific prior written permission. | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||||||||||||||||||||||||||||
| 21 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||||||||||||||||||||||||||
| 22 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||||||||||||||||||||||||||||
| 23 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||||||||||||||||||||||||||||
| 24 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||||||||||||||||||||||||||||
| 25 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||||||||||||||||||||||||||||
| 26 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||||||||||||||||||||||||||||
| 27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||||||||||||||||||||||||||||
| 28 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||||||||||||||||||||||||||||
| 29 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||||||||||||||||||||||||||||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 31 | # ************************************************************************* | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # ------------------------ | ||||||||||||||||||||||||||||
| 34 | # store and dispense DBIx::Connector objects | ||||||||||||||||||||||||||||
| 35 | # ------------------------ | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | package App::Dochazka::REST::ConnBank; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 42 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 42 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 42 | ▶ | use feature "state"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 42 | ▶ | use App::CELL qw( $log $site ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 42 | ▶ | use DBIx::Connector; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 42 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 NAME | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | App::Dochazka::REST::ConnBank - Provide DBIx::Connector objects | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | use App::Dochazka::REST::ConnBank qw( $dbix_conn conn_status ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | $dbix_conn->run( fixup => sub { | ||||||||||||||||||||||||||||
| 60 | ... | ||||||||||||||||||||||||||||
| 61 | } ); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | print "Database connection status: " . conn_status() . "\n"; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # construct an arbitrary DBIx::Connector object | ||||||||||||||||||||||||||||
| 66 | my $conn = App::Dochazka::REST::ConnBank::get_arbitrary_dbix_conn( | ||||||||||||||||||||||||||||
| 67 | 'mydb', 'myuser', 'mypass' | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | This module contains routines relating to L<DBIx::Connector>. Mostly, | ||||||||||||||||||||||||||||
| 75 | the application uses the C<$dbix_conn> singleton. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 42 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | 42 | our @EXPORT_OK = qw( $dbix_conn conn_status conn_up ); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head1 PACKAGE VARIABLES | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | This module stores the L<DBIx::Connector> singleton object that is imported by | ||||||||||||||||||||||||||||
| 93 | all modules that need to communicate with the database. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 42 | our $dbix_conn; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 get_arbitrary_dbix_conn | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Wrapper for DBIx::Connector->new. Takes database name, database user and | ||||||||||||||||||||||||||||
| 107 | password. Returns a DBIx::Connector object (even if the database is | ||||||||||||||||||||||||||||
| 108 | unreachable). | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 39 | ▶ | sub get_arbitrary_dbix_conn { | ||||||||||||||||||||||||||
|
Subroutine
get_arbitrary_dbix_conn: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 39 | my ( $dbname, $dbuser, $dbpass ) = @_; | |||||||||||||||||||||||||||
| 114 | 39 | my $dbhost = $site->DOCHAZKA_DBHOST; | |||||||||||||||||||||||||||
| 115 | 39 | my $dbport = $site->DOCHAZKA_DBPORT; | |||||||||||||||||||||||||||
| 116 | 39 | my $dbsslmode = $site->DOCHAZKA_DBSSLMODE; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 39 | my $data_source = "Dbi:Pg:dbname=\"$dbname\""; | |||||||||||||||||||||||||||
| 119 | 39 | ▶ | $data_source .= ";host=$dbhost" if $dbhost; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 39 | ▶ | $data_source .= ";port=$dbport" if $dbport; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 39 | ▶ | $data_source .= ";sslmode=$dbsslmode" if $dbsslmode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 39 | $log->debug( "Returning DBIx::Connector object for data source $data_source and user $dbuser" ); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 39 | return DBIx::Connector->new( | |||||||||||||||||||||||||||
| 126 | $data_source, | ||||||||||||||||||||||||||||
| 127 | $dbuser, | ||||||||||||||||||||||||||||
| 128 | $dbpass, | ||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | PrintError => 0, | ||||||||||||||||||||||||||||
| 131 | RaiseError => 1, | ||||||||||||||||||||||||||||
| 132 | AutoCommit => 1, | ||||||||||||||||||||||||||||
| 133 | AutoInactiveDestroy => 1, | ||||||||||||||||||||||||||||
| 134 | }, | ||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 init_singleton | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Initialize the C<$dbix_conn> singleton using dbname, dbuser, and dbpass values | ||||||||||||||||||||||||||||
| 142 | from site configuration. Also set the PGTZ environment variable to the | ||||||||||||||||||||||||||||
| 143 | value of the DOCHAZKA_TIMEZONE config param. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Idempotent. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =cut | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 39 | ▶ | sub init_singleton { | ||||||||||||||||||||||||||
|
Subroutine
init_singleton: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 39 | $ENV{'PGTZ'} = $site->DOCHAZKA_TIMEZONE; | |||||||||||||||||||||||||||
| 151 | 39 | ▶ | return if ref( $dbix_conn ) and $dbix_conn->can( 'dbh' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $App::Dochazka::REST::ConnBank::dbix_conn and $App::Dochazka::REST::ConnBank::dbix_conn->can("dbh")
MC/DC0%0/2
ref $App::Dochazka::REST::ConnBank::dbix_conn and $App::Dochazka::REST::ConnBank::dbix_conn->can("dbh")
ref $App::Dochazka::REST::ConnBank::dbix_conn
$App::Dochazka::REST::ConnBank::dbix_conn->can("dbh")
Truth table33%1/3
A and B
A= ref $App::Dochazka::REST::ConnBank::dbix_connB= $App::Dochazka::REST::ConnBank::dbix_conn->can("dbh")
| |||||||||||||||||||||||||||||
| 152 | 39 | $dbix_conn = get_arbitrary_dbix_conn( | |||||||||||||||||||||||||||
| 153 | $site->DOCHAZKA_DBNAME, | ||||||||||||||||||||||||||||
| 154 | $site->DOCHAZKA_DBUSER, | ||||||||||||||||||||||||||||
| 155 | $site->DOCHAZKA_DBPASS, | ||||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 conn_up | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Given a L<DBIx::Connector> object, call L<ping> on the associated | ||||||||||||||||||||||||||||
| 163 | database handle and return true or false based on the result. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | If no argument is given, returns the status of the C<$dbix_conn> | ||||||||||||||||||||||||||||
| 166 | singleton. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | sub conn_up { | ||||||||||||||||||||||||||
|
Subroutine
conn_up: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 0 | my $arg = shift; | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | my $conn = $arg || $dbix_conn; | ||||||||||||||||||||||||||
|
Condition0%0/3
$arg || $App::Dochazka::REST::ConnBank::dbix_conn
MC/DC0%0/2
$arg || $App::Dochazka::REST::ConnBank::dbix_conn
$arg
$App::Dochazka::REST::ConnBank::dbix_conn
Truth table0%0/3
A || B
A= $argB= $App::Dochazka::REST::ConnBank::dbix_conn
| |||||||||||||||||||||||||||||
| 173 | 0 | my $bool = 0; | |||||||||||||||||||||||||||
| 174 | 0 | ▶ | return $bool unless ref( $conn ) eq 'DBIx::Connector'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # the ping command can and will throw and exception if the database server | ||||||||||||||||||||||||||||
| 177 | # is unreachable | ||||||||||||||||||||||||||||
| 178 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 179 | 0 | $bool = $conn->dbh->ping; | |||||||||||||||||||||||||||
| 180 | 0 | }; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | return $bool; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head2 conn_status | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Given a L<DBIx::Connector> object, call L<ping> on the associated | ||||||||||||||||||||||||||||
| 189 | database handle and return either 'UP' or 'DOWN' based on the result. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | If no argument is given, returns the status of the C<$dbix_conn> | ||||||||||||||||||||||||||||
| 192 | singleton. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =cut | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | ▶ | sub conn_status { | ||||||||||||||||||||||||||
|
Subroutine
conn_status: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 197 | 0 | my $arg = shift; | |||||||||||||||||||||||||||
| 198 | 0 | ▶ | return conn_up( $arg ) ? "UP" : "DOWN"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 42 | 1; | |||||||||||||||||||||||||||