Advertisement
srchulo

Catalyst::Model::DBIx::Raw Temp fix

Jan 9th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.21 KB | None | 0 0
  1. package Catalyst::Model::DBIx::Raw;
  2. use strict;
  3. use warnings;
  4.  
  5. use Carp ();
  6. use DBIx::Raw .08;
  7. use Try::Tiny;
  8. use Moose;
  9. extends 'Catalyst::Model';
  10. with 'Catalyst::Component::InstancePerContext';
  11. no Moose;
  12.  
  13. #ABSTRACT: A Catalyst Model for DBIx::Raw
  14.  
  15. sub new {
  16.     my $self  = shift->next::method(@_);
  17.     my $class = ref($self);
  18.    
  19.     my ($c) = @_;  
  20.     $self->_create_raw($c);
  21.     return $self;
  22. }
  23.  
  24. sub _create_raw {
  25.     my ($self, $c) = @_;
  26.  
  27.     if($self->{dbix_class_model}) {
  28.         try {
  29.             # Instantiate a new DBIx::Raw object...
  30.             $self->{raw} = DBIx::Raw->new( $c->model($self->{dbix_class_model})->storage->dbh );
  31.         }
  32.     }
  33.     else {
  34.         Carp::croak("Must provide either dbix_class_model, or (dsn, user, password), or conf") unless $self->{dbix_class_model}
  35.                     or ($self->{dsn} and $self->{user} and $self->{password}) or $self->{conf};
  36.  
  37.         # Instantiate a new DBIx::Raw object...
  38.         $self->{raw} =  DBIx::Raw->new (
  39.                         dsn => $self->{dsn},
  40.                         user => $self->{user},
  41.                         password => $self->{password},
  42.                         conf => $self->{conf},
  43.                     );
  44.     }
  45. }
  46.  
  47.  
  48. sub build_per_context_instance {
  49.     my ($self, $c) = @_;
  50.  
  51.     return $self unless ref $c;
  52.  
  53.     #reuse same DBIx::Raw object, but get new dbh
  54.     if($self->{dbix_class_model}) {
  55.         if($self->{raw}) {
  56.             $self->{raw}->dbh( $c->model($self->{dbix_class_model})->storage->dbh );
  57.         }
  58.         else {
  59.             try {
  60.                 $self->{raw} = DBIx::Raw->new( $c->model($self->{dbix_class_model})->storage->dbh );
  61.             }
  62.             catch {
  63.                 Carp::croak("Error with dbix_class_model - $_");
  64.             }
  65.         }
  66.     }
  67.     else {
  68.         $self->{raw}->connect;
  69.     }
  70.  
  71.     return $self;
  72. }
  73.  
  74. sub AUTOLOAD {
  75.     my $self = shift;
  76.     our $AUTOLOAD;
  77.  
  78.     my $program = $AUTOLOAD;
  79.     $program =~ s/.*:://;
  80.  
  81.     # pass straight through to our DBIx::Raw object
  82.     return $self->{raw}->$program(@_);
  83. }
  84.  
  85. =head1 SYNOPSIS
  86.  
  87.     # Use the helper to add a DBIx::Raw model to your application
  88.     script/myapp_create.pl model Raw DBIx::Raw
  89.  
  90.     package YourApp::Model::Raw;
  91.     use parent 'Catalyst::Model::DBIx::Raw';
  92.  
  93.     __PACKAGE__->config(
  94.         dsn => 'dsn',
  95.         user => 'user',
  96.         password => 'password',
  97.     );
  98.  
  99.     #or
  100.     __PACKAGE__->config(
  101.         conf => '/path/to/conf.pl',
  102.     );
  103.  
  104.     #or
  105.     __PACKAGE__->config(
  106.         dbix_class_model => 'DB', #will use same dbh as DBIx::Class if you have a DBIx::Class model named 'DB'
  107.     );
  108.  
  109.  
  110.     1;
  111.    
  112.     package YourApp::Controller::Foo;
  113.  
  114.     sub index : Path('/') {
  115.         my ($self, $c) = @_;
  116.         my $name = $c->model('Raw')->raw("SELECT name FROM people WHERE id=1");
  117.         $c->res->body("Hello, $name!");
  118.     }
  119.  
  120.     1;
  121.  
  122. =method new
  123.  
  124. L<Catalyst> calls this method.
  125.  
  126. =cut
  127.  
  128. =head1 CONFIG
  129.  
  130. L<Catalyst::Model::DBIx::Raw> takes in all of the same options as config options that L<DBIx::Raw> accepts for new. You can use C<dsn>, C<user>, and C<password> to connect:
  131.  
  132.     __PACKAGE__->config(
  133.         dsn => 'dsn',
  134.         user => 'user',
  135.         password => 'password',
  136.     );
  137.  
  138. Or you can use a conf file:
  139.  
  140.     __PACKAGE__->config(
  141.         conf => '/path/to/conf.pl',
  142.     );
  143.  
  144. See L<DBIx::Raw> for more information on those options. Additionally, there is one new option in L<Catalyst::Model::DBIx::Raw>, and that is C<dbix_class_model>:
  145.  
  146.     __PACKAGE__->config(
  147.         dbix_class_model => 'DB',
  148.     );
  149.  
  150. This is the name of your L<DBIx::Class> model, if you have one. If passed in, L<Catalyst::Model::DBIx::Raw> will reuse the same dbh that L<DBIx::Class> is using. This
  151. can be useful if you have L<DBIx::Class> being used for things such as session management or CRUD with forms, but you are using L<Catalyst::Model::DBIx::Raw> to query yourself. This
  152. way you do not unecessarily create two database handles. Even if you do not use L<DBIx::Class> in a particular call, L<Catalyst::Model::DBIx::Raw> can still use the L<DBIx::Class> model
  153. to get a database handle.
  154.  
  155. =head1 NOTES
  156.  
  157. One thing to note is that L<Catalyst::Model::DBIx::Raw> uses the same L<DBIx::Raw> object every request, but gets a new dbh every request using L<DBIx::Raw>'s
  158. L<connect|DBIx::Raw/"connect"> method.
  159.  
  160. =cut
  161.  
  162. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement