| 1 | |||
| 2 | package DBIx::Romani::ResultSet; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | |||
| 6 | use constant FETCHMODE_ASSOC => 1; | ||
| 7 | use constant FETCHMODE_NUM => 2; | ||
| 8 | |||
| 9 | sub new | ||
| 10 | { | ||
| 11 | my $class = shift; | ||
| 12 | my $args = shift; | ||
| 13 | |||
| 14 | my $conn; | ||
| 15 | my $sth; | ||
| 16 | my $mode; | ||
| 17 | |||
| 18 | if ( ref($conn) eq 'HASH' ) | ||
| 19 | { | ||
| 20 | $conn = $args->{conn}; | ||
| 21 | $sth = $args->{sth}; | ||
| 22 | $mode = $args->{fetchmode}; | ||
| 23 | } | ||
| 24 | else | ||
| 25 | { | ||
| 26 | $conn = $args; | ||
| 27 | $sth = shift; | ||
| 28 | $mode = shift; | ||
| 29 | } | ||
| 30 | |||
| 31 | if ( not defined $mode ) | ||
| 32 | { | ||
| 33 | $mode = FETCHMODE_ASSOC; | ||
| 34 | } | ||
| 35 | |||
| 36 | my $self = { | ||
| 37 | conn => $conn, | ||
| 38 | sth => $sth, | ||
| 39 | row => undef, | ||
| 40 | mode => $mode, | ||
| 41 | }; | ||
| 42 | |||
| 43 | bless $self, $class; | ||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub get_conn { return shift->{conn}; } | ||
| 48 | sub get_row { return shift->{row}; } | ||
| 49 | sub get_fetchmode { return shift->{mode}; } | ||
| 50 | |||
| 51 | sub next | ||
| 52 | { | ||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | if ( $self->get_fetchmode() == FETCHMODE_NUM ) | ||
| 56 | { | ||
| 57 | $self->{row} = $self->{sth}->fetchrow_arrayref(); | ||
| 58 | } | ||
| 59 | else | ||
| 60 | { | ||
| 61 | $self->{row} = $self->{sth}->fetchrow_hashref(); | ||
| 62 | } | ||
| 63 | |||
| 64 | # return true if we still have data | ||
| 65 | return defined( $self->{row} ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub get | ||
| 69 | { | ||
| 70 | my ($self, $column) = @_; | ||
| 71 | |||
| 72 | if ( $self->get_fetchmode() == FETCHMODE_NUM ) | ||
| 73 | { | ||
| 74 | return $self->{row}->[$column]; | ||
| 75 | } | ||
| 76 | else | ||
| 77 | { | ||
| 78 | return $self->{row}->{$column}; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | # TODO: implement these: | ||
| 84 | # | ||
| 85 | # TODO: Or, maybe, I could provide these with an AUTOLOAD and then if | ||
| 86 | # the child class overrides it, then cool. Otherwise just get(). | ||
| 87 | # | ||
| 88 | # * sub get_blob($column) | ||
| 89 | # * sub get_clob($column) | ||
| 90 | # * sub get_date($column) | ||
| 91 | # * sub get_time($column) | ||
| 92 | # * sub get_timestamp($column) | ||
| 93 | |||
| 94 | 1; | ||
| 95 |