| 1 | # $Id: ResultSet.pm,v 1.21 2009-03-05 17:49:33 mike Exp $ | ||
| 2 | |||
| 3 | package Net::Z3950::DBIServer::ResultSet; | ||
| 4 | use Net::Z3950::DBIServer::Exception; | ||
| 5 | use strict; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Net::Z3950::DBIServer::ResultSet - in-progress result set objects for DBIServer | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | $rs = new Net::Z3950::DBIServer::ResultSet($server, $SQLcond, $config); | ||
| 14 | $n = $rs->count(); | ||
| 15 | for ($i = 0; $i < $n; $i++) { | ||
| 16 | $hashref = $rs->fetch($i); | ||
| 17 | } | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Represents a result set in the process of being inspected by a client. | ||
| 22 | The initial search is executed at construction time, and the number of | ||
| 23 | record satisfying the condition stored and made available. | ||
| 24 | Thereafter, records may be fetched via a random-access interface. | ||
| 25 | |||
| 26 | In truth, records must be fetched in order - that's how the DBI | ||
| 27 | interface, and RDBMSs in general work - and that's how we expect | ||
| 28 | ResultSets to be used most of the time; but we give the illusion | ||
| 29 | of random access by reading ahead to the required record if asked for | ||
| 30 | one further ahead than the next in sequence; and by caching a few of the more | ||
| 31 | recently-read records after reading them, to support requests for | ||
| 32 | earlier records. The upshot is that code like | ||
| 33 | |||
| 34 | for ($i = $n-1; $i >= 0; $i--) { | ||
| 35 | $hashref = $rs->fetch($i); | ||
| 36 | } | ||
| 37 | |||
| 38 | will work provided that I<$n> is not ``too large'', which means larger | ||
| 39 | than the record cache associated with the result set. This may be | ||
| 40 | configured using the configuration file's C<cachesize> directive: the | ||
| 41 | default value is 10. If the value is set to zero, this means never to | ||
| 42 | discard records. | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | =head2 new() | ||
| 47 | |||
| 48 | $rs = new Net::Z3950::DBIServer::ResultSet($server, $config, $cond); | ||
| 49 | |||
| 50 | Creates and returns a new C<ResultSet> on behalf of the DBIServer | ||
| 51 | object I<$server>, using that server's DBI connection handle, | ||
| 52 | satisfying the condition I<$cond> and interpreted by the | ||
| 53 | database-specific configuration segment I<$config>, | ||
| 54 | |||
| 55 | I<$config> must be a reference to a | ||
| 56 | C<Net::Z3950::DBIServer::Config::PerDB> object - one of the objects in | ||
| 57 | the array returned as a C<Net::Z3950::DBIServer::Config> object from | ||
| 58 | that class's constructor. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub new { | ||
| 63 | my $class = shift(); | ||
| 64 | my($server, $config, $cond) = @_; | ||
| 65 | my $dbh = $server->{dbh}; | ||
| 66 | #warn "server='$server', dbh", defined $dbh ? "='$dbh'" : " undefined"; | ||
| 67 | my $table = $config->tablename(); | ||
| 68 | |||
| 69 | my $aux = $config->auxiliary(); | ||
| 70 | foreach my $auxspec (@$aux) { | ||
| 71 | my $join = $config->join(); | ||
| 72 | if (defined $join && $join eq "outer") { | ||
| 73 | $table .= (" LEFT OUTER JOIN " . $auxspec->tableName() . | ||
| 74 | " ON " . $auxspec->cond()); | ||
| 75 | } else { | ||
| 76 | $table .= ", " . $auxspec->tableName(); | ||
| 77 | $cond = "($cond) AND (" . $auxspec->cond() . ")"; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | # It would be nice to optimise _count() by selecting from a subset | ||
| 82 | # of tables and using only those parts of the condition necessary | ||
| 83 | # to join the main table to whichever ones are being searched. In | ||
| 84 | # the common case of searching only in the main table, of course, | ||
| 85 | # that would mean no linked tables (and no linking conditions). | ||
| 86 | # But to do this generally would mean parsing the initial $cond to | ||
| 87 | # see which tables are referenced and adding in all the necessary | ||
| 88 | # links: complex and error-prone just for an optimisation. So | ||
| 89 | # we'll just use the full condition/links for now. | ||
| 90 | |||
| 91 | #warn "counting '$table' WHERE $cond"; | ||
| 92 | my $count = _count($server, $table, $cond); | ||
| 93 | |||
| 94 | my @cols = $config->columns(); | ||
| 95 | #print "*** cols = {\n", join("", map { "\t'$_'\n" } @cols), "}\n"; | ||
| 96 | my $columns = join(', ', @cols); | ||
| 97 | my $sql = "SELECT $columns FROM $table WHERE $cond"; | ||
| 98 | warn "$sql\n"; | ||
| 99 | my $sth; | ||
| 100 | if (!$server->{noop}) { | ||
| 101 | $sth = $dbh->prepare($sql); | ||
| 102 | $sth->execute() or | ||
| 103 | die new Net::Z3950::DBIServer::Exception(108, $dbh->errstr(). ": $sql"); | ||
| 104 | } | ||
| 105 | |||
| 106 | return bless { | ||
| 107 | server => $server, | ||
| 108 | config => $config, | ||
| 109 | count => $count, | ||
| 110 | sth => $sth, | ||
| 111 | cache => [], | ||
| 112 | }, $class; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | # Here we attempt to discover the number of records satisfying the | ||
| 117 | # query in as efficient a way as possible. There's no standard way to | ||
| 118 | # do this (is there?) so we have to pox about with driver-specific | ||
| 119 | # code. | ||
| 120 | # | ||
| 121 | sub _count { | ||
| 122 | my($server, $tables, $cond) = @_; | ||
| 123 | my $dbh = $server->{dbh}; | ||
| 124 | |||
| 125 | return 9999 if $server->{noop}; | ||
| 126 | |||
| 127 | if ($server->dbi_driver() eq "CSV") { | ||
| 128 | return _count_by_hand(@_); | ||
| 129 | } | ||
| 130 | |||
| 131 | my $sql = "SELECT COUNT(*) FROM $tables WHERE $cond"; | ||
| 132 | warn $sql; | ||
| 133 | my $countref = $dbh->selectall_arrayref($sql); | ||
| 134 | if (!defined $countref) { | ||
| 135 | # Might be a relatively benign condition like searching for a | ||
| 136 | # string value in a numeric field, so be tolerant. 229 (Term | ||
| 137 | # type not supported) is appealing, but not really legit, | ||
| 138 | # since we don't truthfully know what's gone wrong. | ||
| 139 | warn "count: $sql"; | ||
| 140 | die new Net::Z3950::DBIServer::Exception(108, | ||
| 141 | $dbh->errstr(). ": $sql"); | ||
| 142 | } | ||
| 143 | |||
| 144 | return $countref->[0]->[0]; | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | # Some really brain-dead back-ends -- notably the Nearly Wonderful | ||
| 149 | # DBD::CSV -- don't support SELECT COUNT(); so we just do the actual | ||
| 150 | # query twice and damned well count the records. Obviously very | ||
| 151 | # inefficient. So it goes: it'll never happen on a Real Database. | ||
| 152 | # | ||
| 153 | sub _count_by_hand { | ||
| 154 | my($dbh, $tables, $cond) = @_; | ||
| 155 | |||
| 156 | my $sql = "SELECT * FROM $tables WHERE $cond"; | ||
| 157 | my $sth = $dbh->prepare($sql); | ||
| 158 | if (!defined $sth || !$sth->execute()) { | ||
| 159 | die new Net::Z3950::DBIServer::Exception(1, $dbh->errstr()); | ||
| 160 | } | ||
| 161 | |||
| 162 | my $count = 0; | ||
| 163 | while ($sth->fetchrow_array()) { | ||
| 164 | $count++; | ||
| 165 | } | ||
| 166 | return $count; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | =head2 config() | ||
| 171 | |||
| 172 | $config = $rs->config(); | ||
| 173 | |||
| 174 | Returns the database-specific configuration segment with which the | ||
| 175 | result set was created. | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub config { | ||
| 180 | my $this = shift(); | ||
| 181 | |||
| 182 | return $this->{config}; | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | =head2 count() | ||
| 187 | |||
| 188 | $config = $rs->count(); | ||
| 189 | |||
| 190 | Returns the number of records in the result set. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub count { | ||
| 195 | my $this = shift(); | ||
| 196 | |||
| 197 | return $this->{count}; | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | =head2 fetch() | ||
| 202 | |||
| 203 | $hashref = $rs->fetch($offset); | ||
| 204 | foreach $key (keys %$hashref) { | ||
| 205 | print($key, "->", $hashref->{$key}, "\n"); | ||
| 206 | } | ||
| 207 | |||
| 208 | Returns a reference to hash mapping fieldnames to values for record | ||
| 209 | number I<$offset> (counting starts at 0), if that information is | ||
| 210 | known. If it can't be obtained (most likely because the underlying | ||
| 211 | DBI statement handle has already read past the requested record, and | ||
| 212 | it's fallen out of the result set's cache), then an Exception is | ||
| 213 | thrown representing BIB-1 diagnostic 33 (``Resources exhausted - valid | ||
| 214 | subset of results available''). | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | sub fetch { | ||
| 219 | my $this = shift(); | ||
| 220 | my($offset) = @_; | ||
| 221 | |||
| 222 | if ($offset < 0 || $offset >= $this->{count}) { | ||
| 223 | die new Net::Z3950::DBIServer::Exception(13); | ||
| 224 | } | ||
| 225 | |||
| 226 | if ($this->{server}->{noop}) { | ||
| 227 | # Sample record knows about specific table structure -- ugh | ||
| 228 | return { | ||
| 229 | "author.name" => "T. H. White", | ||
| 230 | "name" => "The Once and Future King", | ||
| 231 | }; | ||
| 232 | } | ||
| 233 | |||
| 234 | my $cache = $this->{cache}; | ||
| 235 | my $hashref = $cache->[$offset]; | ||
| 236 | return $hashref | ||
| 237 | if defined $hashref; ### INVALID if the configuration has changed | ||
| 238 | |||
| 239 | if ($offset < @$cache) { | ||
| 240 | # Request for a record earlier in the set that the last one. | ||
| 241 | # We must have had it earlier, but discarded it because the | ||
| 242 | # cache was getting too big. The only way to get it back now | ||
| 243 | # would be by re-executing the query, which might yield | ||
| 244 | # different results this time around. Diagnostic 33 | ||
| 245 | # (Resources exhausted - valid subset of results available) is | ||
| 246 | # not perfect, but it's the best I can find. | ||
| 247 | die new Net::Z3950::DBIServer::Exception(33); | ||
| 248 | } | ||
| 249 | |||
| 250 | my $config = $this->config(); | ||
| 251 | my $tablename = $config->tablename(); | ||
| 252 | my $sth = $this->{sth}; | ||
| 253 | while (@$cache <= $offset) { | ||
| 254 | my @columns = $config->columns(); | ||
| 255 | my @values = $sth->fetchrow_array(); | ||
| 256 | #warn "got values: " . join('; ', map { DBI::data_string_desc($_) . (defined $_ ? " '$_'" : "") } @values); | ||
| 257 | my $hashref; | ||
| 258 | while (@columns) { | ||
| 259 | my $col = shift @columns; | ||
| 260 | $col =~ s/^$tablename\.//; # canonicalise fieldnames | ||
| 261 | $hashref->{$col} = shift @values; | ||
| 262 | } | ||
| 263 | |||
| 264 | push @$cache, $hashref; | ||
| 265 | |||
| 266 | my $cacheSize = $this->config()->dataSpec()->cacheSize(); | ||
| 267 | if ($cacheSize != 0 && @$cache > $cacheSize) { | ||
| 268 | my $dropIndex = @$cache-$cacheSize-1; | ||
| 269 | undef $cache->[$dropIndex]; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | return $cache->[$offset]; | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | =head1 AUTHOR | ||
| 278 | |||
| 279 | Mike Taylor E<lt>mike@miketaylor.org.ukE<gt> | ||
| 280 | |||
| 281 | First version Saturday 23rd February 2002. | ||
| 282 | |||
| 283 | =head1 SEE ALSO | ||
| 284 | |||
| 285 | C<Net::Z3950::DBIServer> | ||
| 286 | is the module that uses this. | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | |||
| 291 | 1; |