On Tue, Mar 22, 2005 at 04:46:33PM -0800, David Wheeler wrote:
> YES! :-)

:)

> On Mar 22, 2005, at 4:21 PM, Tim Bunce wrote:
> 
> >The return; is because I've not decided how to interpret return values
> >so to avoid problems later I've mandated that callbacks must return 
> >nothing.
> 
> I can certainly live with that. Are return values currently ignored?

An exception will be thrown if anything is returned.

> I think I'd be inclined to have them mean nothing, and leave it up to 
> callbacks to throw exceptions when something exceptional happens.

Sure, but we may come up with interesting non-exceptional behavior.
Like, for example, allowing the callback to say 'don't call the
method but return @foo instead' - effectively replacing the call.

> >What's missing, roughly in order of importance...
> >
> >0. Documentation.
> 
> Oh, I guess I didn't include any docs in my original patch. I'll take a 
> shot at this in the next couple of days.

Let's let Steven have a crack at the docs first.

> >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.
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;
    };

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 :)


> >2. No support for post-call callbacks via
> >     $dbh->{Callbacks}->{prepare} => [ \&pre, \&post ]
> 
> So when do the execute now?

Pre.

> >3. The method name isn't available via a localized $_
> 
> Is it available at all?

Not currently. Probably only needs a couple of lines of C code to
add that though.

> >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.)

> >Any volunteers for any of those?
> 
> Docs coming soon.

Let's let Steven have a crack at the docs first. Hopefully he can
absorb my extra comments and examples from this thread.

> >p.s. There's no PurePerl support and I've no plans to add it anytime 
> >soon.
> 
> It was in my original patch. Quite simple, really:
> 
> --- lib/DBI/PurePerl.pm       (revision 870)
> +++ lib/DBI/PurePerl.pm       (working copy)
> @@ -143,6 +143,7 @@
>       Attribution
>       BegunWork
>       CachedKids
> +        Callbacks
>       CursorName
>       Database
>       DebugDispatch
> 
> 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.

> Regards and many thanks,

You're welcome, but it's not over yet... :)

Take a look at the comment block in t/70callbacks.t - I've not
given it much thought yet but the implication is that the
connect and connect_cached callbacks may need to change slightly
(in name, or timing, or both) to be more consistent with the generic
callbacks.

Tim.

Reply via email to