| 1 | package Catalyst::Model::DBI; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use base 'Catalyst::Model'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use MRO::Compat; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use DBIx::Connector; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use constant LOG_LEVEL_BASIC => 1; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use constant LOG_LEVEL_INTERMEDIATE => 2; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use constant LOG_LEVEL_FULL => 3; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | 1 | our $VERSION = '0.32'; | |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | 1 | __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 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my $self = shift->next::method( @_ ); | |||||||||||||||||||||||||||||||||||||
| 95 | 0 | my ( $c, $config ) = @_; | |||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | $self->{dsn} ||= $config->{dsn}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"dsn"} ||= $config->{"dsn"}
MC/DC0%0/2
$self->{"dsn"} ||= $config->{"dsn"}
$self->{"dsn"}
$config->{"dsn"}
Truth table0%0/3
A ||= B
A= $self->{"dsn"}B= $config->{"dsn"}
| |||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | $self->{username} ||= $config->{username} || $config->{user}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$self->{"username"} ||= $config->{"username"} || $config->{"user"}
$config->{'username'} || $config->{'user'}
MC/DC0%0/3
$self->{"username"} ||= $config->{"username"} || $config->{"user"}
$self->{"username"}
$config->{'username'}
$config->{'user'}
Truth table0%0/4
A ||= B || C
A= $self->{"username"}B= $config->{'username'}C= $config->{'user'}
| |||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $self->{password} ||= $config->{password} || $config->{pass}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$self->{"password"} ||= $config->{"password"} || $config->{"pass"}
$config->{'password'} || $config->{'pass'}
MC/DC0%0/3
$self->{"password"} ||= $config->{"password"} || $config->{"pass"}
$self->{"password"}
$config->{'password'}
$config->{'pass'}
Truth table0%0/4
A ||= B || C
A= $self->{"password"}B= $config->{'password'}C= $config->{'pass'}
| |||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | $self->{options} ||= $config->{options}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"options"} ||= $config->{"options"}
MC/DC0%0/2
$self->{"options"} ||= $config->{"options"}
$self->{"options"}
$config->{"options"}
Truth table0%0/3
A ||= B
A= $self->{"options"}B= $config->{"options"}
| |||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $self->{namespace} ||= ref $self; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"namespace"} ||= ref $self
MC/DC0%0/2
$self->{"namespace"} ||= ref $self
$self->{"namespace"}
ref $self
Truth table0%0/3
A ||= B
A= $self->{"namespace"}B= ref $self
| |||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $self->{additional_base_classes} ||= (); | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"additional_base_classes"} ||= ()
MC/DC0%0/2
$self->{"additional_base_classes"} ||= ()
$self->{"additional_base_classes"}
()
Truth table0%0/3
A ||= B
A= $self->{"additional_base_classes"}B= ()
| |||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $self->{log} = $c->log; | |||||||||||||||||||||||||||||||||||||
| 105 | 0 | $self->{debug} = $c->debug; | |||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $self->{loglevel} ||= LOG_LEVEL_BASIC; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"loglevel"} ||= 1
MC/DC0%0/1
$self->{"loglevel"} ||= 1
$self->{"loglevel"}
Truth table0%0/2
A
A= $self->{"loglevel"}
| |||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||
| 111 | =item $self->connection | ||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||
| 113 | Returns the current DBIx::Connector connection handle. | ||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub connection { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
connection: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 118 | 0 | return shift->connect( 0 ) ; | |||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||
| 121 | =item $self->dbh | ||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||
| 123 | Returns the current database handle. | ||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub dbh { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
dbh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 128 | 0 | 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 | 0 | ▶ | sub connect { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
connect: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 138 | 0 | my ( $self, $want_dbh ) = @_; | |||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||
| 140 | 0 | my $connection = $self->_connection; | |||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $dbh = $self->_dbh; | |||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my $log = $self->{log}; | |||||||||||||||||||||||||||||||||||||
| 144 | 0 | my $debug = $self->{debug}; | |||||||||||||||||||||||||||||||||||||
| 145 | 0 | my $loglevel = $self->{loglevel}; | |||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | unless ( $connection ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 148 | 0 | eval { | |||||||||||||||||||||||||||||||||||||
| 149 | $connection = DBIx::Connector->new( | ||||||||||||||||||||||||||||||||||||||
| 150 | $self->{dsn}, | ||||||||||||||||||||||||||||||||||||||
| 151 | $self->{username} || $self->{user}, | ||||||||||||||||||||||||||||||||||||||
| 152 | $self->{password} || $self->{pass}, | ||||||||||||||||||||||||||||||||||||||
| 153 | $self->{options} | ||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | ); | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$self->{'username'} || $self->{'user'}
$self->{'password'} || $self->{'pass'}
MC/DC0%0/2
$self->{'username'} || $self->{'user'}
$self->{'username'}
$self->{'user'}
MC/DC0%0/2
$self->{'password'} || $self->{'pass'}
$self->{'password'}
$self->{'pass'}
Truth table0%0/3
A || B
A= $self->{'username'}B= $self->{'user'}
Truth table0%0/3
A || B
A= $self->{'password'}B= $self->{'pass'}
| |||||||||||||||||||||||||||||||||||||||
| 155 | 0 | $dbh = $connection->dbh; | |||||||||||||||||||||||||||||||||||||
| 156 | 0 | $self->_dbh( $dbh ); | |||||||||||||||||||||||||||||||||||||
| 157 | 0 | $self->_connection( $connection ); | |||||||||||||||||||||||||||||||||||||
| 158 | }; | ||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | $log->debug( | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$debug and $loglevel >= 1
MC/DC0%0/2
$debug and $loglevel >= 1
$debug
$loglevel >= 1
Truth table0%0/3
A and B
A= $debugB= $loglevel >= 1
| |||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | ) if $debug && $loglevel >= LOG_LEVEL_BASIC; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$debug and $loglevel >= 1
MC/DC0%0/2
$debug and $loglevel >= 1
$debug
$loglevel >= 1
Truth table0%0/3
A and B
A= $debugB= $loglevel >= 1
| |||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | my $handle = $want_dbh ? $dbh : $connection; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 172 | 0 | 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 | 1; | |||||||||||||||||||||||||||||||||||||