⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbsmgw.cpp

📁 电信的97接口程序,用于把话单入库。这里是采用FTP方式采集话单
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*+============================================================================
File         dbsmgw.cpp
Summary      This file includes the DBMS interface application functions
History:
2000/05/27	V1.0
	Start it by shencan@263.net
2001/11/13
	Add bcp for insert data by zoohoo@163.com
2001/12/25
	Add new class for interface97 
	Add the method of shared directory by zoohoo@163.com
2001/12/28
	Add the license control for interface97 by zoohoo@163.com
2002/01/18
	Add the Rollback method.
	Add the LinkAcct control by zoohoo@163.com 
2002/01/29
	Add the examination for disconnect database by zoohoo@163.com
2002/01/30
	Add the examination for UserType changing by zoohoo@163.com.
	Can't change UserType from one to the other.
============================================================================+*/


#include "dbsmgw.h"
#include "fstream.h"
#include <time.h>

const int maxAccountNum = 0;	// 0 means no strict!

LOGINREC * login;
PDBPROCESS * dbConnect;		// database connection pool!
int * connectLock;
int totalConnect;			// Current database connect number
int serverNo;				// Current database server number
static PMutex dbMutex;		// Lock of db connect
static PMutex connectMutex;	// Serialize db connect

// count of auth continue fail and account fail!
int authFailContinue = 0;
int acctFailContinue = 0;

static char * userPassword = "66666"; // the initial password of users
static char * cTSI_Service = "65"; // the service of ctsi
static char * errLogFile = "errExec.log";

CInterfaceContent * interfaceContent;

#define RAD_MAX_ALTER_DB	3
#define MAX_BCP_ROWS		500 //the rows number for bcp_batch once
#define DB_DAY_DIFFER		25567
#define BCP_FAILFILE		"bcpAdsiFail.txt"
#define BCP_ERRFILE			"bcpAdsiErr.txt"
#define MAX_ROW_IN_MEM		2000 //max rows in memory
#define DB_CONNECT_TIME		300	 //The seconds value for connect timeout



class SqlString {
public:
	char str[SQL_SIZE];
};


//////////////////////////////////////////////////////////////////////////
// Common Database connection manage functions
//////////////////////////////////////////////////////////////////////////

// login DBMS
int DbLogin()
{
	int i;
	int success = 1;

	RTRACE(2, "\n------------------------------Database initial : " << dbinit() << "------------------------------\n" );

	// Install user-supplied error- and message-handling functions.
	dberrhandle (DbErrorHandler);
	dbmsghandle (DbMsgHandler);

	// alloc db connect pool memory
	totalConnect = 1;
	dbConnect = new PDBPROCESS[totalConnect];

	connectLock = new int[totalConnect];
	for ( i = 0; i<totalConnect; i++)
		dbConnect[i] = NULL;

	// alloc login memory
	login = dblogin();
	//BCP_SETL(login, TRUE);
	DBSETLUSER(login, CGlobal::Instance()->m_dbAccount);
	DBSETLPWD(login, CGlobal::Instance()->m_dbPassword);
	DBSETLAPP(login, "Short Message Gateway");
	//DBSETLTIME(login, DB_CONNECT_TIME);
	dbsetlogintime (DB_CONNECT_TIME);

	// get connections
	for ( i = 0; i<totalConnect; i++) {
		dbConnect[i] = DbConnect();
		if ( dbConnect[i] == NULL )
			break;
		connectLock[i] = 0;	// all of connect is free!
	}

	if ( i != totalConnect ) {
		DbFreeResource();	// try next database server
	}

	return 0;
}


// Free database resource
int DbFreeResource()
{
	for ( int i = 0; i<totalConnect; i++)
		DbConnectOff(dbConnect[i]);

#ifdef MSSQL
	dbfreelogin (login);
#else
	dbloginfree (login);
#endif /* MSSQL */

	delete [] dbConnect;
	delete [] connectLock;
	return 0;
}


// logoff DBMS
int DbLogoff()
{
	DbFreeResource();
	dbexit();
	return 0;
}


// relogin DBMS
int DbRelogin()
{
	DbLogoff();

	// Log on the database.
	if ( DbLogin() == -1 ) {
		RTRACE(1, "Database login error");
		return -1;
	}
	return 0;
}


PDBPROCESS DbConnect()
{
	PDBPROCESS dbproc;

	RTRACE(3, "Database connect : " << CGlobal::Instance()->m_dbServer);
	dbproc = dbopen(login, CGlobal::Instance()->m_dbServer);
	RTRACE(5, "Attemp to open database " << CGlobal::Instance()->m_dbName);

	if( dbproc == NULL ) {
		return NULL;
	}

	dbuse (dbproc, CGlobal::Instance()->m_dbName);
	if ( dbsettime(DB_TIMEOUT) == FAIL )
		RTRACE(2, "Set db timeout fail");

	return dbproc;
}


int DbConnectOff(PDBPROCESS dbproc)
{
	if ( dbproc != NULL )
		dbclose ( dbproc );
	return 0;
}


