#!/usr/bin/perl
package IndexedTableLink;
use strict;
use warnings;
use SimpleObject;
use SimpleObject;
use Carp qw(croak);
use Storable qw(dclone store retrieve);
use Data::Dumper;
use Hash::Merge qw(merge);
use IndexedTable;

our @ISA = qw(SimpleObject);

sub create {
	my ($class, $table, $column) =  @_; 

	croak ("Error in IndexedTableLink->create : Not a valid IndexedTable\n") if (not ref($table) eq 'IndexedTable');
	croak ("Error in IndexedTableLink->create : Should be called from class, not ref\n") if (ref($class));
	
	my $colnr;
	if ($column =~ /\d+/) {
		$colnr = $column;
	}
	else {
		$colnr = $table->column_by_name_keys_allowed($column);
	}
	
	my $self = {};
	$self->{table} = $table;
	$self->{col} = $colnr;
	$self->{colname} = $column;
	

	
	$self->{expected_numkeys} =  ($table->numkeys() >= $colnr) ? $colnr -1 : $table->numkeys() ;
	$self->{is_set} = ($table->numkeys() > $colnr) ? 1:0;
	bless $self, $class;
}
sub query {
	my ($self, $keyref) =  @_; 
    croak ("Error in IndexedTableLink->evaluate : Invalid number of keys\n") if (scalar(@$keyref) != $self->{expected_numkeys});
    return $self->{table}->query($keyref, $self->{col});
}
sub numkeys {
	return $_[0]->{expected_numkeys};
}
1;
