⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bdedatabase.cpp

📁 程序1
💻 CPP
📖 第 1 页 / 共 5 页
字号:
{
	if (!CheckValidCursor()) return 0;

	DBIResult dbiResult;
	CURProps CurProps;
	pFLDDesc pFldDesc;
	UINT16 nField;
	BOOL bFound = FALSE;
	CString strError;
	int nRetVal = 0;

	dbiResult = DbiGetCursorProps(m_hCursor, &CurProps);

	if (dbiResult != DBIERR_NONE)
	{
		strError.Format("Failed to cursor properties.");
		throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
		return nRetVal;
	}


	pFldDesc = (pFLDDesc)malloc(CurProps.iFields * sizeof(FLDDesc));
	if (pFldDesc == NULL)
	{
		// Throw exception for DBIERR_NOMEMORY
		throw new CBdeException(DBIERR_NOMEMORY);
		return nRetVal;
	}


	dbiResult = DbiGetFieldDescs(m_hCursor, pFldDesc);

	if (dbiResult != DBIERR_NONE)
	{
		// TODO:  Throw exception here
		strError.Format("Failed to get field information.");
		throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
		free(pFldDesc);
		return nRetVal;
	}

	for (nField = 0; nField < CurProps.iFields; nField++)
	{
		if (_stricmp(pFldDesc[nField].szName, szFieldName) == 0)
		{
			bFound = TRUE;
			nRetVal = pFldDesc[nField].iFldNum;
			free(pFldDesc);
			return nRetVal;
		}
	}
	free(pFldDesc);

	// did not find the name so throw an exception
		

	return nRetVal;
}



/////////////////////////////////////////////////////////////////////////
// Data type conversion functions

CString CBdeDatabase::FormatDate(INT32 Date)
{
	DBIResult   dbiResult;       // Return Value from IDAPI
	FMTDate     fmtDate;    // Date Format
	UINT16      uDay;       // Day portion of date
	UINT16      uMonth;     // Month portion of date
	INT16       iYear;      // Year portion of date
	CString strRetVal;
            
	// Get the formatting of the Date.
	dbiResult = DbiGetDateFormat(&fmtDate);
	if (dbiResult != DBIERR_NONE) return "";


	// Decode the date.
	dbiResult = DbiDateDecode(Date, &uMonth, &uDay, &iYear);
	if (dbiResult != DBIERR_NONE) return "";

	// Determine if date should be displayed year based.
	if (!(fmtDate.bFourDigitYear) && (fmtDate.bYearBiased))
	{
		iYear = (INT16)(iYear + 1900);
	}

	if (!(fmtDate.bFourDigitYear))
	{
		iYear = (INT16)(iYear - 1900);
	}

	// Make certain the seperator is not the
	//   escape character.
	if (!strcmp(fmtDate.szDateSeparator, "\\"))
	{
		strcpy(fmtDate.szDateSeparator, "/");
	}

	// Format the date.
	switch(fmtDate.iDateMode)
	{
			// MM/DD/YY - Month, Day, Year.
			case 0:
					strRetVal.Format("%0*d%s%0*d%s%0*d",
									1 + fmtDate.bMonthLeadingZero,
									uMonth,
									fmtDate.szDateSeparator,
									1 + fmtDate.bDayLeadingZero,
									uDay,
									fmtDate.szDateSeparator,
									2,
									iYear);
					break;
			// DD/MM/YY - Day, Month, Year
			case 1:
					strRetVal.Format("%0*d%s%0*d%s%0*d",
									1 + fmtDate.bDayLeadingZero,
									uDay,
									fmtDate.szDateSeparator,
									1 + fmtDate.bMonthLeadingZero,
									uMonth,
									fmtDate.szDateSeparator,
									2,
									iYear);
					break;
			// YY/MM/DD - Year, Month, Day
			case 2:
					strRetVal.Format("%0*d%s%0*d%s%0*d",
									2,
									iYear,
									fmtDate.szDateSeparator,
									1 + fmtDate.bMonthLeadingZero,
									uMonth,
									fmtDate.szDateSeparator,
									1 + fmtDate.bDayLeadingZero,
									uDay);
					break;
	}
	return strRetVal;
}


