/[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 1207 by dpavlin, Fri May 29 21:37:58 2009 UTC revision 1211 by dpavlin, Sat May 30 15:34:55 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    
46          eval {          if ( -e $self->schema ) {
47          $self->{_dbh}->do( scalar read_file( $self->schema ) ) if -e $self->schema;                  foreach my $sql ( split(/;/, scalar read_file( $self->schema )) ) {
48          };                          $log->debug( $sql );
49                            eval { $self->{_dbh}->do( $sql ); };
50                    }
51            }
52    
53          return 1;          return 1;
54  }  }
# Line 69  sub add { Line 73  sub add {
73    
74          $id = $self->input . '-' . $id if $self->input;          $id = $self->input . '-' . $id if $self->input;
75    
76          my @rows = @{ $ds->{_rows} };          foreach my $table ( keys %{ $ds->{_rows} } ) {
         foreach my $row ( @rows ) {  
77    
78                  my @cols = sort keys %$row;                  my @rows = @{ $ds->{_rows}->{$table} };
79                    foreach my $row ( @rows ) {
80    
81                  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 );  
82    
83                  warn "# row ",dump( $row );                          my $sth_id = $table . ':' . join(',',@cols);
                 $sth->execute( map { $row->{$_} } @cols );  
         }  
84    
85          push @{ $self->{_rows} }, $_ foreach @rows;                          my $sth
86                                    = $self->{_sth}->{$sth_id}
87                                    ;
88                            
89                            if ( ! $sth ) {
90    
91                                    my $sql = join( ''
92                                            , 'insert into '
93                                            , $table
94                                            . ' (' . join(',', @cols), ')'
95                                            , ' values ('
96                                            , join(',', map { '?' } 0 .. $#cols )
97                                            , ')'
98                                    );
99    
100                                    $log->debug( "SQL $sth_id: $sql" );
101    
102                                    $sth
103                                            = $self->{_sth}->{$sth_id}
104                                            = $self->{_dbh}->prepare( $sql )
105                                            ;
106                            };
107    
108                            $log->debug( "row $table ", sub { dump( $row ) } );
109                            $sth->execute( map { $row->{$_} } @cols );
110            
111                            push @{ $self->{_rows}->{$table} }, $_ foreach @rows;
112    
113                    }
114            }
115    
116          return 1;          return 1;
117  }  }
# Line 105  sub finish { Line 127  sub finish {
127    
128          my $log = $self->_get_logger();          my $log = $self->_get_logger();
129    
130          $log->info('finish and dump data into database');          $log->info('finish');
131    
132          warn dump( $self->{_rows} );  #       warn dump( $self->{_rows} );
133    
134          return 1;          return 1;
135  }  }

Legend:
Removed from v.1207  
changed lines
  Added in v.1211

  ViewVC Help
Powered by ViewVC 1.1.26