bigpresh / Dancer-Plugin-Database

Dancer::Plugin::Database - easy database support for Dancer applications
http://search.cpan.org/dist/Dancer-Plugin-Database
37 stars 36 forks source link

New features in ::Handle #23

Closed knutov closed 12 years ago

knutov commented 12 years ago

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 12 years ago

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

bor commented 12 years ago

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

knutov commented 12 years ago

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

bigpresh commented 12 years ago

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?

knutov commented 12 years ago

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.

bigpresh commented 12 years ago

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.

knutov commented 12 years ago

Thanks, it'll be great!

bigpresh commented 12 years ago

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!

knutov commented 12 years ago

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.

bigpresh commented 12 years ago

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