/** * @file XTPCalendarDatabaseDataProvider.h * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ #if !defined(_XTPCALENDARDATABASEDATAPROVIDER_H__) # define _XTPCALENDARDATABASEDATAPROVIDER_H__ # if _MSC_VER > 1000 # pragma once # endif // _MSC_VER > 1000 # include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" class CXTPCalendarSchedule; class CXTPCalendarSchedules; struct XTP_CALENDAR_RECURRENCE_OPTIONS; /** @cond */ XTP_DEFINE_SMART_PTR_INTERNAL(CXTPCalendarSchedules) /** @endcond */ /** * @brief * This class is the implementation of the XTPCalendarData abstract * class which represents the data portion of the Calendar control. * * @details * This implementation uses a MS Access database as the data source. * The Data source stores all Events data and provides a way to retrieve * and save CXTPCalendarEvent objects. * * To use CXTPCalendarDatabaseDataProvider, first construct an instance * of the CXTPCalendarDatabaseDataProvider object. Then use the Open() member * function to establish a connection to the data source and initialize the * object. The CXTPCalendarDatabaseDataProvider class provides a number of * functions to manipulate events and event related data. After finishing * work with the data source, call the Close() member function to close the * connection, de-initialize the object and free any unused resources. * * @see CXTPCalendarData * @see CXTPCalendarMemoryDataProvider */ class _XTP_EXT_CLASS CXTPCalendarDatabaseDataProvider : public CXTPCalendarData { class CADOCommand; /** @cond */ DECLARE_DYNAMIC(CXTPCalendarDatabaseDataProvider) /** @endcond */ public: /** * @brief * Default constructor. * * @details * Constructs a Data object. To establish a connection to a * specified data source and initialize the object the * Open() member function must be called immediately after * construction. * * @see Open() */ CXTPCalendarDatabaseDataProvider(); /** * @brief * Default Destructor. * * @details * Handles all deallocation. */ virtual ~CXTPCalendarDatabaseDataProvider(); /** * @brief * Call this member function to initialize a newly constructed * Data object. * * @details * Data object must be created before you call Open. * * @return TRUE is successful. FALSE otherwise. */ virtual BOOL Open(); /** * @brief * Call this member function to initialize a data provider using an * existing ADO Connection object. * * @param pdispADOConnection [in] An IDispatch interface of ADO Connection. * * @details * Set Connection String as "Provider=ADO;" (or "Provider=Access;") * Connection object must be opened before pass to OpenEx. * Also the calendar data provider does not close the ADO Connection * through the Close method. You have to close it yourself. * * @return TRUE is successful. FALSE otherwise. */ virtual BOOL OpenEx(LPDISPATCH pdispADOConnection); /** * @brief * Call this member function to create an empty database. * * @details * A database is created with the necessary tables structure. * It is opened by default. * * @return TRUE is successful. FALSE otherwise. */ virtual BOOL Create(); /** * @brief * This method saves data to the database. * * @details * Actually, this method just always returns TRUE for the * database data provider. The reason is that database data provider * always keep consistency of its data and could be closed without * saving. The database file would contain the most recent data. * * @return TRUE is successful. FALSE otherwise. */ virtual BOOL Save(); /** * @brief * Call this member function to close the connection to a data source. */ virtual void Close(); protected: /** * @brief * Retrieve day events for a specified day. * * @param dtDay A specified day. * * @details * This method creates and executes a database query on order to * retrieve all appointments for a specified day. * * @return A collection of events for a specified day. */ virtual CXTPCalendarEventsPtr DoRetrieveDayEvents(COleDateTime dtDay); /** * @brief * Remove all events from the database. * * @details * This method creates and executes a database query which * removes all appointments from the database. */ virtual void DoRemoveAllEvents(); /** * @brief * Read event from the database. * * @param dwEventID Event ID of the read target. * * @details * This method creates and executes a database query which * retrieves all data for a specified event. * * @return A pointer to the new CXTPCalendarEvent object which fields * contains a data from the database. */ virtual CXTPCalendarEventPtr DoRead_Event(DWORD dwEventID); /** * @brief * Read recurrence pattern from the database. * * @param dwPatternID Pattern ID of the read target. * * @details * This method creates and executes a database query which * retrieves all data for a specified recurrence pattern. * * @return A pointer to the created CXTPCalendarRecurrencePattern object. */ virtual CXTPCalendarRecurrencePatternPtr DoRead_RPattern(DWORD dwPatternID); /** * @brief * Creates a new event in the database. * * @param pEvent Pointer to the CXTPCalendarEvent object which describes * all data fields of a newly created record. * @param rdwNewEventID [out] EventID of a newly created record, * it is an auto-generated numeric events table key. * * @details * This method creates and executes a database query which * adds a new record to the events table. * * @return TRUE if event created successfully, * FALSE in case of any error during the process. */ virtual BOOL DoCreate_Event(CXTPCalendarEvent* pEvent, DWORD& rdwNewEventID); /** * @brief * Updates event properties in the database. * * @param pEvent Pointer to the CXTPCalendarEvent object which describes * all data fields of an event. * * @details * This method creates and executes a database query which updates * a corresponding database record with properties from the specified * event. * * @return TRUE if an event updated successfully, * FALSE in case of any errors during the process. */ virtual BOOL DoUpdate_Event(CXTPCalendarEvent* pEvent); /** * @brief * Deletes an event from the database. * * @param pEvent Pointer to the CXTPCalendarEvent object which describes * all data fields of an event. * * @details * This method creates and executes a database query which deletes * a corresponding database record. * * @return TRUE if an event deleted successfully, * FALSE in case of any errors during the process. */ virtual BOOL DoDelete_Event(CXTPCalendarEvent* pEvent); /** * @brief * Creates a new recurrence pattern in the database * * @param pPattern Pointer to the CXTPCalendarRecurrencePattern object which * describes all data fields of a newly created record. * @param rdwNewPatternID [out] ID of a newly created record, * it is an auto-generated numeric table key. * * @details * This method creates and executes a database query which * adds a new record to the recurrence patterns table. * * @return TRUE if recurrence pattern created successfully, * FALSE in case of any error during the process. */ virtual BOOL DoCreate_RPattern(CXTPCalendarRecurrencePattern* pPattern, DWORD& rdwNewPatternID); /** * @brief * Updates a recurrence pattern in the database. * * @param pPattern Pointer to the CXTPCalendarRecurrencePattern object. * * @details * This method creates and executes a database query which * updates a corresponding record in the recurrence patterns table. * * @return TRUE if recurrence pattern updated successfully, * FALSE in case of any error during the process. */ virtual BOOL DoUpdate_RPattern(CXTPCalendarRecurrencePattern* pPattern); /** * @brief * Deletes a recurrence pattern from the database * * @param pPattern Pointer to the CXTPCalendarRecurrencePattern object * which should be deleted. * * @details * This method creates and executes a database query which * deletes a corresponding record from the recurrence patterns table. * * @return TRUE if recurrence pattern deleted successfully, * FALSE in case of any error during the process. */ virtual BOOL DoDelete_RPattern(CXTPCalendarRecurrencePattern* pPattern); /** * @brief * This method implements Getting all events from the database * without generating recurrence occurrences. * * @details * Simple events are included as is. * For the recurrence events only master events are included, * recurrence occurrences are not generated. * * @return * A CXTPCalendarEvents pointer containing a collection of * all events. * * @see GetAllEvents_raw() */ virtual CXTPCalendarEventsPtr DoGetAllEvents_raw(); /** * @brief * Retrieves a collection of events expiring in the period from * dtFrom during next spPeriod. * * @param dtFrom Start date and time of a specified period. * @param spPeriod A duration of a specified period. * * @details * This method creates and executes a database query which * returns a collection of upcoming events. * It is used by a reminders manager. * * @return A CXTPCalendarEvents pointer containing a collection of * upcoming events. * * @see CXTPCalendarRemindersManager overview */ virtual CXTPCalendarEventsPtr DoGetUpcomingEvents(COleDateTime dtFrom, COleDateTimeSpan spPeriod); public: static void AFX_CDECL TRACE_ProviderError(XTPADODB::_Connection* pConnDB); static void AFX_CDECL TRACE_ComError(_com_error& e); private: virtual CXTPCalendarEventPtr _ReadEvent_common(XTPADODB::_Recordset* pRS, BOOL bEcxception); virtual BOOL _ReadRExceptions(CXTPCalendarRecurrencePattern* pPattern); virtual BOOL _GetRPatternOptions(XTPADODB::_Recordset* pRS, XTP_CALENDAR_RECURRENCE_OPTIONS* rROptions); /** * @brief * This member function is used to update options data in the database. * * @param strOptionsData Options data in XML format. * * @return TRUE if successful. Otherwise FALSE. * * @see LoadOptions */ virtual BOOL SaveOptions(const CString& strOptionsData); /** * @brief * This member function is used to load options data from the database. * * @return TRUE if successful. Otherwise FALSE. * * @see SaveOptions */ virtual BOOL LoadOptions(); virtual CXTPCalendarSchedulesPtr LoadSchedules(); virtual void UpdateSchedules(); virtual void _AddSchedules(CXTPCalendarSchedules* pAddSet); virtual void _UpdateSchedules(CXTPCalendarSchedules* pUpdateSet, CXTPCalendarSchedules* pOrigSet); virtual void _DeleteSchedules(CUIntArray* pDeleteIDs); virtual CXTPCalendarSchedule* _ReadSchedule(XTPADODB::_Recordset* pRS); protected: BOOL m_bTraceOptions; /** Indicates whether there should be only one record in the options table, or every save should add the new one. */ private: BOOL _Open(); void CreateEventTable(XTPADOX::_CatalogPtr ptrCatalog); void CreateRPatternTable(XTPADOX::_CatalogPtr ptrCatalog); void CreateOptionsTable(XTPADOX::_CatalogPtr ptrCatalog); void CreateSchedulesTable(XTPADOX::_CatalogPtr ptrCatalog); BOOL IsTableExist(XTPADOX::_CatalogPtr ptrCatalog, LPCWSTR pwszTableName); // void CreateDicTable(XTPADOX::_CatalogPtr ptrCatalog, LPCWSTR strTable, // LPCWSTR strNameID, bool bAuto = true); BOOL UpdateDBStructure(XTPADODB::_Connection* pconnDb); // members XTPADODB::_ConnectionPtr m_pconnDb; BOOL m_bCloseDbConnection; CADOCommand* m_pcmdGetLastID; CADOCommand* m_pcmdAddEvent; CADOCommand* m_pcmdDelEvent; CADOCommand* m_pcmdUpdEvent; CADOCommand* m_pcmdGetDayEvents; CADOCommand* m_pcmdGetRExceptions; CADOCommand* m_pcmdGetEvent; CADOCommand* m_pcmdUpdEventPatternID; CADOCommand* m_pcmdAddRPattern; CADOCommand* m_pcmdDelRPattern; CADOCommand* m_pcmdUpdRPattern; CADOCommand* m_pcmdGetRPattern; CADOCommand* m_pcmdRemoveAllEvents; CADOCommand* m_pcmdRemoveAllRPatterns; CADOCommand* m_pcmdRemoveAllOptions; CADOCommand* m_pcmdGetOptions; CADOCommand* m_pcmdAddOptions; CADOCommand* m_pcmdAddSchedule; CADOCommand* m_pcmdUpdSchedule; private: // internal only used member functions virtual BOOL _SetRPatternOptions(CADOCommand* pCmd, CXTPCalendarRecurrencePattern* pPattern); void DeleteAllDBCommands(); void CreateGetLastIDCommand(); void CreateAddEventCommand(); void CreateDelEventCommand(); void CreateUpdEventCommand(); void CreateGetDayEventCommand(); void CreateAddRPatternCommand(); void CreateDelRPatternCommand(); void CreateUpdRPatternCommand(); void CreateGetRPatternCommand(); void CreateGetRExceptionsCommand(); void CreateGetEventCommand(); void CreateUpdEventPatternIDCommand(); void CreateRemoveAllCommands(); void CreateGetOptionsCommand(); void CreateAddOptionsCommand(); void CreateAddScheduleCommand(); void CreateUpdScheduleCommand(); }; # include "Common/Base/Diagnostic/XTPEnableNoisyWarnings.h" #endif // !defined(_XTPCALENDARDATABASEDATAPROVIDER_H__)