line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Application::Plugin::DBH; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$CGI::Application::Plugin::DBH::VERSION = '4.04'; |
4
|
|
|
|
|
|
|
} |
5
|
4
|
|
|
4
|
|
725669
|
use parent 'Exporter'; |
|
4
|
|
|
|
|
1178
|
|
|
4
|
|
|
|
|
27
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# ABSTRACT: Easy DBI access from CGI::Application |
8
|
|
|
|
|
|
|
|
9
|
4
|
|
|
4
|
|
226
|
use strict; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
112
|
|
10
|
4
|
|
|
4
|
|
23
|
use Carp; |
|
4
|
|
|
|
|
14
|
|
|
4
|
|
|
|
|
3936
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
13
|
|
|
|
|
|
|
dbh |
14
|
|
|
|
|
|
|
dbh_config |
15
|
|
|
|
|
|
|
dbh_default_name |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub dbh { |
19
|
29
|
|
|
29
|
1
|
130286
|
my $self = shift; |
20
|
29
|
|
|
|
|
54
|
my $name = shift; |
21
|
|
|
|
|
|
|
|
22
|
29
|
|
100
|
|
|
104
|
$self->{__DBH_DEFAULT_NAME} ||= "__cgi_application_plugin_dbh"; # First use case. |
23
|
29
|
|
66
|
|
|
95
|
$name ||= $self->{__DBH_DEFAULT_NAME}; # Unnamed handle case. |
24
|
|
|
|
|
|
|
|
25
|
29
|
100
|
|
|
|
97
|
unless ($self->{__DBH_CONFIG}{$name}){ |
26
|
8
|
|
|
|
|
33
|
__auto_config($self, $name); |
27
|
8
|
100
|
|
|
|
13113
|
croak "must call dbh_config() before calling dbh()." unless $self->{__DBH_CONFIG}{$name}; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
25
|
100
|
66
|
|
|
267
|
unless( defined($self->{__DBH}{$name}) && $self->{__DBH}{$name}->ping ) { |
31
|
|
|
|
|
|
|
# create DBH object |
32
|
7
|
50
|
|
|
|
27
|
if(my $config = $self->{__DBH_CONFIG}{$name} ) { |
33
|
|
|
|
|
|
|
# Use a callback |
34
|
7
|
50
|
33
|
|
|
48
|
if (ref $config && ref $config eq 'CODE') { |
35
|
0
|
|
|
|
|
0
|
$self->{__DBH}{$name} = $config->(); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
# use the parameters the user supplied |
38
|
|
|
|
|
|
|
else { |
39
|
7
|
|
|
|
|
15004
|
require DBI; |
40
|
7
|
|
|
|
|
178060
|
$self->{__DBH}{$name} = DBI->connect(@{ $self->{__DBH_CONFIG}{$name} }); |
|
7
|
|
|
|
|
72
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} else { |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
25
|
|
|
|
|
165516
|
return $self->{__DBH}{$name}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub dbh_config { |
50
|
10
|
|
|
10
|
1
|
906
|
my $self = shift; |
51
|
|
|
|
|
|
|
|
52
|
10
|
|
100
|
|
|
63
|
$self->{__DBH_DEFAULT_NAME} ||= "__cgi_application_plugin_dbh"; # First use case. |
53
|
|
|
|
|
|
|
|
54
|
10
|
100
|
|
|
|
177
|
my $name = shift if( ref($_[1]) ); |
55
|
10
|
|
66
|
|
|
53
|
$name ||= $self->{__DBH_DEFAULT_NAME}; # Unnamed handle case. |
56
|
|
|
|
|
|
|
|
57
|
10
|
50
|
|
|
|
53
|
croak "Calling dbh_config after the dbh has already been created" if( defined $self->{__DBH}{$name} ); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# See if a handle is being passed in directly. |
60
|
10
|
|
|
|
|
1906
|
require UNIVERSAL; |
61
|
10
|
100
|
66
|
|
|
138
|
if( ref($_[0]) eq 'ARRAY' or ref $_[0] eq 'CODE' ) { |
|
|
100
|
66
|
|
|
|
|
62
|
3
|
|
|
|
|
27
|
$self->{__DBH_CONFIG}{$name} = shift; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
elsif( ref($_[0]) and $_[0]->isa('DBI::db') ) { |
65
|
3
|
|
|
|
|
42
|
$self->{__DBH}{$name} = shift; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Set this to note that we have completed the 'config' stage. |
68
|
3
|
|
|
|
|
17
|
$self->{__DBH_CONFIG}{$name} = 1; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
else { |
71
|
4
|
|
|
|
|
43
|
$self->{__DBH_CONFIG}{$name} = \@_; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub __auto_config { |
77
|
|
|
|
|
|
|
# get parameters for dbh_config from CGI::App instance parameters |
78
|
8
|
|
|
8
|
|
14
|
my $app = shift; |
79
|
8
|
|
|
|
|
10
|
my $name = shift; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
8
|
|
|
|
|
46
|
my $params = $app->param('::Plugin::DBH::dbh_config'); |
83
|
8
|
100
|
|
|
|
151
|
return __auto_config_env($app, $name) unless $params; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# if array reference: only one handle configured, pass array contents to dbh_config |
86
|
5
|
100
|
|
|
|
24
|
if (UNIVERSAL::isa($params, 'ARRAY')){ |
87
|
|
|
|
|
|
|
# verify that we really want the default handle |
88
|
2
|
100
|
|
|
|
6
|
return unless $name eq dbh_default_name($app); |
89
|
1
|
|
|
|
|
7
|
dbh_config($app, @$params); |
90
|
1
|
|
|
|
|
3
|
return; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# if hash reference: many handles configured, named with the hash keys |
94
|
3
|
50
|
|
|
|
15
|
if (UNIVERSAL::isa($params, 'HASH')){ |
95
|
3
|
|
|
|
|
8
|
$params = $params->{$name}; |
96
|
3
|
100
|
|
|
|
9
|
return __auto_config_env($app, $name) unless $params; |
97
|
2
|
|
|
|
|
9
|
dbh_config($app, $name, $params); |
98
|
2
|
|
|
|
|
4
|
return; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
0
|
croak "Parameter ::Plugin::DBH::dbh_config must be an array or hash reference"; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub __auto_config_env{ |
105
|
|
|
|
|
|
|
# check if DBI environment variable is set |
106
|
|
|
|
|
|
|
# this can be used to configure the default handle |
107
|
4
|
|
|
4
|
|
9
|
my $app = shift; |
108
|
4
|
|
|
|
|
9
|
my $name = shift; |
109
|
|
|
|
|
|
|
|
110
|
4
|
100
|
|
|
|
14
|
return unless $name eq dbh_default_name($app); |
111
|
2
|
100
|
|
|
|
13
|
return unless $ENV{DBI_DSN}; |
112
|
|
|
|
|
|
|
# DBI_DSN is set, so autoconfigure with all DSN, user id, pass all undefined |
113
|
1
|
|
|
|
|
41
|
dbh_config($app, undef, undef, undef); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub dbh_default_name { |
117
|
13
|
|
|
13
|
1
|
25956
|
my $self = shift; |
118
|
13
|
|
100
|
|
|
48
|
my $old_name = $self->{__DBH_DEFAULT_NAME} || "__cgi_application_plugin_dbh"; # Possible first use case. |
119
|
13
|
100
|
|
|
|
38
|
$self->{__DBH_DEFAULT_NAME} = shift if $_[0]; |
120
|
13
|
|
|
|
|
72
|
return $old_name; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
__END__ |