Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | |
2 | my $mplx_trace = 0; | |
3 | ||
4 | # vim:ts=4 | |
5 | { | |
6 | package DBD::Multiplex; | |
7 | ||
8 | require DBI; | |
9 | require Carp; | |
10 | ||
11 | @EXPORT = (); | |
12 | $VERSION = substr(q$Revision: 1.2 $, 9,-1) -1; | |
13 | ||
14 | # $Id: Multiplex.pm,v 1.2 1999/07/29 00:11:57 timbo Exp $ | |
15 | # | |
16 | # Copyright (c) 1999, Tim Bunce & Thomas Kishel | |
17 | # | |
18 | # You may distribute under the terms of either the GNU General Public | |
19 | # License or the Artistic License, as specified in the Perl README file. | |
20 | ||
21 | $drh = undef; # holds driver handle once initialised | |
22 | $err = 0; # The $DBI::err value | |
23 | ||
24 | sub driver { | |
25 | return $drh if $drh; | |
26 | my($class, $attr) = @_; | |
27 | $class .= "::dr"; | |
28 | ($drh) = DBI::_new_drh($class, { | |
29 | 'Name' => 'Multiplex', | |
30 | 'Version' => $VERSION, | |
31 | 'Attribution' => 'DBD Multiplex by Tim Bunce & Thomas Kishel', | |
32 | }); | |
33 | return $drh; | |
34 | } | |
35 | ||
36 | ||
37 | # functions for calling a method for each child handle | |
38 | ||
39 | sub mplx_do_calls { # 'bottom-level' support function to do the calls | |
40 | my ($method, $handles, $wantarray, $opts) = (shift,shift,shift,shift); | |
41 | my $errh = $opts->{errh}; # handle to record error on | |
42 | my $stop = $opts->{stop_on}||'';# stop on first 'err' or 'ok' else do all | |
43 | my $trace= $opts->{trace}; | |
44 | my @results; | |
45 | my @errors; # sparse array paralleling $results[0..n] | |
46 | warn "mplx_do_calls $method for (@$handles)" if $trace; | |
47 | foreach my $h (@$handles) { # child handle | |
48 | push @results, ($wantarray) ? [ $h->$method(@_) ] | |
49 | : [ scalar $h->$method(@_) ]; | |
50 | if (my $err = $h->err) { | |
51 | my $errstr = $h->errstr; | |
52 | $errors[ @results-1 ] = [ $err, $errstr ]; | |
53 | DBI::set_err($errh, $err, $errstr) if $errh; | |
54 | warn "mplx_do_calls $h->$method ERROR: $err, $errstr" if $trace; | |
55 | last if $stop eq 'err'; | |
56 | } | |
57 | else { | |
58 | warn "mplx_do_calls $h->$method OK: @{ $results[-1] }" if $trace; | |
59 | last if $stop eq 'ok'; | |
60 | } | |
61 | } | |
62 | return (\@results, \@errors); | |
63 | } | |
64 | ||
65 | # XXX need variants/flags for | |
66 | # - call-each-till-first-success-then-stop | |
67 | # - call-all-return-first-success-else-error | |
68 | # - call-all-return-most-common-result (eg three-way-voting etc) | |
69 | # In some cases the default behaviour may depend on the method being called. | |
70 | # In others the application may want to override that for specific cases | |
71 | # (using \%attrib) | |
72 | sub mplx_method_all { | |
73 | my $method = shift; | |
74 | my $ph = shift; | |
75 | my $handles = $ph->{mplx_h} || die; | |
76 | ||
77 | my %opts = ( errh => $ph, stop_on => 'err', trace => $mplx_trace ); | |
78 | ||
79 | my ($results, $errors) | |
80 | = mplx_do_calls($method, $handles, wantarray, \%opts, @_); | |
81 | ||
82 | my $result = $results->[0]; # pick results to return | |
83 | return $result->[0] unless wantarray; | |
84 | return @$result; | |
85 | } | |
86 | ||
87 | } | |
88 | ||
89 | ||
90 | ||
91 | { package DBD::Multiplex::dr; # ====== DRIVER ====== | |
92 | $imp_data_size = 0; | |
93 | ||
94 | sub connect { my ($drh, $dsn, $user, $auth, $attr) = @_; | |
95 | ||
96 | # XXX parse $dsn and make multiple connects | |
97 | # need to define good syntax | |
98 | my $dbh1 = DBI->connect($dsn, $user, $auth, $attr); | |
99 | return DBI::set_err($drh, $DBI::err, $DBI::errstr) unless $dbh1; | |
100 | ||
101 | my ($this) = DBI::_new_dbh($drh, { | |
102 | Name => $dsn, | |
103 | User => $user, | |
104 | mplx_h => [ $dbh1 ], | |
105 | }); | |
106 | ||
107 | return $this; | |
108 | } | |
109 | ||
110 | sub disconnect_all { } | |
111 | sub DESTROY { } | |
112 | } | |
113 | ||
114 | ||
115 | ||
116 | { package DBD::Multiplex::db; # ====== DATABASE ====== | |
117 | $imp_data_size = 0; | |
118 | ||
119 | use strict; | |
120 | ||
121 | sub prepare { | |
122 | my $dbh = shift; | |
123 | my ($statement, $attribs) = @_; | |
124 | ||
125 | my $handles = $dbh->{mplx_h} || die; | |
126 | my %opts = ( errh => $dbh ); | |
127 | my ($results, $errors) | |
128 | = DBD::Multiplex::mplx_do_calls('prepare', $handles, wantarray, \%opts, @_); | |
129 | return if @$errors; | |
130 | ||
131 | my ($outer, $sth) = DBI::_new_sth($dbh, { | |
132 | 'Statement' => $statement, | |
133 | mplx_h => [ map { $_->[0] } @$results ], | |
134 | }); | |
135 | $outer; | |
136 | } | |
137 | ||
138 | # XXX replace this with dynamic info from updated DBI | |
139 | # XXX needs expanding manually in the short term | |
140 | use subs qw(FETCH STORE DESTROY); | |
141 | ||
142 | sub AUTOLOAD { | |
143 | my $method = $DBD::Multiplex::db::AUTOLOAD; | |
144 | $method =~ s/^DBD::Multiplex::db:://; | |
145 | warn "db AUTOLOAD $method(@_)" if $mplx_trace; | |
146 | ||
147 | my @results = (wantarray) | |
148 | ? ( DBD::Multiplex::mplx_method_all($method, @_)) | |
149 | : (scalar DBD::Multiplex::mplx_method_all($method, @_)); | |
150 | ||
151 | return $results[0] unless wantarray; | |
152 | return @results; | |
153 | } | |
154 | ||
155 | } | |
156 | ||
157 | ||
158 | { package DBD::Multiplex::st; # ====== STATEMENT ====== | |
159 | $imp_data_size = 0; | |
160 | use strict; | |
161 | ||
162 | # XXX replace this with dynamic info from updated DBI | |
163 | # XXX needs expanding manually in the short term | |
164 | use subs qw(execute fetch fetchrow_arrayref finish STORE FETCH DESTROY); | |
165 | ||
166 | sub AUTOLOAD { | |
167 | my $method = $DBD::Multiplex::st::AUTOLOAD; | |
168 | $method =~ s/^DBD::Multiplex::st:://; | |
169 | warn "st AUTOLOAD $method(@_)" if $mplx_trace; | |
170 | ||
171 | my @results = (wantarray) | |
172 | ? ( DBD::Multiplex::mplx_method_all($method, @_)) | |
173 | : (scalar DBD::Multiplex::mplx_method_all($method, @_)); | |
174 | ||
175 | return $results[0] unless wantarray; | |
176 | return @results; | |
177 | } | |
178 | ||
179 | } | |
180 | ||
181 | 1; | |
182 | __END__ | |
183 | ||
184 | =head1 NAME | |
185 | ||
186 | DBD::Multiplex - A DBI driver multiplexer | |
187 | ||
188 | =head1 SYNOPSIS | |
189 | ||
190 | use DBI; | |
191 | ||
192 | $dbh = DBI->connect("dbi:Multiplex:dsn", $user, $passwd); | |
193 | ||
194 | # See the DBI module documentation for full details | |
195 | ||
196 | =head1 DESCRIPTION | |
197 | ||
198 | To be written | |
199 | ||
200 | =cut |