CString CBdeDatabase::FormatTime(TIME Time)
{
	DBIResult   dbiResult;       // Return value from IDAPI functions
	FMTTime     fmtTime;    // Time format
	UINT16      uHour;      // Hour portion of the time
	UINT16      uMinute;    // Minute portion of the time
	UINT16      uMilSec;    // Second portion (in ms) of the time
	UINT16      uIsAm = 0;      // Is Time AM?
	CHAR        szTemp[10]; // Temp buffer, used for AM, PM string
	CHAR szTime[48];
	CString strRetVal;

	// Get the formatting of the Time.
	dbiResult = DbiGetTimeFormat(&fmtTime);
	if (dbiResult != DBIERR_NONE) return strRetVal;

	// Decode the time.
	dbiResult = DbiTimeDecode(Time, &uHour, &uMinute, &uMilSec);
	if (dbiResult != DBIERR_NONE) return strRetVal;

	// Make certain the seperator is not the
	//   escape character.
	if (fmtTime.cTimeSeparator == '\\')
	{
		fmtTime.cTimeSeparator  = '/';
	}

	// Check if time should be displayed in 12 or 24 hour format.
	if (fmtTime.bTwelveHour)
	{
		// Temporary variable used to determine if the time is AM or PM.
		uIsAm = uHour;
		uHour = (UINT16)(uHour % 12);
		if (uHour == 0)
		{
			uHour = 12;
		}
		// If AM, set uIsAm to TRUE, else set uIsAm to 0.
		if (uIsAm == uHour)
		{
			uIsAm = 1;
		}
		else
		{
			uIsAm = 0;
		}
	}

	// Format the hour and minute of the time.
	wsprintf(szTime, "%2u%c%02u", uHour, fmtTime.cTimeSeparator, uMinute);

	// Determine if seconds are to be displayed.
	if (fmtTime.bSeconds)
	{
		wsprintf(szTemp, "%c%02u", fmtTime.cTimeSeparator, (uMilSec / 1000));
		strcat(szTime, szTemp);

		// Determine if milliseconds are to be displayed.
		if (fmtTime.bMilSeconds)
		{
			wsprintf(szTemp, "%c%03u", fmtTime.cTimeSeparator, (uMilSec % 1000));
			strcat(szTime, szTemp);   
		}
	}

	// Add a string to the time if the time is 12-hour.
	if (fmtTime.bTwelveHour)
	{
		strcat(szTime, " ");
		if (uIsAm)
		{
			strcat(szTime, fmtTime.szAmString);
		}
		else // otherwise it's PM
		{
			strcat(szTime, fmtTime.szPmString);
		}
	}

	strRetVal.Format("%s", szTime);
	return strRetVal;
}



CString CBdeDatabase::FormatTimeStamp(TIMESTAMP TimeStamp)
{
	DBIResult   rslt;       // Return value from IDAPI functions
	TIME        Time;       // Time portion of the TimeStamp
	CString strRetVal;
	INT32 nDate32;

	// Get the date and time components
	rslt = ::DbiTimeStampDecode(TimeStamp, &nDate32, &Time);

	// Format the TimeStamp
	strRetVal.Format("%s %s", FormatDate(nDate32), FormatTime(Time));

	return strRetVal;
}


// function to convert a TIMESTAMP to a COleDateTime
COleDateTime CBdeDatabase::TimeStampToOleDateTime(TIMESTAMP TimeStamp)
{
	DBIResult dbiResult;       // Return value from IDAPI functions
	TIME Time; // Time portion of the TimeStamp, actually a LONG
	INT32 n32; // date portion of the timestamp
	UINT16 hour, min, msec, day, month;
	INT16 year;
	COleDateTime dtRetVal = 0.0;
	dtRetVal.m_status = COleDateTime::null;

	// Get the date and time components
	dbiResult = ::DbiTimeStampDecode(TimeStamp, &n32, &Time);
	if (dbiResult != DBIERR_NONE) return dtRetVal;

	dbiResult = ::DbiTimeDecode(Time, &hour, &min, &msec);
	if (dbiResult != DBIERR_NONE) return dtRetVal;

	dbiResult = DbiDateDecode(n32, &month, &day, &year);
	if (dbiResult != DBIERR_NONE) return dtRetVal;

	dtRetVal.SetDateTime(year, month, day, hour, min, msec/1000);
	return dtRetVal;	
}





COleDateTime CBdeDatabase::TimeToOleDateTime(TIME time)
{
	DBIResult dbiResult;       // Return value from IDAPI functions
	UINT16 hour, min, msec;
	COleDateTime dtRetVal = 0.0;
	dtRetVal.m_status = COleDateTime::null;

	dbiResult = ::DbiTimeDecode(time, &hour, &min, &msec);
	if (dbiResult != DBIERR_NONE) return dtRetVal;

	dtRetVal.SetTime(hour, min, msec/1000);
	return dtRetVal;	
}


COleDateTime CBdeDatabase::DateToOleDateTime(INT32 Date)
{
	DBIResult dbiResult;       // Return value from IDAPI functions
	UINT16 day, month;
	INT16 year;
	COleDateTime dtRetVal = 0.0;
	dtRetVal.m_status = COleDateTime::null;

	dbiResult = DbiDateDecode(Date, &month, &day, &year);
	if (dbiResult != DBIERR_NONE) return dtRetVal;

	dtRetVal.SetDate(year, month, day);
	return dtRetVal;	
}



