// Generic Recordset
#include "stdafx.h"
#include <afx.h>
#include "GenericRecordSet.h"
#include "CStringExtension.h"
////////////////////////////////////////////////////////////////
// GenericRecordset class
// Get by Column Name
CString GenericRecordset::GetCString(CString ColumnName)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
CString Holder;
GetFieldValue(ColumnName, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
Holder.Format("%s",vFieldValue->pbVal);
return Holder;
};
short GenericRecordset::GetShort(CString ColumnName)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnName, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return vFieldValue->iVal;
};
long GenericRecordset::GetLong(CString ColumnName)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnName, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return vFieldValue->lVal;
};
double GenericRecordset:: GetDouble(CString ColumnName)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnName, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return vFieldValue->dblVal;
};
float GenericRecordset::GetFloat(CString ColumnName)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnName, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return vFieldValue->fltVal;
};
BOOL GenericRecordset::GetBool(CString ColumnName)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnName, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return vFieldValue->iVal;
};
COleDateTime GenericRecordset::GetDate(CString ColumnName)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnName, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
COleDateTime MyTime(vFieldValue->date);
return MyTime;
};
VARIANT GenericRecordset::GetColumn(CString ColumnName)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnName, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return *vFieldValue;
};
COleVariant GenericRecordset::GetOleColumn(CString ColumnName)
{
COleVariant covFieldValue;
GetFieldValue(ColumnName, covFieldValue);
return covFieldValue;
};
// Get by Column Number
CString GenericRecordset::GetCString(int ColumnNumber)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
CString Holder;
GetFieldValue(ColumnNumber, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
Holder.Format("%s",vFieldValue->pbVal);
return Holder;
};
short GenericRecordset::GetShort(int ColumnNumber)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnNumber, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return vFieldValue->iVal;
};
long GenericRecordset::GetLong(int ColumnNumber)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnNumber, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return vFieldValue->lVal;
};
double GenericRecordset:: GetDouble(int ColumnNumber)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnNumber, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return vFieldValue->dblVal;
};
float GenericRecordset::GetFloat(int ColumnNumber)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnNumber, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return vFieldValue->fltVal;
};
BOOL GenericRecordset::GetBool(int ColumnNumber)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnNumber, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return vFieldValue->iVal;
};
COleDateTime GenericRecordset::GetDate(int ColumnNumber)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnNumber, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
COleDateTime MyTime(vFieldValue->date);
return MyTime;
};
VARIANT GenericRecordset::GetColumn(int ColumnNumber)
{
COleVariant covFieldValue;
VARIANT *vFieldValue;
GetFieldValue(ColumnNumber, covFieldValue);
vFieldValue = (LPVARIANT)covFieldValue;
return *vFieldValue;
};
COleVariant GenericRecordset::GetOleColumn(int ColumnNumber)
{
COleVariant covFieldValue;
GetFieldValue(ColumnNumber, covFieldValue);
return covFieldValue;
};
int GenericRecordset::Fields()
{
return this->GetFieldCount();
}
GenericRecordset &GenericRecordset::operator = (GenericRecordset &TempSet)
{
int x;
int y;
y = this->GetFieldCount();
this->Edit();
for(x=0;x<y;x++)
this->SetFieldValue(x,TempSet.GetOleColumn(x));
this->Update();
return *this;
}
GenericRecordset &GenericRecordset::operator = (CString Temp)
{
int x;
int y;
CStringEx Values = Temp;
y = this->GetFieldCount();
this->Edit();
for(x=0;x<y;x++)
this->SetFieldValue(x,(LPCTSTR)Values.GetField(',',x));
this->Update();
return *this;
}
GenericRecordset &GenericRecordset::operator += (GenericRecordset &TempSet)
{
int x;
int y;
y = this->GetFieldCount();
this->AddNew();
for(x=0;x<y;x++)
this->SetFieldValue(x,TempSet.GetOleColumn(x));
this->Update();
return *this;
}
GenericRecordset &GenericRecordset::operator += (CString Temp)
{
int x;
int y;
CStringEx Values = Temp;
y = this->GetFieldCount();
this->AddNew();
for(x=0;x<y;x++)
this->SetFieldValue(x,(LPCTSTR)Values.GetField(',',x));
this->Update();
return *this;
}
BOOL GenericRecordset::operator == (GenericRecordset &TempSet)
{
// This assumes that the recordsets are the same with he same keys.
int x = 0;
CDaoFieldInfo FieldInfo;
int FirstOne = TRUE;
CString SQL = "";
CString Holder;
CString Key;
while((Key = m_csxKeyList.GetField(',',x))!="")
{
this->GetFieldInfo(Key,FieldInfo);
if(!FirstOne)
{
SQL+=" AND ";
}
FirstOne = FALSE;
SQL+=Key;
if(FieldInfo.m_nType == dbText)
{
SQL += " = '";
SQL +=TempSet.GetCString(Key);
SQL += "'";
}
if(FieldInfo.m_nType == dbInteger)
{
SQL += " = ";
Holder.Format("%d",TempSet.GetShort(Key));
SQL += Holder;
}
if(FieldInfo.m_nType == dbLong)
{
SQL += " = ";
Holder.Format("%d",TempSet.GetLong(Key));
SQL += Holder;
}
if(FieldInfo.m_nType == dbDouble)
{
SQL += " = ";
Holder.Format("%d",TempSet.GetDouble(Key));
SQL += Holder;
}
x++;
}
if(this->FindFirst(SQL) ==0)
return FALSE;
else
return TRUE;
}
BOOL GenericRecordset::operator == (CString &TempString)
{
// This assumes that the recordsets are the same with he same keys.
// The temp string is a coma delimited list, no quites around the strings
int x = 0;
CStringEx String = TempString;
CDaoFieldInfo FieldInfo;
int FirstOne = TRUE;
CString SQL = "";
CString Key;
while((Key = m_csxKeyList.GetField(',',x))!="")
{
this->GetFieldInfo(Key,FieldInfo);
if(!FirstOne)
{
SQL+=" AND ";
}
FirstOne = FALSE;
SQL+=Key;
if(FieldInfo.m_nType == dbText)
{
SQL += " = '";
SQL += String.GetField(',',x);
SQL += "'";
}
if(FieldInfo.m_nType == dbInteger)
{
SQL += " = ";
SQL += String.GetField(',',x);
}
if(FieldInfo.m_nType == dbLong)
{
SQL += " = ";
SQL += String.GetField(',',x);
}
if(FieldInfo.m_nType == dbDouble)
{
SQL += " = ";
SQL += String.GetField(',',x);
}
x++;
}
if(this->FindFirst(SQL) ==0)
return FALSE;
else
return TRUE;
}
void GenericRecordset::SetKeys(CString TempKeyList)
{
// format is KeyName,KeyName.....
m_csxKeyList = TempKeyList;
}