Closed bluefeet closed 9 years ago
What is OnConnectDo?
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?
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.
Here's what I'm using it for:
OnConnectDo => sub{
my ($db) = @_;
$db->run(fixup => sub{
$_->do(q[SET SESSION sql_mode = 'TRADITIONAL']);
});
}
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;
} },
);
Crap, can't believe I missed that. Thanks.
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. :-)
And I missed that too??? /me hangs head in shame Thanks, dude. :)
Well, it is just a single sentence.
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.