###########################################################################
#
# dbutil.pm -- gateway to utilities for reading/writing to different databases
# Copyright (C) 2008  DL Consulting Ltd
#
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################

package dbutil;

use strict;


use dbutil::gdbmtxtgz;
use dbutil::gdbm;
use dbutil::jdbm;
use dbutil::sqlite;
use dbutil::mssql;

sub open_infodb_write_handle
{
  my $infodb_type = shift(@_);
  my $infodb_file_path = shift(@_);
  
  if ($infodb_type eq "sqlite") 
  {
    return &dbutil::sqlite::open_infodb_write_handle($infodb_file_path);
  } 
  elsif ($infodb_type eq "gdbm-txtgz") 
  {
    return &dbutil::gdbmtxtgz::open_infodb_write_handle($infodb_file_path);
  } 
  elsif ($infodb_type eq "jdbm") 
  {
    return &dbutil::jdbm::open_infodb_write_handle($infodb_file_path);
  } 
  elsif ($infodb_type eq "mssql") 
  {
    return &dbutil::mssql::open_infodb_write_handle($infodb_file_path);
  } 

  # Use GDBM if the infodb type is empty or not one of the values above
  return &dbutil::gdbm::open_infodb_write_handle($infodb_file_path);
}


sub close_infodb_write_handle
{
  my $infodb_type = shift(@_);
  my $infodb_handle = shift(@_);

  if ($infodb_type eq "sqlite")
  {
    return &dbutil::sqlite::close_infodb_write_handle($infodb_handle);
  }
  elsif ($infodb_type eq "gdbm-txtgz")
  {
    return &dbutil::gdbmtxtgz::close_infodb_write_handle($infodb_handle);
  }
  elsif ($infodb_type eq "jdbm")
  {
    return &dbutil::jdbm::close_infodb_write_handle($infodb_handle);
  }
  elsif ($infodb_type eq "mssql")
  {
    return &dbutil::mssql::close_infodb_write_handle($infodb_handle);
  }
    
  # Use GDBM if the infodb type is empty or not one of the values above
  return &dbutil::gdbm::close_infodb_write_handle($infodb_handle);
}


sub get_default_infodb_type
{
  # The default is GDBM so everything works the same for existing collections
  # To use something else, specify the "infodbtype" in the collection's collect.cfg file
  return "gdbm";
}


sub get_infodb_file_path
{
  my $infodb_type = shift(@_);
  my $collection_name = shift(@_);
  my $infodb_directory_path = shift(@_);

  if ($infodb_type eq "sqlite")
  {
    return &dbutil::sqlite::get_infodb_file_path($collection_name, $infodb_directory_path);
  }
  elsif ($infodb_type eq "gdbm-txtgz")
  {
    return &dbutil::gdbmtxtgz::get_infodb_file_path($collection_name, $infodb_directory_path);
  }
  elsif ($infodb_type eq "jdbm")
  {
    return &dbutil::jdbm::get_infodb_file_path($collection_name, $infodb_directory_path);
  }
  elsif ($infodb_type eq "mssql")
  {
    #==================================================================================================#
    # Updated by Jeffrey (2008/08/25 Monday)
    # After look into the run-time code, it seems we should still create a database file.
    # Since the run-time code is always try to read a database file, the easiest way here is not
    # to change the whole structure, but to give whatever the system is looking for. 
    #==================================================================================================#
    # Added by Jeffrey (2008/08/15 Friday)
    # No file path required for MS SQL, it is a server-client connection.
    # At the moment the information is hard coded in dbutil::mssql::open_infodb_write_handle
    # the this might need some tidy up sometime.
    #==================================================================================================#
    return &dbutil::mssql::get_infodb_file_path($collection_name, $infodb_directory_path); 
  }
  
  # Use GDBM if the infodb type is empty or not one of the values above
  return &dbutil::gdbm::get_infodb_file_path($collection_name, $infodb_directory_path);
}


sub read_infodb_file
{
  my $infodb_type = shift(@_);
  my $infodb_file_path = shift(@_);
  my $infodb_map = shift(@_);

  if ($infodb_type eq "sqlite")
  {
    return &dbutil::sqlite::read_infodb_file($infodb_file_path, $infodb_map);
  }
  elsif ($infodb_type eq "gdbm-txtgz")
  {
    return &dbutil::gdbmtxtgz::read_infodb_file($infodb_file_path, $infodb_map);
  }
  elsif ($infodb_type eq "jdbm")
  {
    return &dbutil::jdbm::read_infodb_file($infodb_file_path, $infodb_map);
  }
  elsif ($infodb_type eq "mssql")
  {
    return &dbutil::mssql::read_infodb_file($infodb_file_path, $infodb_map);
  }
  
  # Use GDBM if the infodb type is empty or not one of the values above
  return &dbutil::gdbm::read_infodb_file($infodb_file_path, $infodb_map);
}


sub write_infodb_entry
{
  my $infodb_type = shift(@_);
  my $infodb_handle = shift(@_);
  my $infodb_key = shift(@_);
  my $infodb_map = shift(@_);

  if ($infodb_type eq "sqlite")
  {
    return &dbutil::sqlite::write_infodb_entry($infodb_handle, $infodb_key, $infodb_map);
  }
  elsif ($infodb_type eq "gdbm-txtgz")
  {
    return &dbutil::gdbmtxtgz::write_infodb_entry($infodb_handle, $infodb_key, $infodb_map);
  }
  elsif ($infodb_type eq "jdbm")
  {
    return &dbutil::jdbm::write_infodb_entry($infodb_handle, $infodb_key, $infodb_map);
  }
  elsif ($infodb_type eq "mssql")
  {
    return &dbutil::mssql::write_infodb_entry($infodb_handle, $infodb_key, $infodb_map);
  }

  # Use GDBM if the infodb type is empty or not one of the values above
  return &dbutil::gdbm::write_infodb_entry($infodb_handle, $infodb_key, $infodb_map);
}


sub delete_infodb_entry
{
  my $infodb_type = shift(@_);
  my $infodb_handle = shift(@_);
  my $infodb_key = shift(@_);

  if ($infodb_type eq "sqlite")
  {
    return &dbutil::sqlite::delete_infodb_entry($infodb_handle, $infodb_key);
  }
  elsif ($infodb_type eq "gdbm-txtgz")
  {
    return &dbutil::gdbmtxtgz::delete_infodb_entry($infodb_handle, $infodb_key);
  }
  elsif ($infodb_type eq "jdbm")
  {
    return &dbutil::jdbm::delete_infodb_entry($infodb_handle, $infodb_key);
  }
  elsif ($infodb_type eq "mssql")
  {
    return &dbutil::mssql::delete_infodb_entry($infodb_handle, $infodb_key);
  }

  # Use GDBM if the infodb type is empty or not one of the values above
  return &dbutil::gdbm::delete_infodb_entry($infodb_handle, $infodb_key);
}


1;
