#!/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: TestSession.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 Session
#
# Change-log (See at end of this file)
#
# --- Known Bugs -------------
# please look at the BUGS-File
# --- TODO -------------------
# please look at the TODO-File
# ----------------------------------------------------------------------------


use DBI;
use WWWdb::Session;

my $pDbHdlMGL;


$pDbHdlMGL = DBI->connect
    ("dbi:Pg(RaiseError=>0, PrintError=>0, AutoCommit=>1)" .
     ":dbname=test")
    || die("Connection to Database failed!");

foreach (0, 8, undef, 64)
{
   $oSessionGL = Session->new($pDbHdlMGL,
                              "Id" => "1db9",
                              "IdLength"   => $_);

   $oSessionGL->Dump();

   printf STDERR
       ("Session (new):\nId -> %s\nLength -> %s\n",
        $oSessionGL->getId(),
        $oSessionGL->getIdLength());  # use old Access-Method

   printf STDERR "-" x 80 . "\n";

}

$cId = 'TEST0000TEST0000';

$pDbHdlMGL->do
    (sprintf("INSERT INTO wwwdb_state
                       (
                          session_id,
                          key_name,
                          key_value
                       )
                       VALUES
                       (
                          \'$cId\',
                          'last_access',
                          'TEST'
                       )"));

if($pDbHdlMGL->err())
{
   warn(sprintf("Database Error occured during %s!" . "<BR>" .
               "    SQL-Error: (%d) %s" . "<BR>" .
               "    Statement: %s" . "<BR>",
               "INSERT INTO wwwdb_state",
               $pDbHdlMGL->err,
               $pDbHdlMGL->errstr));
}

$oSessionGL = Session->new($pDbHdlMGL,
                           "Id"         => $cId,
                           "IdLength"   => length($cId));


$oSessionGL->setState("1", "one");
$oSessionGL->getState("1");
$oSessionGL->getState("2");
$oSessionGL->setState("2", "two");
$oSessionGL->setState("2", "zwei");
$oSessionGL->setState("2", "due");
$oSessionGL->setState("3", "three");
$oSessionGL->setState("4", "four");

foreach (1..100)
{
   $oSessionGL->setState("2", "II");
}

$oSessionGL->setState("toDel", "!@&%...");
$oSessionGL->setState("toDel", undef);
$oSessionGL->setState("ActualLogin", undef);
$oSessionGL->setState("IsAdmin", "Yes");
$oSessionGL->setState("Counter",
                      $oSessionGL->getState("Counter") + 1);

printf STDERR "State (1, 2, toDel, u) = ('%s', '%s', '%s', '%s')\n",
    $oSessionGL->getState("1"),
    $oSessionGL->getState("2"),
    $oSessionGL->getState("toDel"),
    $oSessionGL->getState("undef");


$oSessionGL->ShowAttributes();

$iCounter = $oSessionGL->getState("Counter");
$oSessionGL->Dump();

if($iCounter > 3)
{

   $oSessionGL = Session->new($pDbHdlMGL,
                              "Id"         => $cId,
                              "IdLength"   => length($cId));

   print "<ENTER>";

   <>;

   foreach ("last_access", "Counter", "1", "2", "3", "4", "IsAdmin")
   {
      $oSessionGL->setState("$_", undef);
   }

   $oSessionGL->Dump();
}

$oSessionGL = undef;

$pDbHdlMGL->disconnect();

exit 0;
