File Coverage

blib/lib/App/Dochazka/REST/ConnBank.pm
Criterion Covered Total %
statement 34 43 79.0
branch 4 12 33.3
condition 1 6 16.6
subroutine 9 12 75.0
pod 4 4 100.0
total 52 77 67.5


line stmt bran cond sub pod time code
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              
38             use strict;
39 42     42   2501062 use warnings;
  42         145  
  42         1083  
40 42     42   189 use feature "state";
  42         85  
  42         946  
41 42     42   213  
  42         63  
  42         2567  
42             use App::CELL qw( $log $site );
43 42     42   644 use DBIx::Connector;
  42         83490  
  42         3198  
44 42     42   18591 use Try::Tiny;
  42         733914  
  42         1252  
45 42     42   309  
  42         78  
  42         2545  
46              
47              
48             =head1 NAME
49              
50             App::Dochazka::REST::ConnBank - Provide DBIx::Connector objects
51              
52              
53              
54             =head1 SYNOPSIS
55              
56             use App::Dochazka::REST::ConnBank qw( $dbix_conn conn_status );
57              
58             $dbix_conn->run( fixup => sub {
59             ...
60             } );
61              
62             print "Database connection status: " . conn_status() . "\n";
63              
64             # construct an arbitrary DBIx::Connector object
65             my $conn = App::Dochazka::REST::ConnBank::get_arbitrary_dbix_conn(
66             'mydb', 'myuser', 'mypass'
67             );
68              
69              
70              
71             =head1 DESCRIPTION
72              
73             This module contains routines relating to L<DBIx::Connector>. Mostly,
74             the application uses the C<$dbix_conn> singleton.
75              
76             =cut
77              
78              
79              
80             =head1 EXPORTS
81              
82             =cut
83              
84             use Exporter qw( import );
85 42     42   223 our @EXPORT_OK = qw( $dbix_conn conn_status conn_up );
  42         75  
  42         14903  
86              
87              
88              
89             =head1 PACKAGE VARIABLES
90              
91             This module stores the L<DBIx::Connector> singleton object that is imported by
92             all modules that need to communicate with the database.
93              
94             =cut
95              
96             our $dbix_conn;
97              
98              
99              
100             =head1 FUNCTIONS
101              
102              
103             =head2 get_arbitrary_dbix_conn
104              
105             Wrapper for DBIx::Connector->new. Takes database name, database user and
106             password. Returns a DBIx::Connector object (even if the database is
107             unreachable).
108              
109             =cut
110              
111             my ( $dbname, $dbuser, $dbpass ) = @_;
112             my $dbhost = $site->DOCHAZKA_DBHOST;
113 39     39 1 2809 my $dbport = $site->DOCHAZKA_DBPORT;
114 39         235 my $dbsslmode = $site->DOCHAZKA_DBSSLMODE;
115 39         883  
116 39         837 my $data_source = "Dbi:Pg:dbname=\"$dbname\"";
117             $data_source .= ";host=$dbhost" if $dbhost;
118 39         780 $data_source .= ";port=$dbport" if $dbport;
119 39 50       158 $data_source .= ";sslmode=$dbsslmode" if $dbsslmode;
120 39 50       141  
121 39 50       126 $log->debug( "Returning DBIx::Connector object for data source $data_source and user $dbuser" );
122              
123 39         288 return DBIx::Connector->new(
124             $data_source,
125 39         3208 $dbuser,
126             $dbpass,
127             {
128             PrintError => 0,
129             RaiseError => 1,
130             AutoCommit => 1,
131             AutoInactiveDestroy => 1,
132             },
133             );
134             }
135              
136              
137             =head2 init_singleton
138              
139             Initialize the C<$dbix_conn> singleton using dbname, dbuser, and dbpass values
140             from site configuration. Also set the PGTZ environment variable to the
141             value of the DOCHAZKA_TIMEZONE config param.
142              
143             Idempotent.
144              
145             =cut
146              
147             $ENV{'PGTZ'} = $site->DOCHAZKA_TIMEZONE;
148             return if ref( $dbix_conn ) and $dbix_conn->can( 'dbh' );
149             $dbix_conn = get_arbitrary_dbix_conn(
150 39     39 1 343 $site->DOCHAZKA_DBNAME,
151 39 50 33     1295 $site->DOCHAZKA_DBUSER,
152 39         283 $site->DOCHAZKA_DBPASS,
153             );
154             }
155              
156              
157             =head2 conn_up
158              
159             Given a L<DBIx::Connector> object, call L<ping> on the associated
160             database handle and return true or false based on the result.
161              
162             If no argument is given, returns the status of the C<$dbix_conn>
163             singleton.
164              
165             =cut
166              
167             my $arg = shift;
168             my $conn = $arg || $dbix_conn;
169             my $bool = 0;
170             return $bool unless ref( $conn ) eq 'DBIx::Connector';
171 0     0 1  
172 0   0       # the ping command can and will throw and exception if the database server
173 0           # is unreachable
174 0 0         try {
175             $bool = $conn->dbh->ping;
176             };
177              
178             return $bool;
179 0     0     }
180 0            
181              
182 0           =head2 conn_status
183              
184             Given a L<DBIx::Connector> object, call L<ping> on the associated
185             database handle and return either 'UP' or 'DOWN' based on the result.
186              
187             If no argument is given, returns the status of the C<$dbix_conn>
188             singleton.
189              
190             =cut
191              
192             my $arg = shift;
193             return conn_up( $arg ) ? "UP" : "DOWN";
194             }
195              
196             1;