123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- #include "StdAfx.h"
- #include "AdoImpl.h"
- AdoImpl::AdoImpl(void)
- {
- m_pADODatabase = NULL;
- memset(m_szConnectString, 0, 1024);
- InitializeCriticalSection(&m_csAdo);
- }
- AdoImpl::~AdoImpl(void)
- {
- DeleteCriticalSection(&m_csAdo);
- }
- void AdoImpl::SetConnectString(
- IN const int &nType,
- IN LPCTSTR lpDataSource,
- IN LPCTSTR lpDatabaseTCPPort,
- IN LPCTSTR lpDatabaseAccount,
- IN LPCTSTR lpDatabasePassWord,
- IN LPCTSTR lpDatabaseName,
- IN LPCTSTR lpDatabaseFile)
- {
- switch (nType)
- {
- case ADO_ACCESS97:
- _stprintf_s(m_szConnectString, _T("Provider=Microsoft.Jet.OLEDB.3.51;Data Source=%s"), lpDatabaseFile);
- break;
- case ADO_ACCESS2000:
- _stprintf_s(m_szConnectString, _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s"), lpDatabaseFile);
- break;
- case ADO_ACCESS2007:
- _stprintf_s(m_szConnectString, _T("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%s"), lpDatabaseFile);
- break;
- case ADO_SQLSERVER:
- {
- if ( !_tcsicmp(lpDatabaseTCPPort, _T("0")))
- {
- //sprintf(m_szConnectString, "Provider=sqloledb.1;Persist Security Info=false;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; ",
- _stprintf_s(m_szConnectString, _T("Provider=sqloledb;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; "),
- lpDataSource, // IP地址或服务名称;
- lpDatabaseName,
- lpDatabaseAccount,
- lpDatabasePassWord);
- }
- else
- {
- //sprintf(m_szConnectString, "Provider=sqloledb.1;Persist Security Info=false;Data Source=%s,%s;Initial Catalog=%s;User Id=%s;Password=%s; ",
- _stprintf_s(m_szConnectString, _T("Provider=sqloledb;Data Source=%s,%s;Initial Catalog=%s;User Id=%s;Password=%s; "),
- lpDataSource, // IP地址或服务名称;
- lpDatabaseTCPPort,
- lpDatabaseName,
- lpDatabaseAccount,
- lpDatabasePassWord);
- }
- }
- break;
- case ADO_PGSQL:
- {
- _stprintf_s(m_szConnectString, _T("DRIVER={PostgreSQL ODBC Driver(UNICODE)}; SERVER=%s; port=%s; DATABASE=%s; UID=%s; PWD=%s;"),
- lpDataSource, // IP地址或服务名称;
- lpDatabaseTCPPort,
- lpDatabaseName,
- lpDatabaseAccount,
- lpDatabasePassWord);
- }
- break;
- default:
- break;
- }
- }
- BOOL AdoImpl::IsOpen()
- {
- if ( m_pADODatabase == NULL)
- return FALSE;
- return m_pADODatabase->IsOpen();
- }
- BOOL AdoImpl::Opendatabase(IN LPCTSTR lpConnectString /* = NULL */)
- {
- BOOL bResult = FALSE;
- if( NULL == m_pADODatabase )
- {
- m_pADODatabase = new CADODatabase();
- if( m_pADODatabase )
- {
- if( lpConnectString == NULL )
- {
- m_pADODatabase->SetConnectionString(m_szConnectString);
- }
- else
- {
- m_pADODatabase->SetConnectionString(lpConnectString);
- _tcscpy_s(m_szConnectString, lpConnectString);
- }
- if( TRUE == m_pADODatabase->Open() )
- {
- bResult = TRUE;
- }
- else
- {
- if( m_pADODatabase->IsOpen() )
- {
- m_pADODatabase->Close();
- }
- delete m_pADODatabase;
- m_pADODatabase = NULL;
- }
- }
- }
- return bResult;
- }
- void AdoImpl::Closedatabase()
- {
- if( NULL != m_pADODatabase )
- {
- if( m_pADODatabase->IsOpen() )
- {
- m_pADODatabase->Close();
- }
- if( m_pADODatabase )
- delete m_pADODatabase;
- m_pADODatabase = NULL;
- }
- }
- int AdoImpl::Execute(IN LPCTSTR lpSQL)
- {
- INT nRetCode = 0;
- if( NULL == m_pADODatabase )
- {
- return -1;
- }
- EnterCriticalSection(&m_csAdo);
- if ( m_pADODatabase->IsOpen() )
- {
- nRetCode = m_pADODatabase->Execute(lpSQL);
- LeaveCriticalSection(&m_csAdo);
- }
- else
- {
- LeaveCriticalSection(&m_csAdo);
- return -1;
- }
- return nRetCode;
- }
- void AdoImpl::DeleteADORecordSet(CADORecordset* pRSet)
- {
- CloseADORecordSet(pRSet);
- if( pRSet )
- {
- delete pRSet;
- pRSet = NULL;
- }
- }
- BOOL AdoImpl::OpenADORecordSet(IN CADORecordset *pRSet, IN LPCTSTR lpSQL, IN const int& nCursorLocation)
- {
- BOOL bResult = TRUE;
- if( NULL == pRSet ) return FALSE;
- if ( !pRSet->Open(lpSQL, CADORecordset::openQuery))
- {
- DeleteADORecordSet(pRSet);
- bResult = FALSE;
- }
- return bResult;
- }
- void AdoImpl::CloseADORecordSet(CADORecordset* pRSet)
- {
- if( NULL != pRSet )
- {
- if( pRSet->IsOpen() )
- {
- pRSet->Close();
- }
- }
- }
- bool AdoImpl::GetADODateBaseStatus()
- {
- if( NULL == m_pADODatabase || !m_pADODatabase->IsOpen())
- return false;
- return true;
- }
- DWORD AdoImpl::GetTableRecordCount(IN LPCTSTR lpTableName, IN LPCTSTR lpFilters)
- {
- if ( lpTableName == NULL)
- return -1;
- TCHAR szSQL[1024] = _T("");
- if(lpFilters == NULL || _tcscmp(lpFilters,_T("")) == 0)
- _stprintf_s(szSQL,_T("select count(*) as cot from %s"),lpTableName);
- else
- _stprintf_s(szSQL,_T("select count(*) as cot from %s where %s"),lpTableName,lpFilters);
- DWORD nRecordCount = 0;
- _variant_t vtFieldValue;
- BOOL bResult = FALSE;
- //AutoThreadSection aSection(&s_critSection);
- EnterCriticalSection(&m_csAdo);
- bResult = GetADODateBaseStatus();
- if( FALSE == bResult )
- {
- LeaveCriticalSection(&m_csAdo);
- return -1;
- }
- CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
- if( NULL == pRSet )
- {
- LeaveCriticalSection(&m_csAdo);
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, szSQL);
- if( FALSE == bResult )
- {
- LeaveCriticalSection(&m_csAdo);
- return -1;
- }
- if( !pRSet->IsFieldNull(_T("cot")) )
- {
- pRSet->GetFieldValue(_T("cot"), nRecordCount);
- }
- else
- {
- nRecordCount = -1;
- }// end if( !pRSet->IsFieldNull(0) )
- DeleteADORecordSet(pRSet);
- LeaveCriticalSection(&m_csAdo);
- return nRecordCount;
- }
- /************************************************************************/
- /* 函数:GetFieldValueList[4/12/2016 IT];
- /* 描述:获取指定表查询字段的条件记录集;
- /* 参数:;
- /* [IN] lpTableName:要查询的表;
- /* [IN] lpFields:要查询的字段;
- /* [IN] lpFilters:查询的条件;
- /* [IN] dwCallBackType:回调函数类型;
- /* [IN] lpCallBack:回调函数指针;
- /* [INOUT] lpParamter:传递给回调函数的参数;
- /* 返回:成功查询返回记录数, 参数错误返回-1;
- /* 注意:不使用select count(*) as cot from table来获取记录数;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int AdoImpl::GetFieldValueList(IN LPCTSTR lpTableName, IN LPCTSTR lpFields, IN LPCTSTR lpFilters, IN const DWORD &dwCallBackType, IN LPVOID lpCallBack, IN OUT LPVOID lpParamter)
- {
- if ( lpCallBack == NULL || lpParamter == NULL )
- return -1;
- if ( lpTableName == NULL || lpTableName[0] == _T('\0') || lpFields == NULL || lpFields[0] == _T('\0'))
- return -1;
- int nRecordCount = 0;
- BOOL bResult = FALSE;
- TCHAR szSQL[2048] = _T(""); // 长度可能会不够,_stprintf_s结果导致SQL语句不正确;
- EnterCriticalSection( &m_csAdo );
- bResult = GetADODateBaseStatus();
- if( FALSE == bResult ){
- LeaveCriticalSection( &m_csAdo );
- return -1;
- }
- // 获取查询记录集;
- CADORecordset *pRSet = new CADORecordset(m_pADODatabase);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- return -1;
- if ( lpFilters && lpFilters[0] != _T('\0') )
- _stprintf_s(szSQL, _T("select %s from %s where %s"), lpFields, lpTableName, lpFilters);
- else
- _stprintf_s(szSQL, _T("select %s from %s"), lpFields, lpTableName);
- bResult = OpenADORecordSet(pRSet, szSQL);
- if( FALSE == bResult )
- {
- return -1;
- }
- // 让回调函数处理返回的数据集结果;
- nRecordCount = ((RecordsetCallback)lpCallBack)(dwCallBackType, lpParamter, 0, pRSet);
- DeleteADORecordSet(pRSet);
- return nRecordCount;
- }
- /************************************************************************/
- /* 函数:GetFieldValueList[4/12/2016 IT];
- /* 描述:获取指定表查询字段的条件记录集;
- /* 参数:;
- /* [IN] lpTableName:要查询的表;
- /* [IN] lpFields:要查询的字段;
- /* [IN] lpFilters:查询的条件;
- /* [IN] dwCallBackType:回调函数类型;
- /* [IN] lpCallBack:回调函数指针;
- /* [INOUT] lpParamter:传递给回调函数的参数;
- /* 返回:成功查询返回记录数, 参数错误返回-1;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int AdoImpl::GetFieldValueListEx(IN LPCTSTR lpTableName, IN LPCTSTR lpFields, IN LPCTSTR lpFilters, IN const DWORD &dwCallBackType, IN LPVOID lpCallBack, IN OUT LPVOID lpParamter)
- {
- if ( lpCallBack == NULL || lpParamter == NULL )
- return -1;
- if ( lpTableName == NULL || lpTableName[0] == _T('\0') || lpFields == NULL || lpFields[0] == _T('\0'))
- return -1;
- int nRecordCount = 0;
- BOOL bResult = FALSE;
- TCHAR szSQL[2048] = _T(""); // 长度可能会不够,_stprintf_s结果导致SQL语句不正确;
- if ( lpFilters && lpFilters[0] != _T('\0') )
- {
- _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), lpTableName, lpFilters);
- }
- else
- {
- _stprintf_s(szSQL, _T("select count(*) as cot from %s"), lpTableName);
- }
- EnterCriticalSection( &m_csAdo );
- bResult = GetADODateBaseStatus();
- if( FALSE == bResult ){
- LeaveCriticalSection( &m_csAdo );
- return -1;
- }
- // 获取记录数;
- CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- return -1;
- bResult = OpenADORecordSet(pRSet, szSQL);
- if( FALSE == bResult ){
- return -1;
- }
- if( !pRSet->IsFieldNull(_T("cot")) )
- {
- pRSet->GetFieldValue(_T("cot"), nRecordCount);
- }
- else
- {
- nRecordCount = -1;
- // 没有记录则返回;
- DeleteADORecordSet(pRSet);
- return -1;
- }
- DeleteADORecordSet(pRSet);
- // 获取查询记录集;
- pRSet = new CADORecordset(m_pADODatabase);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- return -1;
- if ( lpFilters && lpFilters[0] != _T('\0') )
- _stprintf_s(szSQL, _T("select %s from %s where %s"), lpFields, lpTableName, lpFilters);
- else
- _stprintf_s(szSQL, _T("select %s from %s"), lpFields, lpTableName);
- bResult = OpenADORecordSet(pRSet, szSQL);
- if( FALSE == bResult )
- {
- return -1;
- }
- // 让回调函数处理返回的数据集结果;
- nRecordCount = ((RecordsetCallback)lpCallBack)(dwCallBackType, lpParamter, nRecordCount, pRSet);
- DeleteADORecordSet(pRSet);
- return nRecordCount;
- }
- BOOL AdoImpl::AppenChunkFromFile(IN LPCTSTR lpTableName, IN LPCTSTR lpFieldName, IN LPCTSTR lpFileName)
- {
- if ( lpTableName == NULL ||
- lpTableName[0] == _T('\0') ||
- lpFieldName == NULL ||
- lpFieldName[0] == _T('\0') ||
- lpFileName == NULL ||
- !PathFileExists(lpFileName) )
- return FALSE;
- CFile cf;
- BYTE* pBuffer = NULL;
- if ( !cf.Open(lpFileName, CFile::modeRead) )
- return FALSE;
- DWORD dwLength = cf.GetLength();
- pBuffer = new BYTE[dwLength+1];
- memset(pBuffer, 0, dwLength + 1);
- cf.Read(pBuffer, dwLength);
- cf.Close();
- BOOL bResult = AppenChunkFromBuffer(lpTableName, lpFieldName, pBuffer, dwLength);
- if ( pBuffer )
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- return bResult;
- }
- /************************************************************************/
- /* 函数:AppenChunkFromBuffer[4/13/2016 IT];
- /* 描述:从指定的缓冲区里把二进制数据写入字段中;
- /* 参数:;
- /* [IN] lpTableName:表名;
- /* [IN] lpFieldName:字段名;
- /* [IN] lpBuffer:缓冲区数据;
- /* [IN] nBuflen:缓冲区大小;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- BOOL AdoImpl::AppenChunkFromBuffer(IN LPCTSTR lpTableName, IN LPCTSTR lpFieldName, IN BYTE* lpBuffer, IN const int& nBuflen)
- {
- if ( lpTableName == NULL ||
- lpTableName[0] == _T('\0') ||
- lpFieldName == NULL ||
- lpFieldName[0] == _T('\0') ||
- lpBuffer == NULL )
- return FALSE;
- EnterCriticalSection( &m_csAdo );
- BOOL bResult = GetADODateBaseStatus();
- if( FALSE == bResult ){
- LeaveCriticalSection( &m_csAdo );
- return FALSE;
- }
- CADORecordset *pRSet = new CADORecordset(m_pADODatabase);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- return -1;
- TCHAR szSQL[2048] = {0};
- _stprintf_s(szSQL, _T("select %s from %s"), lpFieldName, lpTableName);
- // 写二进制大文件时,_RecordSetPtr必须以adUseSever方式打开,则否更新不成功;
- bResult = OpenADORecordSet(pRSet, szSQL, adUseServer);
- if( FALSE == bResult )
- {
- return FALSE;
- }
- pRSet->Edit();
- bResult = pRSet->AppendChunk(lpFieldName, lpBuffer, nBuflen);
- if ( bResult )
- {
- pRSet->Update();
- }
- DeleteADORecordSet(pRSet);
- return bResult;
- }
|