| 1 | =head1 NAME | ||
| 2 | |||
| 3 | SQLite::VirtualTable::Pivot::Cursor - a cursor for a pivot table query | ||
| 4 | |||
| 5 | =head1 DESCRIPTION | ||
| 6 | |||
| 7 | Objects in this class represent cursors for queries on pivot tables. | ||
| 8 | |||
| 9 | They maintatin a statement handle corresponding to an unpivotted | ||
| 10 | query on the base table. The pivotting operation is done one | ||
| 11 | row at a time, as data is returned; i.e. we order by the id, and | ||
| 12 | group on the fly. | ||
| 13 | |||
| 14 | It must be possible for multiple cursors to exist at once on the | ||
| 15 | same pivot table, in case it occurs more than once in a query. | ||
| 16 | |||
| 17 | =head1 SEE ALSO | ||
| 18 | |||
| 19 | L<SQLite::VirtualTable::Pivot> | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | package SQLite::VirtualTable::Pivot::Cursor; | ||
| 24 | |||
| 25 | use base 'Class::Accessor::Contextual'; | ||
| 26 | use Data::Dumper; | ||
| 27 | |||
| 28 | # State variables (probably could be simplified) | ||
| 29 | __PACKAGE__->mk_accessors(qw| first last done row_id queued |); | ||
| 30 | __PACKAGE__->mk_accessors(qw| sth current_row |); | ||
| 31 | |||
| 32 | # list of temp tables | ||
| 33 | __PACKAGE__->mk_accessors(qw| temp_tables |); | ||
| 34 | |||
| 35 | # the virtual table to whom we belong | ||
| 36 | __PACKAGE__->mk_accessors(qw| virtual_table |); | ||
| 37 | |||
| 38 | sub debug($) { return unless $ENV{DEBUG}; print STDERR "# $_[0]\n"; } | ||
| 39 | |||
| 40 | sub reset { | ||
| 41 | my $self = shift; | ||
| 42 | $self->set( first => 1 ); | ||
| 43 | $self->set( done => 0 ); | ||
| 44 | $self->set( queued => {} ); | ||
| 45 | $self->set( "last" => 0 ); | ||
| 46 | $self->set( current_row => [] ); | ||
| 47 | $self->set( last_row => [] ); | ||
| 48 | $self->set( temp_tables => [] ); | ||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub get_next_row { | ||
| 53 | my $self = shift; | ||
| 54 | $self->{queued} = {}; | ||
| 55 | $self->{last_row} ||= []; | ||
| 56 | $self->{current_row} ||= []; | ||
| 57 | return if $self->done; | ||
| 58 | $self->{row_id}++; | ||
| 59 | $self->{done} = $self->{last}; | ||
| 60 | my $row = $self->{current_row}; | ||
| 61 | my $last_row = $self->{last_row}; | ||
| 62 | my $queued = $self->{queued}; | ||
| 63 | $self->{last} = 0; | ||
| 64 | while ($self->first || $self->virtual_table->_row_values_are_equal($row->[0],$last_row->[0])) { | ||
| 65 | $self->set( first => 0 ); | ||
| 66 | if (@$row) { | ||
| 67 | $queued->{ $row->[1] } = $row->[2]; | ||
| 68 | $queued->{$self->virtual_table->pivot_row} = $row->[0]; | ||
| 69 | } | ||
| 70 | @$last_row = @$row; | ||
| 71 | $self->{last} = !( @$row = $self->sth->fetchrow_array ); | ||
| 72 | last if $self->{last}; | ||
| 73 | } | ||
| 74 | my $dumped = Dumper($self->queued); | ||
| 75 | $dumped =~ s/\n|\s//g; | ||
| 76 | debug "called next, queued is now $dumped "; | ||
| 77 | @$last_row = @$row; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub column_value { | ||
| 81 | my $self = shift; | ||
| 82 | my $column_name = shift; | ||
| 83 | debug "column $column_name: $self->{queued}->{$column_name}"; | ||
| 84 | return $self->queued->{$column_name}; | ||
| 85 | } | ||
| 86 | |||
| 87 | 1; | ||
| 88 | |||
| 89 |