📄 bdedatabase.cpp
字号:
}
// allocate memory for the string buffer to recieve data
pBYTE pDestBuf = (pBYTE)malloc(nSize*sizeof BYTE);
memset(pDestBuf, 0, nSize*sizeof BYTE);
if (pDestBuf == NULL)
{
// Throw memory exception here
free(pRecBuf);
strError.Format("Failed to get record information.");
throw new CBdeException(DBIERR_NOMEMORY, m_szTableName, m_szDatabaseName, strError);
return dtRetVal;
}
// get the actual field value
switch (nFieldType)
{
case fldDATE:
{
dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf,
(pBYTE)pDestBuf, pbBlank);
if (*pbBlank == FALSE)
{
// Here is a difficult situation...
// dates are returned as 4 bytes, but in the 32-bit world
// the DATE type is 8 bytes. Typcasting directly to DATE
// does not work. We have to type cast to 32 bit int, then
// to 64-bit int, and then to DATE
INT32 n32 = (*(INT32 *)pDestBuf);
dtRetVal = DateToOleDateTime(n32);
}
break;
}
case fldTIME:
{
dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf,
(pBYTE)pDestBuf, pbBlank);
if (*pbBlank == FALSE)
{
dtRetVal = TimeToOleDateTime((*(TIME *)pDestBuf));
}
break;
}
case fldTIMESTAMP:
{
// sie of timestamp is 8
dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf,
(pBYTE)pDestBuf, pbBlank);
if (*pbBlank == FALSE)
{
TIMESTAMP timestamp = *(TIMESTAMP*)pDestBuf;
dtRetVal = TimeStampToOleDateTime(timestamp);
}
break;
}
default:
{
nBdeExError = BDEEXERR_FIELDNOTDATE;
}
} // end of swtich
if (dbiResult != DBIERR_NONE || nBdeExError != 0)
{
// Throw exception here
strError.Format("Failed to retrieve data for field %d.",
nFieldNumber);
free(pRecBuf);
free(pDestBuf);
throw new CBdeException(dbiResult, nBdeExError, m_szTableName,
m_szDatabaseName, strError);
return dtRetVal;
}
free(pDestBuf);
free(pRecBuf);
return dtRetVal;
} // end of GetFieldAsDate
// function to get the field value as an integer
// throws an exception for incompatible data types
BOOL CBdeDatabase::GetFieldAsBoolean(UINT16 nFieldNumber, BOOL* pbBlank)
{
// Throw an exception if database is in Edit mode
if (!CheckValidCursor("Failed to get field value.")) return 0L;
if (!CheckNotEditMode("Failed to get field value.")) return 0L;
DBIResult dbiResult = DBIERR_NONE;
BOOL bValue = FALSE;
CString strError;
CURProps curProps; // Table Properties
pFLDDesc pFldDesc; // field information
pBYTE pRecBuf; // Record Buffer
int nBdeExError = 0;
// get the cursor properties for the table
dbiResult = DbiGetCursorProps(m_hCursor, &curProps);
if (dbiResult != DBIERR_NONE)
{
strError.Format("Failed to get cursor properties.");
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
return FALSE;
}
// make sure the field number is valid
// TODO: This should throw an exception
if (nFieldNumber < 1 || nFieldNumber > curProps.iFields)
{
strError.Format("Invalid field index %d. Valid numbers are between 1 and %d.",
nFieldNumber, curProps.iFields);
throw new CBdeException(DBIERR_NONE, BDEEXERR_INVALIDFIELDINDEX,
m_szTableName, m_szDatabaseName, strError);
return FALSE;
}
// allocate memory for the field descriptors
pFldDesc = (pFLDDesc)malloc(curProps.iFields * sizeof(FLDDesc));
if (pFldDesc == NULL)
{
// Throw exception for DBIERR_NOMEMORY
throw new CBdeException(DBIERR_NOMEMORY);
return FALSE;
}
dbiResult = DbiGetFieldDescs(m_hCursor, pFldDesc);
if (dbiResult != DBIERR_NONE)
{
free(pFldDesc);
strError.Format("Failed to retrieve field information.");
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
return FALSE;
}
// save the size and type of the field
UINT16 nSize = pFldDesc[nFieldNumber-1].iLen;
UINT16 nFieldType = pFldDesc[nFieldNumber-1].iFldType;
free(pFldDesc); // clean up the field descriptor
pFldDesc = NULL;
// allocate the record buffer for the table
pRecBuf = (pBYTE) malloc(curProps.iRecBufSize * sizeof(BYTE));
if (pRecBuf == NULL)
{
strError.Format("Insufficient memory to get field number %d.", nFieldNumber);
throw new CBdeException(DBIERR_NOMEMORY, m_szTableName, m_szDatabaseName, strError);
return FALSE;
}
// fill the record buffer with information
dbiResult = DbiGetRecord(m_hCursor, dbiNOLOCK, pRecBuf, NULL);
if (dbiResult != DBIERR_NONE)
{
free(pRecBuf);
strError.Format("Failed to get record information.");
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
return FALSE;
}
// allocate memory for the string buffer to recieve data
pBYTE pDestBuf = (pBYTE)malloc(nSize*sizeof BYTE);
memset(pDestBuf, 0, nSize*sizeof BYTE);
if (pDestBuf == NULL)
{
// Throw memory exception here
free(pRecBuf);
strError.Format("Failed to get record information.");
throw new CBdeException(DBIERR_NOMEMORY, m_szTableName, m_szDatabaseName, strError);
return FALSE;
}
// get the actual field value
switch (nFieldType)
{
case fldBOOL:
{
dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)pDestBuf, pbBlank);
if (*pbBlank == FALSE)
{
if (*(BYTE*)pDestBuf == 0) bValue = FALSE;
else bValue = TRUE;
}
break;
}
default:
{
nBdeExError = BDEEXERR_FIELDNOTINTEGER;
}
} // end of swtich
// process the final exceptions
if (dbiResult != DBIERR_NONE || nBdeExError != 0)
{
// Throw exception here
strError.Format("Failed to retrieve data for field %d.",
nFieldNumber);
free(pRecBuf);
free(pDestBuf);
throw new CBdeException(dbiResult, nBdeExError, m_szTableName,
m_szDatabaseName, strError);
return FALSE;
}
free(pDestBuf);
free(pRecBuf);
return bValue;
} // end of GetFieldAsBoolean
// returns the 1 based index of a field name
// returns 0 on error
int CBdeDatabase::GetFieldSize(int nFieldNumber)
{
// Throw an exception if not in edit mode
if (!CheckValidCursor("Failed to get field size.")) return 0;
DBIResult dbiResult;
CURProps CurProps;
pFLDDesc pFldDesc;
int nSize = -1;
CString strError;
dbiResult = DbiGetCursorProps(m_hCursor, &CurProps);
if (dbiResult != DBIERR_NONE)
{
strError.Format("Failed to retrieve size of field %d.",
nFieldNumber);
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
return -1;
}
pFldDesc = (pFLDDesc)malloc(CurProps.iFields * sizeof(FLDDesc));
if (pFldDesc == NULL)
{
// Throw exception for DBIERR_NOMEMORY
throw new CBdeException(DBIERR_NOMEMORY);
return -1;
}
dbiResult = DbiGetFieldDescs(m_hCursor, pFldDesc);
if (dbiResult != DBIERR_NONE)
{
free(pFldDesc);
strError.Format("Failed to retrieve field information.");
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
return -1;
}
nSize = pFldDesc[nFieldNumber-1].iLen;
free(pFldDesc);
return nSize;
}
// returns the number of fields in the current table
// returns -1 on error
int CBdeDatabase::GetFieldCount()
{
if (!CheckValidCursor("Failed to get field count.")) return -1;
DBIResult dbiResult;
CURProps CurProps;
CString strError;
CString strRetVal;
dbiResult = DbiGetCursorProps(m_hCursor, &CurProps);
if (dbiResult != DBIERR_NONE)
{
strError.Format("Failed to cursor properties in table %s.",
m_szTableName);
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
return 0;
}
return CurProps.iFields;
}
// function to get the filed type
// returns 0 on error
int CBdeDatabase::GetFieldType(int nFieldNumber)
{
// Throw an exception if not in edit mode
if (!CheckValidCursor("Failed to get field type.")) return FALSE;
if (!CheckEditMode("Failed to get field type.")) return FALSE;
DBIResult dbiResult;
CURProps curProps; // Table Properties
pFLDDesc pFldDesc; // field information
CString strError;
// get the cursor properties for the table
dbiResult = DbiGetCursorProps(m_hCursor, &curProps);
if (dbiResult != DBIERR_NONE)
{
strError.Format("Failed to get cursor properties.");
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
return 0;
}
// make sure the field number is valid
if (nFieldNumber < 1 || nFieldNumber > curProps.iFields)
{
strError.Format("Invalid field index %d. Valid numbers are between 1 and %d.",
nFieldNumber, curProps.iFields);
throw new CBdeException(DBIERR_NONE, BDEEXERR_INVALIDFIELDINDEX,
m_szTableName, m_szDatabaseName, strError);
return 0;
}
// allocate memory for the field descriptors
pFldDesc = (pFLDDesc)malloc(curProps.iFields * sizeof(FLDDesc));
if (pFldDesc == NULL)
{
// Throw exception for DBIERR_NOMEMORY
throw new CBdeException(DBIERR_NOMEMORY);
return 0;
}
dbiResult = DbiGetFieldDescs(m_hCursor, pFldDesc);
if (dbiResult != DBIERR_NONE)
{
free(pFldDesc);
strError.Format("Failed to retrieve field information.");
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
return 0;
}
UINT16 nFieldType = pFldDesc[nFieldNumber-1].iFldType;
free(pFldDesc); // clean up the field descriptor
return (int)nFieldType;
}
// function to get the filed type
// returns 0 on error or not a blob
int CBdeDatabase::GetBlobType(int nFieldNumber)
{
// Throw an exception if not in edit mode
if (!CheckValidCursor("Failed to get field type.")) return FALSE;
if (!CheckEditMode("Failed to get field type.")) return FALSE;
DBIResult dbiResult;
CURProps curProps; // Table Properties
pFLDDesc pFldDesc; // field information
CString strError;
// get the cursor properties for the table
dbiResult = DbiGetCursorProps(m_hCursor, &curProps);
if (dbiResult != DBIERR_NONE)
{
strError.Format("Failed to get cursor properties.");
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
return 0;
}
// make sure the field number is valid
if (nFieldNumber < 1 || nFieldNumber > curProps.iFields)
{
strError.Format("Invalid field index %d. Valid numbers are between 1 and %d.",
nFieldNumber, curProps.iFields);
throw new CBdeException(DBIERR_NONE, BDEEXERR_INVALIDFIELDINDEX,
m_szTableName, m_szDatabaseName, strError);
return 0;
}
// allocate memory for the field descriptors
pFldDesc = (pFLDDesc)malloc(curProps.iFields * sizeof(FLDDesc));
if (pFldDesc == NULL)
{
// Throw exception for DBIERR_NOMEMORY
throw new CBdeException(DBIERR_NOMEMORY);
return 0;
}
dbiResult = DbiGetFieldDescs(m_hCursor, pFldDesc);
if (dbiResult != DBIERR_NONE)
{
free(pFldDesc);
strError.Format("Failed to retrieve field information.");
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
return 0;
}
UINT16 nFieldType = pFldDesc[nFieldNumber-1].iFldType;
UINT16 nFieldSubType = pFldDesc[nFieldNumber-1].iSubType;
free(pFldDesc); // clean up the field descriptor
if (nFieldType != fldBLOB) return 0;
return (int)nFieldSubType;
}
// function to get the field name from it's number
CString CBdeDatabase::GetFieldName(int nFieldNumber)
{
if (!CheckValidCursor("Failed to get field name.")) return "";
DBIResult dbiResult;
CURProps CurProps;
pFLDDesc pFldDesc;
CString strError;
CString strRetVal;
dbiResult = DbiGetCursorProps(m_hCursor, &CurProps);
if (dbiResult != DBIERR_NONE)
{
strError.Format("Failed to cursor properties in table %s.",
m_szTableName);
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
return "";
}
// make sure the number is within the proper range
if (nFieldNumber < 1 || nFieldNumber > CurProps.iFields)
{
strError.Format("Invalid field index %d. Valid numbers are between 1 and %d.",
nFieldNumber, CurProps.iFields);
throw new CBdeException(DBIERR_NONE, BDEEXERR_INVALIDFIELDINDEX,
m_szTableName, m_szDatabaseName, strError);
return "";
}
// allocate memory for the field descriptor
pFldDesc = (pFLDDesc)malloc(CurProps.iFields * sizeof(FLDDesc));
if (pFldDesc == NULL)
{
// Throw exception for DBIERR_NOMEMORY
throw new CBdeException(DBIERR_NOMEMORY);
return "";
}
dbiResult = DbiGetFieldDescs(m_hCursor, pFldDesc);
if (dbiResult != DBIERR_NONE)
{
// Throw exception here
strError.Format("Failed to get field information.");
throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
free(pFldDesc);
return "";
}
strRetVal.Format("%s", pFldDesc[nFieldNumber-1].szName);
free(pFldDesc);
return strRetVal;
}
// returns the 1 based index of a field name
// returns 0 on error
int CBdeDatabase::FieldNumberFromName(LPCTSTR szFieldName)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -