oracle linux proc 多线程连接库 代码示例

#include "sqlca.h"
#include "sqlda.h"


#define SQLCODE sqlca.sqlcode 
#define SQLNOTFOUND 1403


#define SQLCODE sqlca.sqlcode
#define SQLERRMSG sqlca.sqlerrm.sqlerrmc

static int g_db_debug_switch = 0;
#define G_DB_DEBUG(format,...) \
if (g_db_debug_switch ){                                    \
printf(format, ##__VA_ARGS__);\
}



//bool CDBOperation::OpenDB(const char *user, const char *passwd, const char *db)
bool CDBOperation::OpenDB( const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket)
{
	(void)host;
	(void)port;
	(void)unix_socket;
	m_pDBAccess = (void *)malloc(sizeof(sql_context));


	struct sqlca sqlca;
	EXEC SQL BEGIN DECLARE SECTION;
		sql_context *DBcontest = (sql_context *)m_pDBAccess;


		VARCHAR username[32]={0};// = user;
		VARCHAR password[32]={0}; // = passwd;
		VARCHAR dbname[32]={0}; // = db;
    EXEC SQL END DECLARE SECTION;


	strcpy((char *)username.arr, user);
	username.len = strlen((char *)username.arr);
	
	strcpy((char *)password.arr, passwd);
	password.len = strlen((char *)password.arr);
	
	strcpy((char *)dbname.arr, db);
	dbname.len = strlen((char *)dbname.arr);
	
    //一定要顺序来
    EXEC SQL ENABLE THREADS; 
    EXEC SQL CONTEXT ALLOCATE :*DBcontest; 
    EXEC SQL CONTEXT USE :*DBcontest;
 	EXEC SQL CONNECT :username IDENTIFIED BY :password  USING :dbname;
//////////////////////////////////////////// 
   if(SQLCODE)
    {   
		G_DB_DEBUG("%d | %s | open db sql_init fail %s %s %d %s\n",pthread_self(), __FUNCTION__, user, db, SQLCODE,sqlca.sqlerrm.sqlerrmc);
		EXEC SQL CONTEXT FREE :*DBcontest;
		free(m_pDBAccess);
		m_pDBAccess = NULL;
		return false;
    }
  
	G_DB_DEBUG("%d | %s | open db ok %s %s %d %s\n",pthread_self(), __FUNCTION__,  user, db, SQLCODE,sqlca.sqlerrm.sqlerrmc);
 
	return true;
	
}
void CDBOperation::CloseDB()
{
	struct sqlca sqlca;
	EXEC SQL BEGIN DECLARE SECTION;
		sql_context *DBcontest = (sql_context *)m_pDBAccess;
   EXEC SQL END DECLARE SECTION;


    EXEC SQL CONTEXT USE :*DBcontest;
	//EXEC SQL ROLLBACK WORK RELEASE;
    EXEC SQL COMMIT WORK RELEASE;
    EXEC SQL CONTEXT FREE :*DBcontest;
    if(SQLCODE)
    {
        G_DB_DEBUG("%d | %s | Close oracle fail[%d][%s]/n",pthread_self(), __FUNCTION__, sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
    }
    else
    {
	G_DB_DEBUG("%d | %s | close db %d\n",pthread_self(), __FUNCTION__, 	time(NULL) - survival_time );
    }
	
	free(m_pDBAccess);
	m_pDBAccess = NULL;
	
}
int CDBOperation::UpdateBankWorkKey(char* pMerchantID, char* pTermID, char* pPinKey, char* pMacKey, char* pBatchNo)
{
	struct sqlca sqlca;
	EXEC SQL BEGIN DECLARE SECTION;
		sql_context *DBcontest = (sql_context *)m_pDBAccess;                                                                                                                 
		
		char szBankMacKey[48]={0};
		char szBankPInKey[48]={0};
		char szTerminalID_41[48]={0};
		char szMerchantID_42[48]={0};
		char szBatchNo[48]={0};


		
		char szSql[512]={0};
		int nRet;
	EXEC SQL END DECLARE SECTION;


	EXEC SQL CONTEXT USE :*DBcontest;//这个在每个函数里必须有

	
	strcpy(szBankMacKey, pMacKey);
	strcpy(szBankPInKey, pPinKey);
	strcpy(szTerminalID_41, pTermID);
	strcpy(szMerchantID_42, pMerchantID);


	
	
	EXEC SQL UPDATE tab_platkey SET MacKey=:szBankMacKey, PinKey=:szBankPInKey, SignTime=to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') WHERE PlatTermNo=:szTerminalID_41 AND PlatMerchantNo=:szMerchantID_42;
	
	if (SQLCODE){
	sprintf(szSql, "UPDATE tab_platkey SET MacKey=:'%s', PinKey=:'%s', SignTime=:to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') WHERE PlatTermNo=:'%s' AND PlatMerchantNo=:'%s'", 
		pMacKey,pPinKey, pTermID,pMerchantID);
	G_DB_DEBUG("%d | %s | %s \n",pthread_self(), __FUNCTION__, szSql);	

		return -1;
	}
	
	strcpy(szBatchNo, pBatchNo);


	EXEC SQL UPDATE  tab_platmerterm SET BatchNo =:szBatchNo WHERE PlatTermNo =:szTerminalID_41 AND PlatMerchantID =:szMerchantID_42;
	if (SQLCODE){
	sprintf(szSql, "UPDATE  tab_platmerterm SET BatchNo =:'%s' WHERE PlatTermNo =:'%s' AND PlatMerchantID =:'%s'", 
		pBatchNo, pTermID,pMerchantID);
	G_DB_DEBUG("%d | %s | %s\n",pthread_self(), __FUNCTION__, szSql);


		return -1;
	}


	EXEC SQL COMMIT;
	return 0;


}

proc -lclntsh parse=no THREADS=YES  code=cpp cpp_suffix=cpp iname=DBOperation.pc 

 
 

你可能感兴趣的:(sql,oracle,linux,proc,多线)