package myHive::Model::DB;
use Moose;
use namespace::autoclean;
use SQL::Abstract;
use strict;
use warnings;
use DBI;
use DBD::Pg;
use Data::Dumper;

extends 'Catalyst::Model';

=head1 NAME

myHive::Model::DB - Catalyst Model

=head1 DESCRIPTION

Catalyst Model.

=head1 AUTHOR

root

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

has 'sql' => ( is => 'ro', isa => 'SQL::Abstract', default => sub { SQL::Abstract->new; } );
has 'dbh' => ( is => 'rw' );


sub BUILD {
	my ( $self ) =@_;
	#$self->sql( SQL::Abstract->new );
	$self->dbh(
		DBI->connect(
                        qq/dbi:Pg:dbname=myhive;host=localhost;/,
                        q/postgres/,
                        q/portobello414/,
                        {AutoCommit => 1, RaiseError => 1, PrintError => 0},
                )
	);


} 




=head2 selectSTH


=cut

sub selectSTH {
        my ( $self, %params ) = @_;
        my $table             = $params{table},
        my $fields            = $params{fields};
        my $where             = $params{where};
        my $order             = $params{order} || [];
        my ($stmt, @bind)     = $self->{sql}->select( $table, $fields ,$where, $order);
        
        #check to see if we should log 
        if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($stmt);
                print STDERR Dumper(@bind);
        }
        my $sth = $self->{dbh}->prepare($stmt);
        $sth->execute(@bind);
        return $sth;

}


sub insert {
	my ( $self, %params ) = @_;
	my $table = $params{table};
	my $data  = $params{data};
	print STDERR Dumper($data);
	my ($stmt, @bind)     = $self->{sql}->insert( $table, $data );
	#check to see if we should log
	if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($stmt);
                print STDERR Dumper(@bind);
        }
	my $sth = $self->{dbh}->prepare($stmt);
        $sth->execute(@bind);
        $sth->finish;
	return;
}

sub update {
	my ( $self, %params ) = @_;
        my $table           = $params{table};
        my $data            = $params{data};
        my $where           = $params{where};

        my ( $stmt, @bind )   = $self->{sql}->update($table, $data, $where );
        #check to see if we should log
        if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($stmt);
                print STDERR Dumper(@bind);
        }
        my $sth = $self->{dbh}->prepare($stmt);
        $sth->execute(@bind);
        $sth->finish();

        return;

}

sub count {
	my ( $self, %params ) = @_;
	my $table             = $params{table};
	my $fields            = 'count(*) as count';
	my $where             = $params{where};
	my $order             = $params{order} || [];
	my ($stmt, @bind)     = $self->sql->select( $table, $fields ,$where, $order);
	
	#check to see if we should log
	if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($stmt);
                print STDERR Dumper(@bind);
        }
	my $sth = $self->{dbh}->prepare($stmt);
	$sth->execute(@bind);
	my $row = $sth->fetchrow_hashref;
	$sth->finish;

	return $row->{count}
}


=head2 storedProcedureBoolean

will return either TRUE or FALSE which will result in 1:0, in the case that their is a failure 0 will be returned
and messages will appear in the logs, due to PL/psgpl exceptions

=cut

sub storedProcedureBoolean {
        my ( $self, %params ) = @_;
        print STDERR $params{in};
        my $procedure = $params{procedure};
        my $in        = $params{in};
        my $stmt      = qq/select $procedure($in)/;
        my $sth       = $self->{dbh}->prepare($stmt);
        #check to see if we should log
        if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($stmt);
        }

        $sth->execute;
        return $sth;
}







__PACKAGE__->meta->make_immutable;

1;
