| 1 | #!perl | ||
| 2 | |||
| 3 | # ########################################################################## # | ||
| 4 | # Title: Inserts rows - processor | ||
| 5 | # Creation date: 2007-03-05 | ||
| 6 | # Author: Michael Zedeler | ||
| 7 | # Description: Inserts row found in stream | ||
| 8 | # Data Stream class | ||
| 9 | # File: $Source: /data/cvs/lib/DSlib/lib/DS/Transformer/Insert.pm,v $ | ||
| 10 | # Repository: kronhjorten | ||
| 11 | # State: $State: Exp $ | ||
| 12 | # Documentation: inline | ||
| 13 | # Recepient: - | ||
| 14 | # ########################################################################## # | ||
| 15 | |||
| 16 | package DS::Transformer::Insert; | ||
| 17 | |||
| 18 | use base qw { DS::Transformer }; | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use Carp::Assert; | ||
| 22 | |||
| 23 | our ($VERSION) = $DS::VERSION; | ||
| 24 | our ($REVISION) = '$Revision: 1.1 $' =~ /(\d+\.\d+)/; | ||
| 25 | |||
| 26 | |||
| 27 | sub new { | ||
| 28 | my( $class, $dbh, $typespec, $source ) = @_; | ||
| 29 | |||
| 30 | my $self = $class->SUPER::new( $typespec, $source ); | ||
| 31 | |||
| 32 | assert($dbh->isa('DBI::db')); | ||
| 33 | |||
| 34 | $self->{dbh} = $dbh; | ||
| 35 | |||
| 36 | my @placeholders = ''; | ||
| 37 | foreach my $field (@{$self->{typespec}->{fields}}) { | ||
| 38 | push @placeholders, '?'; | ||
| 39 | } | ||
| 40 | |||
| 41 | my $insert = join(' ', | ||
| 42 | 'INSERT INTO', | ||
| 43 | $self->{typespec}->{name}, | ||
| 44 | ' (', | ||
| 45 | join(', ', @{$self->{typespec}->{fields}}), | ||
| 46 | ') VALUES (', | ||
| 47 | join(', ', @placeholders), | ||
| 48 | ')' | ||
| 49 | ); | ||
| 50 | |||
| 51 | my $sth = $self->{dbh}->prepare_cached( $insert, undef, 3 ); | ||
| 52 | assert(defined($sth)); | ||
| 53 | |||
| 54 | $self->{insert_sth} = $sth; | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | sub process { | ||
| 59 | my( $self, $row ) = @_; | ||
| 60 | my $p_num = 1; | ||
| 61 | |||
| 62 | foreach my $field (@{$self->{typespec}->{fields}}) { | ||
| 63 | $self->{insert_sth}->bind_param( $p_num++, $row->{$field} ); | ||
| 64 | } | ||
| 65 | |||
| 66 | return $row; | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; |