ADODataset.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:16k
- #include ".ADODataset.h"
- //Constructor and destructor.
- CADODataSet::CADODataSet()
- {
- //set the ADO Connection object is null.
- m_pADOConnection = NULL;
- //record set smart pointer
- m_pRecordset = NULL;
-
- //Empty the sql text.
- m_sSQLText.erase();
- //create the recordset object.
- HRESULT hr = this->m_pRecordset.CreateInstance("ADODB.Recordset");
- if( FAILED(hr) ){
- throw new CADOException("CADODataSet::CADODataSet", "Recordset CreateInstance Failed.");
- }
- else{
- this->m_pRecordset->CursorType = adOpenDynamic;
- this->m_pRecordset->LockType = adLockPessimistic;
- }
- }
- CADODataSet::CADODataSet(_RecordsetPtr pRecordset)
- {
- //set the ADO Connection object is null.
- m_pADOConnection = NULL;
- //record set smart pointer
- m_pRecordset = pRecordset;
-
- //Empty the sql text.
- m_sSQLText.erase();
- }
- CADODataSet::~CADODataSet()
- {
- this->Close();
- //Empty the sql text.
- m_sSQLText.erase();
- }
- //-------------------------------------------------
- //Set / Get the dataset's connection object.
- void CADODataSet::SetConnection(CADOConnection &pADOConnection)
- {
- this->Close();
- this->m_pADOConnection = &pADOConnection;
- }
- CADOConnection &CADODataSet::GetConnection()
- {
- return (*this->m_pADOConnection);
- }
- //Open or close the dataset.
- bool CADODataSet::Open(LPCSTR lpszSQLText)
- {
- if( this->IsOpened() ) return true;
- if( _STREMPTY(lpszSQLText) ){
- throw new CADOException("CADODataSet::Open", "Dataset SQL Text cann't be empty.");
- }
- this->m_sSQLText = lpszSQLText;
- try{
- //open the recordset
- HRESULT hr = this->m_pRecordset->Open(lpszSQLText,
- _variant_t((IDispatch *)m_pADOConnection->getConnectionPtr(), true),
- adOpenDynamic, adLockPessimistic, adCmdText);
- return SUCCEEDED(hr);
- /*
- * this->m_pRecordset = this->m_pADOConnection->Execute(lpszSQLText);
- * return ( NULL != this->m_pRecordset );
- */
- }
- catch(_com_error e){
- throw new CADOException("CADODataSet::Open", e.Description());
- }
- return false;
- }
- bool CADODataSet::Close()
- {
- if( this->IsOpened() ){
- try{
- this->m_pRecordset->Close();
- return true;
- }
- catch(_com_error e){
- throw new CADOException("CADODataSet::Close", e.Description());
- }
- }
- return false;
- }
- //-------------------------------------------------
- //get the dataset's state.
- bool CADODataSet::IsOpened()
- {
- if( NULL == this->m_pRecordset ){
- return false;
- }
- return (0 != m_pRecordset->State);
- }
- //determine the dataset cursor whether or not
- //arrival the begin(end) of the dataset.
- bool CADODataSet::IsBof()
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::IsOpened", "Dataset is closed yet.");
- }
- return (m_pRecordset->ADOBOF == VARIANT_TRUE);
- }
- bool CADODataSet::IsEof()
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::IsOpened", "Dataset is closed yet.");
- }
- return (this->m_pRecordset->ADOEOF == VARIANT_TRUE);
- }
- //determine the dataset whether is empty.
- bool CADODataSet::IsEmpty()
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::IsEmpty", "Dataset is closed yet.");
- }
- return ( this->GetRecordCounts() != 0 );
- }
- //Cursor Location.
- void CADODataSet::Prior() //return the BOF flag.
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::PriorGetRecordCounts", "Dataset is closed yet.");
- }
- try{
- this->m_pRecordset->MovePrevious();
- }
- catch (_com_error e) {
- throw new CADOException("CADODataSet::Prior", e.Description());
- }
- }
- void CADODataSet::Next() //return the EOF flag.
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::Next", "Dataset is closed yet.");
- }
- try{
- this->m_pRecordset->MoveNext();
- }
- catch (_com_error e) {
- throw new CADOException("CADODataSet::Next", e.Description());
- }
- }
- void CADODataSet::First() //move the cursor to the begin.
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::First", "Dataset is closed yet.");
- }
- try{
- this->m_pRecordset->MoveFirst();
- }
- catch (_com_error e) {
- throw new CADOException("CADODataSet::First", e.Description());
- }
- }
- void CADODataSet::Last() //move the cursor to the end.
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::Last", "Dataset is closed yet.");
- }
- try{
- this->m_pRecordset->MoveLast();
- }
- catch (_com_error e) {
- throw new CADOException("CADODataSet::Last", e.Description());
- }
- }
- //Get the dataset's informations.
- LONG CADODataSet::GetRecordCounts()
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::GetRecordCounts", "Dataset is closed yet.");
- }
- return this->m_pRecordset->RecordCount;
- }
- //Get the dataset's fields object.
- CFields CADODataSet::Fields()
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::Fields", "Dataset is closed yet.");
- }
- FieldsPtr pFldsPtr = m_pRecordset->GetFields();
- if( pFldsPtr ){
- CFields Fields(pFldsPtr);
- return Fields;
- }
- return NULL;
- }
- //Get the dataset edit mode.
- /*
- * adEditNone = 0,
- * adEditInProgress = 1,
- * adEditAdd = 2,
- * adEditDelete = 4
- */
- EditModeEnum CADODataSet::GetEditMode()
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::GetEditMode", "Dataset is closed yet.");
- }
- return this->m_pRecordset->EditMode;
- }
- //Insert / Edit / Delete / Update.
- void CADODataSet::Edit()
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::Edit", "Dataset is closed yet.");
- }
- }
- void CADODataSet::Insert()
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::Insert", "Dataset is closed yet.");
- }
- try{
- this->m_pRecordset->AddNew();
- }
- catch(_com_error e){
- throw new CADOException("CADODataSet::Insert", e.Description());
- }
- }
- void CADODataSet::Delete()
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::Delete", "Dataset is closed yet.");
- }
- try{
- this->m_pRecordset->Delete(adAffectCurrent);
- }
- catch(_com_error e){
- throw new CADOException("CADODataSet::Delete", e.Description());
- }
- }
- void CADODataSet::Update()
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::Update", "Dataset is closed yet.");
- }
- try{
- this->m_pRecordset->Update();
- }
- catch(_com_error e){
- this->m_pRecordset->CancelUpdate();
- throw new CADOException("CADODataSet::Update", e.Description());
- }
- }
- //Storage COM support
- bool CADODataSet::SaveToXMLFile(LPCSTR szfile)
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::SaveToXMLFile", "Dataset is closed yet.");
- }
- try{
- _variant_t V(szfile);
- HRESULT hr = this->m_pRecordset->Save(V, adPersistXML);
- if(FAILED(hr)){
- return false;
- }
- return true;
- }
- catch(_com_error e){
- throw new CADOException("CADODataSet::SaveToXMLFile", e.Description());
- }
- }
- bool CADODataSet::SaveToXMLStream(_StreamPtr &pstream)
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::SaveToXMLStream", "Dataset is closed yet.");
- }
- try{
- if(NULL == pstream){
- pstream.CreateInstance(__uuidof(Stream));
- }
-
- HRESULT hr = this->m_pRecordset->Save(pstream.GetInterfacePtr(), adPersistXML);
- if(FAILED(hr)){
- return false;
- }
- return true;
- }
- catch(_com_error e){
- throw new CADOException("CADODataSet::SaveToXMLStream", e.Description());
- }
- }
- //Warning::Here has some bugs!!!.
- bool CADODataSet::SaveToBuffer(char **buffer, LONG &size)
- {
- if( NULL == this->m_pRecordset || !this->IsOpened() ){
- throw new CADOException("CADODataSet::SaveToBuffer", "Dataset is closed yet.");
- }
- assert(NULL != buffer);
- _StreamPtr pstream = NULL;
- if(this->SaveToXMLStream(pstream)){
-
- size = pstream->Size;
-
- if(NULL != *buffer){
- delete [](*buffer);
- }
- *buffer = new char[size+1];
- memset(*buffer, 0x0, size+1);
- memcpy(*buffer, (char*)(pstream->ReadText(size)), size);
-
- char *pend = strrchr(*buffer, '>');
- assert(NULL != pend);
- size = pend - *buffer + 1;
- memset(pend+1, 0x0, pstream->Size-size);
- pstream->Close();
- return true;
- }
- return false;
- }
- //Load from XML file or Stream object.
- bool CADODataSet::LoadFromXMLFile(LPCSTR szfile)
- {
- if( NULL == this->m_pRecordset ){
- throw new CADOException("CADODataSet::LoadFromXMLFile", "Dataset instance isn't created yet.");
- }
- if(this->IsOpened()){
- this->Close();
- }
- try{
- HRESULT hr = this->m_pRecordset->Open(szfile, "Provider=MSPersist;",
- adOpenUnspecified, adLockReadOnly, adCmdFile);
- return !FAILED(hr);
- }
- catch(_com_error e){
- throw new CADOException("CADODataSet::LoadFromXMLFile", e.Description());
- }
- }
- bool CADODataSet::LoadFromXMLStream(_StreamPtr &pstream)
- {
- if( NULL == this->m_pRecordset ){
- throw new CADOException("CADODataSet::LoadFromXMLFile", "Dataset instance isn't created yet.");
- }
- assert(NULL != pstream);
- if(this->IsOpened()){
- this->Close();
- }
- try{
- pstream->Position = 0;
- HRESULT hr = this->m_pRecordset->Open(pstream.GetInterfacePtr(),
- vtMissing, adOpenUnspecified, adLockReadOnly, adCmdFile);
-
- return !FAILED(hr);
- }
- catch(_com_error e){
- throw new CADOException("CADODataSet::LoadFromXMLStream", e.Description());
- }
- }
- /********************************************************
- * Class Name : CFields. *
- * Purpose : Dataset Field SubAggregate Class; *
- * FieldPtr encapsulation class. *
- * File Name : ADODataSet.h / ADODataSet.cpp. *
- *------------------------------------------------------*
- * Author : Devia Lee. Date: 2004-04-02 *
- ********************************************************/
- //constructor or destructor procedure
- CField::CField()
- {
- m_spField = NULL;
- }
- CField::CField(FieldPtr &fldPtr)
- {
- m_spField = fldPtr;
- }
- CField::~CField()
- {
- if(NULL != m_spField){
- m_spField->Release();
- }
- }
- //get fieldPtr smart pointer
- FieldPtr CField::getSmartPtr()
- {
- return this->m_spField;
- }
- //operator "=" procedure
- CField& CField::operator=(CField &fldObj)
- {
- if(NULL != this->m_spField){
- this->m_spField.Release();
- }
- this->m_spField = fldObj.getSmartPtr();
- return (*this);
- }
- //operator "=" procedure
- CField& CField::operator=(FieldPtr fldPtr)
- {
- if(NULL != this->m_spField){
- this->m_spField.Release();
- }
- this->m_spField = fldPtr;
- return (*this);
- }
- //get the field type
- DataTypeEnum CField::getType()
- {
- try{
- DataTypeEnum dtDataType;
- m_spField->get_Type(&dtDataType);
- return dtDataType;
- }
- catch(_com_error e){
- throw new CADOException("CField::getType()", e.Description());
- }
- }
- //get the field define size
- long CField::getDefineSize()
- {
- try{
- return m_spField->DefinedSize;
- }
- catch(_com_error e){
- throw new CADOException("CField::getDefineSize()", e.Description());
- }
- }
- //get the field actual size
- long CField::getActualSize()
- {
- try{
- return m_spField->ActualSize;
- }
- catch(_com_error e){
- throw new CADOException("CField::getActualSize()", e.Description());
- }
- }
- //get the field name
- string CField::getName()
- {
- return static_cast<string>(m_spField->Name);
- }
- //get the field value(CeFtpVariant)
- CVariantEx CField::getValue()
- {
- CVariantEx varObj;
- varObj = this->m_spField->Value;
- return varObj;
- }
- //set the field value.
- void CField::setValue(CVariantEx var)
- {
- this->m_spField->put_Value( var.get_variant_t() );
- }
- //determine the field whether is null
- bool CField::IsNull()
- {
- try{
- _variant_t val;
- val = m_spField->Value;
- return (val.vt == VT_NULL);
- }
- catch(_com_error e){
- throw new CADOException("CField::IsNull()", e.Description());
- }
- }
- /********************************************************
- * Class Name : CBlobField. *
- * Purpose : Dataset Blob Field SubAggregate Class;*
- * File Name : ADODataSet.h / ADODataSet.cpp. *
- *------------------------------------------------------*
- * Author : Devia Lee. Date: 2004-04-03 *
- ********************************************************/
- CBlobField::CBlobField(CField &oField)
- {
- this->m_spField = oField.getSmartPtr();
- }
- CBlobField::~CBlobField(){}
- //Load contents from stream / file.
- void CBlobField::LoadFromStream(
- const char *lpBuffer,
- const ULONG vBufSize)
- {
- assert( NULL != lpBuffer );
-
- char *p = (char*)lpBuffer;
- VARIANT varBLOB;
- SAFEARRAY *psa;
- SAFEARRAYBOUND rgsabound[1];
- rgsabound[0].lLbound = 0;
- rgsabound[0].cElements = vBufSize;
- //create the "SAFEARRAY" object.
- psa = SafeArrayCreate(VT_UI1, _ARRAYSIZE(rgsabound, SAFEARRAYBOUND), rgsabound);
- for(long i = 0; i < vBufSize; ++i){
- SafeArrayPutElement(psa, &i, p++);
- }
- varBLOB.vt = VT_ARRAY | VT_UI1;
- varBLOB.parray = psa;
- try{
- this->m_spField->AppendChunk(varBLOB);
- }
- catch (_com_error e) {
- throw new CADOException("CBlobField::LoadFromStream", e.Description());
- }
- }
- void CBlobField::LoadFromFile(LPCSTR lpszFileName)
- {
- /*
- CXFile xFile;
- char lpBuffer[MAX_BOLB_BLOCKSIZE];
-
- if( !xFile.Open(lpszFileName) ){
- throw new CADOException("CBlobField::LoadFromFile", "Open File Failed.");
- }
- while( !xFile.Eof() ){
- _MEMSET(lpBuffer, sizeof(MAX_BOLB_BLOCKSIZE))
- int nReaded = xFile.Read(lpBuffer, MAX_BOLB_BLOCKSIZE);
- this->LoadFromStream(lpBuffer, nReaded);
- }
- xFile.Close();
- */
- }
- //Save current contents to stream / file.
- long CBlobField::SaveToStream(
- char *lpBuffer,
- const ULONG vMaxBufSize)
- {
- assert( NULL != lpBuffer );
- long lDataSize = this->getActualSize();
- if( lDataSize > 0 ){
-
- _variant_t varBLOB;
- varBLOB = this->m_spField->GetChunk(lDataSize);
- if( varBLOB.vt == (VT_ARRAY | VT_UI1) ){
-
- char *pBuf = NULL;
- SafeArrayAccessData(varBLOB.parray, (void **)&pBuf);
- memcpy( lpBuffer, pBuf, min(vMaxBufSize, lDataSize) );
- SafeArrayUnaccessData(varBLOB.parray);
- return min(vMaxBufSize, lDataSize);
- }
- }
- return 0;
- }
- long CBlobField::SaveToFile(LPCSTR lpszFileName)
- {
- /*
- CXFile xFile;
- long lSaved;
- char *lpBuffer;
- if( !xFile.Open(lpszFileName, fomCreateAlways) ){
- throw new CADOException("CBlobField::SaveToFile", "Create File Failed.");
- }
- lSaved = this->getActualSize();
- lpBuffer = new char[lSaved + 1];
- _MEMSET(lpBuffer, lSaved + 1);
- lSaved = this->SaveToStream(lpBuffer, lSaved);
- xFile.Write(lpBuffer, lSaved);
- delete []lpBuffer;
- xFile.Close();
- return lSaved;
- */
- return 0;
- }
- /********************************************************
- * Class Name : CFields. *
- * Purpose : Dataset Field Aggregate Class. *
- * File Name : ADODataSet.h / ADODataSet.cpp. *
- *------------------------------------------------------*
- * Author : Devia Lee. Date: 2004-04-02 *
- ********************************************************/
- //constructor and destructor procedure
- CFields::CFields()
- {
- m_spFileds = NULL;
- }
- CFields::CFields(FieldsPtr fldsPtr)
- {
- m_spFileds = fldsPtr;
- }
- CFields::~CFields()
- {
- if(NULL != m_spFileds){
- m_spFileds.Release();
- }
- }
- //get fieldPtr smart pointer
- FieldsPtr &CFields::getSmartPtr()
- {
- return m_spFileds;
- }
- //Get the subfields count.
- const long CFields::FieldCount()
- {
- return m_spFileds->Count;
- }
- //operator "=" procedure
- CFields &CFields::operator=(CFields &fldsObj)
- {
- if(NULL != this->m_spFileds){
- this->m_spFileds.Release();
- }
- this->m_spFileds = fldsObj.getSmartPtr();
- return (*this);
- }
- //operator "=" procedure
- CFields &CFields::operator=(FieldsPtr &fldsPtr)
- {
- if(NULL != this->m_spFileds){
- this->m_spFileds.Release();
- }
- this->m_spFileds = fldsPtr;
- return (*this);
- }
- //operator "[]" procedure
- CField CFields::operator[](long i)
- {
- CField fldObj;
- try{
- fldObj = m_spFileds->GetItem(_variant_t(i));
- return fldObj;
- }
- catch (_com_error e) {
- throw e.ErrorInfo();
- }
- }
- //
- CField CFields::FieldByName(LPCSTR lpszFieldName)
- {
- CField fldObj;
- try{
- if(NULL != m_spFileds){
- CField fldObj = m_spFileds->GetItem(_variant_t(lpszFieldName));
- return fldObj;
- }
- }
- catch (_com_error e) {
- throw CADOException("CFields::FieldByName", e.Description());
- }
- return fldObj;
- }