Problem overriding connection method from Schema module

6 messages Options
Embed this post
Permalink
Emmanuel Quevillon

Problem overriding connection method from Schema module

Reply Threaded More More options
Print post
Permalink
Hi,

I encounter a problem using my Schema::MyAppDB.pm module, especially
outside Catalyst application.

What I've done, I've overridden my connection method to get some
more infos about the database, especially driver name.

Here is my Schema::MyAppDB.pm module:

package Schema::MyAppDB;

use strict;
use warnings;

use base qw/DBIx::Class::Schema/;

__PACKAGE__->load_classes;

#Our specific method
__PACKAGE__->mk_group_accessors('simple' => qw/driver_name/);


sub connection {

    my ($self, @rest) = @_;
    $self->next::method(@rest);

    $self->driver_name($self->handler()->{Driver}->{Name});

}
# You can replace this text with custom content, and it will be
preserved on regeneration

1;


Using this module as it is into my Catalyst application works just fine.
As soon as I want to use it outside Catalyst, in a perl script for
example, e.g. :

#!/usr/bin/perl

use strict;
use Schema::MyAppDB;

my $schema =
Schema::MyAppDB->connect('dib:Pg:dbname=test','test','pass');
my $a = $schema->resultset("Public");
....


I get the following error

Can't locate object method "resultset" via package "Pg" (perhaps you
forgot to load "Pg"?) at ./test.pl line 7.


To get it working properly, I have to comment the entire
'connection' method previously overridden in Schema::MyAppDB.pm module.
So is this behaviour normal? What should I do not to always
comment/uncomment my connection method?

Note: I get the overriding method connection from this Catalyst Manual
http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/Cookbook.pod#Create_accessors_to_preload_static_data_once_per_server_instance



Thanks for any hint

Regards

--
-------------------------
Emmanuel Quevillon
Biological Software and Databases Group
Institut Pasteur
+33 1 44 38 95 98
tuco at_ pasteur dot fr
-------------------------

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Quinn Fazigu

Re: Problem overriding connection method from Schema module

Reply Threaded More More options
Print post
Permalink


On Fri, Aug 21, 2009 at 4:59 AM, Emmanuel Quevillon <[hidden email]> wrote:
sub connection {

   my ($self, @rest) = @_;
   $self->next::method(@rest);

   $self->driver_name($self->handler()->{Driver}->{Name});

}

Does standard DBIx::Class::Schema have a "handler" method, or is that a decoration added by Catalyst?  You could try wrapping the driver_name assignment like so:

if ($self->can('handler')) {
    $self->driver_name($self->handler()->{Driver}->{Name});
}

Schema::MyAppDB->connect('dib:Pg:dbname=test','test','pass');

That "dib" there in the connect parameter is a typo for "dbi", right?

 

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Emmanuel Quevillon

Re: Problem overriding connection method from Schema module

Reply Threaded More More options
Print post
Permalink
Quinn Fazigu wrote:

>
>
> On Fri, Aug 21, 2009 at 4:59 AM, Emmanuel Quevillon <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     sub connection {
>
>        my ($self, @rest) = @_;
>        $self->next::method(@rest);
>
>        $self->driver_name($self->handler()->{Driver}->{Name});
>
>     }
>
>
> Does standard DBIx::Class::Schema have a "handler" method, or is that a
> decoration added by Catalyst?  You could try wrapping the driver_name
> assignment like so:
>


hanlder method is a method I've removed from the example as my
connection method is much more longer...
It is a shortcut to $self->storage()->dbh().

> if ($self->can('handler')) {
>     $self->driver_name($self->handler()->{Driver}->{Name});
> }
>
>     Schema::MyAppDB->connect('dib:Pg:dbname=test','test','pass');
>
>
> That "dib" there in the connect parameter is a typo for "dbi", right?
>

Typo sorry :(

>  


--
-------------------------
Emmanuel Quevillon
Biological Software and Databases Group
Institut Pasteur
+33 1 44 38 95 98
tuco at_ pasteur dot fr
-------------------------

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Byron Young

RE: Problem overriding connection method from Schema module

Reply Threaded More More options
Print post
Permalink
In reply to this post by Emmanuel Quevillon

Emmanuel Quevillon wrote on 2009-08-21:

> sub connection {
>
>     my ($self, @rest) = @_;
>     $self->next::method(@rest);
>    
>     $self->driver_name($self->handler()->{Driver}->{Name});
>
> }
> # You can replace this text with custom content, and it will be
> preserved on regeneration
>
> 1;
>

Hey Emmanuel,

connection() must return the schema object (which will be returned by $self->next::method(@rest)).  In this case it will actually return the driver name string instead, which explains why the interpreter chokes when it tries to call the resultset() method on a string, as in your error below.

> my $schema =
> Schema::MyAppDB->connect('dib:Pg:dbname=test','test','pass'); my $a =
> $schema->resultset("Public"); ....
>
>
> I get the following error
>
> Can't locate object method "resultset" via package "Pg" (perhaps you
> forgot to load "Pg"?) at ./test.pl line 7.

