#!/usr/bin/perl -w
#
# $Id: Attributes.pm,v 1.1 2002/03/18 11:01:45 fpmip Exp $
#
# IPFC::DB::Attributes object encapsulates attribute definitions in the database
#
# Author : Tycho Fruru <tycho@fruru.com>
#
# Copyright (C) 2001 Tycho Fruru <tycho@fruru.com>
# Copyright (C) 2001 Conostix S.A. Luxembourg - Tycho Fruru <tycho@conostix.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
## This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
package IPFC::Backend::DB::Attributes;

use strict;
use warnings;

my %attribute_name_id;		# key = name , value = id
my %attribute_name_type;	# key = name , value = type
my %attribute_id_type;		# key = id , value = type
my %attribute_id_name;		# key = id , value = name
my %attribute_id_description;	# key = id , value = description

sub init($) {
# takes a IPFC::Backend::DB object and gets attribute definitions

	my $self = shift; # not used, but good for OO notation in perl
	my $backend_interface = shift;
	
	if (not $backend_interface->isa("IPFC::Backend::DB::Interface")) {
		die "Called IPFC::Backend::DB::Attributes without IPFC::Backend::DB::Interface object !";
	}
	
	my $dbh = $backend_interface->{'database-handle'} or die "No database handle";
	
	my $sth;
	my $problem = 0;
	
	undef %attribute_name_id;
	undef %attribute_name_type;
	undef %attribute_id_name;
	undef %attribute_id_description;
	
	foreach my $attr_type ("attr_int","attr_int8","attr_text","attr_timestamp",
			  "attr_date","attr_bytea","attr_numeric","attr_boolean","attr_null","attr_hash") {
	    my $query = "select attr_id,attr_name,attr_description from attributes where $attr_type = 't';";
	    $sth = $dbh->prepare($query) or $problem = 1;
	    if (!$problem) {
	      $sth->execute or $problem = 1;
	    }
	    while ((!$problem && (my ($attr_id,$attr_name,$attr_description) = $sth->fetchrow_array))) {
	    	$attribute_name_id{$attr_name} = $attr_id;
		$attribute_name_type{$attr_name} = $attr_type;
		$attribute_id_type{$attr_id} = $attr_type;
		$attribute_id_name{$attr_id} = $attr_name;
		$attribute_id_description{$attr_id} = $attr_description;
	    }
	    $sth->finish or $problem = 1;
	}
	
	if ($problem) {
		return 0;
	} else {
		return 1;
	}

}

sub get_attribute_name_id() {
# gets a reference to attribute_name_id (without being able to change it !)
	if (!%attribute_name_id) {
		die "Call IPFC::Backend::DB::Attributes::init first !";
	}
	my %a = %attribute_name_id;
	return \%a;
}

sub get_attribute_name_type() {
# gets a reference to attribute_name_type (without being able to change it !)
	if (!%attribute_name_type) {
		die "Call IPFC::Backend::DB::Attributes::init first !";
	}
	my %a = %attribute_name_type;
	return \%a;
}

sub get_attribute_id_type() {
# gets a reference to attribute_id_type (without being able to change it !)
	if (!%attribute_id_type) {
		die "Call IPFC::Backend::DB::Attributes::init first !";
	}
	my %a = %attribute_id_type;
	return \%a;
}

sub get_attribute_id_name() {
# gets a reference to attribute_id_name (without being able to change it !)
	if (!%attribute_id_name) {
		die "Call IPFC::Backend::DB::Attributes::init first !";
	}
	my %a = %attribute_id_name;
	return \%a;
}

sub get_attribute_id_description() {
# gets a reference to attribute_id_description (without being able to change it !)
	if (!%attribute_id_description) {
		die "Call IPFC::Backend::DB::Attributes::init first !";
	}
	my %a = %attribute_id_description;
	return \%a;
}


return 1;
