| 1 | #!perl | ||
| 2 | |||
| 3 | # ########################################################################## # | ||
| 4 | # Title: Statement handle to datastream importer | ||
| 5 | # Creation date: 2007-03-05 | ||
| 6 | # Author: Michael Zedeler | ||
| 7 | # Description: Produces a datastream from a DBI statement handle (DBI::st) | ||
| 8 | # Data Stream class | ||
| 9 | # Data importer | ||
| 10 | # File: $Source: /data/cvs/lib/DSlib/lib/DS/Importer/Sth.pm,v $ | ||
| 11 | # Repository: kronhjorten | ||
| 12 | # State: $State: Exp $ | ||
| 13 | # Documentation: inline | ||
| 14 | # Recepient: - | ||
| 15 | # ########################################################################## # | ||
| 16 | |||
| 17 | package DS::Importer::Sth; | ||
| 18 | |||
| 19 | use base qw{ DS::Importer }; | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | use Carp::Assert; | ||
| 23 | |||
| 24 | use DS::TypeSpec; | ||
| 25 | use DS::TypeSpec::Field; | ||
| 26 | |||
| 27 | our ($VERSION) = $DS::VERSION; | ||
| 28 | our ($REVISION) = '$Revision: 1.1 $' =~ /(\d+\.\d+)/; | ||
| 29 | |||
| 30 | |||
| 31 | sub new { | ||
| 32 | my( $class, $sth, $target ) = @_; | ||
| 33 | |||
| 34 | assert($sth->isa('DBI::st')); | ||
| 35 | |||
| 36 | my $type_fields = []; | ||
| 37 | # TODO (V3) Replace NAME_lc with NAME to let developers choose case themselves | ||
| 38 | foreach my $field_name ( @{$sth->{NAME_lc}} ) { | ||
| 39 | push @$type_fields, new DS::TypeSpec::Field( $field_name ); | ||
| 40 | } | ||
| 41 | |||
| 42 | my $typespec = new DS::TypeSpec( $type_fields ); | ||
| 43 | assert( $typespec ); | ||
| 44 | |||
| 45 | my $self = $class->SUPER::new( $typespec, $target ); | ||
| 46 | |||
| 47 | # Create keys in hash from $sth->{NAME_lc} | ||
| 48 | @{ $self->{row} }{ @{$sth->{NAME_lc}} } = (); | ||
| 49 | # Bind key row to statement handle | ||
| 50 | $sth->bind_columns( \( @{ $self->{row} }{ @{$sth->{NAME_lc}} } ) ); | ||
| 51 | |||
| 52 | $self->{sth} = $sth; | ||
| 53 | |||
| 54 | return $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _fetch { | ||
| 58 | my($self) = @_; | ||
| 59 | |||
| 60 | return $self->{sth}->fetch ? $self->{row} : undef; | ||
| 61 | |||
| 62 | } | ||
| 63 | |||
| 64 | 1; |