#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
# TWC
# Treuwert-Computer GmbH
# Schloßbergring 9
# 79098 Freiburg
#
# Author: Klaus Reger <K.Reger@twc.de>
#
# -----------------------------------------------------------------------------
# Program-Data
# -----------------------------------------------------------------------------
# Project  :
# System   :
# Program  :
# Module   : $RCSfile: TestDbSession.t,v $
# Version  : $Revision: 1.1 $
# Date     : $Date: 2002/02/28 15:23:50 $
# State    : $State: Exp $
#
# Description:
#   This module handles all the things of WWWdb, which have
#   to do with DbSession
#
# Change-log (See at end of this file)
#
# --- Known Bugs -------------
# please look at the BUGS-File
# --- TODO -------------------
# please look at the TODO-File
# ----------------------------------------------------------------------------


use WWWdb::DbSession;
use strict;

my $oDbSessionGL;
my $lResult;
my $pRecord;
my $cField;

my @lTestDates =
    (# OK
     "20000423123456",
     "2000:04:23:12:34:56",
     "231298",
     "23.12.98",
     "23-12-98",
     "23.12.",
     "23.",
     "23",
     "",
     "2312",
     "2000-12-23",
     "00-12-23",
     "Sat Nov 19 21:05:57 1994",
     "23-mar-99",
     "23-mor-99",
     "November, 12 70",
     "2. Februar 1965",
     "12:30 PM",
     "10:15",

     # Wrong
     "99",
     "32.12.",
     "1.13.00",
     "000000",
     "2341998",
     "29.02.00",
     );
my @lTestDateFormats =
    (
     "%B, %e %y",               # mmmmmmmm, d yy
     "%Y-%m-%d",
     "%a %b %d %H:%M:%S %Y",
     "%d",
     "%d%m",
     "%d%m%Y",
     "%d%m%y",
     "%d.%m.%Y",
     "%d.%m.%y",
     "%d\\.?%m\\.?%y",
     "%d\\.%m\\.%y",
     "%d\\.%m\\.%y",
     "%d\\.%m\\.?",
     "%e-%b-%y",                # d-mmm-yy
     "%e\. %B %Y",              # d. mmmmmmmmm yyyy
     "%m/%y",
     "%H:%M:%S",
     "%Y:%m:%d:%H:%M:%S",
     "%Y%m%d%H%M%S",
     "%H:%M %p",
     "%H:%M",
     );


$oDbSessionGL = DbSession->new("Pg", "test", "", "",
                               PrintFormat     => "%d.%m.%Y",
                               Language        => "de",
                               DebugLvl        => 1,
                               DateScanFormats => \@lTestDateFormats);
# $oDbSessionGL = DbSession->new("Oracle", "ORCL", "TWC", "TWC", DbHost => "se2");

foreach ("2000-11-22 12:23:34",
         "Sat Nov 19 21:05:57 1994",
         "2000-11-22")
{
   printf STDERR ("o->DateFromDb(%s): %s\n",
           $_, $oDbSessionGL->DateFromDb($_));
#   printf STDERR ("DateFromDb(o, %s): %s\n",
#           $_, DbSession::Pg::DateFromDb($oDbSessionGL, $_));
   printf STDERR ("o->datetime_Db2Form(%s): %s\n",
           $_, $oDbSessionGL->datetime_Db2Form($_));
#   printf STDERR ("datetime_Db2Form(o, %s): %s\n",
#           $_, DbSession::Pg::datetime_Db2Form($oDbSessionGL, $_));
}

exit;

foreach my $cDate (@lTestDates)
{

   my @lResult = DbSession::Pg::ParseDate($oDbSessionGL, $cDate);
   printf STDERR
       ("%s => %s\n",
        $cDate,
        "@lResult")
           if @lResult;
}


print
    "quote(\"Don\'t do this, else y\\'a get \"big Problems\"!\n)\n",
    $oDbSessionGL->getDbHandle->quote("Don't do this, else y\\'a get \"big Problems\"!\n");

