home *** CD-ROM | disk | FTP | other *** search
- unit RestForm;
-
- interface
-
- uses
- {$IFDEF WIN32}Windows, {$ELSE}WinTypes, WinProcs, {$ENDIF}
- Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Menus, QDB, StdCtrls, Grids, ExtCtrls, QDBView {, ComCtrls};
-
- type
- TRestructureForm = class(TForm)
- RestructureButton : TButton;
- CancelButton : TButton;
- Fields : TStringGrid;
- HelpButton : TButton;
- procedure FieldsDragOver(Sender, Source : TObject; X, Y : integer;
- State : TDragState; var Accept : boolean);
- procedure FieldsDragDrop(Sender, Source : TObject; X, Y : integer);
- procedure CancelButtonClick(Sender : TObject);
- procedure FieldsMouseDown(Sender : TObject; Button : TMouseButton;
- Shift : TShiftState; X, Y : integer);
- procedure RestructureButtonClick(Sender : TObject);
- procedure FormActivate(Sender : TObject);
- procedure HelpButtonClick(Sender : TObject);
- procedure FormCreate(Sender : TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- FileName : string;
- Panel : TCustomPanel;
- end;
-
- var
- RestructureForm : TRestructureForm;
- Q : TQDBView;
-
- implementation
-
- {$R *.DFM}
-
- uses
- RestHelp;
-
- const
- SBadTemp = 'could not create a temp file';
-
- { Only accept dragged items from the grid's column 1 }
-
- procedure TRestructureForm.FieldsDragOver(Sender, Source : TObject; X, Y : integer;
- State : TDragState; var Accept : boolean);
- var
- c, R : longint;
- begin
- Accept := false;
- Fields.MouseToCell(X, Y, c, R);
- if (Source = Fields) and (c = 1) then
- Accept := true;
- end;
-
- procedure TRestructureForm.FieldsDragDrop(Sender, Source : TObject; X, Y : integer);
- var
- c, R : longint;
- begin
- Fields.MouseToCell(X, Y, c, R);
- if Fields.Cells[1, R] = '' then
- begin
- if Source is TStringGrid then
- begin
- Fields.Cells[1, R] := TStringGrid(Source).Cells[1, TStringGrid(Source).Row];
- Fields.Cells[1, TStringGrid(Source).Row] := '';
- end;
- end;
- end;
-
- procedure TRestructureForm.FieldsMouseDown(Sender : TObject; Button : TMouseButton;
- Shift : TShiftState; X, Y : integer);
- begin
- Fields.BeginDrag(true);
- end;
-
- procedure TRestructureForm.CancelButtonClick(Sender : TObject);
- begin
- Close;
- end;
-
- type
- TQDBItemHack = class(TQDBItem);
-
- procedure TRestructureForm.RestructureButtonClick(Sender : TObject);
- const
- zero : longint = 0;
- var
- n : integer;
- i : integer;
- m : TMemoryStream;
- FName2 : string;
- FIndex2 : integer;
- newkey : TKey;
- TmpFileName : string;
- OldQDB : TQDBItem;
- NewQDB : TQDBItem;
- begin
- if (mrNo = MessageDlg('Restructuring the file will change any data stored in it. ' +
- 'Are you sure you want to do this?',mtWarning,[mbYes,mbNo],0)) then
- Exit;
- try
- { first open up the file via a QDBItem }
- OldQDB := TQDBItem.Create(Self);
- OldQDB.FileName := FileName;
- { load up its field definitions }
- OldQDB.FetchStructure;
- { now get a temp file name ... }
- TmpFileName := TempFileName('QZZ');
- if TmpFileName = '' then
- begin
- raise Exception.Create(SBadTemp);
- end;
- { there mustn't actually be any file yet }
- if FileExists(TmpFileName) then DeleteFile(TmpFileName);
- { ... and open it up via a new QDBItem }
- NewQDB := TQDBItem.Create(Self);
- NewQDB.FileName := TmpFileName;
- { now set up its field definitons from the QDBPanel ... the panel is still attached }
- for n := 1 to Q.FieldCount do
- NewQDB.AddField(Q.FieldNames[n - 1], Q.FieldTypes[n - 1]);
- { next we set up the field equivalences using the grids objects }
- { property to store the index of the matching fields }
- for i := 1 to NewQDB.FieldCount do
- begin
- FName2 := Fields.Cells[1, i];
- if FName2 = '' then
- FIndex2 := -1
- else
- FIndex2 := OldQDB.FieldIndex(FName2);
- Fields.Objects[1, i] := pointer(Findex2);
- end;
- { now we can iterate through the file modifying each item }
- for n := 1 to OldQDB.Count do
- begin
- { get the old item }
- OldQDB.ItemIndex := n - 1;
- OldQDB.Fetch;
- { copy the only the appropriate data into the new item (using m as a carrier) }
- for i := 1 to NewQDB.FieldCount do
- begin
- if longint(Fields.Objects[1, i]) <> -1 then
- m:=OldQDB.GetField(longint(Fields.Objects[1, i]))
- else
- m.Clear;
- TQDBItemHack(NewQDB).SetField(i - 1, m);
- m.Position:=0;
- end;
- { the new item is ready and can be stored in the new file }
- newkey := OldQDB.Key;
- NewQDB.StoreAs(newkey);
- end;
- { save and close the old file }
- OldQDB.FileName := '';
- OldQDB.Free;
- OldQDB := nil;
- { save and close the new file }
- NewQDB.FileName := '';
- NewQDB.Free;
- NewQDB := nil;
- { get rid of the old file }
- DeleteFile(FileName);
- { rename the new (temp) file to the old name }
- RenameOrMoveFile(TmpFileName, FileName);
- { open up the brand new file in a QDBPanel }
- Q.FileName := FileName;
- { setting the Panel property brands the file with }
- { the proper field definitions }
- Q.Panel := Panel;
- { all done so save and close the file }
- Q.FileName := '';
- finally
- ModalResult := mrOK;
- end;
- end;
-
- type
- TQDBViewHack = class(TQDBView);
-
- procedure TRestructureForm.FormActivate(Sender : TObject);
- var
- n : integer;
- T : TStringList;
- begin
- { first set up the grid }
- Fields.DefaultColWidth := Fields.ClientWidth div 2;
- Fields.Cells[0, 0] := 'New Fields in Panel';
- Fields.Cells[1, 0] := 'Old Fields in File';
- { open a QDBView and use it to get the field names from the file }
- T := TStringList.Create;
- try
- { this makes the file really open even though we are in design mode }
- TQDBViewHack(Q).Restructuring := true;
- Q.FileName := FileName;
- Q.ListFileFieldNames(T);
- Fields.RowCount := T.Count + 1;
- for n := 1 to T.Count do
- begin
- with Fields do
- begin
- Cells[1, n] := T[n - 1];
- end;
- end;
- { close the file down }
- Q.FileName := '';
- finally
- T.Free;
- end;
- { now attach our panel and get its field names }
- Q.Panel := Panel;
- Fields.RowCount := Fields.RowCount + Q.FieldCount;
- for n := 1 to Q.FieldCount do
- begin
- with Fields do
- begin
- Cells[0, n] := Q.FieldNames[n - 1];
- end;
- end;
- end;
-
- procedure TRestructureForm.HelpButtonClick(Sender : TObject);
- var
- RestHelpForm : TRestructureHelpForm;
- begin
- { just show a little help on restructuring }
- RestHelpForm := TRestructurehelpForm.Create(Application);
- try
- RestHelpForm.Memo1.WordWrap := true;
- RestHelpForm.ShowModal;
- finally
- RestHelpForm.Free;
- end;
- end;
-
- procedure TRestructureForm.FormCreate(Sender : TObject);
- begin
- Q := TQDBView.Create(Self);
- end;
-
- end.
-
-