#ifndef _CGWRAP_H
#define _CGWRAP_H   1
///////////////////////////////////////////////////////////////////////////////
//                                                         
// cgwrap.h
// --------
// Cego C wrapper API
//                                               
// Design and Implementation by Bjoern Lemke               
//                                                         
// (C)opyright 2009 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.
//
///////////////////////////////////////////////////////////////////////////////

#ifdef  __cplusplus
#include <lfc/Logger.h>
#include <lfc/NetHandler.h>

#include "CegoDbHandler.h"
#endif


#define CG_INT      1
#define CG_VARCHAR  2
#define CG_LONG     3
#define CG_BOOL     4
#define CG_DATETIME 5
#define CG_BIGINT   6
#define CG_FLOAT    7
#define CG_DOUBLE   8
#define CG_DECIMAL  9
#define CG_FIXED    10
#define CG_SMALLINT 11
#define CG_TINYINT  12
#define CG_BLOB     13
#define CG_NULL     14

#define CG_LOG_ERROR 1
#define CG_LOG_NOTICE 2
#define CG_LOG_DEBUG 3

extern char cgerrmsg[];

typedef struct CegoDBHandle
#ifdef  __cplusplus
{
    CegoModule *pModule;
    NetHandler *pNet;
    CegoDbHandler *pDB;
} 
#endif
CGDB;

typedef struct CegoFetchHandle
#ifdef  __cplusplus
{
    CGDB *cgdb;
    bool isActive;
    ListT<CegoField> *pSchema;
} 
#endif
CGFetch;

typedef struct CegoValue
{
    int type;
    int len;
    void *val;
} CGVal;

typedef struct CegoBlobValue
{
    int fileId;
    int pageId;
    int len;
    unsigned char *buf;
} CGBlob;

#ifdef  __cplusplus
class CegoBindValue
{
public:
    
    enum BindType { BINDIN, BINDOUT, BINDINOUT };
    
    int pos;
    CGVal* cgval;
    BindType type;

    CegoBindValue() {}
    CegoBindValue(int p)
    {
	pos = p;
    }
    
    CegoBindValue& operator = ( const CegoBindValue& bv)
    {
	pos = bv.pos;
	cgval = bv.cgval;
        type = bv.type;
	return (*this);
    }
    bool operator == ( const CegoBindValue& bv)
    {
	if ( pos == bv.pos )
	    return true;
	return false;
    }
};
#endif

typedef struct CegoStatement
#ifdef  __cplusplus
{
    int numParams;
    bool isOut;
    ListT<Chain> *pStmtChunks;
    ListT<CegoBindValue> *pParamList;
} 
#endif
CGStmt;

#ifdef  __cplusplus
extern "C" {
#endif

    void cego_modlog(char* module, int level);
    
    CGDB* cego_connect(char *servername, int portno, char *prot, char *tsname, char *user, char *pass, char* logfile);
    void cego_disconnect (CGDB *cgdb);

    CGFetch* cego_allocate_fetch();
    void cego_free_fetch(CGFetch *cgfetch);
    int cego_num_col(CGFetch* cgfetch);
    int cego_getpos(CGFetch* cgfetch, char* attr);
    char* cego_getcol(CGFetch* cgfetch, int pos);
    
    CGStmt* cego_prepare (char* stmt);
    int cego_bind_in (CGStmt* cgstmt, CGVal* cgval, int pos);
    int cego_bind_out (CGStmt* cgstmt, CGVal* cgval, int pos);
    int cego_bind_inout (CGStmt* cgstmt, CGVal* cgval, int pos);

    int cego_execute (CGDB *cgdb, CGStmt* cgstmt, CGFetch* cgfetch);

    void cego_free_stmt(CGStmt* cgstmt);
    
    int cego_query (CGDB* cgdb, char* stmt, CGFetch* cgfetch);
    

    int cego_fetch (CGFetch* cgfetch, CGVal cgval[], int numCol);
    int cego_abort (CGFetch* cgfetch);
    int cego_reset (CGFetch* cgfetch);

    int cego_putblob (CGDB *cgdb, CGBlob* cgblob);
    int cego_getblob (CGDB *cgdb, CGBlob* cgblob);
    int cego_delblob (CGDB *cgdb, CGBlob* cgblob);

    char* cego_error ();
#ifdef  __cplusplus
}
#endif

#endif 

