Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

API question 2 #4

Closed
mlawren opened this Issue · 2 comments

2 participants

@mlawren

From the documentation:

Instance Methods
"dbh"

     my $dbh = $conn->dbh;

   Returns the connection’s database handle. It will use a cached
   copy of the handle if the process has not been "fork"ed or a new
   thread spawned, and if the database connection is alive.
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I'm just curious what the reason for making the ->dbh method heavy by
default? The reason I ask is because I think this contributes to yet
more confusing behaviour. After reading about the existence of
$conn->dbh(), one assumes that it would make sense to use it. However,
the recommended/better use of DBIx::Connector is via the do() method,
which passes in a dbh argument:

 $conn->do(sub {
     my $dbh = shift;
     $dbh->do('INSERT INTO foo (name) VALUES (?)', undef, 'Fred' );
 });

This is confusing. "We provide ->dbh, but please don't use it, take
this shifted one instead". I also think the use of shift or @_ within
a method makes it look less like a block.

I keep wanting to write the following because it is the direction in
which the API/documentation sends me (even if unintentionally):

 $conn->do({
     $conn->dbh->do('INSERT INTO foo (name) VALUES (?)', undef, 'Fred' );
 });

Perhaps it is merely a documentation issue? Rather than attempting to
convince people that DBIx::Connector is a drop-in replacement for
@others and later on describing how it should really be used, perhaps
that should be reversed?

1. *This* is how this module works. You should use it.
2. If you are unsure and/or just want the benefits do this.

Maybe even split out the Compat stuff into a separate
module? Dunno.

Thoughts?

Cheers,
Mark.

@theory
Owner

You have a point. I think I'll change dbh() to skip all the validation if it's called from within a do() block (or txn_do() or svp_do(), of course). That should eliminate that particular issue. I'll also put the database handle in $_ for the block call, which should give a more blocky feel to it.

—Theory

@theory
Owner

Okay, you can now just use $_ in a block, which should make it feel more block-like, and call $conn->dbh from within a block without the overhead of a ping.

—Theory

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.