/[webpac2]/trunk/lib/WebPAC/Output/DBI.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Output/DBI.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1209 by dpavlin, Fri May 29 21:37:58 2009 UTC revision 1210 by dpavlin, Sat May 30 15:26:25 2009 UTC
# Line 38  sub init { Line 38  sub init {
38    
39          $log->info($self->dsn);          $log->info($self->dsn);
40    
41          $self->{_rows} = [];          $self->{_rows} = {};
42            $self->{_sth} = {};
43    
44          $self->{_dbh} = DBI->connect( $self->dsn, $self->user, $self->passwd, { RaiseError => 1 } );          $self->{_dbh} = DBI->connect( $self->dsn, $self->user, $self->passwd, { RaiseError => 1 } );
45    
# Line 69  sub add { Line 70  sub add {
70    
71          $id = $self->input . '-' . $id if $self->input;          $id = $self->input . '-' . $id if $self->input;
72    
73          my @rows = @{ $ds->{_rows} };          foreach my $table ( keys %{ $ds->{_rows} } ) {
         foreach my $row ( @rows ) {  
74    
75                  my @cols = sort keys %$row;                  my @rows = @{ $ds->{_rows}->{$table} };
76                    foreach my $row ( @rows ) {
77    
78                  my $sql = join( ''                          my @cols = sort keys %$row;
                         , 'insert into '  
                         , ( $self->table || $self->input || 'webpac2' )  
                         . ' (' . join(',', @cols), ')'  
                         , ' values ('  
                         , join(',', map { '?' } 0 .. $#cols )  
                         , ')'  
                 );  
                 warn "# SQL: $sql\n";  
                 my $sth = $self->{_dbh}->prepare( $sql );  
79    
80                  warn "# row ",dump( $row );                          my $sth_id = $table . ':' . join(',',@cols);
81                  $sth->execute( map { $row->{$_} } @cols );  
82          }                          my $sth
83                                    = $self->{_sth}->{$sth_id}
84                                    ;
85                            
86                            if ( ! $sth ) {
87    
88                                    my $sql = join( ''
89                                            , 'insert into '
90                                            , $table
91                                            . ' (' . join(',', @cols), ')'
92                                            , ' values ('
93                                            , join(',', map { '?' } 0 .. $#cols )
94                                            , ')'
95                                    );
96    
97                                    $log->debug( "SQL $sth_id: $sql" );
98    
99                                    $sth
100                                            = $self->{_sth}->{$sth_id}
101                                            = $self->{_dbh}->prepare( $sql )
102                                            ;
103                            };
104    
105                            $log->debug( "row $table ", sub { dump( $row ) } );
106                            $sth->execute( map { $row->{$_} } @cols );
107            
108                            push @{ $self->{_rows}->{$table} }, $_ foreach @rows;
109    
110          push @{ $self->{_rows} }, $_ foreach @rows;                  }
111            }
112    
113          return 1;          return 1;
114  }  }

Legend:
Removed from v.1209  
changed lines
  Added in v.1210

  ViewVC Help
Powered by ViewVC 1.1.26