| 1 | # $Id: DBI.pm,v 1.20 2009/11/30 14:28:19 dk Exp $ | ||
| 2 | package IO::Lambda::DBI::Storable; | ||
| 3 | |||
| 4 | use Storable qw(freeze thaw); | ||
| 5 | |||
| 6 | my $DEBUG_DUMP = (($IO::Lambda::DEBUG{dbi} || 0) > 1); | ||
| 7 | require Data::Dumper if $DEBUG_DUMP; | ||
| 8 | |||
| 9 | sub encode | ||
| 10 | { | ||
| 11 | my $self = $_[0]; | ||
| 12 | |||
| 13 | return Data::Dumper::Dumper($_[1]) if $DEBUG_DUMP; | ||
| 14 | |||
| 15 | my $msg; | ||
| 16 | eval { $msg = freeze( $_[1] ) }; | ||
| 17 | return $@ ? ( undef, $@) : $msg; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub decode | ||
| 21 | { | ||
| 22 | my $self = $_[0]; | ||
| 23 | |||
| 24 | if ( $DEBUG_DUMP) { | ||
| 25 | my $VAR1; | ||
| 26 | eval { eval $_[1] }; | ||
| 27 | return $@ ? ( undef, $@) : $VAR1; | ||
| 28 | } | ||
| 29 | |||
| 30 | my $msg; | ||
| 31 | eval { $msg = thaw( $_[1] ); }; | ||
| 32 | return $@ ? ( undef, $@) : $msg; | ||
| 33 | } | ||
| 34 | |||
| 35 | package IO::Lambda::DBI; | ||
| 36 | use base qw( | ||
| 37 | IO::Lambda::Message | ||
| 38 | IO::Lambda::DBI::Storable | ||
| 39 | ); | ||
| 40 | |||
| 41 | our $DEBUG = $IO::Lambda::DEBUG{dbi}; | ||
| 42 | |||
| 43 | use strict; | ||
| 44 | use warnings; | ||
| 45 | use Carp; | ||
| 46 | use IO::Lambda qw(:all :dev); | ||
| 47 | use IO::Lambda::Message; | ||
| 48 | |||
| 49 | sub _d { "dbi(" . _o($_[0]) . ")" } | ||
| 50 | |||
| 51 | sub outcoming | ||
| 52 | { | ||
| 53 | my ( $self, $msg) = @_; | ||
| 54 | my $error; | ||
| 55 | |||
| 56 | ($msg, $error) = $self-> decode( $msg); | ||
| 57 | if ( defined $error) { | ||
| 58 | warn _d($self), " error: $error\n" if $DEBUG; | ||
| 59 | return ( undef, $error); | ||
| 60 | } | ||
| 61 | |||
| 62 | unless ( $msg and ref($msg) and ref($msg) eq 'ARRAY' and @$msg > 0) { | ||
| 63 | warn _d($self), " error: bad response($msg)\n" if $DEBUG; | ||
| 64 | return ( undef, "bad response"); | ||
| 65 | } | ||
| 66 | |||
| 67 | # remote eval failed, or similar | ||
| 68 | unless ( shift @$msg) { | ||
| 69 | warn _d($self), " error: @$msg\n" if $DEBUG; | ||
| 70 | return ( undef, @$msg); | ||
| 71 | } | ||
| 72 | |||
| 73 | # ok, finally | ||
| 74 | warn _d($self), " < ok: @$msg\n" if $DEBUG; | ||
| 75 | return ( 1, @$msg); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub begin_group | ||
| 79 | { | ||
| 80 | my $self = shift; | ||
| 81 | croak "Group already started" if exists $self-> {post}; | ||
| 82 | $self-> {post} = []; | ||
| 83 | return (); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub end_group | ||
| 87 | { | ||
| 88 | my $self = shift; | ||
| 89 | my $p = delete $self-> {post}; | ||
| 90 | croak "Group not started" unless $p; | ||
| 91 | return lambda {} unless @$p; | ||
| 92 | |||
| 93 | my ( $msg, $error) = $self-> encode([ 'multicall', 1, $p ]); | ||
| 94 | |||
| 95 | return lambda { $error } if $error; | ||
| 96 | warn _d($self) . " > end_group(@_)\n" if $DEBUG; | ||
| 97 | |||
| 98 | return $self-> new_message( $msg, $self-> {timeout} ); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub dbi_message | ||
| 102 | { | ||
| 103 | my ( $self, $method, $wantarray) = ( shift, shift, shift ); | ||
| 104 | |||
| 105 | my $packet = [ $method, $wantarray, @_ ]; | ||
| 106 | if ( $self-> {post}) { | ||
| 107 | push @{ $self->{post} }, $packet; | ||
| 108 | return (); | ||
| 109 | } | ||
| 110 | |||
| 111 | my ( $msg, $error) = $self-> encode( $packet); | ||
| 112 | |||
| 113 | return lambda { $error } if $error; | ||
| 114 | warn _d($self) . " > $method(@_)\n" if $DEBUG; | ||
| 115 | return $self-> new_message( $msg, $self-> {timeout} ); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub connect { shift-> dbi_message( connect => 0, @_) } | ||
| 119 | sub disconnect { shift-> dbi_message( disconnect => 0, @_) } | ||
| 120 | sub call { shift-> dbi_message( call => wantarray, @_) } | ||
| 121 | sub set_attr { shift-> dbi_message( set_attr => 0, @_) } | ||
| 122 | sub get_attr { shift-> dbi_message( get_attr => wantarray, @_) } | ||
| 123 | |||
| 124 | sub prepare | ||
| 125 | { | ||
| 126 | my ( $self, $stmt) = @_; | ||
| 127 | lambda { | ||
| 128 | context $self-> dbi_message( prepare => 0, $stmt); | ||
| 129 | tail { | ||
| 130 | my $ok = shift; | ||
| 131 | return 0, $_[0] unless $ok; | ||
| 132 | return 1, IO::Lambda::DBI::Statement-> new($self, $_[0]); | ||
| 133 | }} | ||
| 134 | } | ||
| 135 | |||
| 136 | sub DESTROY {} | ||
| 137 | |||
| 138 | sub AUTOLOAD | ||
| 139 | { | ||
| 140 | use vars qw($AUTOLOAD); | ||
| 141 | my $method = $AUTOLOAD; | ||
| 142 | $method =~ s/^.*:://; | ||
| 143 | shift-> dbi_message( call => wantarray, $method, @_); | ||
| 144 | } | ||
| 145 | |||
| 146 | package IO::Lambda::DBI::Statement; | ||
| 147 | |||
| 148 | sub new | ||
| 149 | { | ||
| 150 | my ( $class, $owner, $obj_id) = @_; | ||
| 151 | return bless { | ||
| 152 | owner => $owner, | ||
| 153 | id => $obj_id, | ||
| 154 | }, $class; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub DESTROY | ||
| 158 | { | ||
| 159 | } | ||
| 160 | |||
| 161 | sub AUTOLOAD | ||
| 162 | { | ||
| 163 | use vars qw($AUTOLOAD); | ||
| 164 | my $method = $AUTOLOAD; | ||
| 165 | $method =~ s/^.*:://; | ||
| 166 | my $self = shift; | ||
| 167 | |||
| 168 | return $self-> {owner}-> dbi_message( | ||
| 169 | execute => wantarray, | ||
| 170 | $method, $self-> {id}, | ||
| 171 | @_ | ||
| 172 | ); | ||
| 173 | } | ||
| 174 | |||
| 175 | package IO::Lambda::Message::DBI; | ||
| 176 | use base qw( | ||
| 177 | IO::Lambda::Message::Simple | ||
| 178 | IO::Lambda::DBI::Storable | ||
| 179 | ); | ||
| 180 | |||
| 181 | use DBI; | ||
| 182 | |||
| 183 | sub connect | ||
| 184 | { | ||
| 185 | my $self = shift; | ||
| 186 | die "already connected\n" if $self-> {dbh}; | ||
| 187 | $self-> {dbh} = DBI-> connect(@_); | ||
| 188 | die $DBI::errstr, "\n" unless $self-> {dbh}; | ||
| 189 | $self-> {prepared} = {}; | ||
| 190 | return undef; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub disconnect | ||
| 194 | { | ||
| 195 | my $self = shift; | ||
| 196 | die "not connected\n" unless $self-> {dbh}; | ||
| 197 | my @r = $self-> {dbh}-> disconnect; | ||
| 198 | undef $self-> {dbh}; | ||
| 199 | $self-> quit; | ||
| 200 | return @r; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub call | ||
| 204 | { | ||
| 205 | my ( $self, $method, @p) = @_; | ||
| 206 | die "not connected\n" unless $self-> {dbh}; | ||
| 207 | return $self-> {dbh}-> $method(@p); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub set_attr | ||
| 211 | { | ||
| 212 | my ( $self, %attr) = @_; | ||
| 213 | die "not connected\n" unless $self-> {dbh}; | ||
| 214 | while ( my ( $k, $v) = each %attr) { | ||
| 215 | $self-> {dbh}-> {$k} = $v; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | sub get_attr | ||
| 220 | { | ||
| 221 | my ( $self, @keys) = @_; | ||
| 222 | die "not connected\n" unless $self-> {dbh}; | ||
| 223 | return @{$self->{dbh}}{@keys}; | ||
| 224 | |||
| 225 | } | ||
| 226 | |||
| 227 | sub prepare | ||
| 228 | { | ||
| 229 | my ( $self, $stmt) = @_; | ||
| 230 | die "not connected\n" unless $self-> {dbh}; | ||
| 231 | |||
| 232 | my $s = $self-> {dbh}-> prepare($stmt); | ||
| 233 | die $self-> {dbh}-> errstr unless $s; | ||
| 234 | |||
| 235 | $self-> {prepared}-> {"$s"} = $s; | ||
| 236 | return "$s"; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub execute | ||
| 240 | { | ||
| 241 | my ( $self, $method, $stmt, @p) = @_; | ||
| 242 | |||
| 243 | die "not connected\n" unless $self-> {dbh}; | ||
| 244 | my $p = $self-> {prepared}-> {$stmt}; | ||
| 245 | die "no such prepared statement\n" unless $p; | ||
| 246 | delete $self-> {prepared}-> {$stmt} if $method eq 'finish'; | ||
| 247 | return $p-> $method(@p); | ||
| 248 | } | ||
| 249 | |||
| 250 | sub multicall | ||
| 251 | { | ||
| 252 | my ( $self, $post) = @_; | ||
| 253 | |||
| 254 | my @ret; | ||
| 255 | for ( @$post) { | ||
| 256 | my $method = shift @$_; | ||
| 257 | my $want = shift @$_; | ||
| 258 | die "no such method: $method" unless $self-> can($method); | ||
| 259 | unless ( defined $want) { | ||
| 260 | $self-> $method(@$_); | ||
| 261 | } elsif ( $want) { | ||
| 262 | push @ret, $self-> $method(@$_); | ||
| 263 | } else { | ||
| 264 | push @ret, scalar($self-> $method(@$_)); | ||
| 265 | } | ||
| 266 | } | ||
| 267 | return wantarray ? @ret : $ret[0]; | ||
| 268 | } | ||
| 269 | |||
| 270 | 1; | ||
| 271 | |||
| 272 | __DATA__ |