#!/usr/bin/perl
# -----------------------------------------------------------------------------
# TWC
# Treuwert-Computer GmbH
# Schloßbergring 9
# 79098 Freiburg
#
# Author: Klaus Reger <K.Reger@twc.de>
#
# -----------------------------------------------------------------------------
# Program-Data
# -----------------------------------------------------------------------------
# Project  :
# System   :
# Program  :
# Module   : $RCSfile: InterBase.pm,v $
# Version  : $Revision: 1.1 $
# Date     : $Date: 2001/01/25 08:56:02 $
# State    : $State: Exp $
#
# Description:
#   This module handles all the things of WWWdb, which have
#   to do with DbSessions
#
# Change-log (Seeat end of this file)
#
# --- Known Bugs -------------
# please look at the BUGS-File
# --- TODO -------------------
# please look at the TODO-File
# ----------------------------------------------------------------------------

# use DBI;
# use Data::Dumper;

use strict;

use vars qw(@lDbTimestampScanFormats @lDbDateScanFormats @lDbTimeScanFormats);

################################################################################
#
# PLEASE NOTE:
#
# Currently, we have some problems with the DBI-interface for Interbase
# Dates are returned as mm/dd/yy regardless in which century
# Timestamps come from the db in a forat it will refuse to accept later
#
# So the DBD-Driver has to be fixed
#
################################################################################


@lDbTimestampScanFormats = ("%m/%d/%Y %H:%M:%S", "%m/%d/%y %H:%M:%S");
@lDbTimestampScanFormats = ("%m/%d/%Y", "%m/%d/%y");
@lDbDateScanFormats      = ("%m/%d/%Y", "%m/%d/%y");
@lDbTimeScanFormats      = ("%H:%M:%S");


# --- Interbase -------------------------------------------------------------
sub _CreateDataSource()
# ----------------------------------------------------------------------------
{
   my $hSelfPI   = shift;

   $hSelfPI->{"DataSource"} =
       sprintf("dbi:%s:database=%s;ib_dialect=3",
               $hSelfPI->{"DbDriver"},
               $hSelfPI->{"DbName"});

   return $hSelfPI->{"DataSource"};
}


sub DateFromDb($$)
# ----------------------------------------------------------------------------
{
   my $hSelfPI  = shift;
   my $cFieldPI = shift;

   my $cResult = &DbSession::PrintDate
       ($hSelfPI->getDatePrintFormat(),
        &DbSession::ScanDate($cFieldPI, @lDbTimestampScanFormats)) ||
            $cFieldPI;

   return $cResult;

}


sub DateToDb($$)
# ----------------------------------------------------------------------------
{
   my $hSelfPI  = shift;
   my $cFieldPI = shift;

   my $cResult = &DbSession::PrintDate
       ($lDbTimestampScanFormats[0],
        &DbSession::ScanDate($cFieldPI,
                             @{$hSelfPI->getDateScanFormats()})) ||
                                 $cFieldPI;

   return $cResult;

}


# --- Convert datetime-fields from form to db ---------------------------------
sub SQL_TYPE_TIMESTAMP_Form2Db ($$)
{
   return &DateToDb(shift, shift);

}

# --- Convert datetime-fields from db to form ---------------------------------
sub SQL_TYPE_TIMESTAMP_Db2Form ($$)
{
   return &DateFromDb(shift, shift);
}


sub SQL_TYPE_DATE_Db2Form($$)
# ----------------------------------------------------------------------------
{
   my $hSelfPI  = shift;
   my $cFieldPI = shift;

   my $cResult = &DbSession::PrintDate
       ($hSelfPI->getDatePrintFormat(),
        &DbSession::ScanDate($cFieldPI, @lDbDateScanFormats)) ||
            $cFieldPI;

   return $cResult;

}


sub SQL_TYPE_DATE_Form2Db($$)
# ----------------------------------------------------------------------------
{
   my $hSelfPI  = shift;
   my $cFieldPI = shift;

   my $cResult = &DbSession::PrintDate
       ($lDbDateScanFormats[0],
        &DbSession::ScanDate($cFieldPI,
                             @{$hSelfPI->getDateScanFormats()})) ||
                                 $cFieldPI;

   return $cResult;

}



1;
