Ejemplo n.º 1
0
void CQueryMod::TaskEntry( StruQueryArgs *pArgs)
{
	
EnumJouErrno eRet = eJOU_R_SUCCESS;
StruQueryResult stResult;
CGSString strSql;
CGSString strTotalSql;
IConnection *pCnn = NULL;
IRecordSet *pRcdSet = NULL;
std::string strValue;
INT i, iCols;
CJouXmlMaker czXml;
std::vector<CGSString> vColNames;


	

	bzero(&stResult, sizeof(stResult) );

	stResult.iCliPmsID = pArgs->iCliID;
	stResult.iCliPmsID = pArgs->iCliPmsID;
	stResult.iCmdTag = pArgs->iCmdTag;
	stResult.pChn = pArgs->pChn;


	if( m_eStatus )
	{
		stResult.eResult = eJOU_E_NINIT;
		goto exit_func;
	}

	

	
	eRet = m_pSrv->MakeFunctionSql(strSql, pArgs->czFuncName, pArgs->czArgs );
	if( eRet )
	{
		stResult.eResult = eRet;
		goto exit_func;
	}

	//计算总条数
	
	strTotalSql = "SELECT COUNT(*) AS NROWTOTAL FROM (";

	strTotalSql += strSql;
	strTotalSql += ") TB_NTOTAL";


	pCnn  = m_pSrv->m_csDB.GetConnection();
	if( !pCnn )
	{
		MSLEEP(1000);
		pCnn = m_pSrv->m_csDB.GetConnection();
		if(!pCnn)
		{
			stResult.eResult = eJOU_E_DB_GETCONN;
			goto exit_func;
		}
	}
	pRcdSet = pCnn->ExecuteQuery(strTotalSql.c_str());
	if( pRcdSet )
	{
		if( pRcdSet->Eof() )
		{
			GS_ASSERT(0);
			stResult.eResult = eJOU_E_DB_ASSERT;
			goto exit_func;
		}
		else if( pRcdSet->GetCollect("NROWTOTAL", strValue ) && strValue.length() )
		{
				stResult.iTotals = atol( strValue.c_str() );
		}
		else
		{
			GS_ASSERT(0);
			stResult.eResult = eJOU_E_DB_ASSERT;
			goto exit_func;
		}

		pRcdSet->ReleaseRecordSet();
		pRcdSet = NULL;
	}
	else
	{
		MY_LOG_ERROR(g_pLog, "JouDB exesql: '%s' fail.\n", strTotalSql.c_str() );		
		stResult.eResult = eJOU_E_DB_EXESQL;
		goto exit_func;
	}
	
	//查询真正结果
	if( pArgs->iPageRows < 1  )
	{
		pArgs->iPageRows = 1000;
	}
	else if( pArgs->iPageRows > 100000  )
	{
		pArgs->iPageRows = 10000;
	}

	if( pArgs->iRowStart<1 )
	{
		pArgs->iRowStart = 1;
	}
	stResult.iRowStart = pArgs->iRowStart;

	pRcdSet = pCnn->ExecutePageQuery(strSql.c_str(), pArgs->iRowStart, pArgs->iPageRows  );
	if( !pRcdSet )
	{
		MY_LOG_ERROR(g_pLog, "JouDB ExecutePageQuery: '%s' fail.\n", strSql.c_str() );		
		stResult.eResult = eJOU_E_DB_EXESQL;
		goto exit_func;
	}

	//获取属性名称
	iCols = pRcdSet->GetColumnNumber();
	if( iCols<1 )
	{
		GS_ASSERT(0);
		stResult.eResult = eJOU_E_DB_ASSERT;
		goto exit_func;
	}
	
	for( i=0; i<iCols; i++ )
	{
		
		if( pRcdSet->GetCloumnName(i, strValue) )
		{
			if( strValue != "R" )
			{
				vColNames.push_back(strValue);
			}
		}
	}
	if( !czXml.Init(vColNames) )
	{
		GS_ASSERT(0);
		stResult.eResult = eJOU_E_UNKNOWN;
		goto exit_func;
	}

	for( ; !pRcdSet->Eof(); pRcdSet->MoveNext() )
	{
		if( !czXml.AddRow() )
		{
			GS_ASSERT(0);
			stResult.eResult = eJOU_E_NMEM;
			goto exit_func;
		}
		for(iCols = 0; iCols < (int) vColNames.size(); iCols++  )
		{
			if( pRcdSet->GetCollect(vColNames[iCols].c_str(), strValue) )
			{
				if(!czXml.PutRowValue(iCols, strValue ) )
				{
					GS_ASSERT(0);
					stResult.eResult = eJOU_E_UNKNOWN;
					goto exit_func;
				}
			}
			else
			{
				GS_ASSERT(0);
				stResult.eResult = eJOU_E_DB_ASSERT;
				goto exit_func;
			}
			
		}
		stResult.iRows++;
	}
	if( !czXml.SerialToXml(strValue) )
	{
		GS_ASSERT(0);
		stResult.eResult = eJOU_E_NMEM;
		goto exit_func;
	}
	stResult.iResultSize = strValue.length()+1;
	stResult.pResultData = (void*) strValue.c_str();


exit_func :
	if( pRcdSet )
	{
		pRcdSet->ReleaseRecordSet();
		pRcdSet = NULL;
	}
	if( pCnn )
	{
		pCnn->ReleaseConnection();
		pCnn = NULL;
	}
	_FreeQueryArgs(pArgs);
	if( m_fnCallback )
	{
		m_fnCallback(&stResult, m_pUserContent);
	}

}