# Postgresql wigwam module

package Text::Wigwam::Directives::pgsql;

    use strict;
    use Text::Wigwam::Const qw(:all);
    use DBI;

    our $VERSION = '0.01';

sub _proto_connect{ [ STRING ] }
#sub _connect{ return( DBI->connect( "dbi:Pg:dbname=".$_[0]->get_arg, "postgres", "" ) ); }
sub _connect{
#    my $dbobj = DBI->connect( "dbi:Pg:dbname=".$_[0]->get_arg.";host=192.168.200.50", "postgres", "", { RaiseError => 0, PrintError => 0 } );
    my $dbobj = DBI->connect( "dbi:Pg:dbname=".$_[0]->get_arg.";host=192.168.100.50", "postgres", "" ) || return $_[0]->exception( $DBI::errstr );
    $dbobj->{RaiseError} = 0;
    $dbobj->{PrintError} = 0;
    return( $dbobj );
}

sub _proto_prepare{ [ BLESSED, STRING ] }
sub _prepare{
    my $db = $_[0]->get_arg;
    my $query = $_[0]->get_arg;
#    print $query;
    my $statement = $db->prepare( $query ) || return $_[0]->exception( $DBI::errstr );
#    $statement->execute;
    return( $statement );
}

sub _proto_execute{ [ BLESSED ] }
sub _execute{
    my $sth = $_[0]->get_arg;
    return $sth->execute || $_[0]->exception( $DBI::errstr );
}

sub _proto_fetchrow{ [ BLESSED ] }
sub _fetchrow{
    my( $statement ) = $_[0]->get_arg;
    return $statement->fetchrow_arrayref;# || $_[0]->exception( $DBI::errstr );
}

sub _proto_fetchrow_hash{ [ BLESSED ] }
sub _fetchrow_hash{
    my $sth = $_[0]->get_arg;
    return $sth->fetchrow_hashref;# || $_[0]->exception( $DBI::errstr );
}

sub _proto_fetchrow_matches{ [ BLESSED ] }
sub _fetchrow_matches{
    my $sth = $_[0]->get_arg;
    my $array = [];
    my $temp;
    push( @$array, $temp ) while $temp = $sth->fetchrow_hashref;
    return( $array );
}

sub _proto_fetchrow_matches_hash{ [ BLESSED ] }
sub _fetchrow_matches_hash{
    my $sth = $_[0]->get_arg;
    my $array = [];
    my $temp;
    push( @$array, $temp ) while $temp = $sth->fetchrow_hashref;
    return( $array );
}

sub _proto_state{ [ BLESSED ] }
sub _state{
    my $obj = $_[0]->get_arg;
    return( $obj->state );
}

sub _proto_finish{ [ VAR ] }
sub _finish{
    my $statement_var = $_[0]->get_arg();
    my $statement = $_[0]->get_value( $statement_var );
    $statement->finish || return $_[0]->exception( $DBI::errstr );
    $_[0]->undefine( $statement_var );
    return;
}

sub _proto_disconnect{ [ BLESSED ] }
sub _disconnect{
    my $db = $_[0]->get_arg();
#warn "db=$db engine=".$_[0]->{engobj};
    $db->disconnect;# || return $_[0]->exception( $DBI::errstr );
    return;
}

sub _proto_quote{ [ BLESSED, STRING ] }
sub _quote{
    my $obj = $_[0]->get_arg;
    return( $obj->quote( $_[0]->get_arg ) );
}

# The #trim directive removes leading and trailing whitespace from a scalar,
# or all elements of an array, depending on the datatype passed to it.

sub _proto_trim{ [ STRING|ARRAY ] }
sub _trim{
    my( $var ) = $_[0]->get_arg;
    local( $_ );
    if( ref( $var ) eq "ARRAY" ){
        my( @array ) = ();
#        foreach my $val ( @$var ){ push( @array, &trim( $val ) ); }
        push( @array, &trim($_) ) for @$var;
        return( \@array );
    }
    return( &trim( $var ) );
}

sub trim{
    my $var = shift;
    $var =~ s/^\s*//;
    $var =~ s/\s*$//;
    return $var;
}

sub _proto_read_file_to_string{ [ STRING ] }
sub _read_file_to_string{ return( &read_file( $_[0]->get_arg ) ); }

1;
