The Data Set Component

Instead of focusing right away on the use of a specific dataset, I prefer starting with a generic introduction of the features of the TDataSet class, which are shared by all inherited data-access classes. The DataSet component is a very complex one, so I won't list all of its capabilities but only discuss its core elements.

The idea behind this component is to provide access to a series of records that are read from some source of data, kept in internal buffers (for performance reasons), and eventually modified by a user, with the possibility of writing back changes to the persistent storage. This approach is generic enough to be applied to different types of data (even non-database data) but has a few rules. First, there can be only one active record at a time, so if you need to access data in multiple records, you must move to each of them, read the data, then move again, and so on. You'll find an example of this and related techniques in the section about navigation.

Second, you can edit only the active record: you cannot modify a set of records at the same time, as you can in a relational database. Moreover, you can modify data in the active buffer only after you explicitly declare you want to do so, by giving the Edit command to the dataset. You can also use the Insert command to create a new blank record, and close both operations (insert or edit) by giving a Post command.

Other interesting elements of a dataset I will explore in the following sections are its status (and the status change events), navigation and record positions, and the role of the field objects. As a summary of the capabilities of the DataSet component, I've included the public methods of its class in Listing 13.1 (the code has been edited and commented for clarity). Not all of these methods are directly used everyday, but I decided to keep them all in the listing. In

Chapter 18, I'll also discuss the virtual methods of the protected portion of the class, which we'll need to override to build custom dataset components.

O |Listing 13.1: The public interface of the TDataSet class (excerpted)

TDataSet = class(TComponent, IProviderSupport) public

// create and destroy, open and close constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure Open; procedure Close;

property BeforeOpen: TDataSetNotifyEvent read FBeforeOpen write FBeforeOpen; property AfterOpen: TDataSetNotifyEvent read FAfterOpen write FAfterOpen; property BeforeClose: TDataSetNotifyEvent read FBeforeClose write FBeforeClose; property AfterClose: TDataSetNotifyEvent read FAfterClose write FAfterClose;

// status information function IsEmpty: Boolean;

property Active: Boolean read GetActive write SetActive default False; property State: TDataSetState read FState; function ActiveBuffer: PChar; property IsUniDirectional: Boolean read FIsUniDirectional write FIsUniDirectional default False; function UpdateStatus: TUpdateStatus; virtual; property RecordSize: Word read GetRecordSize;

property ObjectView: Boolean read FObjectView write SetObjectView; property RecordCount: Integer read GetRecordCount; function IsSequenced: Boolean; virtual; function IsLinkedTo(DataSource: TDataSource): Boolean;

  • datasource property DataSource: TDataSource read GetDataSource; procedure DisableControls; procedure EnableControls; function ControlsDisabled: Boolean;
  • fields, including blobs, details, calculated, and more function FieldByName(const FieldName: string): TField; function FindField(const FieldName: string): TField; procedure GetFieldList(List: TList; const FieldNames: string); procedure GetFieldNames(List: TStrings);

unt: Integer read GetFieldCount; fs: TFieldDefs read FFieldDefs write SetFieldDefs; fList: TFieldDefList read FFieldDefList; TFields read FFields; st: TFieldList read FFieldList; lues[const FieldName: string]: Variant

property

Fi

el

property

Fi

el

property

Fi

el

property

Fi

el

property

Fi

el

property

Fi

el

read GetFieldValue write SetFieldValue; default; property AggFields: TFields read FAggFields; property DataSetField: TDataSetField read FDataSetField write SetDataSetField; property DefaultFields: Boolean read FDefaultFields; procedure ClearFields;

function GetBlobFieldData(FieldNo: Integer;

var Buffer: TBlobByteData): Integer; virtual; function CreateBlobStream(Field: TField;

Mode: TBlobStreamMode): TStream; virtual; function GetFieldData(Field: TField;

Buffer: Pointer): Boolean; overload; virtual; procedure GetDetailDataSets(List: TList); virtual;

procedure GetDetailLinkFields(MasterFields, DetailFields: TList); virtual; function GetFieldData(FieldNo: Integer;

Buffer: Pointer): Boolean; overload; virtual; function GetFieldData(Field: TField; Buffer: Pointer; NativeFormat: Boolean):

Boolean; overload; virtual; property AutoCalcFields: Boolean read FAutoCalcFields write FAutoCalcFields default True; property OnCalcFields: TDataSetNotifyEvent read FOnCalcFields write FOnCalcFields;

