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
|
|
|
|
|
|
|
package App::Dochazka::REST::ConnBank; |
38
|
|
|
|
|
|
|
|
39
|
42
|
|
|
42
|
|
1777584
|
use strict; |
|
42
|
|
|
|
|
154
|
|
|
42
|
|
|
|
|
1031
|
|
40
|
42
|
|
|
42
|
|
196
|
use warnings; |
|
42
|
|
|
|
|
94
|
|
|
42
|
|
|
|
|
1044
|
|
41
|
42
|
|
|
42
|
|
182
|
use feature "state"; |
|
42
|
|
|
|
|
70
|
|
|
42
|
|
|
|
|
2534
|
|
42
|
|
|
|
|
|
|
|
43
|
42
|
|
|
42
|
|
470
|
use App::CELL qw( $log $site ); |
|
42
|
|
|
|
|
54029
|
|
|
42
|
|
|
|
|
3077
|
|
44
|
42
|
|
|
42
|
|
13222
|
use DBIx::Connector; |
|
42
|
|
|
|
|
640046
|
|
|
42
|
|
|
|
|
1194
|
|
45
|
42
|
|
|
42
|
|
292
|
use Try::Tiny; |
|
42
|
|
|
|
|
86
|
|
|
42
|
|
|
|
|
2516
|
|
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
|
|
|
42
|
|
247
|
use Exporter qw( import ); |
|
42
|
|
|
|
|
85
|
|
|
42
|
|
|
|
|
13799
|
|
86
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub get_arbitrary_dbix_conn { |
113
|
39
|
|
|
39
|
1
|
2505
|
my ( $dbname, $dbuser, $dbpass ) = @_; |
114
|
39
|
|
|
|
|
236
|
my $dbhost = $site->DOCHAZKA_DBHOST; |
115
|
39
|
|
|
|
|
896
|
my $dbport = $site->DOCHAZKA_DBPORT; |
116
|
39
|
|
|
|
|
827
|
my $dbsslmode = $site->DOCHAZKA_DBSSLMODE; |
117
|
|
|
|
|
|
|
|
118
|
39
|
|
|
|
|
736
|
my $data_source = "Dbi:Pg:dbname=\"$dbname\""; |
119
|
39
|
50
|
|
|
|
156
|
$data_source .= ";host=$dbhost" if $dbhost; |
120
|
39
|
50
|
|
|
|
132
|
$data_source .= ";port=$dbport" if $dbport; |
121
|
39
|
50
|
|
|
|
125
|
$data_source .= ";sslmode=$dbsslmode" if $dbsslmode; |
122
|
|
|
|
|
|
|
|
123
|
39
|
|
|
|
|
275
|
$log->debug( "Returning DBIx::Connector object for data source $data_source and user $dbuser" ); |
124
|
|
|
|
|
|
|
|
125
|
39
|
|
|
|
|
3133
|
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
|
|
|
|
|
|
|
sub init_singleton { |
150
|
39
|
|
|
39
|
1
|
365
|
$ENV{'PGTZ'} = $site->DOCHAZKA_TIMEZONE; |
151
|
39
|
50
|
33
|
|
|
1237
|
return if ref( $dbix_conn ) and $dbix_conn->can( 'dbh' ); |
152
|
39
|
|
|
|
|
275
|
$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
|
|
|
|
|
|
|
sub conn_up { |
171
|
0
|
|
|
0
|
1
|
|
my $arg = shift; |
172
|
0
|
|
0
|
|
|
|
my $conn = $arg || $dbix_conn; |
173
|
0
|
|
|
|
|
|
my $bool = 0; |
174
|
0
|
0
|
|
|
|
|
return $bool unless ref( $conn ) eq 'DBIx::Connector'; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# the ping command can and will throw and exception if the database server |
177
|
|
|
|
|
|
|
# is unreachable |
178
|
|
|
|
|
|
|
try { |
179
|
0
|
|
|
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
|
|
|
|
|
|
|
sub conn_status { |
197
|
0
|
|
|
0
|
1
|
|
my $arg = shift; |
198
|
0
|
0
|
|
|
|
|
return conn_up( $arg ) ? "UP" : "DOWN"; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
1; |