/[Frey]/trunk/lib/Frey/DBI/Sponge.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/Frey/DBI/Sponge.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 935 - (hide annotations)
Tue Jan 6 00:23:51 2009 UTC (15 years, 5 months ago) by dpavlin
File size: 1530 byte(s)
export Frey's sponge into database
1 dpavlin 935 package Frey::DBI::Sponge;
2     use Moose;
3    
4     extends 'Frey';
5    
6     #use Frey::Types;
7    
8     has sponge => (
9     is => 'rw',
10     isa => 'Sponge',
11     required => 1,
12     );
13    
14     has 'dsn' => (
15     is => 'ro',
16     isa => 'dsn',
17     default => 'dbi:Pg:dbname=frey',
18     required => 1,
19     );
20    
21     sub to_database_as_markup {
22     my ($self) = @_;
23    
24     my $table = $self->sponge->{table} || die "no table in sponge ", $self->dump( $self->sponge );
25    
26     warn "# dsn: ", $self->dsn;
27     my $dbh = DBI->connect( $self->dsn, '', '', { RaiseError => 1 } ) || die $DBI::errstr;
28     $dbh->do( qq{ set client_encoding='utf-8' } );
29    
30     my @columns = @{ $self->sponge->{NAME} };
31    
32    
33     my $create_sql = qq{
34     create table $table (
35     } . join( ",\n", map { s/%//; "$_ text" } @columns ) . qq{
36     );
37     };
38     eval { $dbh->do( $create_sql ); };
39     if ( $@ ) {
40     die "$create_sql\n$@" if $@ !~ m{$table.*already exists};
41     warn "INFO: using existing table $table because of $@";
42     } else {
43     warn "INFO: created $table using\n$create_sql";
44     }
45    
46     my $sth = $dbh->prepare( qq{
47     insert into $table values (
48     } . join( ",", map { '?' } @columns ) . qq{
49     );
50     });
51    
52     my $sponge_rows = $#{ $self->sponge->{rows} };
53     warn "# convert ", $sponge_rows + 1 ," rows from spunge into $table in ", $self->dsn;
54    
55     foreach my $row_nr ( 0 .. $sponge_rows ) {
56     $sth->execute( @{ $self->sponge->{rows}->[$row_nr] } );
57     }
58    
59     $sponge_rows++;
60    
61     my $html =
62     ( $sponge_rows
63     ? qq|Inserted <b>$sponge_rows</b> rows|
64     : qq|No rows inserted into $table|
65     )
66     . qq| to table <tt>$table</tt> at <tt>| . $self->dsn . qq|</tt>|
67     ;
68    
69     return $html;
70     }
71    
72     1;

  ViewVC Help
Powered by ViewVC 1.1.26