line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::OverWatch::DB; |
2
|
|
|
|
|
|
|
# ABSTRACT: Database connector base class |
3
|
|
|
|
|
|
|
|
4
|
6
|
|
|
6
|
|
27
|
use strict; |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
196
|
|
5
|
6
|
|
|
6
|
|
24
|
use warnings; |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
126
|
|
6
|
6
|
|
|
6
|
|
3706
|
use utf8; |
|
6
|
|
|
|
|
52
|
|
|
6
|
|
|
|
|
21
|
|
7
|
|
|
|
|
|
|
|
8
|
6
|
|
|
6
|
|
2556
|
use App::OverWatch::Config; |
|
6
|
|
|
|
|
20
|
|
|
6
|
|
|
|
|
234
|
|
9
|
|
|
|
|
|
|
|
10
|
6
|
|
|
6
|
|
4746
|
use DBIx::Connector; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Try::Tiny; |
12
|
|
|
|
|
|
|
use Module::Load qw( load ); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
|
|
|
|
|
|
my $class = shift; |
16
|
|
|
|
|
|
|
my $Config = shift || die "Error: require 'Config' arg"; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $type = $Config->db_type(); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $subclass = $class . '::' . $type; |
21
|
|
|
|
|
|
|
load($subclass); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $self = bless( {}, $subclass ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
$self->{Config} = $Config; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
return $self; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub type { |
31
|
|
|
|
|
|
|
my $self = shift; |
32
|
|
|
|
|
|
|
return $self->{Config}->db_type(); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub dbix_run { |
36
|
|
|
|
|
|
|
my ($self, $sql, @bind_values) = @_; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $conn = $self->_dbix_conn(); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
return $conn->run( |
41
|
|
|
|
|
|
|
ping => sub { |
42
|
|
|
|
|
|
|
my $ret = 0; |
43
|
|
|
|
|
|
|
try { |
44
|
|
|
|
|
|
|
$ret = $_->do( $sql, {}, @bind_values); |
45
|
|
|
|
|
|
|
} catch { |
46
|
|
|
|
|
|
|
warn "Caught exception: $_\n"; |
47
|
|
|
|
|
|
|
$ret = 0; |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
$ret; |
50
|
|
|
|
|
|
|
}); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub dbix_select { |
54
|
|
|
|
|
|
|
my ($self, $sql, @bind_values) = @_; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $conn = $self->_dbix_conn(); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
return $conn->run( ping => sub { |
59
|
|
|
|
|
|
|
my $sth = undef; |
60
|
|
|
|
|
|
|
try { |
61
|
|
|
|
|
|
|
$sth = $_->prepare( $sql ); |
62
|
|
|
|
|
|
|
$sth->execute( @bind_values ); |
63
|
|
|
|
|
|
|
} catch { |
64
|
|
|
|
|
|
|
warn "Caught exception: $_\n"; |
65
|
|
|
|
|
|
|
$sth = undef; |
66
|
|
|
|
|
|
|
}; |
67
|
|
|
|
|
|
|
$sth; |
68
|
|
|
|
|
|
|
}); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
## Private |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _dbix_conn { |
74
|
|
|
|
|
|
|
my $self = shift; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $DBIx_conn = $self->{_conn}; |
77
|
|
|
|
|
|
|
return $DBIx_conn if (defined($DBIx_conn) && $DBIx_conn->connected()); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
## Not connected - try full connect |
80
|
|
|
|
|
|
|
my $ret = undef; |
81
|
|
|
|
|
|
|
my $conn = undef; |
82
|
|
|
|
|
|
|
try { |
83
|
|
|
|
|
|
|
$conn = $self->connect(); |
84
|
|
|
|
|
|
|
} catch { |
85
|
|
|
|
|
|
|
warn "Caught exception: $_\n"; |
86
|
|
|
|
|
|
|
}; |
87
|
|
|
|
|
|
|
$self->{_conn} = $conn; |
88
|
|
|
|
|
|
|
return $conn; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
__END__ |