On Fri, Mar 25, 2005 at 03:06:56PM -0800, David Wheeler wrote:
> 
> >>>1. callbacks are only passed the handle, not the other method args.
> >>
> >>That seems smart to me. If you passed it via method args (I assume you
> >>mean \%attrs), then would it last only for the execution of that
> >>method, or become permanent? The former would be okay, I think.
> >
> >Good question. Given:
> >
> >    $dbh->{Callbacks}->{prepare} => sub {
> >     my ($dbh, $sql, $attr) = @_;
> >     $attr->{foo} = 42;
> >     return;
> >    };
> >
> >and
> >
> >    A) $dbh->prepare($sql, { foo => 1 })
> >    B) $dbh->prepare($sql, \%attr)
> >
> >then for A the effect is temporary but for B it's permenant.
> 
> Sure, that makes sense. But if I did this:
> 
>   $dbh->prepare($sql, { Callbacks => { prepare => sub { ... } });
> 
> ...well, I guess it'd be pretty dumb to do that.

Not if you remember that the (or at least _a_ plan) plan is to rework
$dbh->prepare so it looks like this:

        sub prepare {
            my ($dbh, $sql, $attr) = @_;
             my $sth = $dbh->new_child($attr);
             $sth->prepare($sql);
             return $sth;
        }

But I suspect that's not going to happen for perl 5.

> Callbacks should only apply to the database handle, I think.

I don't see any reason to limit them in that way. They already work
for all methods on all handles. Note that sth callbacks, for example,
give you a relatively clean way to set the UTF8 flag on fetched data.


> >You can avoid that problem by doing:
> >
> >    $dbh->{Callbacks}->{prepare} => sub {
> >     $_[2] = { %{ $_[2] }; # shallow copy \%attr first
> >     my ($dbh, $sql, $attr) = @_;
> >     $attr->{foo} = 42;
> >     return;
> >    };
> 
> Sure.
> 
> >But all the above will have problems with
> >
> >    $dbh->prepare($sql);
> >
> >since there's no \%attr argument (or slot on the stack) to manipulate.
> >I can see a way to fudge around that in the C code, but it won't be
> >pretty :)
> 
> Wouldn't it just be undef?

As an rvalue yes, but something like $_[2]=... wouldn't work.


> >>>3. The method name isn't available via a localized $_
> 
> Yeah, that'd be neat.

Done.


> >>>5. A DBI::Callbacks module to provide an interface for managing
> >>>  callbacks, including a way to define callbacks to be applied
> >>>  to all future child statement handles (by using a post-prepare).
> >>
> >>Not sure I understand what you mean here. Would you care to elaborate?
> >
> >Currently each method can only have one callback. Two bits of code
> >trying to add/remove callbacks will tread on each others toes.
> >
> >What I've not said explicitly so far is that it would be nice to
> >optionally support multiple callbacks per method by allowing
> >callbacks to be arrays of code refs:
> >
> >  $dbh->{Callbacks}->{prepare} => [ [EMAIL PROTECTED], [EMAIL PROTECTED], ]
> >
> >but managing that would be a pain, an API would help:
> >
> >  $h->callback_add( $method_name, $code_ref [, $pre_or_post ]);
> >  $h->callback_delete( $method_name, $code_ref [, $pre_or_post ]);
> >
> >But that's some way down the road. The bigger short term gain is
> >that the docs for Callbacks won't clutter the main DBI docs.
> >(Similar to DBI::Profile being separated out.)
> 
> I gotcha. Seems like you're talking about syntactic sugar for the most 
> part, though, eh?

Strong sugar I guess, plus the benefits of encapsulation.
But unless someone contributes it I'm unlikely to anytime soon.

> >>That may not be enough now, though, I honestly don't know.
> >
> >It's enough for the 'explicit' callbacks written into connect and
> >connect_cached[1]. But not for the generic implicit callbacks
> >handled by the dispatcher.
> 
> Ah, something would have to be added to the PP dispatcher, no?

Exactly. Modeled closely on what's been added to the C code dispatcher.


> >The big problem here is that conceptually the Callbacks attribute
> >is applied to the $dbh _during_ the $drh->connect() call, so you can't
> >set a callback on "connect" on the $dbh because connect isn't called
> >on the dbh, but on the $drh.
> >
> >So a "connect" callback would have to be defined on the $drh, but that's
> >cumbersom for the user and then it would apply to all future connects
> >using that driver.
> >
> >The best thing to do is probably to special-case "connect", "connect_cached"
> >and (the already special-case) "connect_cached.reused".
> 
> That seems reasonable to me. Since the drh isn't really available in 
> user space, it doesn't matter so much to me.

   $drh = DBI->install_driver('Pg');
   $drh->{Callbacks} = ...

but that's discouraged :)

> The other option I can 
> think of, though, is to perhaps make those callbacks configurable via a 
> class method, so then they apply to all uses of connect() and 
> connect_cached() across the board. But woe betide the module that uses 
> DBI that doesn't know that some other module has set up such a global 
> callback.

True. I can definitely see a place for global callbacks drh, dbh,
and sth callbacks though. It's up to the callbacks to be 'well behaved'.
Umm, perhaps:

    DBI->callback_add('dr', connect => sub { ... });
    DBI->callback_add('db', prepare => sub { ... });

Would only apply to future handles though.

> The current approach makes sense to me. In what other way can you see 
> that it might make sense to change their names?

I've left in the "connect_cached.reused" special case but removed the
"connect" one as it wasn't 'special' enough and there's no clear need.
That'll do for now.

Tim.

Reply via email to