int CGenerateSQLFile::_CheckLine( VrtLinesConT& vrtAllLines )
{
	int							nFunRes = 0;
	VrtLinesConIterT			iterAllLine;
	std::string					strOneLineTmp;

	iterAllLine = vrtAllLines.begin();
	while ( iterAllLine != vrtAllLines.end() )
	{
		strOneLineTmp.clear();
		strOneLineTmp = *iterAllLine;

		if (!strOneLineTmp.empty()
			&&std::string::npos == strOneLineTmp.find(";")
			&& std::string::npos == strOneLineTmp.find("+")
			&& std::string::npos == strOneLineTmp.find("=")
			&& std::string::npos == strOneLineTmp.find("[")
			&& std::string::npos == strOneLineTmp.find("[[")
			&& std::string::npos == strOneLineTmp.find("AQ"))
		{
			_SysLog(SourceFLInfo, DebugError, "inVaild Line:%s", strOneLineTmp.c_str());
			nFunRes = -1;
			break;
		}//if 

		iterAllLine++;
	}//while

	return nFunRes;
}
int CGenerateSQLFile::_RemoveFile(const std::string& strFileName)
{
	int nFunRes = 0;
	std::ifstream IfstreamSrcFile;

#ifdef WIN32
	BOOL bSetRes = FALSE; 	
	IfstreamSrcFile.open(strFileName.c_str());
	if (IfstreamSrcFile.is_open())
	{
		IfstreamSrcFile.close();
		bSetRes = ::SetFileAttributesA((LPCSTR)strFileName.c_str(), FILE_ATTRIBUTE_NORMAL);			
		if (!bSetRes)
		{
			DWORD dwErr = GetLastError();
			_SysLog(SourceFLInfo, DebugError, "error! SetFileAttributesA %s, error=%d", strFileName.c_str(), dwErr);
		}
		
		//remove old files
		//strFileName exist,but remove failed, return -1
		nFunRes = remove(strFileName.c_str());
	}
	else
	{
		IfstreamSrcFile.close();
	}
#endif

	//not need to check remove res here	
	remove(strFileName.c_str());  

	return nFunRes;
}
int CGenerateSQLFile::_ReadFile( const std::string& strFileName, VrtLinesConT& vrtAllLines )
{
	int nFunRet = 0;
	std::ifstream IfstreamSrcFile;
	char* pszInBuff = NULL;
	int nFilelines = 0;	 

	if (strFileName.empty())
	{
		nFunRet = -1;
		return nFunRet;
	}

	IfstreamSrcFile.open(strFileName.c_str());
	if (IfstreamSrcFile.fail())
	{
		_SysLog(SourceFLInfo, DebugError, "error! open file error. FileName=%s", strFileName.c_str());
		nFunRet = -1;
		return nFunRet;
	}

	pszInBuff = new char[DEF_INT_MAXLINELEN];
	memset(pszInBuff, 0, DEF_INT_MAXLINELEN);

	// Read in all lines and add them to a vector
	while ( IfstreamSrcFile.getline ( pszInBuff, DEF_INT_MAXLINELEN ) )
	{
		nFilelines++;
		if (LINE_COMMONT != pszInBuff[0])
		{
			std::string strLine = pszInBuff;
			
			CUtilityFun::getInstance().removeUnVisableStr(strLine);
			CUtilityFun::getInstance().trim(strLine);

			//check line is not empty or commont line
			if (false == strLine.empty() && ';' != strLine[0])
			{
				vrtAllLines.push_back( strLine );
			}	
		}
		memset(pszInBuff, 0, DEF_INT_MAXLINELEN);
	}//while

	// close file
	if (IfstreamSrcFile.is_open())
	{
		IfstreamSrcFile.close();
	}

	if (NULL != pszInBuff)
	{
		delete []pszInBuff;
		pszInBuff = NULL;
	}
	nFunRet = 0;
	return nFunRet;
}
int CGenerateSQLFile::_ParseCmdLine(const VrtLinesConT& vecArgs)
{
	int nFunRes = 0;
	unsigned int nIndex = 0;
	std::string strDefInputFile = def_Str_Argv_inputfile;
	std::string strDefOutPath = def_Str_Argv_outpath; 
	std::string strParamName;
	std::string strParamValue;
	const char* str = NULL;

	// First arg is USUALLY the program name
	if (vecArgs.size() > 0)
	{
		if (vecArgs[0].size() > 0)
		{
			if (vecArgs[0][0] != '-')
			{
				m_strParamProgName = vecArgs[0];
				nIndex = 1;
			}
		}
	}
	
	//defualt value
	m_strParamInputFile = defSQLFileName_IN_SQLCODETXT;
	m_strParamOutpath = "./";

	// The rest should be standard parameters	
	for (; nIndex < vecArgs.size(); nIndex++)
	{
		str = vecArgs[nIndex].c_str();
		if (!_ExtractArg(str, strParamName, strParamValue))
		{
			_SysLog(SourceFLInfo, DebugError, "Err: _ExtractArg() %s", str);
			nFunRes = -1;
			return nFunRes;
		}

		if (!strParamName.empty())
		{
			if (0 == CUtilityFun::getInstance().sysStricmp(strDefInputFile.c_str(), strParamName.c_str()))
			{
				m_strParamInputFile	= strParamValue;
			}
			else if (0 == CUtilityFun::getInstance().sysStricmp(strDefOutPath.c_str(), strParamName.c_str()))
			{
				m_strParamOutpath	= strParamValue;
			}

		}

	}//for

	return nFunRes;
}
int CSQLItem::checkDuplicateSqlID()
{
    int nFunRes = 0;
    
    //check SqlID, SqlID String will not contain ' ', must contain '0123456789'
	nFunRes = _CheckSqlID();
	if (0 != nFunRes)
	{
		_SysLog(SourceFLInfo, DebugError,  "check SqlID Error, SqlID String will not contain ' ', must contain '0123456789', SqlId=%s", m_strSqlId.c_str());
		return nFunRes;
	} 
    
    
	nFunRes = CSQLDataProcessor::getInstance().checkDuplicateSqlID(m_strSqlId);
	if (0 != nFunRes)
	{
		_SysLog(SourceFLInfo, DebugError,  "checkDuplicateSqlID Error: SqlId=%s", m_strSqlId.c_str());
	}  

    return nFunRes;
}
int CGenerateSQLFile::_OpenAllOutPutFiles()
{
	int nFunRet = 0;

	m_ofstreamSQLH.open(m_strDirSqlH.c_str());
	if (m_ofstreamSQLH.fail())
	{
		_SysLog(SourceFLInfo, DebugError, "open outPut file error, fileName=%s", m_strDirSqlH.c_str());
		m_ofstreamSQLH.close();
		nFunRet = -1;
		return nFunRet;
	}

	m_ofstreamSQLMACRODEFH.open(m_strDirSqlMacrodefH.c_str());
	if (m_ofstreamSQLMACRODEFH.fail())
	{
		_SysLog(SourceFLInfo, DebugError, "open outPut file error, fileName=%s", m_strDirSqlMacrodefH.c_str());
		m_ofstreamSQLMACRODEFH.close();
		nFunRet = -1;
		return nFunRet;
	}

	return nFunRet;
}
int CGenerateSQLFile::_WriteDataToFiles()
{
	int nFunRes = 0; 
	
	nFunRes = _OpenAllOutPutFiles();
	if (0 != nFunRes)
	{
		_SysLog(SourceFLInfo, DebugError, "error! _OpenAllOutPutFiles()");	
		nFunRes = -1;
		return nFunRes;
	}
	nFunRes = _WriteTimeStrToFiles(); 	
	nFunRes = _WriteBeginToFiles();	
	CSQLDataProcessor::getInstance().writeToFileSQLH(&m_ofstreamSQLH);
	CSQLDataProcessor::getInstance().writeToFileSQLMACRODEFH(&m_ofstreamSQLMACRODEFH);
	
	nFunRes = _WriteEndToFiles();
	nFunRes = _ClossAllOutPutFiles();

	return nFunRes;	
}
std::string CGenerateSQLFile::_GetFirstLineOfFile(const std::string& strFileName)
{
	std::ifstream IfstreamSrcFile;
	char* pszInBuff = NULL;
	char* pLineEndPos = NULL;
	std::string strFirstLine;

	strFirstLine.clear();
	//get the file time of sql.h
	IfstreamSrcFile.open(strFileName.c_str());
	if (IfstreamSrcFile.fail())
	{
		_SysLog(SourceFLInfo, DebugError, "file open error! file name=%s", strFileName.c_str());
		IfstreamSrcFile.close();
		return strFirstLine;
	}

	pszInBuff = new char[DEF_INT_MAXLINELEN];
	memset(pszInBuff, 0, DEF_INT_MAXLINELEN);

	IfstreamSrcFile.getline ( pszInBuff, DEF_INT_MAXLINELEN );
	strFirstLine = pszInBuff;

	if (NULL != pszInBuff)
	{
		delete []pszInBuff;
		pszInBuff = NULL;
	}


	if (IfstreamSrcFile.is_open())
	{
		IfstreamSrcFile.close();
	}
	
	return strFirstLine;
}
int CGenerateSQLFile::_GeneralSQLFiles()
{
	int nFunRes = 0;

	nFunRes = _RemoveOldSQLFiles();
	if (0 != nFunRes)
	{
		_SysLog(SourceFLInfo, DebugError, "error! remove OldSQLFiles: %s %s",
				m_strDirSqlH.c_str(), m_strDirSqlMacrodefH.c_str());
		nFunRes = -1;
		return nFunRes;
	}

	//read file
	nFunRes =  _ReadFile(m_strDirSqlCodeTxt, m_vrtDataInFile);
	if (0 != nFunRes)
	{
		_SysLog(SourceFLInfo, DebugError, "error! _ReadAllLineFromFile()");
		nFunRes = -1;
		return nFunRes;
	}

	//check file data
	nFunRes =  _CheckLine(m_vrtDataInFile);
	if (0 != nFunRes)
	{
		_SysLog(SourceFLInfo, DebugError, "error! _CheckLine()");
		nFunRes = -1;
		return nFunRes;
	}
	
	//process data
	nFunRes =  _MutiLinesToSingleLine(m_vrtDataInFile);
	nFunRes =  _RemoveBlackLine(m_vrtDataInFile);
	
	//m_strDirSqlCodeTxt
	nFunRes = m_pSQLCodeInfo->setFileName(m_strDirSqlCodeTxt);	
	nFunRes =  m_pSQLCodeInfo->analyzeData(m_vrtDataInFile);
	if (0 != nFunRes)
	{
		_SysLog(SourceFLInfo, DebugError, "error! CSQLCodeInfo analyzeData()");
		nFunRes = -1;
		return nFunRes;
	}
	
	//check Oracle and Mysql sql Num
	nFunRes = m_pSQLCodeInfo->checkOracleAndMysqlSqlNum();
	if (0 != nFunRes)
	{
		_SysLog(SourceFLInfo, DebugError, "error! checkOracleAndMysqlSqlNum()");
		nFunRes = -1;
		return nFunRes;
	}

	nFunRes = m_pSQLCodeInfo->analyzeDataToFiles();
	if (0 != nFunRes)
	{
		_SysLog(SourceFLInfo, DebugError, "error! analyzeDataToFiles()");
		nFunRes = -1;
		return nFunRes;
	} 

	//write data to sql.h sqlmacrodef.h
	nFunRes = _WriteDataToFiles();

	//clear data
	if (NULL != m_pSQLCodeInfo)
	{
		delete m_pSQLCodeInfo;
		m_pSQLCodeInfo = NULL;
	}
	m_vrtDataInFile.clear();

	//remove file ErrorReport.log
	if (0 == nFunRes)
	{
		CLogger::getInstance().deletelogFile();
	}

	return nFunRes;	 
}
Beispiel #10
0
int CTableInfo::checkSupportDBSqlNum()
{
	int				nFunRes = 0;
	int				nListCount = 0;	
	int				nOralceSqlCount = 0;
	int				nMysqlSqlCount = 0;
	int				nSQLiteSqlCount = 0;

	std::string     strDbOracle;
	std::string     strDbMysql;
	std::string     strDbSQLite;
	std::list<CSQLType*>::iterator itemLst;
	CSQLType*        pDBInfo = NULL;
	std::string     strDBName;
	
	//contain [[oracle]] [[mysql]]
	nListCount = m_LstDBInfo.size();
	if (nListCount < 2)
	{
		nFunRes = 0;
		return nFunRes;
	}
	
	strDbOracle = DB_ORACLE;
	strDbMysql = DB_MYSQL;
	strDbSQLite = DB_SQLite;

	nOralceSqlCount = 0;
	nMysqlSqlCount = 0;
	nSQLiteSqlCount = 0;
	
	//get data
	itemLst = m_LstDBInfo.begin();
	while (itemLst != m_LstDBInfo.end())
	{
		pDBInfo = (*itemLst);
		strDBName = pDBInfo->getDBName();

		if (0 == CUtilityFun::getInstance().sysStricmp(strDBName.c_str(), strDbOracle.c_str()))
		{
			nOralceSqlCount = pDBInfo->getDataNum();
		}
		else if (0 == CUtilityFun::getInstance().sysStricmp(strDBName.c_str(), strDbMysql.c_str()))
		{
			nMysqlSqlCount =  pDBInfo->getDataNum();
		}
		else if (0 == CUtilityFun::getInstance().sysStricmp(strDBName.c_str(), strDbSQLite.c_str()))
		{
			nSQLiteSqlCount =  pDBInfo->getDataNum();
		}
	
		itemLst++;
	}//while
	
	//check
	if (nOralceSqlCount != nMysqlSqlCount)
	{
		nFunRes = -1;
		_SysLog(SourceFLInfo, DebugError, "error! nOralceSqlCount != nMysqlSqlCount TableName=%s", m_strTableName.c_str());
		return nFunRes;
	} 
	if (nOralceSqlCount != nSQLiteSqlCount)
	{
		nFunRes = -1;
		_SysLog(SourceFLInfo, DebugError, "error! nOralceSqlCount != nSQLiteSqlCount TableName=%s", m_strTableName.c_str());
		return nFunRes;
	} 
	if (nMysqlSqlCount != nSQLiteSqlCount)
	{
		nFunRes = -1;
		_SysLog(SourceFLInfo, DebugError, "error! nMysqlSqlCount != nSQLiteSqlCount TableName=%s", m_strTableName.c_str());
		return nFunRes;
	} 



	return nFunRes;
}