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 dbh {
	DBI->connect_cached('DBI:Pg:dbname=apkpm','','', {
		RaiseError => 1,
		AutoCommit => 1,
	});
}

sub pg_insert {
	my ( $self, $table, $h ) = @_;


	my @c = keys %$h;

	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 );
}

sub insert : Job : Decode(d_json) : MinProcesses(0) {
	my ( $self, $job, $workload ) = @_;
	my $table = delete $workload->{_table} || die "no _table";
	$self->pg_insert($table => $workload);
}

sub sql : Job : Encode(e_json) : MinProcesses(0) {
	my ( $self, $job, $workload ) = @_;

	my $sth = $self->dbh->prepare($workload);
	my $rows = eval { $sth->execute };
	return { error => $@ } if $@;

	warn "# $rows rows get_username_table $workload\n";

	return {
		columns => $sth->{NAME},
		rows => $sth->fetchall_arrayref,
	};
}

1;