DBIResult CBdeDatabase::OleDateTimeToTimeStamp(COleDateTime dt, pTIMESTAMP pTimeStamp)
{
	DBIResult dbiResult;       // Return value from IDAPI functions
	TIME Time; // Time portion of the TimeStamp, actually a LONG
	INT32 n32; // date portion of the timestamp
	UINT16 hour, min, msec, day, month;
	INT16 year;

	// Get the date and time components
	year = (INT16)dt.GetYear();
	month = (UINT16)dt.GetMonth();
	day = (UINT16)dt.GetDay();
	hour = (UINT16)dt.GetHour();
	min = (UINT16)dt.GetMinute();
	msec = (UINT16)(dt.GetSecond()*1000);

	dbiResult = ::DbiTimeEncode(hour, min, msec, &Time);
	if (dbiResult != DBIERR_NONE) return dbiResult;

	dbiResult = DbiDateEncode(month, day, year, &n32);
	if (dbiResult != DBIERR_NONE) return dbiResult;

	dbiResult = ::DbiTimeStampEncode(n32, Time, pTimeStamp);
	if (dbiResult != DBIERR_NONE) return dbiResult;

	return DBIERR_NONE;	
}


INT32 CBdeDatabase::OleDateTimeToDate(COleDateTime dt)
{
	INT32 n32 = 0;
	UINT16 day, month;
	INT16 year;

	day = (UINT16)dt.GetDay();
	month = (UINT16)dt.GetMonth();
	year = (INT16)dt.GetYear();
		
	::DbiDateEncode(month, day, year, &n32);
	return n32;	
}


TIME CBdeDatabase::OleDateTimeToTime(COleDateTime dt)
{
	UINT16 hour, min, msec;
	TIME time = 0L;

	hour = (UINT16)dt.GetHour();
	min = (UINT16)dt.GetMinute();
	msec = (UINT16)(dt.GetSecond() * 1000);

	::DbiTimeEncode(hour, min, msec, &time);
	return time;	
}




/////////////////////////////////////////////////////////////////////////////////
//  Functions to set field values


// set field as a string, pass NULL or blank to set as a blank
BOOL CBdeDatabase::SetFieldAsString(INT16 nFieldNumber, LPCTSTR szValue, 
	BOOL bBlank /* = FALSE */)
{
	// Throw an exception if not in edit mode
	if (!CheckValidCursor("Failed to set field value.")) return FALSE;
	if (!CheckEditMode("Failed to set field value.")) return FALSE;

	DBIResult dbiResult;
	int nBdeExError = 0;
  CURProps curProps; // Table Properties
	pFLDDesc pFldDesc; // field information
	CString strError;

	// make sure edit mode allows editing
	// and m_pEditRecordBuffer is not NULL
	if (m_pEditRecordBuffer == NULL)
	{
		strError.Format("Failed to set field %d to value '%s'.", nFieldNumber, szValue);
		throw new CBdeException(DBIERR_NONE, BDEEXERR_NOTINEDITMODE, m_szTableName,
			m_szDatabaseName, strError);
		return FALSE;
	}

	// 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
	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;
	}

	UINT16 nSize = pFldDesc[nFieldNumber-1].iLen;
	UINT16 nFieldType = pFldDesc[nFieldNumber-1].iFldType;
	UINT16 nFieldSubType = pFldDesc[nFieldNumber-1].iSubType;
	UINT16 nPrecision = pFldDesc[nFieldNumber-1].iUnits2;
	if (m_nTableType == TABLETYPE_PARADOX) nPrecision = 32;
	UINT16 nDecimals = pFldDesc[nFieldNumber-1].iUnits2;

	free(pFldDesc); // clean up the field descriptor
	pFldDesc = NULL;

	// handle the situation for a blank
	if (bBlank)
	{
		dbiResult = DbiPutField(m_hCursor, nFieldNumber, m_pEditRecordBuffer, NULL);
		return TRUE;
	}

	// handle the situation for a NULL buffer
	if (szValue == NULL)
	{
		dbiResult = DbiPutField(m_hCursor, nFieldNumber, m_pEditRecordBuffer, NULL);
		return TRUE;
	}

	// if an empty string is passed, then set the field to blank
	if (strlen(szValue) == 0)
	{
		dbiResult = DbiPutField(m_hCursor, nFieldNumber, m_pEditRecordBuffer, NULL);
		return TRUE;
	}

	// allocate memory for the string buffer to recieve data
	pBYTE pSourceBuf = (pBYTE)malloc(nSize*sizeof BYTE);
	memset(pSourceBuf, 0, nSize*sizeof BYTE);

	// check for memory error
	if (pSourceBuf == NULL)
	{
		// Throw memory exception here
		free(pSourceBuf);
		strError.Format("Failed to set field %d to value '%s'.", nFieldNumber, szValue);
		throw new CBdeException(DBIERR_NOMEMORY, m_szTableName

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -