Subject: | Patch that adds support for DBI wrapper classes |
Hi,
as i'm working with small Catalyst::Model::DBI and custom DBI wrappers
for various extensions i needed support for wrapper classes and wrote
the attached patch.
Now you can additionally configure DBI wrapper classes like this:
$ script/create.pl model DBI DBI::Wrapper wrapper dsn user password
In Catalyst::Model::DBI i've taken out the direct use of DBI and made
new() execute Catalyst::Utils::ensure_class_loaded() instead to load a
possibly configured alternative DBI class.
Further i've added a check to connected() to prevent additional errors
in the case the alternative DBI class wasn't found.
I copied Catalyst::Helper::Model::DBI over to
Catalyst::Helper::Model::DBI::Wrapper because i wasn't able to find a
good and intuitive way to do the same with the existing helper.
Besides that, i hope i did all that would be necessary to get a new
release ready.
It would be very nice to see this going upstream as it would ease the
work for everyone who's using DBI wrappers.
Kind regards,
Simon
Subject: | Catalyst-Model-DBI.diff |
Index: t/01use.t
===================================================================
--- t/01use.t (revision 8084)
+++ t/01use.t (working copy)
@@ -1,5 +1,6 @@
use strict;
-use Test::More tests => 2;
+use Test::More tests => 3;
BEGIN { use_ok('Catalyst::Model::DBI') }
BEGIN { use_ok('Catalyst::Helper::Model::DBI') }
+BEGIN { use_ok('Catalyst::Helper::Model::DBI::Wrapper') }
Index: lib/Catalyst/Helper/Model/DBI/Wrapper.pm
===================================================================
--- lib/Catalyst/Helper/Model/DBI/Wrapper.pm (revision 0)
+++ lib/Catalyst/Helper/Model/DBI/Wrapper.pm (revision 0)
@@ -0,0 +1,103 @@
+package Catalyst::Helper::Model::DBI::Wrapper;
+
+use strict;
+use File::Spec;
+
+our $VERSION = '0.19';
+
+=head1 NAME
+
+Catalyst::Helper::Model::DBI - Helper for DBI Wrapper Models
+
+=head1 SYNOPSIS
+
+ script/create.pl model DBI DBI::Wrapper wrapper dsn user password
+
+=head1 DESCRIPTION
+
+Helper for DBI Wrapper Model.
+
+=head2 METHODS
+
+=over 4
+
+=item mk_compclass
+
+Reads the database and makes a main model class
+
+=item mk_comptest
+
+Makes tests for the DBI Model.
+
+=back
+
+=cut
+
+sub mk_compclass {
+ my ( $self, $helper, $wrapper, $dsn, $user, $pass ) = @_;
+ $helper->{wrapper} = $wrapper || 'DBI';
+ $helper->{dsn} = $dsn || '';
+ $helper->{user} = $user || '';
+ $helper->{pass} = $pass || '';
+ my $file = $helper->{file};
+ $helper->render_file( 'dbiclass', $file );
+ return 1;
+}
+
+=head1 SEE ALSO
+
+L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
+L<Catalyst::Response>, L<Catalyst::Helper>
+
+=head1 AUTHOR
+
+Alex Pavlovic, C<alex.pavlovic@taskforce-1.com>
+
+=head1 LICENSE
+
+This library is free software . You can redistribute it and/or modify
+it under the same terms as perl itself.
+
+=cut
+
+1;
+__DATA__
+
+__dbiclass__
+package [% class %];
+
+use strict;
+use base 'Catalyst::Model::DBI';
+
+__PACKAGE__->config(
+ wrapper => '[% wrapper %]',
+ dsn => '[% dsn %]',
+ user => '[% user %]',
+ password => '[% pass %]',
+ options => {},
+);
+
+=head1 NAME
+
+[% class %] - DBI Wrapper Model Class
+
+=head1 SYNOPSIS
+
+See L<[% app %]>
+
+=head1 DESCRIPTION
+
+DBI Wrapper Model Class.
+
+=head1 AUTHOR
+
+[% author %]
+
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+1;
Index: lib/Catalyst/Model/DBI.pm
===================================================================
--- lib/Catalyst/Model/DBI.pm (revision 8084)
+++ lib/Catalyst/Model/DBI.pm (working copy)
@@ -3,9 +3,8 @@
use strict;
use base 'Catalyst::Model';
use NEXT;
-use DBI;
-our $VERSION = '0.19';
+our $VERSION = '0.20';
__PACKAGE__->mk_accessors( qw/_dbh _pid _tid/ );
@@ -51,12 +50,15 @@
sub new {
my $self = shift;
- my ( $c ) = @_;
+ my ( $c, $config ) = @_;
$self = $self->NEXT::new( @_ );
- $self->{namespace} ||= ref $self;
- $self->{additional_base_classes} ||= ();
+ $self->{namespace} ||= ref $self;
+ $self->{class} = $config->{wrapper} || 'DBI';
$self->{log} = $c->log;
$self->{debug} = $c->debug;
+
+ Catalyst::Utils::ensure_class_loaded($self->{class});
+
return $self;
}
@@ -101,6 +103,7 @@
sub connected {
my $self = shift;
+ return unless $self->dbh;
return $self->_dbh->{Active} && $self->_dbh->ping;
}
@@ -114,7 +117,7 @@
my $self = shift;
my $dbh;
eval {
- $dbh = DBI->connect(
+ $dbh = $self->{class}->connect(
$self->{dsn},
$self->{user},
$self->{password},
Index: MANIFEST
===================================================================
--- MANIFEST (revision 8084)
+++ MANIFEST (working copy)
@@ -1,5 +1,6 @@
Changes
lib/Catalyst/Helper/Model/DBI.pm
+lib/Catalyst/Helper/Model/DBI/Wrapper.pm
lib/Catalyst/Model/DBI.pm
Makefile.PL
MANIFEST This list of files