line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::MonM::Checkit::DBI; # $Id: DBI.pm 78 2019-07-07 19:48:16Z abalama $ |
2
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
3
|
1
|
|
|
1
|
|
5
|
use utf8; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
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.00 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Enable yes |
19
|
|
|
|
|
|
|
Type dbi |
20
|
|
|
|
|
|
|
DSN DBI:mysql:database=DBNAME;host=127.0.0.1 |
21
|
|
|
|
|
|
|
SQL "SELECT 'OK' AS OK FROM DUAL" # By default |
22
|
|
|
|
|
|
|
User USER |
23
|
|
|
|
|
|
|
Password PASSWORD |
24
|
|
|
|
|
|
|
Timeout 15 # Connect and request timeout, secs |
25
|
|
|
|
|
|
|
Set RaiseError 0 |
26
|
|
|
|
|
|
|
Set PrintError 0 |
27
|
|
|
|
|
|
|
Set mysql_enable_utf8 0 |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# . . . |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 DESCRIPTION |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Checkit DBI subclass |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head2 check |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Checkit method. |
40
|
|
|
|
|
|
|
This is backend method of L |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Returns: |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=over 4 |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=item B |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
The DBH error code ($dbh->err) |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=item B |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
The merged response content |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item B |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
OK or ERROR value, see "status" |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item B |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
DSN of DBI connection |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=item B |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
0 if error occured; 1 if no errors found |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=back |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 HISTORY |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
See C file |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 TO DO |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
See C file |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 BUGS |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
* none noted |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 SEE ALSO |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
L |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 AUTHOR |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 COPYRIGHT |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 LICENSE |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
95
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
See C file and L |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=cut |
100
|
|
|
|
|
|
|
|
101
|
1
|
|
|
1
|
|
30
|
use vars qw/$VERSION/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
102
|
|
|
|
|
|
|
$VERSION = '1.00'; |
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
1
|
|
5
|
use CTK::DBI; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
16
|
|
105
|
1
|
|
|
1
|
|
12
|
use CTK::ConfGenUtil; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
62
|
|
106
|
1
|
|
|
1
|
|
6
|
use App::MonM::Util qw/set2attr/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
47
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
use constant { |
109
|
1
|
|
|
|
|
365
|
DEFAULT_DSN => "dbi:Sponge:", |
110
|
|
|
|
|
|
|
DEFAULT_TIMEOUT => 0, |
111
|
|
|
|
|
|
|
DEFAULT_SQL => "SELECT 'OK' AS OK FROM DUAL", |
112
|
1
|
|
|
1
|
|
5
|
}; |
|
1
|
|
|
|
|
2
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub check { |
115
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
116
|
0
|
|
|
|
|
|
my $type = $self->type; |
117
|
0
|
0
|
0
|
|
|
|
return $self->maybe::next::method() unless $type && $type eq 'dbi'; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Init |
120
|
0
|
|
0
|
|
|
|
my $dsn = value($self->config, 'dsn') || DEFAULT_DSN; |
121
|
0
|
|
|
|
|
|
$self->source($dsn); |
122
|
0
|
|
0
|
|
|
|
my $timeout = value($self->config, 'timeout') || DEFAULT_TIMEOUT; |
123
|
0
|
|
|
|
|
|
my $attr = set2attr($self->config); |
124
|
0
|
|
0
|
|
|
|
my $sql = value($self->config, 'sql') // value($self->config, 'content') // DEFAULT_SQL; |
|
|
|
0
|
|
|
|
|
125
|
0
|
|
|
|
|
|
my $user = value($self->config, 'user'); |
126
|
0
|
|
|
|
|
|
my $password = value($self->config, 'password'); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# DB |
129
|
0
|
0
|
|
|
|
|
my $db = new CTK::DBI( |
130
|
|
|
|
|
|
|
-dsn => $dsn, |
131
|
|
|
|
|
|
|
-debug => 0, |
132
|
|
|
|
|
|
|
-username => $user, |
133
|
|
|
|
|
|
|
-password => $password, |
134
|
|
|
|
|
|
|
-attr => $attr, |
135
|
|
|
|
|
|
|
$timeout ? ( |
136
|
|
|
|
|
|
|
-timeout_connect => $timeout, |
137
|
|
|
|
|
|
|
-timeout_request => $timeout, |
138
|
|
|
|
|
|
|
) : (), |
139
|
|
|
|
|
|
|
); |
140
|
0
|
0
|
|
|
|
|
my $dbh = $db->connect if $db; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# Connect |
143
|
0
|
|
|
|
|
|
my @resa = (); |
144
|
0
|
|
|
|
|
|
my $error = ""; |
145
|
0
|
0
|
|
|
|
|
if (!$db) { |
|
|
0
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
$error = sprintf("Can't init database \"%s\"", $dsn); |
147
|
|
|
|
|
|
|
} elsif (!$dbh) { |
148
|
0
|
|
0
|
|
|
|
$error = sprintf("Can't connect to database \"%s\": %s", $dsn, $DBI::errstr || "unknown error"); |
149
|
|
|
|
|
|
|
} else { |
150
|
0
|
|
|
|
|
|
my $sth = $db->execute($sql); |
151
|
0
|
|
|
|
|
|
$error = $dbh->errstr(); |
152
|
0
|
0
|
|
|
|
|
if ($sth) { |
153
|
0
|
|
|
|
|
|
@resa = $sth->fetchrow_array; |
154
|
0
|
|
|
|
|
|
$sth->finish; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Result |
159
|
0
|
|
0
|
|
|
|
my $result = join("", @resa) // ''; |
160
|
0
|
|
|
|
|
|
$self->content($result); |
161
|
0
|
0
|
0
|
|
|
|
my $status = (defined($error) && length($error)) ? 0 : 1; |
162
|
0
|
|
|
|
|
|
$self->status($status); |
163
|
0
|
0
|
0
|
|
|
|
$self->error($error) if defined($error) && length($error); |
164
|
0
|
0
|
0
|
|
|
|
$self->code($dbh ? $dbh->err || 0 : 0); |
165
|
0
|
0
|
|
|
|
|
$self->message($self->status ? "OK" : "ERROR"); |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
return; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
1; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
__END__ |