home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1997 May
/
Pcwk0597.iso
/
sybase
/
starbuck
/
hpp.z
/
WDATASRC.HPP
< prev
next >
Wrap
C/C++ Source or Header
|
1996-12-04
|
18KB
|
580 lines
#ifndef _WDATASRC_HPP_INCLUDED
#define _WDATASRC_HPP_INCLUDED
//-----------------------------------------------------------------------
//
// Interfaces used to implement the binding of objects to data sources.
//
// These interfaces are meant to be embedded in other objects. All
// methods are virtual so that specializations can be created.
//
//-----------------------------------------------------------------------
#ifndef _WDATAERR_HPP_INCLUDED
#include "wdataerr.hpp"
#endif
#ifndef _WDATATRG_HPP_INCLUDED
#include "wdatatrg.hpp"
#endif
enum WDSMoveType {
WDSMoveNext = SQL_FETCH_NEXT,
WDSMoveFirst = SQL_FETCH_FIRST,
WDSMoveLast = SQL_FETCH_LAST,
WDSMovePrevious = SQL_FETCH_PRIOR,
WDSMoveAbsolute = SQL_FETCH_ABSOLUTE,
WDSMoveRelative = SQL_FETCH_RELATIVE,
WDSMoveBookmark = SQL_FETCH_BOOKMARK
};
enum WDSEditMode {
WDSEditModeRead, // just reading
WDSEditModeEdit, // editing current row
WDSEditModeAdd // adding a new row
};
//
// Declare the classes for state information
//
typedef void *WDataSourceState;
//
// WDataSource -- An interface exposed by an object which has data
// that is to be bound to other objects.
//
class WCMCLASS WDataSource : public WEventGenerator {
private:
WDataSource( const WDataSource & );
WDataSource& operator=( const WDataSource & );
public:
WDataSource();
~WDataSource();
/*********************************************************
* Operators
*********************************************************/
// operator[]
const WDataColumn & operator[]( WShort i ) const;
const WDataColumn & operator[]( const WString & name ) const;
/*********************************************************
* Properties
*********************************************************/
// AutoEdit
//
// If TRUE, data targets will automatically engage edit mode
// when their value is changed.
virtual WBool GetAutoEdit() const;
virtual WBool SetAutoEdit( WBool autoEdit );
// AutoRefresh
//
// If TRUE, updates and deletes will appear immediately in any
// attached data targets.
virtual WBool GetAutoRefresh() const;
virtual WBool SetAutoRefresh( WBool autoRefresh );
// BOF
//
// Returns TRUE if the cursor is positioned before the
// first row in the result set.
virtual WBool GetBOF() const;
// Bookmark
//
// Returns an integer which is used as a bookmark for
// the current row. You can move to it using the MoveBookmark
// method.
virtual WDWord GetBookmark() const;
// Column
//
// Return a specific column, either by index (columns
// start at 0) or by name.
virtual const WDataColumn & GetColumn( WShort index ) const;
virtual const WDataColumn & GetColumn( const WString & name ) const;
// ColumnCount
//
// Return the number of columns available (note that column 0
// is always a special column).
virtual WShort GetColumnCount() const;
// ColumnIndex
//
// Return the index of a column given its name. Returns -1
// on error.
virtual WShort GetColumnIndex( const WString & str ) const;
// CurrentRow
//
// The current row (rows start at 1), if known.
virtual WLong GetCurrentRow() const;
// EditMode
//
// The current edit mode.
virtual WDSEditMode GetEditMode() const;
// EOF
//
// Returns TRUE if the cursor is positioned after the
// last row in the result set.
virtual WBool GetEOF() const;
// Empty
//
// Returns TRUE if the result set is empty (no rows)
// or null (no columns).
virtual WBool GetEmpty() const;
// ErrorCode
//
// Returns the error code of the last operation.
// Can optionally return a code specifying which driver
// API was called (driver-specific).
virtual WLong GetErrorCode( WLong *apiCode=NULL ) const;
// ErrorList
//
// Returns the list of errors from the last operation.
virtual WDataErrorArray GetErrorList() const;
// ForwardOnly
//
// Returns TRUE if moves are only allowed in the forward
// direction.
virtual WBool GetForwardOnly() const;
// Null
//
// Returns TRUE if the result set is null (no columns).
// This is NOT the same as an empty result set.
virtual WBool GetNull() const;
// Opened
//
// Returns TRUE if the datasource is "open" (it has data).
virtual WBool GetOpened() const;
// RawData
//
// Fetch data directly from the data source, bypassing
// any binding that may have occurred. Use this to get
// chunks of data.
virtual WBool GetRawData( WShort index, WNativeDataType type,
void *buffer, WLong bufferSize=0,
WLong *bytesRemaining=NULL ) const;
// ReadOnly
//
// If TRUE, no changes can be made to the data source.
virtual WBool GetReadOnly() const;
// RowTargetChanged
//
// If TRUE, one or more of the values in the row
// was changed but the data source is readonly
virtual WBool GetRowTargetChanged() const;
// RowChanged
//
// If TRUE, one or more of the values in the row may need
// to be updated.
virtual WBool GetRowChanged() const;
virtual WBool SetRowChanged( WBool changed );
// RowCount
//
// Returns the number of rows. This may be expensive to call,
// as it may force rows to be counted one by one. If you pass
// in FALSE, it simply uses the last known row count value,
// which may be -1 if the count is not yet known.
// If you just need to know whether a result set is empty or null,
// see the Empty and Null properties instead.
virtual WLong GetRowCount( WBool forceCount=TRUE ) const;
// TargetsEnabled
//
// Enable or disable the targets. Disabling does not do a
// clear, merely prevents them from updating themselves.
virtual WBool GetTargetsEnabled() const;
virtual WBool SetTargetsEnabled( WBool enabled );
// ThreadSafe
//
// If TRUE, the object can be accessed safely from different
// threads and the ThreadLock/ThreadUnlock methods are enabled.
virtual WBool GetThreadSafe() const;
virtual WBool SetThreadSafe( WBool safe );
// AlwaysReadWriteTargets
//
// If TRUE, enables targets to be enabled for read and write
// even when the data source is read only
virtual WBool GetAlwaysReadWriteTargets() const;
virtual WBool SetAlwaysReadWriteTargets( WBool enable );
// Value
//
// Set/get the value of a particular column in the
// current row. See RawData as well.
virtual WDataValue GetValue( WShort index,
WNativeDataType type=SQL_C_DEFAULT ) const = 0;
virtual WBool SetValue( WShort index, const WDataValue & val ) = 0;
/*********************************************************
* Methods
*********************************************************/
// Add
//
// Prepare to add a new row. Returns FALSE if there are no
// updatable columns or the query is read-only. After setting
// the new values with SetValue, call Update to actually
// add the new row. Call CancelUpdate to cancel any pending
// add.
virtual WBool Add( WBool copyValues=FALSE, WBool append=FALSE,
WBool copyIntoBuffer=FALSE );
// CancelUpdate
//
// Cancel a pending update or add operation.
virtual WBool CancelUpdate( WBool notifyTargets=TRUE );
// ClearTargets
//
// Tell any targets to clear themselves.
virtual WBool ClearTargets();
// Close
//
// Close the current query, if any.
virtual WBool Close( WBool discardResults=TRUE,
WBool isRequery=FALSE );
// Delete
//
// Deletes the current row.
virtual WBool Delete( WBool triggerEvent=TRUE,
WBool notifyTargets=TRUE );
// Edit
//
// Prepare the current row for editing. Returns FALSE if
// there are no updatable columns or the query is read-only.
// After setting the new values with SetValue, call Update
// to actually update the row. Call CancelUpdate to cancel
// any pending add.
virtual WBool Edit();
// MoreResults
//
// Closes the current result set and opens the next
// result set, if any. Returns TRUE if a new result
// set is available and open, FALSE otherwise.
virtual WBool MoreResults();
// Move
//
// General move routine for navigating through the data
// source. By default does an absolute move.
virtual WBool Move( WLong row, WBool notifyTargets=TRUE,
WDSMoveType type=WDSMoveAbsolute,
WBool triggerEvents=TRUE );
// MoveBookmark
//
// Move to a bookmark.
WBool MoveBookmark( WDWord bookmark, WBool notify=TRUE,
WBool triggerEvents=TRUE );
// MoveFirst
//
// Move to the first row.
WBool MoveFirst( WBool notifyTargets=TRUE, WBool triggerEvents=TRUE );
// MoveLast
//
// Move to the last row.
WBool MoveLast( WBool notifyTargets=TRUE, WBool triggerEvents=TRUE );
// MoveNext
//
// Move to the next row.
WBool MoveNext( WBool notifyTargets=TRUE, WBool triggerEvents=TRUE );
// MovePrevious
//
// Move to the previous row.
WBool MovePrevious( WBool notifyTargets=TRUE, WBool triggerEvents=TRUE );
// MoveRelative
//
// Move to a relative row.
WBool MoveRelative( WLong offset, WBool notifyTargets=TRUE,
WBool triggerEvents=TRUE );
// Open
//
// Execute the SQL statement and open the result set for
// processing.
virtual WBool Open( WBool executeStatement=TRUE,
WBool isRequery=FALSE );
// Refresh
//
// Refresh (re-read) all of the data. The cursor is position is
// retained if possible.
virtual WBool Refresh();
// RefreshTargets
//
// Refresh the targets, causing them to re-read their values
// for the current row. Can optionally specify whether
// multirow targets should reread all their values.
virtual WBool RefreshTargets( WBool multiRow=FALSE );
// RestoreState
//
// Restore the data source's state to a previously saved
// value.
virtual WBool RestoreState( const WDataSourceState state,
WBool restore=TRUE );
// SaveState
//
// Save the state of a data source. What gets saved
// depends on the data source. It might be a bookmark,
// it might be much more.
virtual WBool SaveState( WDataSourceState & state );
// ThreadLock
//
// Lock access to the data source to the thread that
// calls this function. Can specify what to do if
// the thread is already locked. Note that once a
// thread is locked care must be taken in calling
// methods that may trigger events or notify targets,
// since deadlock can occur.
virtual WBool ThreadLock( WBool wait=TRUE, WDWord timeout=0xFFFFFFFF );
// ThreadUnlock
//
// Unlock the data source. Returns TRUE if the source is no
// longer locked (or was never locked) by the calling thread.
virtual WBool ThreadUnlock();
// Update
//
// Update the row (either the current row or a row that is
// being added) to the database. Can optionally cause a
// ValidateData event to be triggered.
virtual WBool Update( WBool triggerEvent=TRUE,
WBool notifyTargets=TRUE );
/*********************************************************
* Other
*********************************************************/
// DataAvailable
//
// Call the DataAvailable method on all registered targets.
virtual WBool DataAvailable( WLong row,
WULong reason,
WULong suggestedAction,
WBool multiValueOnly );
// DataClose
//
// Call the DataClose method on all registered targets.
virtual WBool DataClose( WBool isRequery );
// DataOpen
//
// Call the DataOpen method on all registered targets.
virtual WBool DataOpen( WBool isRequery );
// DataRequest
//
// Call the DataRequest method on all registered targets.
virtual WBool DataRequest( WULong reason, WULong suggestedAction );
// RemoveAllTargets
//
// Call SetDataSource(NULL) on each target object.
virtual WBool RemoveAllTargets();
/*********************************************************
* Target Methods (only targets should call these)
*********************************************************/
// AddTarget
//
// DataTarget objects call this method to register themselves
// with a data source.
virtual WBool AddTarget( WDataTarget *target );
// BindTarget
//
// DataTarget objects call this method when processing a
// DataOpen event to let the source know which columns
// they are interested in.
virtual WBool BindTarget( WDataTarget *target, WShort column,
WNativeDataType type=SQL_C_DEFAULT );
// RemoveTarget
//
// DataTarget objects call this method to deregister themselves.
virtual WBool RemoveTarget( WDataTarget *target );
public:
wllist_header & GetTargetListHeader();
wllist_header & GetBindListHeader();
void ClearBindList();
WBool GetTargetRowExtents( WLong & firstRow, WLong & lastRow );
protected:
// List of registered targets...
struct WTargetEntry {
wllist_link link;
WDataTarget *target;
WDataTargetType type;
WShort firstColumn; // first column bound to the control
};
WTargetEntry *FindTarget( WDataTarget *target );
WBool SortTargets();
// List of binding suggestions...
struct WBindEntry {
wllist_link link;
WShort column;
WDataTarget *target;
WNativeDataType type;
};
wllist_header _targetList;
wllist_header _bindList;
WLong _row;
WBool _rowChanged;
WBool _targetsEnabled;
WBool _alwaysReadWriteTargets;
WBool _rowTargetChanged;
};
typedef WEventData WAdjustCursorEventData;
typedef WEventData WFetchDataEventData;
typedef WEventData WMoveCursorEventData;
typedef WEventData WBeginFetchDataEventData;
typedef WEventData WBeginMoveCursorEventData;
typedef WEventData WQueryBusyEventData;
enum WDSValidateAction {
WDSVAMoveNext = WDSMoveNext,
WDSVAMoveFirst = WDSMoveFirst,
WDSVAMoveLast = WDSMoveLast,
WDSVAMovePrevious = WDSMovePrevious,
WDSVAMoveAbsolute = WDSMoveAbsolute,
WDSVAMoveRelative = WDSMoveRelative,
WDSVAMoveBookmark = WDSMoveBookmark,
WDSVAUpdateCurrentRow,
WDSVAUpdateNewRow,
WDSVADeleteCurrentRow,
};
struct WQueryOpenEventData : public WEventData {
WBool isRequery;
};
struct WQueryCloseEventData : public WEventData {
WBool isRequery;
};
struct WValidateDataEventData : public WEventData {
WDSValidateAction action; // action about to happen
WLong row; // row data for action
WBool cancel; // set to TRUE to cancel the action
WBool update; // if TRUE, data is updated before move
//
// Use when updatePolicy of query object is WQUPUserDefine.
//
// The user should set this to TRUE when successful update to the
// current row in the event handler has been done.
//
WBool hasPerformUpdate;
};
#endif