| 1 | unit FormTables;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
|---|
| 7 | ActnList, Menus, DbEngine, FormEx;
|
|---|
| 8 |
|
|---|
| 9 | type
|
|---|
| 10 |
|
|---|
| 11 | { TFormTables }
|
|---|
| 12 |
|
|---|
| 13 | TFormTables = class(TFormEx)
|
|---|
| 14 | AAdd: TAction;
|
|---|
| 15 | AShowFields: TAction;
|
|---|
| 16 | AShowRecords: TAction;
|
|---|
| 17 | AModify: TAction;
|
|---|
| 18 | ARemove: TAction;
|
|---|
| 19 | ActionList1: TActionList;
|
|---|
| 20 | ListViewTables: TListView;
|
|---|
| 21 | MenuItem1: TMenuItem;
|
|---|
| 22 | MenuItem2: TMenuItem;
|
|---|
| 23 | MenuItem3: TMenuItem;
|
|---|
| 24 | MenuItem4: TMenuItem;
|
|---|
| 25 | MenuItem5: TMenuItem;
|
|---|
| 26 | PopupMenu1: TPopupMenu;
|
|---|
| 27 | ToolBar1: TToolBar;
|
|---|
| 28 | ToolButton1: TToolButton;
|
|---|
| 29 | ToolButton2: TToolButton;
|
|---|
| 30 | ToolButton3: TToolButton;
|
|---|
| 31 | ToolButton4: TToolButton;
|
|---|
| 32 | ToolButton5: TToolButton;
|
|---|
| 33 | procedure AAddExecute(Sender: TObject);
|
|---|
| 34 | procedure AModifyExecute(Sender: TObject);
|
|---|
| 35 | procedure ARemoveExecute(Sender: TObject);
|
|---|
| 36 | procedure AShowFieldsExecute(Sender: TObject);
|
|---|
| 37 | procedure AShowRecordsExecute(Sender: TObject);
|
|---|
| 38 | procedure FormCreate(Sender: TObject);
|
|---|
| 39 | procedure FormDestroy(Sender: TObject);
|
|---|
| 40 | procedure FormShow(Sender: TObject);
|
|---|
| 41 | procedure ListViewTablesData(Sender: TObject; Item: TListItem);
|
|---|
| 42 | procedure ListViewTablesSelectItem(Sender: TObject; Item: TListItem;
|
|---|
| 43 | Selected: Boolean);
|
|---|
| 44 | private
|
|---|
| 45 | FDbClient: TDbClient;
|
|---|
| 46 | FTables: TTables;
|
|---|
| 47 | procedure SetDbClient(AValue: TDbClient);
|
|---|
| 48 | public
|
|---|
| 49 | property DbClient: TDbClient read FDbClient write SetDbClient;
|
|---|
| 50 | property Tables: TTables read FTables;
|
|---|
| 51 | procedure UpdateInterface;
|
|---|
| 52 | procedure ReloadList;
|
|---|
| 53 | end;
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | implementation
|
|---|
| 57 |
|
|---|
| 58 | uses
|
|---|
| 59 | FormTable, FormRecords, FormFields;
|
|---|
| 60 |
|
|---|
| 61 | {$R *.lfm}
|
|---|
| 62 |
|
|---|
| 63 | resourcestring
|
|---|
| 64 | SRemoveTable = 'Remove table';
|
|---|
| 65 | SRemoveTableConfirm = 'Do you want to really remove table ''%s'' ?';
|
|---|
| 66 | SNewTable = 'New table';
|
|---|
| 67 |
|
|---|
| 68 | { TFormTables }
|
|---|
| 69 |
|
|---|
| 70 | procedure TFormTables.ListViewTablesData(Sender: TObject; Item: TListItem);
|
|---|
| 71 | begin
|
|---|
| 72 | if (Item.Index >= 0) and (Item.Index < FTables.Count) then
|
|---|
| 73 | with TTable(FTables[Item.Index]) do begin
|
|---|
| 74 | Item.Caption := Caption;
|
|---|
| 75 | Item.SubItems.Add(IntToStr(RecordsCount));
|
|---|
| 76 | Item.Data := FTables[Item.Index];
|
|---|
| 77 | end
|
|---|
| 78 | end;
|
|---|
| 79 |
|
|---|
| 80 | procedure TFormTables.ListViewTablesSelectItem(Sender: TObject; Item: TListItem;
|
|---|
| 81 | Selected: Boolean);
|
|---|
| 82 | begin
|
|---|
| 83 | UpdateInterface;
|
|---|
| 84 | end;
|
|---|
| 85 |
|
|---|
| 86 | procedure TFormTables.SetDbClient(AValue: TDbClient);
|
|---|
| 87 | begin
|
|---|
| 88 | if FDbClient = AValue then Exit;
|
|---|
| 89 | FDbClient := AValue;
|
|---|
| 90 | FTables.DbClient := AValue;
|
|---|
| 91 | ReloadList;
|
|---|
| 92 | end;
|
|---|
| 93 |
|
|---|
| 94 | procedure TFormTables.UpdateInterface;
|
|---|
| 95 | begin
|
|---|
| 96 | ListViewTables.Enabled := Assigned(Tables) and Assigned(FDbClient);
|
|---|
| 97 | AAdd.Enabled := Assigned(Tables) and Assigned(FDbClient);
|
|---|
| 98 | AModify.Enabled := Assigned(Tables) and Assigned(ListViewTables.Selected) and Assigned(FDbClient);
|
|---|
| 99 | ARemove.Enabled := Assigned(Tables) and Assigned(ListViewTables.Selected) and Assigned(FDbClient);
|
|---|
| 100 | AShowRecords.Enabled := Assigned(Tables) and Assigned(ListViewTables.Selected) and Assigned(FDbClient);
|
|---|
| 101 | AShowFields.Enabled := Assigned(Tables) and Assigned(ListViewTables.Selected) and Assigned(FDbClient);
|
|---|
| 102 | end;
|
|---|
| 103 |
|
|---|
| 104 | procedure TFormTables.AAddExecute(Sender: TObject);
|
|---|
| 105 | var
|
|---|
| 106 | NewTable: TTable;
|
|---|
| 107 | begin
|
|---|
| 108 | NewTable := TTable.Create;
|
|---|
| 109 | NewTable.Name := SNewTable;
|
|---|
| 110 | NewTable.DbClient := Tables.DbClient;
|
|---|
| 111 | with TFormTable.Create(nil) do
|
|---|
| 112 | try
|
|---|
| 113 | Load(NewTable);
|
|---|
| 114 | if ShowModal = mrOk then begin
|
|---|
| 115 | Save(NewTable);
|
|---|
| 116 | Tables.DbClient.Query('INSERT INTO Model ( Name , Caption ) VALUES ( ' +
|
|---|
| 117 | NewTable.Name + ' , ' + NewTable.Caption + ' )');
|
|---|
| 118 | ReloadList;
|
|---|
| 119 | end else NewTable.Free;
|
|---|
| 120 | finally
|
|---|
| 121 | Free;
|
|---|
| 122 | end;
|
|---|
| 123 | end;
|
|---|
| 124 |
|
|---|
| 125 | procedure TFormTables.AModifyExecute(Sender: TObject);
|
|---|
| 126 | begin
|
|---|
| 127 | if Assigned(ListViewTables.Selected) then begin
|
|---|
| 128 | with TFormTable.Create(nil) do
|
|---|
| 129 | try
|
|---|
| 130 | Load(TTable(ListViewTables.Selected.Data));
|
|---|
| 131 | if ShowModal = mrOk then begin
|
|---|
| 132 | Save(TTable(ListViewTables.Selected.Data));
|
|---|
| 133 | DbClient.Query('UPDATE Model SET Caption = ' + TTable(ListViewTables.Selected.Data).Caption + ' WHERE Name = ' + TTable(ListViewTables.Selected.Data).Name);
|
|---|
| 134 | ReloadList;
|
|---|
| 135 | end;
|
|---|
| 136 | finally
|
|---|
| 137 | Free;
|
|---|
| 138 | end;
|
|---|
| 139 | end;
|
|---|
| 140 | end;
|
|---|
| 141 |
|
|---|
| 142 | procedure TFormTables.ARemoveExecute(Sender: TObject);
|
|---|
| 143 | begin
|
|---|
| 144 | if Assigned(ListViewTables.Selected) then begin
|
|---|
| 145 | if MessageDlg(SRemoveTable, Format(SRemoveTableConfirm, [TTable(ListViewTables.Selected.Data).Caption]),
|
|---|
| 146 | mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
|
|---|
| 147 | Tables.DbClient.Query('DELETE FROM Model WHERE Name = ' + TTable(ListViewTables.Selected.Data).Name);
|
|---|
| 148 | ReloadList;
|
|---|
| 149 | end;
|
|---|
| 150 | end;
|
|---|
| 151 | end;
|
|---|
| 152 |
|
|---|
| 153 | procedure TFormTables.AShowFieldsExecute(Sender: TObject);
|
|---|
| 154 | var
|
|---|
| 155 | NewRecords: TRecords;
|
|---|
| 156 | I: Integer;
|
|---|
| 157 | FI: Integer;
|
|---|
| 158 | C: Integer;
|
|---|
| 159 | OldField: TField;
|
|---|
| 160 | OldTable: TTable;
|
|---|
| 161 | FormFields: TFormFields;
|
|---|
| 162 | begin
|
|---|
| 163 | if Assigned(ListViewTables.Selected) then begin
|
|---|
| 164 | OldTable := TTable(ListViewTables.Selected.Data);
|
|---|
| 165 | FormFields := TFormFields.Create(nil);
|
|---|
| 166 | FormFields.TableName := OldTable.Caption;
|
|---|
| 167 | FormFields.Fields := TFields.Create;
|
|---|
| 168 | FormFields.Fields.Table := OldTable;
|
|---|
| 169 | FormFields.Fields.Assign(OldTable.Fields);
|
|---|
| 170 | for I := 0 to FormFields.Fields.Count - 1 do
|
|---|
| 171 | FormFields.Fields[I].Table := OldTable.Fields[I].Table;
|
|---|
| 172 | if FormFields.ShowModal = mrOk then begin
|
|---|
| 173 | // Inefficient way to update table data. Copy original columns to new records and
|
|---|
| 174 | // then replace original table records
|
|---|
| 175 | NewRecords := TRecords.Create;
|
|---|
| 176 | NewRecords.Count := OldTable.Records.Count;
|
|---|
| 177 | for I := 0 to NewRecords.Count - 1 do begin
|
|---|
| 178 | NewRecords.Items[I] := TRecord.Create;
|
|---|
| 179 | NewRecords[I].Values.Count := FormFields.Fields.Count;
|
|---|
| 180 | for C := 0 to FormFields.Fields.Count - 1 do
|
|---|
| 181 | NewRecords[I].Values[C] := FormFields.Fields[C].GetValueClass.Create;
|
|---|
| 182 | end;
|
|---|
| 183 | for C := 0 to FormFields.Fields.Count - 1 do begin
|
|---|
| 184 | OldField := OldTable.Fields.SearchByName(FormFields.Fields[C].Name);
|
|---|
| 185 | if Assigned(OldField) then begin
|
|---|
| 186 | FI := OldTable.Fields.IndexOf(OldField);
|
|---|
| 187 | for I := 0 to NewRecords.Count - 1 do
|
|---|
| 188 | NewRecords[I].Values[C].Assign(OldTable.Records[I].Values[FI]);
|
|---|
| 189 | end;
|
|---|
| 190 | end;
|
|---|
| 191 | OldTable.Fields.Assign(FormFields.Fields);
|
|---|
| 192 | OldTable.Records.Assign(NewRecords);
|
|---|
| 193 | NewRecords.Free;
|
|---|
| 194 | ReloadList;
|
|---|
| 195 | end;
|
|---|
| 196 | FormFields.Fields.Free;
|
|---|
| 197 | FormFields.Free;
|
|---|
| 198 | end;
|
|---|
| 199 | end;
|
|---|
| 200 |
|
|---|
| 201 | procedure TFormTables.AShowRecordsExecute(Sender: TObject);
|
|---|
| 202 | begin
|
|---|
| 203 | if Assigned(ListViewTables.Selected) then begin
|
|---|
| 204 | with TFormRecords.Create(nil) do
|
|---|
| 205 | try
|
|---|
| 206 | Table := TTable(ListViewTables.Selected.Data);
|
|---|
| 207 | ShowModal;
|
|---|
| 208 | finally
|
|---|
| 209 | Free;
|
|---|
| 210 | end;
|
|---|
| 211 | ReloadList;
|
|---|
| 212 | end;
|
|---|
| 213 | end;
|
|---|
| 214 |
|
|---|
| 215 | procedure TFormTables.FormCreate(Sender: TObject);
|
|---|
| 216 | begin
|
|---|
| 217 | FTables := TTables.Create;
|
|---|
| 218 | end;
|
|---|
| 219 |
|
|---|
| 220 | procedure TFormTables.FormDestroy(Sender: TObject);
|
|---|
| 221 | begin
|
|---|
| 222 | FreeAndNil(FTables);
|
|---|
| 223 | end;
|
|---|
| 224 |
|
|---|
| 225 | procedure TFormTables.FormShow(Sender: TObject);
|
|---|
| 226 | var
|
|---|
| 227 | I: Integer;
|
|---|
| 228 | begin
|
|---|
| 229 | ReloadList;
|
|---|
| 230 | for I := 0 to ToolBar1.ButtonCount - 1 do
|
|---|
| 231 | ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
|
|---|
| 232 | UpdateInterface;
|
|---|
| 233 | end;
|
|---|
| 234 |
|
|---|
| 235 | procedure TFormTables.ReloadList;
|
|---|
| 236 | var
|
|---|
| 237 | DbRows: TDbRows;
|
|---|
| 238 | NewTable: TTable;
|
|---|
| 239 | I: Integer;
|
|---|
| 240 | begin
|
|---|
| 241 | Tables.Clear;
|
|---|
| 242 | if Assigned(DbClient) then begin
|
|---|
| 243 | DbRows := TDbRows.Create;
|
|---|
| 244 | try
|
|---|
| 245 | DbClient.Query('SELECT * FROM Model', DbRows);
|
|---|
| 246 | for I := 0 to DbRows.Count - 1 do begin
|
|---|
| 247 | NewTable := Tables.AddNew(DbRows[I].Items['Name']);
|
|---|
| 248 | NewTable.Caption := DbRows[I].Items['Caption'];
|
|---|
| 249 | end;
|
|---|
| 250 | finally
|
|---|
| 251 | DbRows.Free;
|
|---|
| 252 | end;
|
|---|
| 253 | end;
|
|---|
| 254 |
|
|---|
| 255 | for I := 0 to Tables.Count - 1 do
|
|---|
| 256 | Tables[I].LoadRecordsCount;
|
|---|
| 257 | if Assigned(Tables) then begin
|
|---|
| 258 | ListViewTables.Items.Count := Tables.Count;
|
|---|
| 259 | ListViewTables.Repaint;
|
|---|
| 260 | end else ListViewTables.Items.Count := 0;
|
|---|
| 261 | UpdateInterface;
|
|---|
| 262 | end;
|
|---|
| 263 |
|
|---|
| 264 | end.
|
|---|
| 265 |
|
|---|