store columns availabe in schema and ignore others
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 5 Jul 2011 19:42:18 +0000 (21:42 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 5 Jul 2011 19:42:18 +0000 (21:42 +0200)
lib/APKPM/Store.pm

index 054e53c..eafd73d 100644 (file)
@@ -5,6 +5,7 @@ use Moose;
 use Time::HiRes;
 use Data::Dump qw(dump);
 use DBD::Pg;
+use Redis;
 
 with 'APKPM::Gearman';
 
@@ -26,13 +27,28 @@ sub dbh {
 sub pg_insert {
        my ( $self, $table, $h ) = @_;
 
+       my $redis = Redis->new;
 
-       my @c = keys %$h;
+       my @c;
+
+       if ( my $cols = $redis->get("pg.$table") ) {
+               @c = split(/\s+/,$cols);
+       } else {
+               my $sth = $self->dbh->prepare( "select * from $table limit 1" );
+               $sth->execute;
+               @c = @{ $sth->{NAME_lc} };
+               $redis->set( "pg.$table" => join(' ',@c) );
+               $redis->expire( "pg.$table" => 5 * 60 ); # refresh every 5 min
+       }
 
        my $sql = "INSERT INTO $table (" . join(',',@c) . ') values (' . join(',', map { '?' } 0 .. $#c) . ')';
        warn $sql;
        my $sth = $self->dbh->prepare($sql);
-       $sth->execute( map { $h->{$_} } @c );
+
+       my $h_lc;
+       $h_lc->{ lc $_ } = $h->{$_} foreach keys %$h;
+
+       $sth->execute( map { $h_lc->{$_} } @c );
 }
 
 sub insert : Job : Decode(d_json) : MinProcesses(0) {