package Items;

use strict;

#use warnings;
use lib '/app/tools/common/lib';
use Common::RSDB;
use Common::RSApp;

sub new {
    my $class  = shift;
    my %params = @_;

    my $self = {};
    bless $self, $class;

    if ( defined $params{client_id} && 0 == $params{client_id} ) {
        $self->{dbo} = Common::RSApp::GetCommonDB();
    } else {
        $self->{dbo} = Common::RSApp::GetClientDB();
    }

    $self->{sth}   = undef;
    $self->{count} = undef;

    return $self;
}

sub getByQuery {
    my $self = shift;
    my $sql  = shift;

    my $sth = $self->{dbo}->DoCmd($sql);

    return undef if ( !defined $sth );

    $self->{sth}   = $sth;
    $self->{count} = $sth->rows;

    return $self->Count;
}

sub getByQueryParams {
    my $self   = shift;
    my $sql    = shift;
    my @params = @_;

    my $sth = $self->{dbo}->DoCmdParams( $sql, @params );

    return undef if ( !defined $sth );

    $self->{sth}   = $sth;
    $self->{count} = $sth->rows;

    return $self->Count;
}

sub Count {
    my $self = shift;

    if ( !defined $self->{count} ) {
        if ( defined $self->{sth} ) {
            $self->{count} = $self->{sth}->rows;
        }
    }
    return $self->{count};
}

sub GetNext {
    my $self = shift;

    croak("Please define this routine in your derived class.");
}

sub DESTROY {
    my $self = shift;
    $self->{sth}->finish() if ( $self->{sth} );
}

###
1;    # Play nicely.
###
