
#ifndef _CEGOEXPR_H_INCLUDED_
#define _CEGOEXPR_H_INCLUDED_
///////////////////////////////////////////////////////////////////////////////
//                                                         
// CegoExpr.h
// ----------
// Cego expression class definition
//
// Design and Implementation by Bjoern Lemke               
//
// (C)opyright 2000-2025 Bjoern Lemke
//
// INTERFACE MODULE
//
// Class: CegoExpr
// 
// Description: Query expression container class 
//
// Status: CLEAN
//
///////////////////////////////////////////////////////////////////////////////

// LFC INCLUDES
#include <lfcbase/ListT.h>
#include <lfcbase/SetT.h>

// CEGO INCLUDES
#include "CegoTerm.h"
#include "CegoProcStmt.h"

class CegoQueryManager;

class CegoExpr {

public:

    enum ExpType { ADD, SUB, TERM, CONCAT  };
    
    CegoExpr();
    CegoExpr(char* buf);
    CegoExpr(CegoExpr *pExpr, CegoTerm *pTerm, ExpType expType, const Chain& alias = Chain());
    CegoExpr(CegoTerm *pTerm, const Chain& alias = Chain());

    ~CegoExpr();

    void cleanUp();

    void setQueryManager(CegoQueryManager* pTabMng);
    void setTabSetId(unsigned tabSetId);
    
    void setAlias(const Chain& alias);
    const Chain& getAlias() const;

    bool checkConst() const;
    bool checkVar(Chain& var) const;
    CegoAttrDesc* checkAttr() const;
    CegoCondition* checkCondition() const;

    bool checkLob(CegoFieldValue& fv, CegoProcBlock *pBlock);
    
    void getFunctionList(SetT<CegoObject>& funcList);
    
    ListT<CegoAttrDesc*> getAttrRefList() const;
    unsigned evalReferences(CegoContentObject *pCO, const ListT<CegoField>& fl);

    void getFieldList(ListT<CegoField>& fl, ListT<CegoField>** pFLA) const;
    
    CegoField evalField(const ListT<CegoField>& fl, bool graceful = false) const;

    CegoFieldValue evalFieldValue(ListT<CegoField>** pFLA, CegoProcBlock *pBlock) const;

    void clearAttrCache();

    void getSelectQueryList(ListT<CegoSelect*>& queryList);

    ExpType getType() const;
    CegoTerm *getTerm() const;
    CegoExpr *getExpr() const;
   
    ListT<CegoAggregation*>& getAggregationList();

    CegoExpr* clone(bool isAttrRef = false);

    CegoExpr& operator = ( const CegoExpr& e);

    Chain getId(CegoProcBlock *pBlock) const;
    Chain toChain(unsigned defTabSetId, const Chain& indent = Chain("")) const;
    Chain dbFormat(CegoDatabaseFormater *pForm) const;
    
    friend ostream& operator << (ostream& s, const CegoExpr& e);

    void encode(char *buf, CegoProcBlock *pBlock);
    void decode(char *buf);
    unsigned getEncodingLength(CegoProcBlock *pBlock = 0) const;

    void getPlanList(ListT<Element*>& planList);

    CegoExpr* map(const Chain& viewAlias, const ListT<CegoExpr*>& exprList) const;
    
private:
    
    bool _isAgg;
    ListT<CegoAggregation*> _aggList;
    
    ExpType _expType;

    Chain _alias;
    CegoExpr *_pExpr;
    CegoTerm *_pTerm;

    bool _isExtern;
    
};
#endif
