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

📄 myodbcdbgridfile.cpp

📁 编译原理课程设计
💻 CPP
字号:
// MyODBCDBGridFile.cpp: implementation of the CMyODBCDBGridFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyODBCDBGridFile.h"
//#include "waitdlg.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


//extern CWaitDlg waitDlg;
CMyODBCDBGridFile::CMyODBCDBGridFile(CGridCtrl *pGrid, CMyODBC *pODBCDB, CString strSql,  CString strFilePath)
{
	if(pGrid == NULL)
	{
		AfxMessageBox("必须传递一个CGridCtrl对象的指针用于显示查询的数据");
		return;
	}
	this->m_pODBCDB = pODBCDB;

	m_strFilePath = strFilePath;
	m_pGrid = pGrid;
	m_hFile = NULL;
	m_hFilemap = NULL;
	m_pvFile = NULL;
	m_cpWrite = NULL;
	m_iCurrentCount = 1;

	//for select query
	m_strSql = strSql;

	m_iTotalCount = 0;

	m_pRowFileInfo = NULL;

	m_iMapFileSize = 10 * 1024 * 1024;
	m_iOnceFetchRows = 500;
	m_bConnectMode = 0;//已经联接数据库

}

CMyODBCDBGridFile::CMyODBCDBGridFile(CGridCtrl *pGrid,CString strSql, CString strServer, CString strUser, CString strPass, CString strFilePath)
{
	if(pGrid == NULL)
	{
		AfxMessageBox("必须传递一个CGridCtrl对象的指针用于显示查询的数据");
		return;
	}

	m_strFilePath = strFilePath;
	m_pGrid = pGrid;
	m_hFile = NULL;
	m_hFilemap = NULL;
	m_pvFile = NULL;
	m_cpWrite = NULL;
	m_iCurrentCount = 1;

	//for select query
	m_strSql = strSql;
	m_strUser = strUser;
	m_strPass = strPass;
	m_strServer = strServer;
	m_iTotalCount = 0;

	m_pRowFileInfo = NULL;

	m_iMapFileSize = 8 * 1024 * 1024;
	m_iOnceFetchRows = 500;
	m_bConnectMode = 1;//自己联接数据库


}

CMyODBCDBGridFile::~CMyODBCDBGridFile()
{

}

int CMyODBCDBGridFile::InitGrid()
{
	if(m_pGrid == NULL)
	{
		AfxMessageBox("发生错误,指向dbgrid控件的指针为空");
		return -1;
	}

	//connect to oracle
	if(m_bConnectMode)
	{
		m_pODBCDB = new CMyODBC;
		if(m_pODBCDB->ConnectDB((LPCSTR)m_strServer, (LPCSTR)m_strUser, (LPCSTR)m_strPass))
		{
			return -1;
		}
	}

	m_iTotalCount = GetSelectTotalCount();

	m_pGrid->SetVirtualMode(TRUE);
	m_pGrid->SetRowCount(m_iTotalCount + 1);
	m_pGrid->SetFixedRowCount(1);

	if(m_pODBCDB->PrepareSql(m_strSql, m_Set) == FALSE)
	{
		return -1;
	}
	m_pGrid->SetColumnCount(m_Set.GetCols() + 1);
	m_pGrid->SetFixedColumnCount(1);

	this->InitMapFile();
	
	return 0;

}

