ap / DBIx-Connector

Fast, safe DBI connection and transaction management
https://metacpan.org/release/DBIx-Connector
40 stars 14 forks source link

Add on_connect_do support. #42

Closed bluefeet closed 9 years ago

bluefeet commented 9 years ago

I've create a subclass of DBIx::Connector which supports OnConnectDo in the DBI options hash ref. It works very well and I'd like to move it out of @work code and into DBIx::Connector. Is this something you'd be OK with receiving a pull request on? Just want to make sure before I make the effort.

theory commented 9 years ago

What is OnConnectDo?

bluefeet commented 9 years ago

Oh its just DBIC's on_connect_do where any time a connection is made the code ref (the value of the argument) is executed. In DBIx::Connector I have it written up as a final step in _connect().

OnConnectDo is the naming convention I used to match DBI's, so for example:

my $db = DBIx::Connect->new(
    $dsn, $user, $pass,
    { AutoCommit=>1, OnConnectDo=>sub{...} },
);

That make sense?

bluefeet commented 9 years ago

I don't personally like it, but since DBIx::Connector's arguments are vanilla DBI arguments there really isn't a very friendly place to put custom stuff like this in the instantiation of the object, so hijacking DBI's attributes seemed a reasonable place.

bluefeet commented 9 years ago

Here's what I'm using it for:

OnConnectDo => sub{
    my ($db) = @_;
    $db->run(fixup => sub{
        $_->do(q[SET SESSION sql_mode = 'TRADITIONAL']);
    });
}
theory commented 9 years ago

That's what DBI Callbacks are for. Do this instead:

my $conn = DBIx::Connector->new($dsn, $user, $pass, {
    AutoCommit => 1,
    Callbacks => { connected => sub {
        shift->do(q[SET SESSION sql_mode = 'TRADITIONAL']);
        return;
     } },
);
bluefeet commented 9 years ago

Crap, can't believe I missed that. Thanks.

theory commented 9 years ago

It's shocking how often this has come up. I even added a pointer to Callbacks to the DBIx::Connector docs to try to reduce the support overhead. :-)

bluefeet commented 9 years ago

And I missed that too??? /me hangs head in shame Thanks, dude. :)

theory commented 9 years ago

Well, it is just a single sentence.