/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1212 - (hide annotations)
Sat May 30 20:30:26 2009 UTC (14 years, 11 months ago) by dpavlin
File size: 2236 byte(s)
 r1910@llin:  dpavlin | 2009-05-30 22:30:07 +0200
 wrap everything into trasaction for nice speedup (10x)
 and don't store all data in memory

1 dpavlin 1207 package WebPAC::Output::DBI;
2    
3     use warnings;
4     use strict;
5    
6     use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
7     __PACKAGE__->mk_accessors(qw(
8     input
9    
10     dsn
11     user
12     passwd
13    
14     schema
15    
16     table
17     ));
18    
19     use Data::Dump qw/dump/;
20     use DBI;
21     use File::Slurp;
22    
23     =head1 NAME
24    
25     WebPAC::Output::DBI - feed data into RDBMS via DBI
26    
27     =head1 FUNCTIONS
28    
29     =head2 init
30    
31     $out->init;
32    
33     =cut
34    
35     sub init {
36     my $self = shift;
37     my $log = $self->_get_logger;
38    
39     $log->info($self->dsn);
40    
41 dpavlin 1210 $self->{_sth} = {};
42 dpavlin 1207
43     $self->{_dbh} = DBI->connect( $self->dsn, $self->user, $self->passwd, { RaiseError => 1 } );
44    
45 dpavlin 1212 $self->{_dbh}->begin_work;
46    
47 dpavlin 1211 if ( -e $self->schema ) {
48     foreach my $sql ( split(/;/, scalar read_file( $self->schema )) ) {
49     $log->debug( $sql );
50     eval { $self->{_dbh}->do( $sql ); };
51     }
52     }
53 dpavlin 1207
54     return 1;
55     }
56    
57    
58     =head2 add
59    
60     Adds one entry to database.
61    
62     $out->add( 42, $ds );
63    
64     =cut
65    
66     sub add {
67     my $self = shift;
68    
69     my ( $id, $ds ) = @_;
70    
71     return unless defined $ds->{_rows};
72    
73     my $log = $self->_get_logger;
74    
75     $id = $self->input . '-' . $id if $self->input;
76    
77 dpavlin 1210 foreach my $table ( keys %{ $ds->{_rows} } ) {
78 dpavlin 1207
79 dpavlin 1210 my @rows = @{ $ds->{_rows}->{$table} };
80     foreach my $row ( @rows ) {
81 dpavlin 1207
82 dpavlin 1210 my @cols = sort keys %$row;
83 dpavlin 1207
84 dpavlin 1210 my $sth_id = $table . ':' . join(',',@cols);
85    
86     my $sth
87     = $self->{_sth}->{$sth_id}
88     ;
89    
90     if ( ! $sth ) {
91    
92     my $sql = join( ''
93     , 'insert into '
94     , $table
95     . ' (' . join(',', @cols), ')'
96     , ' values ('
97     , join(',', map { '?' } 0 .. $#cols )
98     , ')'
99     );
100    
101     $log->debug( "SQL $sth_id: $sql" );
102    
103     $sth
104     = $self->{_sth}->{$sth_id}
105     = $self->{_dbh}->prepare( $sql )
106     ;
107     };
108    
109     $log->debug( "row $table ", sub { dump( $row ) } );
110     $sth->execute( map { $row->{$_} } @cols );
111    
112     }
113 dpavlin 1207 }
114    
115     return 1;
116     }
117    
118     =head2 finish
119    
120     $out->finish;
121    
122     =cut
123    
124     sub finish {
125     my $self = shift;
126    
127     my $log = $self->_get_logger();
128    
129 dpavlin 1211 $log->info('finish');
130 dpavlin 1207
131 dpavlin 1212 $self->{_dbh}->commit;
132    
133     $log->info('commit done');
134 dpavlin 1207
135     return 1;
136     }
137    
138     =head1 AUTHOR
139    
140     Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
141    
142     =head1 COPYRIGHT & LICENSE
143    
144     Copyright 2009 Dobrica Pavlinusic, All Rights Reserved.
145    
146     This program is free software; you can redistribute it and/or modify it
147     under the same terms as Perl itself.
148    
149     =cut
150    
151     1; # End of WebPAC::Output::CouchDB

  ViewVC Help
Powered by ViewVC 1.1.26