package APKPM::Store;

use base qw(Gearman::Driver::Worker);
use Moose;
use Time::HiRes;
use Data::Dump qw(dump);
use DBD::Pg;

with 'APKPM::Gearman';

sub prefix { 'Store_' }

sub process_name {
	my ( $self, $orig, $job_name ) = @_;
	warn "# process_name $orig $job_name\n";
	return "$orig ($job_name)";
}

sub ADSL : Job : MinProcesses(1) : MaxProcesses(5) : Decode(d_json) {
	my ( $self, $job, $workload ) = @_;

	my $dbh = DBI->connect_cached('DBI:Pg:dbname=apkpm','','', {
		RaiseError => 1,
		AutoCommit => 1,
	});

	my $h = $workload->{ADSL} || die "no ADSL in ",dump $workload;
	foreach my $c ( qw(ip username) ) {
		$h->{$c} = $workload->{$c} || die "no $c in ",dump $workload;
	}

	warn "# ADSL ", dump $h;

	my @c = keys %$h;

	my $sql = 'INSERT INTO adsl (' . join(',',@c) . ') values (' . join(',', map { '?' } 0 .. $#c) . ')';
	warn $sql;
	my $sth = $dbh->prepare($sql);
	$sth->execute( map { $h->{$_} } @c );
}

1;