// position, movement procedure CheckBrowseMode; procedure First; procedure Last; procedure Next; procedure Prior;

function MoveBy(Distance: Integer): Integer;

property RecNo: Integer read GetRecNo write SetRecNo;

property Bof: Boolean read FBOF;

property Eof: Boolean read FEOF;

procedure CursorPosChanged;

property BeforeScroll: TDataSetNotifyEvent read FBeforeScroll write FBeforeScroll; property AfterScroll: TDataSetNotifyEvent read FAfterScroll write FAfterScroll;

// bookmarks procedure FreeBookmark(Bookmark: TBookmark); virtual; function GetBookmark: TBookmark; virtual;

function BookmarkValid(Bookmark: TBookmark): Boolean; virtual; procedure GotoBookmark(Bookmark: TBookmark);

function CompareBookmarks(Bookmark1, Bookmark2: TBookmark): Integer; virtual property Bookmark: TBookmarkStr read GetBookmarkStr write SetBookmarkStr;

// find, locate function FindFirst: Boolean;

function FindLast: Boolean;

function FindNext: Boolean;

function FindPrior: Boolean; property Found: Boolean read GetFound;

function Locate(const KeyFields: string; const KeyValues: Variant;

Options: TLocateOptions): Boolean; virtual; function Lookup(const KeyFields: string; const KeyValues: Variant; const ResultFields: string): Variant; virtual;

// filtering property Filter: string read FFilterText write SetFilterText;

property Filtered: Boolean read FFiltered write SetFiltered default False;

property FilterOptions: TFilterOptions read FFilterOptions write SetFilterOptions default []; property OnFilterRecord: TFilterRecordEvent read FOnFilterRecord write SetOnFilterRecord;

// refreshing, updating procedure Refresh;

property BeforeRefresh: TDataSetNotifyEvent read FBeforeRefresh write FBeforeRefresh; property AfterRefresh: TDataSetNotifyEvent read FAfterRefresh write FAfterRefresh; procedure UpdateCursorPos; procedure UpdateRecord;

function GetCurrentRecord(Buffer: PChar): Boolean; virtual; procedure Resync(Mode: TResyncMode); virtual;

// editing, inserting, posting, and deleting property CanModify: Boolean read GetCanModify;

property Modified: Boolean read FModified;

procedure Append;

procedure Edit;

procedure Insert;

procedure Cancel; virtual;

procedure Delete;

procedure Post; virtual;

procedure AppendRecord(const Values: array of const); procedure InsertRecord(const Values: array of const); procedure SetFields(const Values: array of const);

  • events related to editing, inserting, posting, and deleting property BeforeInsert: TDataSetNotifyEvent read FBeforeInsert write FBeforeInsert; property AfterInsert: TDataSetNotifyEvent read FAfterInsert write FAfterInsert; property BeforeEdit: TDataSetNotifyEvent read FBeforeEdit write FBeforeEdit; property AfterEdit: TDataSetNotifyEvent read FAfterEdit write FAfterEdit; property BeforePost: TDataSetNotifyEvent read FBeforePost write FBeforePost; property AfterPost: TDataSetNotifyEvent read FAfterPost write FAfterPost; property BeforeCancel: TDataSetNotifyEvent read FBeforeCancel write FBeforeCancel; property AfterCancel: TDataSetNotifyEvent read FAfterCancel write FAfterCancel; property BeforeDelete: TDataSetNotifyEvent read FBeforeDelete write FBeforeDelete; property AfterDelete: TDataSetNotifyEvent read FAfterDelete write FAfterDelete; property OnDeleteError: TDataSetErrorEvent read FOnDeleteError write FOnDeleteError; property OnEditError: TDataSetErrorEvent read FOnEditError write FOnEditError; property OnNewRecord: TDataSetNotifyEvent read FOnNewRecord write FOnNewRecord; property OnPostError: TDataSetErrorEvent read FOnPostError write FOnPostError;
  • support, utilities function Translate(Src, Dest: PChar;

ToOem: Boolean): Integer; virtual; property Designer: TDataSetDesigner read FDesigner;

property BlockReadSize: Integer read FBlockReadSize write SetBlockReadSize; property SparseArrays: Boolean read FSparseArrays write SetSparseArrays; end;

Self Publishing a Book The Easy Way

Self Publishing a Book The Easy Way

This comprehensive guide will present you with a variety of self-publishing options and explore their viability. Well take a look at all types of books and book-delivery systems.

Get My Free Ebook


Post a comment