#!/usr/local/bin/perl -w
#
# This software has been created by Genome Research Limited (GRL). 
# GRL hereby grants permission to use, copy, modify and distribute 
# this software and its documentation for non-commercial purposes 
# without fee at the user's own risk on the basis set out below.
# GRL neither undertakes nor accepts any duty whether contractual or 
# otherwise in connection with the software, its use or the use of 
# any derivative, and makes no representations or warranties, express
# or implied, concerning the software, its suitability, fitness for
# a particular purpose or non-infringement.
# In no event shall the authors of the software or GRL be responsible 
# or liable for any loss or damage whatsoever arising in any way 
# directly or indirectly out of the use of this software or its 
# derivatives, even if advised of the possibility of such damage.
# Our software can be freely distributed under the conditions set out 
# above, and must contain this copyright notice.


package Ondemand;
use strict;

sub new
{
my $pkg = shift;

my $setup	={	DBH		=> '',
			User		=> ''};

$setup->{User} = `whoami`;
chop ($setup->{User});

if(@_){$setup->{User} = shift;}


# Label attribute is an array of hashes
bless $setup,$pkg;

return $setup;
}


sub Connect
{
my $pkg = shift;

eval {$pkg->{DBH} = WrapDBI->connect('dev1', {RaiseError => 1});};

if ($@){if($pkg->{Printerlist}){undef $pkg->{Printerlist};} die "ERROR : Cannot connect to Database";}

}


sub Query
{
my ($pkg,$sql) = @_;

my $ref;

eval{

if(!$pkg->{DBH}){$pkg->Connect();}

my $idlist = $pkg->{DBH}->prepare($sql);
$idlist->execute();
	 
if ($DBI::err)
{die "Error: $idlist->errstr \n";}
else
{$ref = $idlist->fetchall_arrayref;}

$idlist->finish();


};
if($@){die "ERROR : Cannot query database $@";}
else
{
$pkg->{DBH}->disconnect();
undef $pkg->{DBH};

$pkg->{Items} = $ref;
}

}



sub Itemstoprint
{
my $pkg = shift;

return shift @{$pkg->{Items}};
}





1;
