line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::MonM::Checkit::DBI; # $Id: DBI.pm 116 2022-08-27 08:57:12Z abalama $ |
2
|
1
|
|
|
1
|
|
306
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
3
|
1
|
|
|
1
|
|
3
|
use utf8; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=encoding utf-8 |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
App::MonM::Checkit::DBI - Checkit DBI subclass |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VIRSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 1.01 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Enable yes |
20
|
|
|
|
|
|
|
Type dbi |
21
|
|
|
|
|
|
|
DSN DBI:mysql:database=DBNAME;host=127.0.0.1 |
22
|
|
|
|
|
|
|
SQL "SELECT 'OK' AS OK FROM DUAL" |
23
|
|
|
|
|
|
|
User USER |
24
|
|
|
|
|
|
|
Password PASSWORD |
25
|
|
|
|
|
|
|
Timeout 15s |
26
|
|
|
|
|
|
|
Set RaiseError 0 |
27
|
|
|
|
|
|
|
Set PrintError 0 |
28
|
|
|
|
|
|
|
Set mysql_enable_utf8 0 |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# . . . |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 DESCRIPTION |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Checkit DBI subclass |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head2 check |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Checkit method. |
41
|
|
|
|
|
|
|
This is backend method of L |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Returns: |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=over 4 |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=item B |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
The DBH error code ($dbh->err) |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item B |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
The merged response content |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item B |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
OK or ERROR value, see "status" |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=item B |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
DSN of DBI connection |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item B |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
0 if error occured; 1 if no errors found |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=back |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 CONFIGURATION DIRECTIVES |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
The basic Checkit configuration options (directives) detailed describes in L |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=over 4 |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=item B, B |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
SQL "SELECT 'OK' AS OK FROM DUAL" |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Specifies the SQL query string (as content) |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Default: "SELECT 'OK' AS OK FROM DUAL" |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item B |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
DSN DBI:mysql:database=DATABASE;host=HOSTNAME |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Sets Database DSN string |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Default: dbi:Sponge: |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=item B |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Set RaiseError 0 |
94
|
|
|
|
|
|
|
Set PrintError 0 |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Defines DBI Attributes. This directive allows you set case sensitive DBI Attributes. |
97
|
|
|
|
|
|
|
There can be several such directives. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Examples: |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Set sqlite_unicode 1 |
102
|
|
|
|
|
|
|
Set mysql_enable_utf8 0 |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Default: no specified |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item B |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Timeout 1m |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Defines the timeout of DBI requests |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Default: off |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=item B, B |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
User USER |
117
|
|
|
|
|
|
|
Password PASSWORD |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Defines database credential: username and password |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Default: no specified |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=back |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 HISTORY |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
See C file |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 TO DO |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
See C file |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 BUGS |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
* none noted |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 SEE ALSO |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
L |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 AUTHOR |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 COPYRIGHT |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Copyright (C) 1998-2022 D&D Corporation. All Rights Reserved |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 LICENSE |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
152
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
See C file and L |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=cut |
157
|
|
|
|
|
|
|
|
158
|
1
|
|
|
1
|
|
34
|
use vars qw/$VERSION/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
159
|
|
|
|
|
|
|
$VERSION = '1.01'; |
160
|
|
|
|
|
|
|
|
161
|
1
|
|
|
1
|
|
5
|
use CTK::DBI; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
162
|
1
|
|
|
1
|
|
13
|
use CTK::ConfGenUtil; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
53
|
|
163
|
1
|
|
|
1
|
|
4
|
use App::MonM::Util qw/set2attr getTimeOffset/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
54
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
use constant { |
166
|
1
|
|
|
|
|
341
|
DEFAULT_DSN => "dbi:Sponge:", |
167
|
|
|
|
|
|
|
DEFAULT_TIMEOUT => 0, |
168
|
|
|
|
|
|
|
DEFAULT_SQL => "SELECT 'OK' AS OK FROM DUAL", |
169
|
1
|
|
|
1
|
|
5
|
}; |
|
1
|
|
|
|
|
1
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub check { |
172
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
173
|
0
|
|
|
|
|
|
my $type = $self->type; |
174
|
0
|
0
|
0
|
|
|
|
return $self->maybe::next::method() unless $type && ($type eq 'dbi' or $type eq 'db'); |
|
|
|
0
|
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Init |
177
|
0
|
|
0
|
|
|
|
my $dsn = lvalue($self->config, 'dsn') || DEFAULT_DSN; |
178
|
0
|
|
|
|
|
|
$self->source($dsn); |
179
|
0
|
|
0
|
|
|
|
my $timeout = getTimeOffset(lvalue($self->config, 'timeout') || DEFAULT_TIMEOUT); |
180
|
0
|
|
|
|
|
|
my $attr = set2attr($self->config); |
181
|
0
|
|
0
|
|
|
|
my $sql = lvalue($self->config, 'sql') // lvalue($self->config, 'content') // DEFAULT_SQL; |
|
|
|
0
|
|
|
|
|
182
|
0
|
|
|
|
|
|
my $user = lvalue($self->config, 'user'); |
183
|
0
|
|
|
|
|
|
my $password = lvalue($self->config, 'password'); |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# DB |
186
|
0
|
0
|
|
|
|
|
my $db = CTK::DBI->new( |
187
|
|
|
|
|
|
|
-dsn => $dsn, |
188
|
|
|
|
|
|
|
-debug => 0, |
189
|
|
|
|
|
|
|
-username => $user, |
190
|
|
|
|
|
|
|
-password => $password, |
191
|
|
|
|
|
|
|
-attr => $attr, |
192
|
|
|
|
|
|
|
$timeout ? ( |
193
|
|
|
|
|
|
|
-timeout_connect => $timeout, |
194
|
|
|
|
|
|
|
-timeout_request => $timeout, |
195
|
|
|
|
|
|
|
) : (), |
196
|
|
|
|
|
|
|
); |
197
|
0
|
0
|
|
|
|
|
my $dbh = $db->connect if $db; |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
# Connect |
200
|
0
|
|
|
|
|
|
my @resa = (); |
201
|
0
|
|
|
|
|
|
my $error = ""; |
202
|
0
|
0
|
|
|
|
|
if (!$db) { |
|
|
0
|
|
|
|
|
|
203
|
0
|
|
|
|
|
|
$error = sprintf("Can't init database \"%s\"", $dsn); |
204
|
|
|
|
|
|
|
} elsif (!$dbh) { |
205
|
0
|
|
0
|
|
|
|
$error = sprintf("Can't connect to database \"%s\": %s", $dsn, $DBI::errstr || "unknown error"); |
206
|
|
|
|
|
|
|
} else { |
207
|
0
|
|
|
|
|
|
my $sth = $db->execute($sql); |
208
|
0
|
|
|
|
|
|
$error = $dbh->errstr(); |
209
|
0
|
0
|
|
|
|
|
if ($sth) { |
210
|
0
|
|
|
|
|
|
@resa = $sth->fetchrow_array; |
211
|
0
|
|
|
|
|
|
$sth->finish; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# Result |
216
|
0
|
|
0
|
|
|
|
my $result = join("", @resa) // ''; |
217
|
0
|
|
|
|
|
|
$self->content($result); |
218
|
0
|
0
|
0
|
|
|
|
my $status = (defined($error) && length($error)) ? 0 : 1; |
219
|
0
|
|
|
|
|
|
$self->status($status); |
220
|
0
|
0
|
0
|
|
|
|
$self->error($error) if defined($error) && length($error); |
221
|
0
|
0
|
0
|
|
|
|
$self->code($dbh ? $dbh->err || 0 : 0); |
222
|
0
|
0
|
|
|
|
|
$self->message($self->status ? "OK" : "ERROR"); |
223
|
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
return; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
1; |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
__END__ |