home *** CD-ROM | disk | FTP | other *** search
- {
- See the README.TXT for instructions on installing the demo db components
- from the BDETable into COMPLIB.DCL. These components are required for
- properly viewing or building the demo applications in this directory.
- }
-
- unit Dbbrowse;
-
- interface
-
- uses WinTypes, WinProcs, Classes, Graphics, Forms, DbForm, Controls, DB,
- Grids, Outline, BDETable, DBGrids, DBTables;
-
- type
- TDbBrowseForm = class(TDbForm)
- DbOutline: TOutline;
- DbTables: TTableList;
- TableFields: TFieldList;
- TableIndices: TIndexList;
- DbTablesNAME: TStringField;
- DbTablesDATE: TDateField;
- DbTablesTIME: TTimeField;
- DbTablesVIEW: TBooleanField;
- DbTablesEXTENSION: TStringField;
- TableFieldsNAME: TStringField;
- TableFieldsTYPE: TWordField;
- TableFieldsSUBTYPE: TWordField;
- TableFieldsUNITS1: TWordField;
- TableFieldsUNITS2: TWordField;
- TableFieldsLENGTH: TWordField;
- TableIndicesNAME: TStringField;
- TableIndicesUNIQUE: TBooleanField;
- procedure FormActivate(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure DbOutlineExpand(Sender: TObject; Index: Longint);
- public
- constructor Create (AOwner: TComponent; const Name: string);
- destructor Destroy; override; { Not normal, but symmetric with Create }
- procedure CreateTableView;
- end;
-
- implementation
-
- {$R *.DFM}
-
- uses SysUtils, DbMain, Dbtable, DbSQL, DbiTypes;
-
- type
-
- { Browse node classes }
-
- TDbBrowseNode = class
- private
- FForm: TDbBrowseForm;
- FBrowseName: TFileName;
- FExpandedOnce: Boolean;
- protected
- property ExpandedOnce: Boolean read FExpandedOnce;
- public
- constructor Create (AForm: TDbBrowseForm; ParentIdx: Longint;
- const BrowseName: TFileName;
- const Description: string);
- procedure Expand (ParentIdx: Longint); virtual;
- property Form: TDbBrowseForm read FForm;
- property BrowseName: TFileName read FBrowseName;
- end;
-
- TDbBrowseDatabaseNode = class (TDbBrowseNode)
- procedure Expand (ParentIdx: Longint); override;
- end;
-
- TDbBrowseTableNode = class (TDbBrowseNode)
- procedure Expand (ParentIdx: Longint); override;
- end;
-
- TDbBrowseFieldNode = class (TDbBrowseNode);
- TDbBrowseIndexNode = class (TDbBrowseNode);
-
- { Create a browse node and add it to DbOutline }
-
- constructor TDbBrowseNode.Create (AForm: TDbBrowseForm; ParentIdx: Longint;
- const BrowseName: TFileName;
- const Description: string);
- begin
- FForm := AForm;
- FBrowseName := BrowseName;
- FExpandedOnce := False;
- Form.DbOutline.AddChildObject (ParentIdx, Description, Self)
- end;
-
- { Expand nothing - for nodes that have nothing to expand }
-
- procedure TDbBrowseNode.Expand (ParentIdx: Longint);
- begin
- FExpandedOnce := True;
- end;
-
- { Get database description for initial browse node }
-
- function GetDbDesc (const Name: TFileName): string;
- var
- Desc: TDatabaseDesc;
- begin
- Desc := TDatabaseDesc.Create (Name);
- try
- Result := Format ('%-16s Type: %s',
- [Desc.Description.szName, Desc.Description.szDbType])
- finally
- Desc.Free
- end
- end;
-
- function GetFieldType (Form: TDbBrowseForm): string;
- const
- Types: array [fldUNKNOWN..fldLOCKINFO] of string [8] =
- ('Unknown', 'String', 'Date', 'Blob', 'Boolean', 'Int16',
- 'Int32', 'Float64', 'Decimal', 'Bytes', 'Time', 'DateTime',
- 'UInt16', 'UInt32', 'Float80', 'VarBytes', 'LockInfo');
-
- Subtypes: array [fldstMONEY..fldstTYPEDBINARY] of string [14] =
- ('Money', 'Memo', 'Binary', 'Formatted Memo', 'OLE',
- 'Graphic', 'dBase OLE', 'User Typed');
- var
- FType, FSubtype, FUnits1, FUnits2, FLen: Word;
- begin
- with Form do
- begin
- FType := TableFieldsTYPE.Value;
- if FType < Low (Types) then FType := Low (Types) else
- if FType > High (Types) then FType := Low (Types);
-
- FSubtype := TableFieldsSUBTYPE.Value;
- if FSubtype < Low (Subtypes) then FSubtype := 0 else
- if FSubtype > High (Subtypes) then FSubtype := 0;
-
- FUnits1 := TableFieldsUNITS1.Value;
- FUnits2 := TableFieldsUNITS2.Value;
- FLen := TableFieldsLENGTH.Value;
-
- case FType of
- fldDATE,
- fldTIME,
- fldTIMESTAMP,
- fldBOOL,
- fldINT16,
- fldINT32,
- fldUINT16,
- fldUINT32,
- fldLOCKINFO:
- Result := Types [FType];
-
- fldZSTRING,
- fldBYTES,
- fldVARBYTES:
- Result := Format ('%s(%d)', [Types [FType], FUnits1]);
-
- fldFLOATIEEE,
- fldFLOAT,
- fldBCD,
- fldBLOB:
- begin
- Result := Types [FType];
- if FSubtype > 0 then
- Result := Format ('%s:%s', [Result, Subtypes [FSubtype]]);
- if (FUnits1 > 1) or (FUnits2 > 0) then
- Result := Format ('%s(%d,%d)', [Result, FUnits1, FUnits2])
- end;
- else
- Result := Format ('%s(%d)', [Types [FType], FLen]);
- end
- end
- end;
-
- { Expand the tables in a database node }
-
- procedure TDbBrowseDatabaseNode.Expand (ParentIdx: Longint);
- var
- TableExt: string;
- Description: string;
- begin
- if not ExpandedOnce then
- with Form do
- begin
- DbOutline.BeginUpdate;
- try
- DbTables.DatabaseName := Form.Database.DatabaseName;
- DbTables.Active := True;
-
- while not DbTables.EOF do
- begin
- Description := Format ('%-24s %3s %4s %12s %8s',
- [DbTablesNAME.Value,
- DbTablesEXTENSION.Value,
- DbTablesVIEW.AsString,
- DbTablesDATE.DisplayText,
- DbTablesTIME.DisplayText]);
- if DbTablesEXTENSION.AsString = '' then
- TableExt := DbTablesNAME.Value
- else
- TableExt := Format ('%s.%s', [DbTablesNAME.Value,
- DbTablesEXTENSION.Value]);
- TDbBrowseTableNode.Create (Form, ParentIdx, TableExt, Description);
- DbTables.Next
- end;
-
- DbTables.Active := False;
- finally
- DbOutline.EndUpdate;
- end;
- end;
-
- inherited Expand (ParentIdx);
- end;
-
- { Expand the fields and indicies in a table node }
-
- procedure TDbBrowseTableNode.Expand (ParentIdx: Longint);
- var
- Description: string;
- begin
- if not ExpandedOnce then
- with Form do
- begin
- DbOutline.BeginUpdate;
- try
- TableIndices.DatabaseName := Database.DatabaseName;
- TableIndices.TableName := BrowseName;
- TableIndices.Active := True;
-
- while not TableIndices.EOF do
- begin
- if TableIndicesUNIQUE.Value then Description := 'Unique'
- else Description := '';
- Description := Format ('%s Index %s',
- [Description, TableIndicesNAME.AsString]);
- TDbBrowseIndexNode.Create (Form, ParentIdx, TableIndicesNAME.AsString,
- Description);
- TableIndices.Next
- end;
-
- TableIndices.Active := False;
-
- TableFields.DatabaseName := Database.DatabaseName;
- TableFields.TableName := BrowseName;
- TableFields.Active := True;
-
- while not TableFields.EOF do
- begin
- Description := Format ('Field %-16s %s',
- [TableFieldsNAME.AsString, GetFieldType (Form)]);
- TDbBrowseFieldNode.Create (Form, ParentIdx, TableFieldsNAME.AsString,
- Description);
- TableFields.Next
- end;
-
- TableFields.Active := False;
- finally
- DbOutline.EndUpdate;
- end;
- end;
-
- inherited Expand (ParentIdx);
- end;
-
- { Expand an outline node }
-
- procedure TDbBrowseForm.DbOutlineExpand(Sender: TObject; Index: Longint);
- begin
- TDbBrowseNode (DbOutline [Index].Data).Expand (Index)
- end;
-
- { Create a new table viewer }
-
- procedure TDbBrowseForm.CreateTableView;
- var
- BNode: TDbBrowseNode;
- begin
- with DbOutline do
- if SelectedItem <> 0 then
- begin
- BNode := TDbBrowseNode (DbOutline [SelectedItem].Data);
-
- if BNode is TDbBrowseTableNode then
- TTableViewForm.Create (Application, Database, BNode.BrowseName)
- else
- raise Exception.CreateFmt ('%s is not a table', [BNode.BrowseName]);
- end
- else raise Exception.Create ('First select a table');
- end;
-
- { Browse an open database }
-
- constructor TDbBrowseForm.Create (AOwner: TComponent; const Name: string);
- begin
- inherited Create (AOwner);
- Database := Session.OpenDatabase (Name);
- Caption := 'Browse - Database ' + Database.DatabaseName;
- TDbBrowseDatabaseNode.Create (Self, 0, Database.DatabaseName,
- GetDbDesc (Database.DatabaseName));
- Show
- end;
-
- destructor TDbBrowseForm.Destroy;
- var
- Ix: Longint;
- begin
- with DbOutline do
- begin
- BeginUpdate;
- for Ix := ItemCount downto 1 do TDbBrowseNode (DbOutline [Ix].Data).Free;
- EndUpdate;
- end;
-
- inherited Destroy;
- end;
-
- procedure TDbBrowseForm.FormActivate(Sender: TObject);
- begin
- DbMainForm.SetActiveForm (nil, nil);
- end;
-
- procedure TDbBrowseForm.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- DbMainForm.FormCloseActive (Sender, Action);
- end;
-
- end.
-