///////////////////////////////////////////////////////////////////////////////
//                                                         
// CegoObject.cc 
// -------------
// Cego object super class implementation
//     
// Design and Implementation by Bjoern Lemke               
//     
// (C)opyright 2000-2010 Bjoern Lemke
//
// 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, 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; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// IMPLEMENTATION MODULE
//
// Class: CegoObject
// 
// Description: 
//
// Status: QG-2.6
//
///////////////////////////////////////////////////////////////////////////////

// cego includes
#include "CegoObject.h"

#include <string.h>

CegoObject::CegoObject()
{
}

CegoObject::CegoObject(const CegoObject& oe)
{
    _type = oe._type;
    _name = oe._name;
    _tabSetId = oe._tabSetId;
}

CegoObject::CegoObject(ObjectType type, const Chain& objName, int tabSetId)
{
    _type = type;
    _tabSetId = tabSetId;
    _name = objName;
}

CegoObject::~CegoObject()
{   
}

void CegoObject::setName(const Chain& name)
{
    _name = name;
}

const Chain& CegoObject::getName() const
{
    return _name;
}

void CegoObject::setType(CegoObject::ObjectType type)
{
    _type = type;
}

CegoObject::ObjectType CegoObject::getType() const
{
    return _type;
}

void CegoObject::setTabSetId(int tabSetId)
{
    _tabSetId = tabSetId;
}

int CegoObject::getTabSetId() const
{
    return _tabSetId;
}

int CegoObject::getBaseSize() const
{

    int baseSize = sizeof(int); // entrySize

    baseSize += 1; // object type
    baseSize += 1; // object name size
    baseSize += _name.length();
    
    baseSize = baseSize + sizeof(int); // tabSetId

    return baseSize;

}

void CegoObject::encodeBase(char *buf, int entrySize)
{
    char* bufPtr = buf;
    
    memcpy(bufPtr, &entrySize, sizeof(int));
    bufPtr = bufPtr + sizeof(int);

    char c = _type;
    memcpy (bufPtr, &c , 1);
    bufPtr++;
    
    c = (char)_name.length();
    memcpy (bufPtr, &c , 1);
    bufPtr++;

    memcpy(bufPtr, (char*)_name, _name.length());
    bufPtr=bufPtr + _name.length();

    memcpy(bufPtr, &_tabSetId, sizeof(int));
    bufPtr = bufPtr + sizeof(int);

}

void CegoObject::decodeBase(char *buf, int& entrySize)
{
    char* bufPtr = buf;

    memcpy(&entrySize, bufPtr, sizeof(int));
    bufPtr = bufPtr + sizeof(int);
    
    // encode table information
    char c;
    memcpy (&c, bufPtr, 1);
    
    _type = (ObjectType)c;
    bufPtr++;
    
    bufPtr++;
    _name =  Chain(bufPtr) ;    
    bufPtr=bufPtr + _name.length();

    memcpy(&_tabSetId, bufPtr, sizeof(int));

}

CegoObject& CegoObject::operator = ( const CegoObject& oe)
{
    _type = oe._type;
    _name = oe._name;
    _tabSetId = oe._tabSetId;
    return (*this);
}

bool CegoObject::operator == ( const CegoObject& oe)
{
    if ( _name == oe._name && _type == oe._type ) // && _tabSetId == oe._tabSetId)
	return true;
    return false;
}

Chain CegoObject::toChain() const
{
    Chain s;
    s = "ObjectName: " + _name + Chain("\n");
    s +=  Chain("ObjectType: ");
    switch ( _type )
    {
    case SYSTEM:
	s += Chain("system");
	break;
    case TABLE:
	s += Chain("table");
	break;
    case PINDEX:
	s += Chain("primary index");
	break;
    case UINDEX:
	s += Chain("unique index");
	break;
    case INDEX:
	s += Chain("index");
	break;
    case PBTREE:
	s += Chain("primary btree");
	break;
    case UBTREE:
	s += Chain("unique btree");
	break;
    case BTREE:
	s += Chain("btree");
	break;
    case VIEW:
	s += Chain("view");
	break;
    case RBSEG:
	s += Chain("rollback segment");
	break;
    case FKEY:
	s += Chain("foreign key");
	break;
    case PROCEDURE:
	s += Chain("procedure");
	break;
    case CHECK:
	s += Chain("check");
	break;
    case JOIN:
	s += Chain("join");
	break;
    case UNDEFINED:
	s += Chain("undefined");
	break;
    }
    s += Chain("\n");
    s += "TableSetId: " + Chain(_tabSetId) + Chain("\n");
    return s;
}
