--- trunk/lib/WebPAC/Output/DBI.pm 2009/05/30 14:21:58 1209 +++ trunk/lib/WebPAC/Output/DBI.pm 2009/05/30 15:26:25 1210 @@ -38,7 +38,8 @@ $log->info($self->dsn); - $self->{_rows} = []; + $self->{_rows} = {}; + $self->{_sth} = {}; $self->{_dbh} = DBI->connect( $self->dsn, $self->user, $self->passwd, { RaiseError => 1 } ); @@ -69,27 +70,45 @@ $id = $self->input . '-' . $id if $self->input; - my @rows = @{ $ds->{_rows} }; - foreach my $row ( @rows ) { + foreach my $table ( keys %{ $ds->{_rows} } ) { - my @cols = sort keys %$row; + my @rows = @{ $ds->{_rows}->{$table} }; + foreach my $row ( @rows ) { - my $sql = join( '' - , 'insert into ' - , ( $self->table || $self->input || 'webpac2' ) - . ' (' . join(',', @cols), ')' - , ' values (' - , join(',', map { '?' } 0 .. $#cols ) - , ')' - ); - warn "# SQL: $sql\n"; - my $sth = $self->{_dbh}->prepare( $sql ); + my @cols = sort keys %$row; - warn "# row ",dump( $row ); - $sth->execute( map { $row->{$_} } @cols ); - } + my $sth_id = $table . ':' . join(',',@cols); + + my $sth + = $self->{_sth}->{$sth_id} + ; + + if ( ! $sth ) { + + my $sql = join( '' + , 'insert into ' + , $table + . ' (' . join(',', @cols), ')' + , ' values (' + , join(',', map { '?' } 0 .. $#cols ) + , ')' + ); + + $log->debug( "SQL $sth_id: $sql" ); + + $sth + = $self->{_sth}->{$sth_id} + = $self->{_dbh}->prepare( $sql ) + ; + }; + + $log->debug( "row $table ", sub { dump( $row ) } ); + $sth->execute( map { $row->{$_} } @cols ); + + push @{ $self->{_rows}->{$table} }, $_ foreach @rows; - push @{ $self->{_rows} }, $_ foreach @rows; + } + } return 1; }