| 1 | # $Id: ResultSet.pm,v 1.4 2008-04-30 16:37:05 mike Exp $ | ||
| 2 | |||
| 3 | package Keystone::Resolver::ResultSet; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Encode; | ||
| 7 | |||
| 8 | |||
| 9 | sub new { | ||
| 10 | my $class = shift(); | ||
| 11 | my($site, $findclass, %query) = @_; | ||
| 12 | $findclass = "Keystone::Resolver::DB::$findclass" if $findclass !~ /::/; | ||
| 13 | |||
| 14 | my $sort = delete $query{_sort}; | ||
| 15 | $sort = join(", ", $findclass->sort_fields()) if !$sort; | ||
| 16 | |||
| 17 | # Discard empty elements and those whose names begin with "_" | ||
| 18 | %query = map { ($_, $query{$_} ) } | ||
| 19 | grep { !/^_/ && defined $query{$_} && $query{$_} ne "" } keys %query; | ||
| 20 | |||
| 21 | # Replace all "'" with "''"; whole terms may still need quoting | ||
| 22 | foreach my $key (keys %query) { | ||
| 23 | $query{$key} =~ s/['']/''/g; | ||
| 24 | } | ||
| 25 | |||
| 26 | # Rather than building a condition by hand here, we should call | ||
| 27 | # directly into a Database method that accepts %query whole; | ||
| 28 | # unfortunately, that method doesn't exist yet. | ||
| 29 | # | ||
| 30 | # At the moment, we don't narrow on $site->id() since resolver | ||
| 31 | # objects like Services and Providers do not have any concept of | ||
| 32 | # ownership -- but that will change. | ||
| 33 | my $db = $site->db(); | ||
| 34 | my $cond = (join(" and ", # "site_id = " . $site->id(), | ||
| 35 | map { $db->quote($_) . " = '" . $query{$_} . "'" } sort keys %query)); | ||
| 36 | $cond = undef if $cond eq ""; | ||
| 37 | $site->log(Keystone::Resolver::LogLevel::SQL, | ||
| 38 | (defined $cond ? "SQL cond: $cond" : "no condition")); | ||
| 39 | |||
| 40 | my($sth, $count, $errmsg) = | ||
| 41 | $db->_findcond($findclass, $cond, $sort); | ||
| 42 | return (undef, $errmsg) if !defined $sth; | ||
| 43 | return bless { | ||
| 44 | site => $site, | ||
| 45 | class => $findclass, | ||
| 46 | db => $db, | ||
| 47 | query => \%query, | ||
| 48 | sth => $sth, | ||
| 49 | count => $count, | ||
| 50 | hwm => 0, # 1-based index of last record read | ||
| 51 | objects => [], # cache of objects made from records read | ||
| 52 | }, $class; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub class { shift()->{class} } | ||
| 57 | sub query { shift()->{query} } | ||
| 58 | sub count { shift()->{count} } | ||
| 59 | |||
| 60 | |||
| 61 | # Fetches the $i'th object in the RS, where $i is a 1-based index | ||
| 62 | sub fetch { | ||
| 63 | my $this = shift(); | ||
| 64 | my($i) = @_; | ||
| 65 | |||
| 66 | die "fetch($this) called with non-positive index $i" | ||
| 67 | if $i < 1; | ||
| 68 | die "fetch($this) called with out-of-range index $i (max=" . | ||
| 69 | $this->count() . ")" | ||
| 70 | if $i > $this->count(); | ||
| 71 | |||
| 72 | my $class = $this->class(); | ||
| 73 | while ($this->{hwm} < $i) { | ||
| 74 | my $ref = $this->{sth}->fetchrow_arrayref(); | ||
| 75 | if (!defined $ref) { | ||
| 76 | my $errmsg = $this->{sth}->errstr(); | ||
| 77 | return (undef, $errmsg) | ||
| 78 | if defined $errmsg; | ||
| 79 | die "unexpected end of result-set after " . $this->{hwm} . | ||
| 80 | " of " . $this->count() . " records"; | ||
| 81 | } | ||
| 82 | my $object = $class->new($this->{db}, map { decode_utf8($_) } @$ref); | ||
| 83 | $this->{objects}->[$this->{hwm}++] = $object; | ||
| 84 | } | ||
| 85 | |||
| 86 | return $this->{objects}->[$i-1]; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | 1; |