bigpresh/Dancer-Plugin-Database

New features in ::Handle

knutov opened this issue · 10 comments

Please take a look at knutov/Dancer-Plugin-Database@fa6acf55096079d436228643696b5362bb337c26

I'm trying to finally move from my own framework (which is obsolete enough now) to Dancer. I used my own module to handle MySQL queries but now I want to switch to D::P::Database - it's great, more universal and can work if threads used. So I have to port some very convenient features from my framework and did it in this commit.

What do you think about it?
Is it ok to extend this module (I will add logging support and tests before pull request later) or it's better to move them to another module? If so, can you add support of adding new handles? Something like

bless $dbh, $_ foreach @handles; return 1;

instead of

return bless $dbh, 'Dancer::Plugin::Database::Handle';

in D::P::Database.pm

(I think support of multiple handles will be great anyway)

Example of usage of new features:

use Data::Dumper;
...

get '/' => sub {
    debug Dumper(database->sql('select * from users')); debug '---';
    debug Dumper(database->select('select * from users where id=?',1)); debug '---';
    debug database->cnt('select * from users where id=?',1); debug '---';
    debug Dumper(database->row('select * from users where id=1')); debug '---';
    my ($a,$b) = database->row('select * from users where id=1');
    debug "$a, $b"; debug '---';
    debug database->insert('insert into users (name) values (?)','123'); debug '---';
    return database->cnt('select * from users where id=?',1);
};
bor commented

'query'/'quick_query' is much better keyword(for method name) then 'sql'

bor commented

and about 'DEFINING MULTIPLE CONNECTIONS'
did you see this section in POD ?
or you mean something else ?

not connections. handles.. example:

bless $dbh, 'Dancer::Plugin::Database::Handle'; # this is the only current supported `Handle`
bless $dbh, 'Dancer::Plugin::Database::MyHandle';
bless $dbh, 'Dancer::Plugin::Database::OneMoreandle';
return $dbh;

in D::P::Database.pm

I'm fairly sure an object can only be blessed into one class at a time, so I'm not sure what behaviour you expect from that example?

Sorry, I've never used it this way before and forgotten about it.

So, what do you think, how to better add possibility of extending the default Handle?

Simple way is to point in config which Handle to use [for every connection], but creating mix of additional short methods is more interesting, I think.

I'd rather not add additional methods to D::P::Database::Handle itself unnecessarily, but I agree that being able to have it bless the handle into a subclass instead would be sensible - so you could write your own class which subclasses Dancer::Plugin::Database::Handle and provides the additional methods you want, and set a config option to force the database handles to be blessed into that class instead.

I'll look at implementing this for you.

Thanks, it'll be great!

OK, implemented in f9b20aa and a few subsequent commits, pushed to devel - please try it out and see if it works for you! I'll get a developer release out shortly for CPAN testers results, and assuming all looks OK, get a stable release out within a few days.

Thanks for the suggestion!

Big thanks, I moved my code to Dancer::Plugin::Database::KISS (https://gist.github.com/1754790) and added
handle_class: 'Dancer::Plugin::Database::KISS' to config.yml
and everything looks fine.

You're welcome - glad to hear it works well for you :)