int GetConnectSub(PDBPROCESS dbproc)
{
	int i;
	for( i=0; i<totalConnect; i++) {
		if ( dbConnect[i] == dbproc ) {
			return i;
		}
	}
	return -1;
}


// As database will fail if network fail, so we write a relogin function.
int DbReconnect(PDBPROCESS dbproc)
{
	// get connect subscript
	int sub = GetConnectSub(dbproc);
	if ( sub == -1 ) 
	{
		RTRACE(3, "Warn, Get database connect subscript error");
		return -1;
	}

	// reconnect this connection
	if(dbproc != NULL)
		dbclose ( dbproc );

	if( ( dbConnect[sub] = dbopen(login, CGlobal::Instance()->m_dbServer)) != NULL )
	{
		dbuse (dbConnect[sub], CGlobal::Instance()->m_dbName);
		if(interfaceContent != NULL)
			dbproc = dbConnect[sub];
		return 0;
	}

	// Now, we try the second and third database server.
	return ConnectAlterDatabase();
}


// As database will fail if network fail, so we write a relogin function.
int DbReconnect(int n)
{
	// reconnect this connection
	if(dbConnect[n] != NULL)
		dbclose ( dbConnect[n] );
	dbsetlogintime (15);	// set login time to 15s
	if( ( dbConnect[n] = dbopen(login, CGlobal::Instance()->m_dbServer) ) != NULL )
	{
		dbuse (dbConnect[n], CGlobal::Instance()->m_dbName);
		return 0;
	}
	
	return ConnectAlterDatabase();
}

int ConnectAlterDatabase()
{
	return -2;
}


#ifdef MSSQL

int DbErrorHandler(PDBPROCESS dbproc, int severity, int dberr,
			int oserr, const char * dberrstr, const char * oserrstr)
{
	if ( oserr != DBNOERR )
	{
		RTRACE(2, "Operating System Error " << oserr <<": " << oserrstr);
	}
	if ( severity >= EXRESOURCE /*|| dberr == 10007*/ ) // 3/4 maybe dbprocess dead
	{
		RTRACE(2, "DB-Library Error " << dberr << ": " << dberrstr);
		RTRACE(2, "severity=" << severity << ", 3/4 maybe dbprocess dead");
	}
	else
		RTRACE(2, "DB-Library Error " << dberr << ": " << dberrstr);

	if ( dberr == 10038 ) {
		dberrhandle(DbErrorNULLHandler);
		DbReconnect(dbproc);
		dberrhandle(DbErrorHandler);
	}

    return (INT_CANCEL);
}


int DbMsgHandler(PDBPROCESS dbproc, DBINT msgno, int msgstate,
				 int severity, const char * msgtext, const char * server,
				 const char * procedure, DBUSMALLINT line)
{
	RTRACE(4, "SQL Server Message " << msgno << ": " << msgtext);
    return (0);
}


int DbErrorNULLHandler(PDBPROCESS dbproc, int severity, int dberr,
			int oserr, const char * dberrstr, const char * oserrstr)
{
	RTRACE(2, "DB-Library Error(NULL) " << dberr << ": " << dberrstr << " severity=" << severity);
    return (INT_CANCEL);
}

#else /* SYBASE */
int __stdcall DbErrorHandler(PDBPROCESS dbproc, int severity, int dberr,
			int oserr, char * dberrstr, char * oserrstr)
{
//	void * p = dbprocerrhandle(dbproc, NULL);	// always fail???

	if ( oserr != DBNOERR )
	{
		if(oserrstr != NULL)
			RTRACE(2, "Operating System Error " << oserr <<": " << oserrstr);
	}
	if ( severity >= EXRESOURCE ) // 3/4 maybe dbprocess dead
	{
		RTRACE(2, "DB-Library Error " << dberr << ": " << dberrstr);
		RTRACE(2, "severity=" << severity << ", 3/4 maybe dbprocess dead");
		//DogClient::Instance()->SendMsg(e_fatalError, "Database error");
	    //return (INT_EXIT);
	}
	else
		RTRACE(2, "DB-Library Error " << dberr << ": " << dberrstr);

	if ( dberr == 20004 || dberr == 20047 || dberr == 20109) {
		dberrhandle(DbErrorNULLHandler);
		dberrhandle(DbErrorHandler);
	}

    return (INT_CANCEL);
}


int __stdcall DbMsgHandler(PDBPROCESS dbproc, DBINT msgno, int msgstate,
				 int severity, char * msgtext, char * server,
				 char * procedure, /*DBUSMALLINT*/int line)
{
	RTRACE(4, "SQL Server Message " << msgno << ": " << msgtext);
    return (0);
}


int __stdcall  DbErrorNULLHandler(PDBPROCESS dbproc, int severity, int dberr,
			int oserr, char * dberrstr, char * oserrstr)
{
	RTRACE(2, "DB-Library Error(NULL) " << dberr << ": " << dberrstr << " severity=" << severity);
    return (INT_CANCEL);
}

#endif /* MSSQL */


int GetConnect()
{
	if ( totalConnect <= 0 )
		return -1;

	return 0;
}