The Catalyst cookbook section is incorrect - it would be great for you to supply a doc patch to the catalyst list so other people don't run into this as well.

Byron



_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Emmanuel Quevillon

Re: Problem overriding connection method from Schema module

Reply Threaded More More options
Print post
Permalink
Byron Young wrote:

> Emmanuel Quevillon wrote on 2009-08-21:
>> sub connection {
>>
>>     my ($self, @rest) = @_;
>>     $self->next::method(@rest);
>>    
>>     $self->driver_name($self->handler()->{Driver}->{Name});
>>
>> }
>> # You can replace this text with custom content, and it will be
>> preserved on regeneration
>>
>> 1;
>>
>
> Hey Emmanuel,
>
> connection() must return the schema object (which will be returned by $self->next::method(@rest)).  In this case it will actually return the driver name string instead, which explains why the interpreter chokes when it tries to call the resultset() method on a string, as in your error below.
>


Hi Byron,

Thanks to pointing me to this. It makes a lot of sense! I fixed it
and it works like a charm both from my scripts and from my Catalyst App.

>> my $schema =
>> Schema::MyAppDB->connect('dib:Pg:dbname=test','test','pass'); my $a =
>> $schema->resultset("Public"); ....
>>
>>
>> I get the following error
>>
>> Can't locate object method "resultset" via package "Pg" (perhaps you
>> forgot to load "Pg"?) at ./test.pl line 7.
>
> The Catalyst cookbook section is incorrect - it would be great for you to supply a doc patch to the catalyst list so other people don't run into this as well.
>
> Byron
>
>
Emmanuel

--
-------------------------
Emmanuel Quevillon
Biological Software and Databases Group
Institut Pasteur
+33 1 44 38 95 98
tuco at_ pasteur dot fr
-------------------------

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Emmanuel Quevillon

Re: Problem overriding connection method from Schema module

Reply Threaded More More options
Print post
Permalink
In reply to this post by Emmanuel Quevillon


-------- Original Message --------
Subject: Re: [Dbix-class] Problem overriding connection method from
Schema module
Date: Mon, 24 Aug 2009 09:23:06 +0200
From: Emmanuel Quevillon <[hidden email]>
Reply-To: [hidden email]
Organization: Institut Pasteur
To: Byron Young <[hidden email]>
CC: 'DBIx::Class user and developer list'
<[hidden email]>,  "DBIx:@" <"Class
useranddeveloperlistdbix-class"@lists.scsys.co.uk>
References: <[hidden email]>
<[hidden email]>

Byron Young wrote:

> Emmanuel Quevillon wrote on 2009-08-21:
>> sub connection {
>>
>>     my ($self, @rest) = @_;
>>     $self->next::method(@rest);
>>    
>>     $self->driver_name($self->handler()->{Driver}->{Name});
>>
>> }
>> # You can replace this text with custom content, and it will be
>> preserved on regeneration
>>
>> 1;
>>
>
> Hey Emmanuel,
>
> connection() must return the schema object (which will be returned by $self->next::method(@rest)).  In this case it will actually return the driver name string instead, which explains why the interpreter chokes when it tries to call the resultset() method on a string, as in your error below.
>


Hi Byron,

Thanks to pointing me to this. It makes a lot of sense! I fixed it
and it works like a charm both from my scripts and from my Catalyst App.

>> my $schema =
>> Schema::MyAppDB->connect('dib:Pg:dbname=test','test','pass'); my $a =
>> $schema->resultset("Public"); ....
>>
>>
>> I get the following error
>>
>> Can't locate object method "resultset" via package "Pg" (perhaps you
>> forgot to load "Pg"?) at ./test.pl line 7.
>
> The Catalyst cookbook section is incorrect - it would be great for you to supply a doc patch to the catalyst list so other people don't run into this as well.
>
> Byron
>
>
Emmanuel

--
-------------------------
Emmanuel Quevillon
Biological Software and Databases Group
Institut Pasteur
+33 1 44 38 95 98
tuco at_ pasteur dot fr
-------------------------


--
-------------------------
Emmanuel Quevillon
Biological Software and Databases Group
Institut Pasteur
+33 1 44 38 95 98
tuco at_ pasteur dot fr
-------------------------

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...