00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef __CAACCOUNTINGDBINTERFACE__
00029 #define __CAACCOUNTINGDBINTERFACE__
00030 #include "doxygen.h"
00031 #ifdef PAYMENT
00032 #include "CAQueue.hpp"
00033 #include "CAXMLCostConfirmation.hpp"
00034 #include "CAThread.hpp"
00035
00036 #define MAX_DB_CONNECTIONS 3
00037 #define PG_PROTOCOL_VERSION_3 3
00038
00039 #define RESULT_FORMAT_TEXT 0
00040
00041 #define PREPARED_STMT_NAME_STORE_CC "storeCCStatement"
00042 #define PREPARED_STMT_QUERY_STORE_CC \
00043 "UPDATE COSTCONFIRMATIONS SET BYTES = $1::bigint, XMLCC = $2::varchar(2000), SETTLED = $3::integer WHERE ACCOUNTNUMBER = $4::bigint AND CASCADE = $5::varchar(200)"
00044 #define PREPARED_STMT_PARAMS_STORE_CC 5
00045
00046 #define STMT_CLEAR_ACCOUNT_STATUS "DELETE FROM ACCOUNTSTATUS WHERE ACCOUNTNUMBER = '%llu'"
00047
00054 class CAAccountingDBInterface
00055 {
00056 public:
00057
00065
00066
00067
00068 SINT32 storeCostConfirmation(CAXMLCostConfirmation &cc, UINT8* ccCascade);
00069
00070 SINT32 getCostConfirmation(UINT64 accountNumber, UINT8* cascadeId, CAXMLCostConfirmation **pCC, bool& a_bSettled);
00071
00072
00077
00078 SINT32 getUnsettledCostConfirmations(CAXMLCostConfirmation ***resultCCs, UINT8* cascadeId, UINT32 *nrOfCCs, UINT32 a_maxCCs);
00079
00084 SINT32 markAsSettled(UINT64 accountNumber, UINT8* cascadeId, UINT64 a_transferredBytes);
00085
00090 SINT32 deleteCC(UINT64 accountNumber, UINT8* cascadeId);
00091
00092 SINT32 storePrepaidAmount(UINT64 accountNumber, SINT32 prepaidBytes, UINT8* cascadeId);
00093 SINT32 getPrepaidAmount(UINT64 accountNumber, UINT8* cascadeId, bool a_bDelete);
00094
00095 SINT32 storeAccountStatus(UINT64 a_accountNumber, UINT32 a_statusCode);
00096 SINT32 getAccountStatus(UINT64 a_accountNumber, UINT32& a_statusCode);
00097 SINT32 clearAccountStatus(UINT64 a_accountNumber);
00098
00105 SINT32 checkCountAllQuery(UINT8* a_query, UINT32& r_count);
00106
00107
00108
00109
00110
00111
00112 static CAAccountingDBInterface *getConnection();
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 static SINT32 releaseConnection(CAAccountingDBInterface *dbIf);
00124
00125
00126 static SINT32 init();
00127
00128 static SINT32 cleanup();
00129
00130 private:
00131
00132 CAAccountingDBInterface();
00133 ~CAAccountingDBInterface();
00134
00135
00136 SINT32 __storeCostConfirmation(CAXMLCostConfirmation &cc, UINT8* ccCascade);
00137 SINT32 __getCostConfirmation(UINT64 accountNumber, UINT8* cascadeId, CAXMLCostConfirmation **pCC, bool& a_bSettled);
00138
00139
00140 SINT32 __getUnsettledCostConfirmations(CAXMLCostConfirmation ***resultCCs, UINT8* cascadeId, UINT32 *nrOfCCs, UINT32 a_maxCCs);
00141
00142 SINT32 __markAsSettled(UINT64 accountNumber, UINT8* cascadeId, UINT64 a_transferredBytes);
00143 SINT32 __deleteCC(UINT64 accountNumber, UINT8* cascadeId);
00144
00145 SINT32 __storePrepaidAmount(UINT64 accountNumber, SINT32 prepaidBytes, UINT8* cascadeId);
00146 SINT32 __getPrepaidAmount(UINT64 accountNumber, UINT8* cascadeId, bool a_bDelete);
00147
00148 SINT32 __storeAccountStatus(UINT64 a_accountNumber, UINT32 a_statusCode);
00149 SINT32 __getAccountStatus(UINT64 a_accountNumber, UINT32& a_statusCode);
00150 SINT32 __clearAccountStatus(UINT64 a_accountNumber);
00151
00152 SINT32 __checkCountAllQuery(UINT8* a_query, UINT32& r_count);
00153
00162 SINT32 initDBConnection();
00163
00168 SINT32 terminateDBConnection();
00169 friend class CAAccountingInstance;
00170
00171 bool isDBConnected();
00172
00177 bool checkConnectionStatus();
00178
00179 bool checkOwner();
00180
00181 bool testAndSetOwner();
00182 bool testAndResetOwner();
00183
00184 PGresult *monitored_PQexec(PGconn *conn, const char *query);
00185
00187 PGconn * m_dbConn;
00188 bool m_bConnected;
00189
00190
00191 volatile thread_id_t m_owner;
00192
00193
00194
00195 volatile bool m_free;
00196 int m_protocolVersion;
00197 #ifdef PG_PROTO_VERSION_3
00198 char *m_pStoreCCStmt;
00199 #endif
00200
00201 CAMutex *m_pConnectionMutex;
00202
00203
00204 static CAConditionVariable *ms_pConnectionAvailable;
00205
00206
00207
00208 static volatile UINT64 ms_threadWaitNr;
00209
00210 static volatile UINT64 ms_nextThreadNr;
00211
00212 static CAAccountingDBInterface *ms_pDBConnectionPool[];
00213 };
00214 #endif //PAYMENT
00215 #endif