Dbi query helper for dbiagent
use Socket qw/:crlf/; use POE qw/Filter::Line Wheel::Run Component::DBIAgent::Helper/; sub _start { my $helper = POE::Wheel::Run ->new( Program => sub { POE::Component::DBIAgent::Helper->run($self->{dsn}, $self->{queries} ); }, StdoutEvent => 'db_reply', StderrEvent => 'remote_stderr', ErrorEvent => 'error', StdinFilter => POE::Filter::Line->new(), StdoutFilter => POE::Filter::Line->new( Literal => CRLF), StderrFilter => POE::Filter::Line->new(), ) or carp "Can't create new DBIAgent::Helper: $!\n"; } sub query { my ($self, $query, $package, $state, @rest) = @_; $self->{helper}->put(join '|', $query, $package, $state, @rest); } sub db_reply { my ($kernel, $self, $heap, $input) = @_[KERNEL, OBJECT, HEAP, ARG0]; # $input is either the string 'EOF' or a Storable object. }
This is our helper routine for DBIAgent. It accepts queries on \s-1STDIN\s0, and returns the results on \s-1STDOUT\s0. Queries are returned on a row-by-row basis, followed by a row consisting of the string '\s-1EOF\s0'.
Each row is the return value of $sth->fetch, which is an arrayref. This row is then passed to Storable for transport, and printed to \s-1STDOUT\s0. \s-1HOWEVER\s0, Storable uses newlines (\*(L"\n\*(R") in its serialized strings, so the Helper is designed to use the \*(L"network newline\*(R" pair \s-1CR\s0 \s-1LF\s0 as the line terminator for \s-1STDOUT\s0.
When fetch() returns undef, one final row is returned to the calling state: the string '\s-1EOF\s0'. Sessions should test for this value \s-1FIRST\s0 when being invoked with input from a query.
The Helper has one public subroutine, called \*(C`run()\*(C', and is invoked with two parameters:
An arrayref of parameters to pass to \s-1DBI-\s0>connect (usually a dsn, username, and password).
A hashref of the form Query_Name => \*(L"$SQL\*(R". See POE::Component::DBIAgent for details.
I have \s-1NO\s0 idea what to do about handling signals intelligently. Specifically, under some circumstances, Oracle will refuse to acknowledge \s-1SIGTERM\s0 (presumably since its libraries are non-reentrant) so sometimes \s-1SIGKILL\s0 is required to terminate a Helper process.
This module has been fine-tuned and packaged by Rob Bloodgood <[email protected]>. However, most of the code came directly from Fletch <[email protected]>, either directly (Po:Co:DBIAgent:Queue) or via his ideas. Thank you, Fletch!
However, I own all of the bugs.
This module is free software; you may redistribute it and/or modify it under the same terms as Perl itself.