📄 oraopt.cpp
字号:
/*
****************************************************************************
** 版权所有: (C) Copyright 2006 - 2030, PM
** ALL RIGHTS RESERVED
** 文件名 : oraopt.cpp
** 作者 : chenqy
** 完成日期: 2006-03-30
** 描述 : 本文件中包含了oracle的数据库接口连接和游标
** 数据结构:
** 编写说明:
** 修改记录:
** 1. Date:
** Author: cqy
** Modification: Create this file
******************************************************************************
*/
extern "C"
{
#include <stdio.h>
#include "oratypes.h"
#include "ociapr.h"
#include "ocidem.h"
#include "stdarg.h"
}
#include <pthread.h>
#include "oraopt.h"
#include "db.h"
#include <iostream>
#include "writelog.h"
using namespace std;
CMyMutex m_OracleMutex ; //
int traceLevel = 0;
int CConnection::ConnectNum = 0;//静态成员的初始化
int CCursor::CursorNum = 0;
unsigned int CConnection::ulNum = 0;//静态成员的初始化
inline int xLogPrint(int level, char *fmt, ...)
{
char buffer[1024];
va_list args;
if (level > traceLevel)
{
return(0);
}
if(fmt[0] == '\0')
{
return 0;
}
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
printf("%s\r\n",buffer);
return (0);
}
/* CConnection destructor */
CConnection::~CConnection()
{
int ret ;
xLogPrint(1,"~CConnection ! ConnectNum =%d ",ConnectNum);
ConnectNum--;
// DisConnect if CConnection exists
if (State == CONNECTED)
{
ret = DisConnect();
if (DB_OK != ret )
{
xLogPrint(0,"DisConnect ORACLE ERROR!");
}
}
}
CConnection::CConnection()
{
ConnectNum++;
xLogPrint(1,"CConnection ! ConnectInstanceNum =%d ",ConnectNum);
State = NOT_CONNECTED;
memset(hda,'\0', HDA_SIZE);
memset(g_szDBErrorSTR,0,sizeof(g_szDBErrorSTR));
g_nErrorCode = 0;
}
sword CConnection::Connect(const char *UserName, const char *PassWord,const char * DBName )
{
sword ret = 0;
char connSql[1024];
if (State == CONNECTED)
{
// this object is already connected
return (CONERR_ALRCON);
}
m_OracleMutex.Lock();
sprintf(connSql,"%s/%s@%s", UserName,PassWord,DBName);
ret = olog(&lda,
(ub1 *)hda,
(text *) connSql,
-1,
(text *) 0,
-1,
(text *) 0,
-1,
(ub4)OCI_LM_DEF
);
if ( DB_OK != ret)
{
Display_Error();
xLogPrint(0,"Connected ORACLE ERROR!");
m_OracleMutex.Unlock();
return DB_ERROR;
}
else
{
ulNum++;
xLogPrint(0,"Connected to ORACLE ! ulNum = %d\r\n",ulNum);
State = CONNECTED;
}
m_OracleMutex.Unlock();
return (ret);
}
/* DisConnect from ORACLE */
sword CConnection::DisConnect(void)
{
sword status;
if (State == NOT_CONNECTED)
{
return (CONERR_NOTCON);
}
m_OracleMutex.Lock();
status = ologof(&lda);
if ( status == DB_OK)
{
ulNum--;
// successful logout
State = NOT_CONNECTED;
xLogPrint(0,"DisConnect ORACLE !");
}
else
{
Display_Error();
xLogPrint(0,"DisConnect ORACLE ERROR!");
}
m_OracleMutex.Unlock();
return (status);
}
/* write error message to the given file */
void CConnection::Display_Error()
{
sword n;
unsigned char msg[512];
m_OracleMutex.Lock();
if (lda.rc != 0)
{
n = oerhms((cda_def *)&lda, lda.rc, (unsigned char*)msg, (sword) sizeof(msg));
strncpy((char*)g_szDBErrorSTR,(char*)msg,sizeof(msg));
g_nErrorCode = lda.rc;
}
m_OracleMutex.Unlock();
}
CCursor::CCursor()
{
CursorNum++;
xLogPrint(1,"CCursor !CursorNum=%d",CursorNum);
State = NOT_OPENED;
conn = (CConnection *)0;
memset(g_szDBErrorSTR,0,sizeof(g_szDBErrorSTR));
g_nErrorCode = 0;
}
/* CCursor destructor */
CCursor::~CCursor()
{
xLogPrint(1,"~cursor !CursorNum =%d",CursorNum);
CursorNum--;
if (State == OPENED)
{
if (Close())
{
xLogPrint(0,"~CCursor ORACLE ERROR!");
}
}
}
sword CCursor::ParseCursor(const text * stmt)
{
int ret = 0;
m_OracleMutex.Lock();
ret = oparse(
&cda,
(text *)stmt,
(sb4)-1,
DEFER_PARSE,
(ub4) VERSION_7
);
if ( DB_OK != ret )
{
WRITELOG(SPMAIN, LOGANDTRACE, NEXT_ALIGN,
"parse ORACLE ERROR FILE =%s ,LINE=%d \r\n",__FILE__,__LINE__) ;
Display_Error();
Close();
m_OracleMutex.Unlock();
}
m_OracleMutex.Unlock();
return ret ;
}
sword CCursor::Bind_By_Position(sword sqlvnum,
ub1 *progvar,
sword progvarlen,
sword datatype,
sword scale,
sb2 *indicator
)
{
int ret = 0 ;
m_OracleMutex.Lock();
ret = obndrn(&cda,
sqlvnum,
progvar,
progvarlen,
datatype,
scale,
indicator,
(text *)0,
-1,
-1
);
if (DB_OK != ret )
{
xLogPrint(0,"Bind_By_Position ORACLE ERROR !");
Display_Error();
Close();
}
m_OracleMutex.Unlock();
return ret ;
}
sword CCursor::Define_By_Position(
sword position,
ub1 *buf,
sword bufl,
sword datatype,
sword scale,
sb2 *indicator,
ub2 *rlen,
ub2 *rcode
)
{
int ret = 0;
m_OracleMutex.Lock();
ret = odefin(&cda,
position,
buf,
bufl,
datatype,
scale,
indicator,
(text *)0,
-1,
-1,
rlen,
rcode
);
if (DB_OK != ret)
{
xLogPrint(0,"Define_By_Position ORACLE ERROR!");
Display_Error();
Close();
}
m_OracleMutex.Unlock();
return ret;
}
sword CCursor::ExecuteSQL()
{
int ret;
m_OracleMutex.Lock();
ret = oexec(&cda);
if (DB_OK != ret)
{
xLogPrint(0,"ExecuteSQL ORACLE ERROR !");
Display_Error();
Close();
m_OracleMutex.Unlock();
return -1;
}
m_OracleMutex.Unlock();
return ret;
}
sword CCursor::FetchData()
{
int ret;
m_OracleMutex.Unlock();
ret = ofetch(&cda);
if( DB_OK != ret )
{
if (cda.rc == 1403)
{
ret = 1403;
Close();
m_OracleMutex.Unlock();
return ret;
}
if (cda.rc == 1405)
{
m_OracleMutex.Unlock();
return 0;
}
Display_Error();
xLogPrint(0,"FetchData ORACLE ERROR!");
Close();
m_OracleMutex.Unlock();
return -1;
}
m_OracleMutex.Unlock();
return ret;
}
sword CCursor::SetAutoCommit()
{
sword glstate;
if (conn == NULL)
{
xLogPrint(0,"AutoCommit ERROR! cursor don't connect,conn is NULL!");
return DB_ERROR ;
}
m_OracleMutex.Lock();
glstate = ocof(&conn->lda);
if (DB_OK != glstate)
{
Display_Error();
xLogPrint(0,"SetAutoCommit ORACLE ERROR!");
m_OracleMutex.Unlock();
return glstate;
}
m_OracleMutex.Unlock();
return DB_OK;
}
sword CCursor::AutoCommit()
{
sword glState;
if (conn == NULL)
{
xLogPrint(0,"AutoCommit ERROR! cursor don't connect,conn is NULL!");
return DB_ERROR ;
}
m_OracleMutex.Lock();
glState = ocom(&conn->lda);
if (DB_OK != glState)
{
Display_Error();
xLogPrint(0,"AutoCommit ORACLE ERROR!");
Close();
}
m_OracleMutex.Unlock();
return glState;
}
enum Conn_State
{
NOT_CONNECTED,
CONNECTED
};
/* open the cursor */
sword CCursor::OpenCursor(CConnection * Conn_Param)
{
sword status;
WRITELOG(SPMAIN, LOGANDTRACE, NEXT_ALIGN,
"OpenCursor trace FILE =%s ,LINE=%d pthread_id = %d\r\n",__FILE__,__LINE__,pthread_self()) ;
#if 0
if(conn->State == NOT_CONNECTED)
{
WRITELOG(SPMAIN, LOGANDTRACE, NEXT_ALIGN,
" OpenCursor-- ERROR ,connect no!\r\n") ;
return -1;
}
#endif
if (State == OPENED)
{
return (CURERR_ALROPN);
}
m_OracleMutex.Lock();
status = oopen(&cda,
&Conn_Param->lda,
(text *)0,
-1,
-1,
(text *)0,
-1);
WRITELOG(SPMAIN, LOGANDTRACE, NEXT_ALIGN,
"OpenCursor trace FILE =%s ,LINE=%d pthread_id = %d\r\n",__FILE__,__LINE__,pthread_self()) ;
if (DB_OK == status)
{
// successfull open
State = OPENED;
conn = Conn_Param;
}
else
{
Display_Error();
WRITELOG(SPMAIN, LOGANDTRACE, NEXT_ALIGN,
" oopen error !\r\n") ;
m_OracleMutex.Unlock();
}
WRITELOG(SPMAIN, LOGANDTRACE, NEXT_ALIGN,
"OpenCursor trace FILE =%s ,LINE=%d \r\n",__FILE__,__LINE__) ;
m_OracleMutex.Unlock();
if (DB_OK == status)
return (status);
}
/* close the cursor */
sword CCursor::Close()
{
sword ret;
if (State == NOT_OPENED )
{
// this cursor has not been opened
return (CURERR_NOTOPN);
}
m_OracleMutex.Lock();
ret = oclose(&cda);
if (ret == 0)
{
// successful cursor close
State = NOT_OPENED;
conn = (CConnection *)0;
}
else
{
Display_Error();
xLogPrint(0,"close cursor ORACLE ERROR !");
}
m_OracleMutex.Unlock();
return (ret);
}
sword CCursor::Get_Error_Code() const
{
return (cda.rc);
}
void CCursor::Display_Error()
{
sword n;
unsigned char msg[512];
m_OracleMutex.Lock();
if (cda.rc != 0)
{
n = oerhms(&conn->lda, cda.rc, (unsigned char*)msg, (sword) sizeof(msg));
strncpy((char*)g_szDBErrorSTR,(char*)msg,sizeof(msg));
g_nErrorCode = cda.rc;
}
m_OracleMutex.Unlock();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -