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

📄 sqlbase.cpp

📁 PDA通讯网关服务器源码程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		//	else                            // columns
		//		length += iPrintLength + 1;

			iLength += iPrintLength;
        }		
		return iLength;              // return the length of the field
}

/***************************************************************
函数描述: 执行SQL语句(查询、插入、删除、修改、存贮过程)
函数性质: public
函数类型: int
输入参数: char		*SqlCmd		--- SQL语句		  	  
输出参数: st_Rec	*pRec		--- 记录集 
		  bool &bLeaveRecvord	--- 是否还有记录未读完(返回),以便程序组织进行读
		  char		*OutMsg		--- 错误提示信息
返回  值: 成功: 0  失败:<0
作  者: 阿义
***************************************************************/
 int CSqlDB::OnExecSql(char *SqlCmd, 
							 st_Rec *pRec,
							 bool &bLeaveRecord,
							 char *OutMsg)
 {
EnterCriticalSection(&g_DBCritical);
    bLeaveRecord = false;
 	if(dbcmd(dbproc,SqlCmd)==FAIL)
	{
LeaveCriticalSection(&g_DBCritical);
		return -1;
	}// between each command and set in
	
 	if(dbsqlexec(dbproc) == FAIL)   // execute command
 	{ 	
LeaveCriticalSection(&g_DBCritical);
		sprintf(OutMsg,"%s","执行命令错误\n\t");
		char chTmp[255] = {0};
TRACE(OutMsg);
		DBINT iDBRet = OnIsAvailConnect(chTmp);
		if (iDBRet < 0)
		{
			sprintf(OutMsg,"%s (%s)","执行命令错误", chTmp);
		}
 		return -1;
 	}
    
 	// command executed correctly, get results information
	retc = dbresults(dbproc);
	if (retc == FAIL)           // if error get out of loop
 	{
 		sprintf(OutMsg,"%s","得到结果信息错误");
LeaveCriticalSection(&g_DBCritical);
 		return -2;
 	}
 	while( retc != NO_MORE_RESULTS)
 	{ 		
 		// headers and data could be printed here with only two
 		// function calls, dbprhead(dbproc), and dbprrow(dbproc),
 		// which would output the headers, and all the data to
 		// standard output.  However, that isn't very informative
 		// toward understanding how this data is obtained and
 		// processed, so I do it the hard way, one column at a time.			
 		// OnPrintHeaders(dbproc);       // print header data			
         
 		if(pRec != NULL)
		{
			pRec->iRecLen = OnDetermineRowSize(dbproc,0);	//reclen			
			pRec->iFieldNum = dbnumcols(dbproc);          // get number of columns			
		
			pRec->FieldFmt = NULL;
			pRec->FieldFmt = new st_FdFmt[pRec->iFieldNum * sizeof(st_FdFmt)+1]; //malloc(pRec->iFieldNum * sizeof(stFieldFmt)+1);
			if (pRec->FieldFmt == NULL)
			{
				sprintf(OutMsg,"FieldFmt分配空间失败");
LeaveCriticalSection(&g_DBCritical);
				return -3;
			}
			
			for(int i=0;i<pRec->iFieldNum;i++)
			{
				int len=OnDetermineRowSize(dbproc,i+1);	//field len
				pRec->FieldFmt[i].iFieldLen=len;
			}

	        pRec->pchRecVal = NULL;			
			pRec->pchRecVal = new char[pRec->iRecLen * pRec->iRecNum +1 ];			
			if(pRec->pchRecVal== NULL)
			{
				sprintf(OutMsg,"pRecVal分配内存空间失败");
				free(pRec->FieldFmt);
LeaveCriticalSection(&g_DBCritical);
				return -4;
			}

			char chRowData[8192] = {0};;
			int iRow  =0;
			int iRecLen=pRec->iRecLen;	
			int iRet = OnGetLeaveRecord(pRec, bLeaveRecord, OutMsg);
			if (iRet != 0)
			{
LeaveCriticalSection(&g_DBCritical);
				return -5;
			}
			if (bLeaveRecord)	//还有记录未读完
			{
LeaveCriticalSection(&g_DBCritical);
				return 0;
			}			
 		}
 		else
		{
 			if (DBCOUNT(dbproc) == 1L)  // print the row count
			{
 				sprintf(OutMsg,"1 row effected");
			}
 			else
			{
 				sprintf(OutMsg,"%ld row effected",DBCOUNT(dbproc));				
			}
 		}
		retc = dbresults(dbproc);
		if (retc == FAIL)           // if error get out of loop
 		{
 			sprintf(OutMsg,"%s","Get result information error");
LeaveCriticalSection(&g_DBCritical);
			return -2;
 		}
 	}
	pRec->iRecNum = DBCOUNT(dbproc);
LeaveCriticalSection(&g_DBCritical);
 	return 0;
}

/***************************************************************
函数描述: 关闭数据库连接
函数性质: public
函数类型: void
输入参数: 无	  	  
输出参数: 无
返回  值: 成功:   失败:
作  者: 阿义
***************************************************************/
void CSqlDB::OnDBClose()
{
    dbclose(dbproc);                // quit/exit input, close connection		
}

/***************************************************************
函数描述: 释放记录集资源
函数性质: public
函数类型: void
输入参数: 无	  	  
输出参数: 无
返回  值: 成功:   失败:
作  者: 阿义
***************************************************************/
void CSqlDB::OnFreeRec(st_Rec *pRec)
{
	if ( pRec->FieldFmt != NULL )
	{		
		delete[] pRec->FieldFmt;		
		pRec->FieldFmt = NULL;
	}
	if ( pRec->pchRecVal != NULL )
	{
		delete[] pRec->pchRecVal;		
		pRec->pchRecVal = NULL;
	}
}


int CSqlDB::OnGetLeaveRecord(st_Rec *pstRecord, 
									 bool &bLeaveRecord, 
									 char *chOutMsg)
{
	char chRowData[8192] = {0};	//用于存取一行的记录
	int	 iRow		= 0;
	int  iRecLen	= 0; 
	bLeaveRecord	= false;
	iRecLen = pstRecord->iRecLen;
	memset(pstRecord->pchRecVal, 0x00, pstRecord->iRecLen * pstRecord->iRecNum);
	while((retc = dbnextrow(dbproc))!=NO_MORE_ROWS)
 	{
 		if(retc == FAIL)        // if fail, then clear
 		{                   // connection completely, just
 			dbcancel(dbproc);   // in case.
 			break;
 		}
 		else
 		{
 			memset(chRowData,'\0',sizeof(chRowData));
 			OnOutputRow(dbproc,chRowData);   // else print the current row
			int iRowDataLen = strlen(chRowData);
			if(iRowDataLen > iRecLen)
			{
				memcpy((char *)&pstRecord->pchRecVal[iRow * iRecLen], chRowData, iRecLen);
			}
			else
			{
 				memcpy((char *)&pstRecord->pchRecVal[iRow * iRecLen],chRowData, strlen(chRowData));
			}
 		}
		iRow ++;
		if (iRow == pstRecord->iRecNum)
		{
			sprintf(chOutMsg, "数据还未读完");
			bLeaveRecord = true;
			return 0;
		}
	}
	pstRecord->iRecNum = DBCOUNT(dbproc);

	return 0;
}

/***************************************************************
函数描述: 判断当前的连接句柄是否有效
函数性质: public
函数类型: int
输入参数: 无	  	  
输出参数: 无
返回  值: 成功:  0 失败: <0 (详情由pchOutMsg获得)
作  者: 阿义
***************************************************************/
int CSqlDB::OnIsAvailConnect(char *pchOutMsg)
{
	DBINT iRet =0;
	iRet = dbretstatus(dbproc);
	if (iRet <0)
	{
		switch (iRet)
		{
		case -1:
			sprintf(pchOutMsg, "***Missing object");
			break;
		case -2:
			sprintf(pchOutMsg, "***Datatype error");
			break;
		case -3:
			sprintf(pchOutMsg, "***Process was chosen as deadlock victim");
			break;
		case -4:
			sprintf(pchOutMsg, "***Permission error");
			break;
		case -5:
			sprintf(pchOutMsg, "***Syntax error");
			break;
		case -6:
			sprintf(pchOutMsg, "***Miscellaneous user error");
			break;
		case -7:
			sprintf(pchOutMsg, "***Resource error, such as out of space");
			break;
		case -8:
			sprintf(pchOutMsg, "***Nonfatal internal problem");
			break;
		case -9:
			sprintf(pchOutMsg, "***System limit was reached");
			break;
		case -10:
			sprintf(pchOutMsg, "***Fatal internal inconsistency");
			break;
		case -11:
			sprintf(pchOutMsg, "***Fatal internal inconsistency");
			break;
		case -12:
			sprintf(pchOutMsg, "***Table or index is corrupt");
			break;
		case -13:
			sprintf(pchOutMsg, "***Database is corrupt");
			break;
		case -14:
			sprintf(pchOutMsg, "***Hardware error");
			break;
		default:
			sprintf(pchOutMsg, "***无法识别的错误");
			break;
		}
	}
	return iRet;
}

⌨️ 快捷键说明

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