int CMyODBCDBGridFile::InitMapFile()
{
	int i = 0;
	m_pRowFileInfo = new SRowInfo[m_iTotalCount];
	if(m_pRowFileInfo == NULL)
	{
		AfxMessageBox("分配用于存储每行记录的信息的结构失败");
		
		return -1;
	}

	for(i = 0; i < m_iTotalCount; i++)
	{
		m_pRowFileInfo[i].bWritten = FALSE;
		m_pRowFileInfo[i].pAddress = NULL;
	}
	
	m_hFile = ::CreateFile((LPCTSTR)m_strFilePath, GENERIC_READ | GENERIC_WRITE ,
		0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	if(m_hFile == INVALID_HANDLE_VALUE)
	{
		AfxMessageBox("创建文件失败");
		return -1;
	}

	m_hFilemap = ::CreateFileMapping(m_hFile, NULL, PAGE_READWRITE, 0, m_iMapFileSize, NULL);
	if(m_hFilemap == NULL)
	{
		AfxMessageBox("创建文件映射失败");
		::CloseHandle(m_hFile);
		return -1;
	}
	::CloseHandle(m_hFile);


	m_pvFile = ::MapViewOfFile(m_hFilemap,FILE_MAP_WRITE,0,0,0);
	if(m_pvFile == NULL)
	{
		AfxMessageBox("映射文件失败");
		::CloseHandle(m_hFilemap);
		::CloseHandle(m_hFile);
		return -1;
	}
	::CloseHandle(m_hFilemap);


	m_cpWrite = (char *)m_pvFile;
	return 0;	

}

int CMyODBCDBGridFile::FetchData()
{
	int j;
	CString strField = "", strTemp = "";
	char cpTemp[2048];
	int iColumnes = m_Set.GetCols();
	int iInitCount = m_iCurrentCount;
	while((m_iCurrentCount - iInitCount) < m_iOnceFetchRows )
	{
		if(  m_pODBCDB->FetchData() == TRUE)
		{
			strTemp = "";
			for(j = 0; j < iColumnes; j++)
			{
				if(strlen(m_Set[0][j].value) == 0)
				{
					strTemp = strTemp + "null,";
				}
				else
				{
					strTemp = strTemp + m_Set[0][j].value + ",";
				}
			}
			sprintf(cpTemp, "%d=%s\r\n", m_iCurrentCount, (LPCTSTR)strTemp);
			m_pRowFileInfo[m_iCurrentCount - 1].bWritten = TRUE;
			m_pRowFileInfo[m_iCurrentCount - 1].pAddress = m_cpWrite;
				
			if(((int)(m_cpWrite - (char *)m_pvFile)) >= m_iMapFileSize - 500)
			{
			//	waitDlg.ShowWait("由于数据量很大,正在重新分配内存,请稍等....");
				return -1;
			}
			strcpy(m_cpWrite, cpTemp);
			m_cpWrite += strlen(cpTemp);
			
			m_iCurrentCount++;
		}
		else
		{
			break;
		}
	}
	*m_cpWrite = '\0';

	return 0;

}

//获得查询得到的结果集的总行数
int CMyODBCDBGridFile::GetSelectTotalCount()
{
	int iResult = 0;
	CString strSql = "select count(*) from ( "+ m_strSql + ") chensl";

	m_pODBCDB->PrepareSql((LPCTSTR)strSql, m_Set);
	m_pODBCDB->FetchData();
	iResult = atoi(m_Set[0][0].value);
	m_Set.Empty();

	return iResult;
}

int CMyODBCDBGridFile::SetGridText(GV_DISPINFO *pDispInfo)
{
	CString strTemp = "";
	if(pDispInfo->item.row == 0)//显示第一行的列名
	{
		if(pDispInfo->item.col > 0)
		{
			pDispInfo->item.strText.Format("%s", m_Set.GetColInfo(pDispInfo->item.col - 1).name);
		}
		
	}
	else if(pDispInfo->item.col == 0)//显示行号
	{
		pDispInfo->item.strText.Format(_T("%d"),pDispInfo->item.row);
	}
	else 
	{
		CString strText;
		if(GetAGridData(pDispInfo->item.row, pDispInfo->item.col, strText) == 0)
		{
			pDispInfo->item.strText = strText;
		}
		else
		{
			pDispInfo->item.strText = "";
			return -1;
			
		}
	}

	return 0;

}

int CMyODBCDBGridFile::GetAGridData(int iRow, int iCol, CString &strOut)
{
	CString strTemp, strField;
	int iCount = 1, i = 0;
	char cpToken[] = ",";
	char *cpTemp1 = NULL, *cpTemp2 = NULL;
	char cpRow[1024];


	strTemp.Format("%d=", iRow);

	while(!m_pRowFileInfo[iRow - 1].bWritten)
	{
	//	waitDlg.ShowWait("正在获取数据,请稍等....");
	
		if(FetchData() != 0)
		{
			DealMapFileSizeLess();
			return -1;
		}
	}
//	waitDlg.ShowWindow(SW_HIDE);
	cpTemp2 = strstr(m_pRowFileInfo[iRow - 1].pAddress, "\r\n");
	if(cpTemp2)
	{
		cpTemp1 = m_pRowFileInfo[iRow - 1].pAddress + strTemp.GetLength();
		strncpy(cpRow, cpTemp1, (int)(cpTemp2 - cpTemp1));
		strcpy(cpRow + (int)(cpTemp2 - cpTemp1), "\0");
	
		char *cpResult = strtok(cpRow, cpToken);
		while(cpResult != NULL)
		{
			if(iCount == iCol)
			{
				strOut.Format("%s", cpResult);
				if(strOut == "null")
				{
					strOut = "";
				}
				break;
			}
			cpResult = strtok( NULL, cpToken );
			iCount++;
		}

	}

	return 0;
}


int CMyODBCDBGridFile::Release()
{
	if(m_cpWrite != NULL)
	{
		m_cpWrite = NULL;
	}
	if(m_pRowFileInfo != NULL)
	{
		delete []m_pRowFileInfo; m_pRowFileInfo = NULL;
	}
	m_Set.Empty();
	if(m_bConnectMode == 1)
	{
		m_pODBCDB->DisConnect();
		delete m_pODBCDB; m_pODBCDB = NULL;
	}

	if(m_pvFile)
	{
		::UnmapViewOfFile(m_pvFile);
	}

	m_iCurrentCount = 1;
	return 0;

}

//

BOOL CMyODBCDBGridFile::SetMapFileSize(int iMapFileSize)
{
	m_iMapFileSize = iMapFileSize;
	return TRUE;

}

int CMyODBCDBGridFile::GetMapFileSize()
{
	return m_iMapFileSize;

}

int CMyODBCDBGridFile::DealMapFileSizeLess()
{
	m_iMapFileSize += 15 * 1024 * 1024;
	m_pGrid->DeleteAllItems();
	this->Release();
	this->InitGrid();
//	waitDlg.ShowWindow(SW_HIDE);
	return 0;

}



⌨️ 快捷键说明

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