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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1210 - (show annotations)
Sat May 30 15:26:25 2009 UTC (14 years, 10 months ago) by dpavlin
File size: 2212 byte(s)
 r1906@llin:  dpavlin | 2009-05-30 17:26:17 +0200
 added table name to row normalize command so we can create
 data in different tables directly from normalization file

1 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 $self->{_rows} = {};
42 $self->{_sth} = {};
43
44 $self->{_dbh} = DBI->connect( $self->dsn, $self->user, $self->passwd, { RaiseError => 1 } );
45
46 eval {
47 $self->{_dbh}->do( scalar read_file( $self->schema ) ) if -e $self->schema;
48 };
49
50 return 1;
51 }
52
53
54 =head2 add
55
56 Adds one entry to database.
57
58 $out->add( 42, $ds );
59
60 =cut
61
62 sub add {
63 my $self = shift;
64
65 my ( $id, $ds ) = @_;
66
67 return unless defined $ds->{_rows};
68
69 my $log = $self->_get_logger;
70
71 $id = $self->input . '-' . $id if $self->input;
72
73 foreach my $table ( keys %{ $ds->{_rows} } ) {
74
75 my @rows = @{ $ds->{_rows}->{$table} };
76 foreach my $row ( @rows ) {
77
78 my @cols = sort keys %$row;
79
80 my $sth_id = $table . ':' . join(',',@cols);
81
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 }
111 }
112
113 return 1;
114 }
115
116 =head2 finish
117
118 $out->finish;
119
120 =cut
121
122 sub finish {
123 my $self = shift;
124
125 my $log = $self->_get_logger();
126
127 $log->info('finish and dump data into database');
128
129 warn dump( $self->{_rows} );
130
131 return 1;
132 }
133
134 =head1 AUTHOR
135
136 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
137
138 =head1 COPYRIGHT & LICENSE
139
140 Copyright 2009 Dobrica Pavlinusic, All Rights Reserved.
141
142 This program is free software; you can redistribute it and/or modify it
143 under the same terms as Perl itself.
144
145 =cut
146
147 1; # End of WebPAC::Output::CouchDB

  ViewVC Help
Powered by ViewVC 1.1.26