#ifndef _CEGOOUTPUT_H_INCLUDED_
#define _CEGOOUTPUT_H_INCLUDED_
///////////////////////////////////////////////////////////////////////////////
//                                                         
// CegoOutput.h
// ------------
// Cego formatting data for output
//      
// Design and Implementation by Bjoern Lemke
//
// (C)opyright 2000-2025 Bjoern Lemke
//
// INTERFACE MODULE
//
// Class: CegoOutput
// 
// Description: The CegoOutput class converts query output to an appropriate format.
//              This may either be XML-based style for serving a client request or 
//              ascii-based tableformat for batchmode or client-side output
//
// Status: CLEAN
//
///////////////////////////////////////////////////////////////////////////////

// base includes
#include <lfcbase/Chain.h>
#include <lfcbase/ListT.h>

// cego includes
#include "CegoProcVar.h"
#include "CegoField.h"
#include "CegoFieldValue.h"
#include "CegoDbHandler.h"
#include "CegoDatabaseManager.h"

class CegoOutput {
    
public:
    
    CegoOutput();
    CegoOutput(const ListT<CegoField>& schema, const Chain& format = Chain(""));
    ~CegoOutput();
    
    void setDbHandle(CegoDbHandler* pDbHandle, unsigned maxRowPerMsg = 0, unsigned maxBytePerMsg = 0);
    void setDBMng(CegoDatabaseManager *pDBMng);
    void setRawMode(bool isOn);
    void setRawSeparator(const Chain& rawSep);
    void headOut();
    void rowOut(const ListT<CegoField>& fl);    
    void rowOut(const ListT<CegoFieldValue>& fl);
    void tailOut();
    void tabOut(const ListT< ListT<CegoFieldValue> >& fa);
    void abort(const Chain& msg);
    void chainOut(const Chain& msg, long affCount = 0);
    void procResultOut(const Chain& msg, const ListT<CegoProcVar>& outParamList, CegoFieldValue *pRetVal);
    void manageOutput(const Chain& cmd, ListT< ListT<CegoFieldValue> > & outData);
	
private:

    Chain fill(const Chain& s, unsigned num);
    Chain formatCell(unsigned i, const Chain& s, unsigned maxLen, bool printSep = true);

    // flow control methods
    void writeHeader(unsigned cols);
    void writeFooter(unsigned rows, unsigned cols, char* filename, unsigned start, unsigned end, unsigned totalRow);

    CegoDbHandler* _pDbHandle;
    CegoDatabaseManager* _pDBMng;
    unsigned long _modId;
    ListT<CegoField> _schema;
    unsigned _rowCount;
    unsigned _maxRowPerMsg;
    unsigned _maxBytePerMsg;
    unsigned _preFill;
    bool _rawMode;
    Chain _rawSep;
    Chain _format;
};
#endif