eval
{
   $oDbSessionGL->SqlDo
       ("CREATE TABLE xxx (id_xxx INTEGER,
                           v_xxx  VARCHAR(20),
                           d_xxx  DATETIME)");
} or warn @_;

foreach (1..200)
{
   print STDERR "."
       unless $_ % 100;

   $oDbSessionGL->TransactionLoop(5,
                                  \&InsertTwoRecords,
                                  $_);

}
print "\n";

eval
{
   $oDbSessionGL->TransactionLoop
       (3,
        sub {
           my $pDbHandle = shift;

           my $lResult = $pDbHandle->SqlSelect
               ("SELECT COUNT(*) FROM xxx");

           print "Count(before) <$lResult->[0]->[0]>\n";

           $pDbHandle->SqlDo("DELETE FROM xxx;");

           $lResult = $pDbHandle->SqlSelect
               ("SELECT COUNT(*) FROM xxx");

           print "Count(after) <$lResult->[0]->[0]>\n";

           $pDbHandle->SqlDo("THIS IS A INVALID SQL-STATEMENT;");

           $lResult = $pDbHandle->SqlSelect
               ("SELECT COUNT(*) FROM xxx");

           print "Count(never) <$lResult->[0]->[0]>\n";
        });
} or warn @_;

$lResult = $oDbSessionGL->SqlSelect
    ("SELECT
          id_xxx, v_xxx, d_xxx
      FROM
          xxx
      WHERE
          id_xxx >    ? and
          v_xxx   LIKE ?",
     50,
     "%0.%");

print "Second Record <@{$lResult->[1]}>\n";
print "Third Record, Second Field <$lResult->[2]->[1]>\n";

foreach $pRecord (@$lResult)
{
   foreach $cField (@$pRecord)
   {
      print "$cField, ";

   }
   print "\n";
}

printf "<ENTER> before DROP: ";
<>;

eval
{
   $oDbSessionGL->SqlDo("DROP TABLE xxx");
} or warn @_;


sub InsertTwoRecords ($)
{
   my $pDbHandle = shift;
   my $iIndex    = shift;
   my $iMaxId;

   $iMaxId = $pDbHandle->getMaxId("id_xxx", "xxx");

   $pDbHandle->SqlDo
       ("INSERT INTO xxx (id_xxx, v_xxx, d_xxx) values (?, ?, ?)",
        $iMaxId,
        $pDbHandle->DateFromDb ($pDbHandle->DateToDb
                                (scalar(localtime()))),
        $pDbHandle->DateToDb(scalar(localtime())));

   $pDbHandle->SqlDo
       ("INSERT INTO xxx (id_xxx, v_xxx, d_xxx) values
                         ($iIndex, '$iIndex. Datens.', '" .
        $pDbHandle->DateToDb(scalar(localtime())) . "')");

   return 1;
}


printf "Pg:      %s\n",
  DbSession::Pg::DateToDb(undef, scalar(localtime()));
printf "Pg:      %s\n",
  DbSession::Pg::DateFromDb
    (undef, DbSession::Pg::DateToDb (undef, scalar(localtime())));
printf "Oracle:  %s\n",
  DbSession::Oracle::DateToDb(undef, scalar(localtime()));
printf "Oracle:  %s\n",
  DbSession::Oracle::DateFromDb
     (undef, DbSession::Oracle::DateToDb(undef, scalar(localtime())));
printf "MySQL:   %s\n",
  DbSession::MySql::DateToDb(undef, scalar(localtime()));
printf "MySQL:   %s\n",
  DbSession::MySql::DateFromDb
    (undef, DbSession::MySql::DateToDb(undef, scalar(localtime())));

$oDbSessionGL->Dump();

$oDbSessionGL->ShowAttributes();

exit 0;

# $oDbSessionGL = undef;

