123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- #include "StdAfx.h"
- #include "AdoImplSvr.h"
- #include "DataDef.h"
- AdoImpl::AdoImpl(void)
- {
- m_pADODatabase = NULL;
- memset(m_szConnectString,0,1024);
- InitializeCriticalSection(&m_csAdo);
- }
- AdoImpl::~AdoImpl(void)
- {
- DeleteCriticalSection(&m_csAdo);
- closedatabase();
- }
- void AdoImpl::SetConnectString(IN const int &nType,IN const STDatabaseInfo &tagSTDatabaseInfo)
- {
- switch (nType)
- {
- case ADO_ACCESS97:
- _stprintf(m_szConnectString, _T("Provider=Microsoft.Jet.OLEDB.3.51;Data Source=%s"), tagSTDatabaseInfo.szDatabaseFile);
- break;
- case ADO_ACCESS2000:
- _stprintf(m_szConnectString, _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s"), tagSTDatabaseInfo.szDatabaseFile);
- break;
- case ADO_ACCESS2007:
- _stprintf(m_szConnectString, _T("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%s"), tagSTDatabaseInfo.szDatabaseFile);
- break;
- case ADO_SQLSERVER:
- {
- if ( !_tcsicmp(tagSTDatabaseInfo.szDatabaseTCPPort, _T("0")))
- {
- //sprintf(m_szConnectString, "Provider=sqloledb.1;Persist Security Info=false;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; ",
- _stprintf(m_szConnectString, _T("Provider=sqloledb;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; "),
- tagSTDatabaseInfo.szDataSource, // IP地址或服务名称;
- tagSTDatabaseInfo.szDatabaseName,
- tagSTDatabaseInfo.szDatabaseAccount,
- tagSTDatabaseInfo.szDatabasePassword);
- }
- else
- {
- //sprintf(m_szConnectString, "Provider=sqloledb.1;Persist Security Info=false;Data Source=%s,%s;Initial Catalog=%s;User Id=%s;Password=%s; ",
- _stprintf(m_szConnectString, _T("Provider=sqloledb;Data Source=%s,%s;Initial Catalog=%s;User Id=%s;Password=%s; "),
- tagSTDatabaseInfo.szDataSource, // IP地址或服务名称;
- tagSTDatabaseInfo.szDatabaseTCPPort,
- tagSTDatabaseInfo.szDatabaseName,
- tagSTDatabaseInfo.szDatabaseAccount,
- tagSTDatabaseInfo.szDatabasePassword);
- }
- }
- break;
- case ADO_PGSQL:
- {
- _stprintf(m_szConnectString, _T("DRIVER={PostgreSQL ODBC Driver(UNICODE)}; SERVER=%s; port=%s; DATABASE=%s; UID=%s; PWD=%s;"),
- tagSTDatabaseInfo.szDataSource, // IP地址或服务名称;
- tagSTDatabaseInfo.szDatabaseTCPPort,
- tagSTDatabaseInfo.szDatabaseName,
- tagSTDatabaseInfo.szDatabaseAccount,
- tagSTDatabaseInfo.szDatabasePassword);
- }
- break;
- default:
- break;
- }
- }
- bool AdoImpl::opendatabase(IN const TCHAR *pConnectString)
- {
- bool bResult = false;
- if( NULL == m_pADODatabase )
- {
- m_pADODatabase = new CADODatabase();
- if( m_pADODatabase )
- {
- if( pConnectString == NULL )
- m_pADODatabase->SetConnectionString(m_szConnectString);
- else
- {
- m_pADODatabase->SetConnectionString(pConnectString);
- _tcscpy_s(m_szConnectString, pConnectString);
- }
- if( TRUE == m_pADODatabase->Open() )
- {
- //LOG4C((LOG_NOTICE, "Database Connetion Success!"));
- bResult = true;
- }
- else
- {
- if( m_pADODatabase->IsOpen() )
- m_pADODatabase->Close();
- delete m_pADODatabase;
- m_pADODatabase = NULL;
- //LOG4C((LOG_NOTICE, "Database Connetion Lost!"));
- }
- }
- }
- 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;
- }
- }
- void AdoImpl::dump_com_error(_com_error &e)
- {
- CString ErrorStr;
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- ErrorStr.Format(_T("CADORecordset Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n"),
- e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription );
- m_strLastError = _T("Query = " + GetQuery() + '\n' + ErrorStr);
- m_dwLastError = e.Error();
- #ifdef _DEBUG
- AfxMessageBox( ErrorStr, MB_OK | MB_ICONERROR );
- #endif
- }
- int AdoImpl::Execute(TCHAR *pSQL)
- {
- INT iRetCode = 0;
- if( NULL == m_pADODatabase )
- return -1;
- try
- {
- EnterCriticalSection(&m_csAdo);
- if ( m_pADODatabase->IsOpen() )
- {
- iRetCode = m_pADODatabase->Execute(pSQL);
- LeaveCriticalSection(&m_csAdo);
- }
- else
- {
- LeaveCriticalSection(&m_csAdo);
- return -1;
- }
- }
- catch (_com_error &e)
- {
- dump_com_error(e);
- return -1;
- }
- return iRetCode;
- }
- void AdoImpl::DeleteADORecordSet(CADORecordset* pRSet)
- {
- CloseADORecordSet(pRSet);
- if( pRSet )
- {
- delete pRSet;
- pRSet = NULL;
- }
- }
- bool AdoImpl::OpenADORecordSet(CADORecordset* pRSet, const TCHAR *pSQL)
- {
- int iRetCode;
- bool bResult = true;
- if( NULL == pRSet ) return false;
- try
- {
- iRetCode = pRSet->Open(pSQL, CADORecordset::openQuery);
- if (iRetCode == 0)
- {
- DeleteADORecordSet(pRSet);
- bResult = false;
- }
- }
- catch (_com_error &e)
- {
- dump_com_error(e);
- //LOG4C( ( LOG_ERROR, "Execute SQL Script Error, Script:%s, ErrorMsg:%s", pSql, e.ErrorMessage() ) );
- 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);
- //LOG4C((LOG_NOTICE, "GetFieldValueList1 pRSet == NULL"));
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, szSQL);
- if( false == bResult )
- {
- LeaveCriticalSection(&m_csAdo);
- //LOG4C((LOG_NOTICE, "GetFieldValueList1 OpenADORecordSet <%s>", pSql));
- DeleteADORecordSet(pRSet);
- return -1;
- }
- if( !pRSet->IsFieldNull(_T("cot")) )
- pRSet->GetFieldValue(_T("cot"), nRecordCount);
- else
- nRecordCount = -1;
- DeleteADORecordSet(pRSet);
- LeaveCriticalSection(&m_csAdo);
- return nRecordCount;
- }
- int AdoImpl::GetFieldValueList(IN const TCHAR *pTableName,__in const TCHAR *pFilters, const TCHAR *pSql, INT &nFieldCount, vector<TString>& vtRet)
- {
- TCHAR szSQL[1024] = _T("");
- _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), pTableName, pFilters);
- int nRecordCount = 0;
- _variant_t vtFieldValue;
- bool bResult = false;
- 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 )
- {
- //LOG4C((LOG_NOTICE, "GetFieldValueList1 pRSet == NULL"));
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, szSQL);
- if( false == bResult )
- {
- DeleteADORecordSet(pRSet);
- return -1;
- }
- if( !pRSet->IsFieldNull(_T("cot")) )
- pRSet->GetFieldValue(_T("cot"), nRecordCount);
- else
- nRecordCount = -1;
- DeleteADORecordSet(pRSet);
- pRSet = new CADORecordset(m_pADODatabase);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- {
- //LOG4C((LOG_NOTICE, "GetFieldValueList1 pRSet == NULL"));
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, pSql);
- if( false == bResult )
- {
- DeleteADORecordSet(pRSet);
- return -1;
- }
- nFieldCount = pRSet->GetFieldCount();
- for( int i = 0; i < nRecordCount*nFieldCount; i++ )
- {
- if( !pRSet->IsFieldNull(i%nFieldCount) )
- {
- pRSet->GetFieldValue(i%nFieldCount, vtFieldValue);
- TString strFieldValue = TString((TCHAR*)(_bstr_t)vtFieldValue);
- vtRet.push_back(strFieldValue);
- }
- else
- {
- vtRet.insert(vtRet.end(), _T(""));
- }
- if ((i != 0) && (i%nFieldCount == 0))
- pRSet->MoveNext();
- }
- DeleteADORecordSet(pRSet);
- return nRecordCount;
- }
- /*-------------------------------------------------------------------------------*/
- /*
- 函数名: GetFieldValueList
- 说明: 获取调查记录
- 参数:
- __in const TCHAR *pTableName, 表名
- __in const TCHAR *pSql, sql语句
- __out int &nFieldCount, 返回查询数量
- __out CArray<CStringArray,CStringArray>& strArray 返回数据集
- 返回: 返回数量,失败=-1
- */
- /*-------------------------------------------------------------------------------*/
- int AdoImpl::GetFieldValueList(__in const TCHAR *pTableName, __in const TCHAR *pSql, __out int &nFieldCount, __out CArray<CStringArray,CStringArray>& strArray)
- {
- int nRecordCount = 0;
- _variant_t vtFieldValue;
- bool bResult = false;
- 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 )
- {
- //LOG4C((LOG_NOTICE, "GetFieldValueList1 pRSet == NULL"));
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, pSql);
- if( false == bResult )
- {
- DeleteADORecordSet(pRSet);
- return -1;
- }
- nRecordCount = pRSet->GetRecordCount();
- nFieldCount = pRSet->GetFieldCount();
- strArray.SetSize(nRecordCount,1);
- for( int i = 0; i < nRecordCount; i++ )
- {
- for ( int n = 0; n < nFieldCount; n++)
- {
- if( !pRSet->IsFieldNull(n) )
- {
- pRSet->GetFieldValue(n, vtFieldValue);
- TString strFieldValue = TString((TCHAR*)(_bstr_t)vtFieldValue);
- strArray.ElementAt(i).Add(strFieldValue.c_str());
- }
- else
- {
- strArray.ElementAt(i).Add(_T(""));
- }
- }
- pRSet->MoveNext();
- }
- DeleteADORecordSet(pRSet);
- return nRecordCount;
- }
- /////////////////////////////////////////////////////////////////////////////////////
- /*
- 函数名: GetFieldValueList
- 说明: 获取调查记录
- 参数:
- __in vector<CDataBase*>& vtValues 需添加的数据集
- 返回: 返回数量,失败=-1
- */
- /////////////////////////////////////////////////////////////////////////////////////
- int AdoImpl::InsertIntoTable(__in vector<CDataBase*>& vtValues)
- {
- CString strInsert = _T("");
- vector<CDataBase*>::iterator it = vtValues.begin();
- for ( ; it != vtValues.end(); it++ )
- {
- CDataBase* p = (*it);
- if(p != NULL)
- {
- TCHAR tszInsert[4096] = {0};
- p->GetInsertSql(tszInsert);
- if ( !Execute(tszInsert) )
- {
- // 执行SQL语句失败;
- continue;
- }
- }
- }
- return 1;
- }
|