DBPROCESS * GetConnectPointer()
{
	int connection = GetConnect();
	if ( connection == -1 )
		return NULL;

	RTRACE(6, "Use DB connection handle : " << connection );
	return dbConnect[connection];
}


int FreeConnect(DBPROCESS * connect)
{
	int sub = GetConnectSub(connect);
	if ( sub == -1 ) {
		RTRACE(3, "Warn, Get database connect subscript error");
		return -1;
	}

	connectLock[sub] = 0;
	return 0;
}


int FreeConnect(int i)
{
	if ( i >= totalConnect )
		return -1;

	connectLock[i] = 0;
	return 0;
}

//////////////////////////////////////////////////////////////////////////
// class CInterfaceContent
//////////////////////////////////////////////////////////////////////////

CInterfaceContent::CInterfaceContent(int connection)
{
	m_connection = connection;
	DbConnectGuard dbGuard(m_connection);
	m_dbproc = dbConnect[m_connection];
	m_license = -1;
	m_userNum = -1;
	m_userStatus = 0;
}

CInterfaceContent::~CInterfaceContent()
{
}

int CInterfaceContent::GetValue(UserInterface *userInterface)
{
	RTRACE(6, "Enter GetValue");
	RTRACE(3, "Users' Operation begin");

	// UserType not Exists
	if(userInterface->userType[0] < 'A' ||
		userInterface->userType[0] > 'h' ||
		(userInterface->userType[0] > 'H' &&
		userInterface->userType[0] < 'a'))
		return -3;

	// Judge formation of endtime(YYYY-MM-DD HH:MI:SS)
	if(JudgeTimeType(userInterface->endTime, 
			sizeof(userInterface->endTime)) == -1)
		return -8;
	
	strncpy(m_serviceId, userInterface->serviceId, sizeof(m_serviceId));
	strncpy(m_userGroup, userInterface->userGroup, sizeof(m_userGroup));
	strncpy(m_areaNo, userInterface->areaNo, sizeof(m_areaNo));
	strncpy(m_teleNumber, userInterface->teleNumber, sizeof(m_teleNumber));
	strncpy(m_oldTeleNum, userInterface->oldTeleNum, sizeof(m_oldTeleNum));
	m_operateType = userInterface->operateType;
	strncpy(m_genTime, userInterface->genTime, sizeof(m_genTime));
	strncpy(m_sendTime, userInterface->sendTime, sizeof(m_sendTime));
	strncpy(m_backTime, userInterface->backTime, sizeof(m_backTime));
	strncpy(m_userType, userInterface->userType, sizeof(m_userType));
	strncpy(m_password, userInterface->password, sizeof(m_password));
	strncpy(m_endTime, userInterface->endTime, sizeof(m_endTime));

	m_remainTime = userInterface->remainTime;
	strncpy(m_aCLICP, userInterface->aCLICP, sizeof(m_aCLICP));
	strncpy(m_aCLContent, userInterface->aCLContent, sizeof(m_aCLContent));
	m_sendRecv = userInterface->sendRecv;
	strncpy(m_addString, userInterface->addString, sizeof(m_addString));

	m_isBackSheet = userInterface->isBackSheet; //add by zjl for ShamhHai 97

	strncpy(m_groupList, userInterface->groupList, sizeof(m_groupList));
	m_protocolType = atoi(userInterface->protocolType);
    m_userSource = userInterface->userSource;

	if(GetUserGroup(m_userGroup) != 0)
		return -6;
	if (CGlobal::Instance()->m_areacode == 3)
		return OperateDb_SH();
	else
		return OperateDb();
}

int CInterfaceContent::GetUserStatusFromTable(char* userAccounts)
{
	RTRACE(6, "Step into CInterfaceContent::GetUserStatusFromTable");
	char sql[SQL_SIZE];
	strcpy(sql, "select UserStatus, GroupList from UserAcct where UserAccounts = '");
	strcat(sql, userAccounts);
	strcat(sql, "'");
	// Run sql
	RTRACE(3, sql);
	dbclrbuf(m_dbproc, sizeof(sql));
	dbcmd(m_dbproc, sql);
	if (dbsqlexec(m_dbproc) != SUCCEED )
	{
		RTRACE(3, "Record UserAcct record fail");
		while ( (m_retRow = dbnextrow(m_dbproc)) != NO_MORE_ROWS )
			if ( m_retRow == FAIL )
				break;
		m_userStatus = 0;
		dbcancel(m_dbproc);
		return -1;
	}
	dbresults(m_dbproc);
	DBINT userStatus;
	dbbind(m_dbproc, 1, INTBIND, (DBINT) 0, (BYTE *)&userStatus);
    dbbind(m_dbproc, 2, NTBSTRINGBIND, sizeof(m_preGroupList), (unsigned char * )m_preGroupList );
	if (dbnextrow(m_dbproc) != NO_MORE_ROWS)
	{
		m_userStatus = (int)userStatus;
	}
	else
	{
		RTRACE(5, "NO_MORE_ROWS " << m_inheritGroup);
		m_userStatus = 0;
		return -1;
	}

	return 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -