| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Catalyst::Model::DBI; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
34710
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
52
|
|
|
4
|
1
|
|
|
1
|
|
5
|
use base 'Catalyst::Model'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
530
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
808
|
use MRO::Compat; |
|
|
1
|
|
|
|
|
3090
|
|
|
|
1
|
|
|
|
|
24
|
|
|
7
|
1
|
|
|
1
|
|
6
|
use mro 'c3'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
5
|
|
|
8
|
1
|
|
|
1
|
|
409
|
use DBIx::Connector; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use constant LOG_LEVEL_BASIC => 1; |
|
11
|
|
|
|
|
|
|
use constant LOG_LEVEL_INTERMEDIATE => 2; |
|
12
|
|
|
|
|
|
|
use constant LOG_LEVEL_FULL => 3; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.32'; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors( qw/_connection _dbh/ ); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 NAME |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Catalyst::Model::DBI - DBI Model Class |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# use the helper to create a model for example |
|
25
|
|
|
|
|
|
|
perl script/myapp_create.pl model MyModel DBI dsn username password |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# lib/MyApp/Model/DBI.pm |
|
28
|
|
|
|
|
|
|
package MyApp::Model::DBI; |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use base 'Catalyst::Model::DBI'; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
__PACKAGE__->config( |
|
33
|
|
|
|
|
|
|
dsn => 'DBI:Pg:dbname=mydb;host=localhost', |
|
34
|
|
|
|
|
|
|
username => 'pgsql', |
|
35
|
|
|
|
|
|
|
password => '', |
|
36
|
|
|
|
|
|
|
options => { AutoCommit => 1 }, |
|
37
|
|
|
|
|
|
|
loglevel => 1 |
|
38
|
|
|
|
|
|
|
); |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
1; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# or load settings from a config file via Config::General for example |
|
43
|
|
|
|
|
|
|
# in your myapp.conf you could have |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
name MyApp |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
<Model::MyModel> |
|
48
|
|
|
|
|
|
|
dsn "DBI:Pg:dbname=mydb;host=localhost" |
|
49
|
|
|
|
|
|
|
username pgsql |
|
50
|
|
|
|
|
|
|
password "" |
|
51
|
|
|
|
|
|
|
<options> |
|
52
|
|
|
|
|
|
|
AutoCommit 1 |
|
53
|
|
|
|
|
|
|
</options> |
|
54
|
|
|
|
|
|
|
loglevel 1 |
|
55
|
|
|
|
|
|
|
</Model> |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# note that config settings always override Model settings |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# do something with $dbh inside a controller ... |
|
60
|
|
|
|
|
|
|
my $dbh = $c->model('MyModel')->dbh; |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# do something with $dbh inside a model ... |
|
63
|
|
|
|
|
|
|
my $dbh = $self->dbh; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
#do something with DBIx::Connector connection inside a controller ... |
|
66
|
|
|
|
|
|
|
my $connection = $c->model('MyModel')->connection; |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
#do something with DBIx::Connector connection inside a model ... |
|
69
|
|
|
|
|
|
|
my $connection = $self->connection; |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
This is the C<DBI> model class. It has been rewritten to use L<DBIx::Connector> since it's internal code |
|
74
|
|
|
|
|
|
|
that deals with connection maintenance has already been ported into there. You now have two options for |
|
75
|
|
|
|
|
|
|
doing custom models with Catalyst. Either by using this model and any related modules as needed |
|
76
|
|
|
|
|
|
|
or by having your custom model decoupled from Catalyst and glued on using L<Catalyst::Model::Adaptor> |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Some general rules are as follows. If you do not wish to use L<DBIx::Connector> directly or DBI and setup |
|
79
|
|
|
|
|
|
|
connections in your custom models or have glue models, then use this model. If you however need models that |
|
80
|
|
|
|
|
|
|
can be re-used outside of your application or simply wish to maintain connection code yourself outside of |
|
81
|
|
|
|
|
|
|
the Catalyst, then use L<Catalyst::Model::Adaptor> which allows you to glue outside models into your Catalyst app. |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 METHODS |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=over 4 |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=item new |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Initializes DBI connection |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub new { |
|
94
|
|
|
|
|
|
|
my $self = shift->next::method( @_ ); |
|
95
|
|
|
|
|
|
|
my ( $c, $config ) = @_; |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
$self->{dsn} ||= $config->{dsn}; |
|
98
|
|
|
|
|
|
|
$self->{username} ||= $config->{username} || $config->{user}; |
|
99
|
|
|
|
|
|
|
$self->{password} ||= $config->{password} || $config->{pass}; |
|
100
|
|
|
|
|
|
|
$self->{options} ||= $config->{options}; |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
$self->{namespace} ||= ref $self; |
|
103
|
|
|
|
|
|
|
$self->{additional_base_classes} ||= (); |
|
104
|
|
|
|
|
|
|
$self->{log} = $c->log; |
|
105
|
|
|
|
|
|
|
$self->{debug} = $c->debug; |
|
106
|
|
|
|
|
|
|
$self->{loglevel} ||= LOG_LEVEL_BASIC; |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
return $self; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=item $self->connection |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Returns the current DBIx::Connector connection handle. |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=cut |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub connection { |
|
118
|
|
|
|
|
|
|
return shift->connect( 0 ) ; |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item $self->dbh |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Returns the current database handle. |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=cut |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub dbh { |
|
128
|
|
|
|
|
|
|
return shift->connect( 1 ); |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item $self->connect |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Connects to the database and returns the handle. |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub connect { |
|
138
|
|
|
|
|
|
|
my ( $self, $want_dbh ) = @_; |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
my $connection = $self->_connection; |
|
141
|
|
|
|
|
|
|
my $dbh = $self->_dbh; |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my $log = $self->{log}; |
|
144
|
|
|
|
|
|
|
my $debug = $self->{debug}; |
|
145
|
|
|
|
|
|
|
my $loglevel = $self->{loglevel}; |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
unless ( $connection ) { |
|
148
|
|
|
|
|
|
|
eval { |
|
149
|
|
|
|
|
|
|
$connection = DBIx::Connector->new( |
|
150
|
|
|
|
|
|
|
$self->{dsn}, |
|
151
|
|
|
|
|
|
|
$self->{username} || $self->{user}, |
|
152
|
|
|
|
|
|
|
$self->{password} || $self->{pass}, |
|
153
|
|
|
|
|
|
|
$self->{options} |
|
154
|
|
|
|
|
|
|
); |
|
155
|
|
|
|
|
|
|
$dbh = $connection->dbh; |
|
156
|
|
|
|
|
|
|
$self->_dbh( $dbh ); |
|
157
|
|
|
|
|
|
|
$self->_connection( $connection ); |
|
158
|
|
|
|
|
|
|
}; |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
if ($@) { |
|
161
|
|
|
|
|
|
|
$log->debug( |
|
162
|
|
|
|
|
|
|
qq/Couldn't connect to the database via DBIx::Connector "$@"/ |
|
163
|
|
|
|
|
|
|
) if $debug && $loglevel >= LOG_LEVEL_BASIC; |
|
164
|
|
|
|
|
|
|
} else { |
|
165
|
|
|
|
|
|
|
$log->debug( |
|
166
|
|
|
|
|
|
|
'Connected to the database using DBIx::Connector via dsn:' . $self->{dsn} |
|
167
|
|
|
|
|
|
|
) if $debug && $loglevel >= LOG_LEVEL_BASIC; |
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
} |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
my $handle = $want_dbh ? $dbh : $connection; |
|
172
|
|
|
|
|
|
|
return $handle; |
|
173
|
|
|
|
|
|
|
} |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=back |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
L<Catalyst>, L<DBI>, L<Catalyst::Model::Proxy>, L<Catalyst::Model::DBI::SQL::Library> |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 AUTHOR |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Alex Pavlovic, C<alex.pavlovic@taskforce-1.com> |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Copyright (c) 2005 - 2012 |
|
188
|
|
|
|
|
|
|
the Catalyst::Model::DBI L</AUTHOR> |
|
189
|
|
|
|
|
|
|
as listed above. |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 LICENSE |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it |
|
194
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=cut |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
1; |