<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Catalyst::Model::DBI;

use strict;
use base 'Catalyst::Model';

use MRO::Compat;
use mro 'c3';
use DBIx::Connector;

use constant LOG_LEVEL_BASIC =&gt; 1;
use constant LOG_LEVEL_INTERMEDIATE =&gt; 2;
use constant LOG_LEVEL_FULL =&gt; 3;

our $VERSION = '0.33';

__PACKAGE__-&gt;mk_accessors( qw/_connection _dbh/ );

=head1 NAME

Catalyst::Model::DBI - DBI Model Class

=head1 SYNOPSIS

  # use the helper to create a model for example
  perl script/myapp_create.pl model MyModel DBI dsn username password

  # lib/MyApp/Model/DBI.pm
  package MyApp::Model::DBI;

  use base 'Catalyst::Model::DBI';

  __PACKAGE__-&gt;config(
    dsn           =&gt; 'DBI:Pg:dbname=mydb;host=localhost',
    username      =&gt; 'pgsql',
    password      =&gt; '',
    options       =&gt; { AutoCommit =&gt; 1 },
    loglevel      =&gt; 1
  );

  1;

  # or load settings from a config file via Config::General for example
  # in your myapp.conf you could have

  name MyApp

  &lt;Model::MyModel&gt;
    dsn "DBI:Pg:dbname=mydb;host=localhost"
    username pgsql
    password ""
    &lt;options&gt;
      AutoCommit 1
    &lt;/options&gt;
    loglevel 1
  &lt;/Model&gt;

  # note that config settings always override Model settings

  # do something with $dbh inside a controller ...
  my $dbh = $c-&gt;model('MyModel')-&gt;dbh;

  # do something with $dbh inside a model ...
  my $dbh = $self-&gt;dbh;

  #do something with DBIx::Connector connection inside a controller ...
  my $connection = $c-&gt;model('MyModel')-&gt;connection;

  #do something with DBIx::Connector connection inside a model ...
  my $connection = $self-&gt;connection;

=head1 DESCRIPTION

This is the C&lt;DBI&gt; model class. It has been rewritten to use L&lt;DBIx::Connector&gt; since it's internal code
that deals with connection maintenance has already been ported into there. You now have two options for 
doing custom models with Catalyst. Either by using this model and any related modules as needed
or by having your custom model decoupled from Catalyst and glued on using L&lt;Catalyst::Model::Adaptor&gt; 

Some general rules are as follows. If you do not wish to use L&lt;DBIx::Connector&gt; directly or DBI and setup 
connections in your custom models or have glue models, then use this model. If you however need models that 
can be re-used outside of your application or simply wish to maintain connection code yourself outside of
the Catalyst, then use L&lt;Catalyst::Model::Adaptor&gt; which allows you to glue outside models into your Catalyst app.

=head1 METHODS

=over 4

=item new

Initializes DBI connection

=cut

sub new {
  my $self = shift-&gt;next::method( @_ );
  my ( $c, $config ) = @_;

  $self-&gt;{dsn} ||= $config-&gt;{dsn};
  $self-&gt;{username} ||= $config-&gt;{username} || $config-&gt;{user};
  $self-&gt;{password} ||= $config-&gt;{password} || $config-&gt;{pass};
  $self-&gt;{options} ||= $config-&gt;{options};
  $self-&gt;{set_names_utf8} ||= $config-&gt;{set_names_utf8};

  $self-&gt;{namespace} ||= ref $self;
  $self-&gt;{additional_base_classes} ||= ();
  $self-&gt;{log} = $c-&gt;log;
  $self-&gt;{debug} = $c-&gt;debug;
  $self-&gt;{loglevel} ||= LOG_LEVEL_BASIC;
  
  return $self;
}

=item $self-&gt;connection

Returns the current DBIx::Connector connection handle.

=cut

sub connection {
  return shift-&gt;connect( 0 ) ;
}

=item $self-&gt;dbh

Returns the current database handle.

=cut

sub dbh {
  return shift-&gt;connect( 1 );
}

=item $self-&gt;connect

Connects to the database and returns the handle.

=cut

sub connect {
  my ( $self, $want_dbh ) = @_;

  my $connection = $self-&gt;_connection;
  my $dbh = $self-&gt;_dbh;

  my $log = $self-&gt;{log};
  my $debug = $self-&gt;{debug};
  my $loglevel = $self-&gt;{loglevel};
  
  unless ( $connection ) {
    eval {
      $connection = DBIx::Connector-&gt;new(
        $self-&gt;{dsn},
        $self-&gt;{username} || $self-&gt;{user},
        $self-&gt;{password} || $self-&gt;{pass},
        $self-&gt;{options}
      );
      $dbh = $connection-&gt;dbh;
      $self-&gt;{set_names_utf8} &amp;&amp; $dbh-&gt;do("SET NAMES 'utf8'");
      $self-&gt;_dbh( $dbh );
      $self-&gt;_connection( $connection );
    };

    if ($@) {
      $log-&gt;debug(
        qq/Couldn't connect to the database via DBIx::Connector "$@"/
      ) if $debug &amp;&amp; $loglevel &gt;= LOG_LEVEL_BASIC;
    } else {
      $log-&gt;debug(
        'Connected to the database using DBIx::Connector via dsn:' . $self-&gt;{dsn}
      ) if $debug &amp;&amp; $loglevel &gt;= LOG_LEVEL_BASIC;
    }
  }

  my $handle = $want_dbh ? $dbh : $connection;
  return $handle;
}

=back

=head1 SEE ALSO

L&lt;Catalyst&gt;, L&lt;DBI&gt;, L&lt;Catalyst::Model::Proxy&gt;, L&lt;Catalyst::Model::DBI::SQL::Library&gt;

=head1 AUTHOR

Alex Pavlovic, C&lt;alex.pavlovic@taskforce-1.com&gt;

=head1 COPYRIGHT

Copyright (c) 2005 - 2012
the Catalyst::Model::DBI L&lt;/AUTHOR&gt;
as listed above.

=head1 LICENSE

This program is free software, you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1;
</pre></body></html>