Changeset 28 for trunk/Forms/UFormTables.pas
- Timestamp:
- Sep 10, 2022, 10:21:48 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormTables.pas
r26 r28 18 18 ARemove: TAction; 19 19 ActionList1: TActionList; 20 ListView 1: TListView;20 ListViewTables: TListView; 21 21 MenuItem1: TMenuItem; 22 22 MenuItem2: TMenuItem; … … 39 39 procedure FormDestroy(Sender: TObject); 40 40 procedure FormShow(Sender: TObject); 41 procedure ListView 1Data(Sender: TObject; Item: TListItem);42 procedure ListView 1SelectItem(Sender: TObject; Item: TListItem;41 procedure ListViewTablesData(Sender: TObject; Item: TListItem); 42 procedure ListViewTablesSelectItem(Sender: TObject; Item: TListItem; 43 43 Selected: Boolean); 44 44 private … … 53 53 end; 54 54 55 var56 FormTables: TFormTables;57 58 55 59 56 implementation … … 71 68 { TFormTables } 72 69 73 procedure TFormTables.ListView 1Data(Sender: TObject; Item: TListItem);70 procedure TFormTables.ListViewTablesData(Sender: TObject; Item: TListItem); 74 71 begin 75 72 if (Item.Index >= 0) and (Item.Index < FTables.Count) then 76 73 with TTable(FTables[Item.Index]) do begin 77 74 Item.Caption := Caption; 78 Item.SubItems.Add(IntToStr(RecordsCount) );75 Item.SubItems.Add(IntToStr(RecordsCount) + ' ' + IntToStr(Fields.Count)); 79 76 Item.Data := FTables[Item.Index]; 80 77 end 81 78 end; 82 79 83 procedure TFormTables.ListView 1SelectItem(Sender: TObject; Item: TListItem;80 procedure TFormTables.ListViewTablesSelectItem(Sender: TObject; Item: TListItem; 84 81 Selected: Boolean); 85 82 begin … … 97 94 procedure TFormTables.UpdateInterface; 98 95 begin 99 ListView 1.Enabled := Assigned(Tables);100 AAdd.Enabled := Assigned(Tables) ;101 AModify.Enabled := Assigned(Tables) and Assigned(ListView 1.Selected);102 ARemove.Enabled := Assigned(Tables) and Assigned(ListView 1.Selected);103 AShowRecords.Enabled := Assigned(Tables) and Assigned(ListView 1.Selected);104 AShowFields.Enabled := Assigned(Tables) and Assigned(ListView 1.Selected);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); 105 102 end; 106 103 … … 112 109 NewTable.Name := SNewTable; 113 110 NewTable.DbClient := Tables.DbClient; 114 FormTable.Load(NewTable); 115 if FormTable.ShowModal = mrOk then begin 116 FormTable.Save(NewTable); 117 Tables.DbClient.Query('INSERT INTO Model ( Name , Caption ) VALUES ( ' + 118 NewTable.Name + ' , ' + NewTable.Caption + ' )'); 119 ReloadList; 120 end else NewTable.Free; 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; 121 123 end; 122 124 123 125 procedure TFormTables.AModifyExecute(Sender: TObject); 124 126 begin 125 if Assigned(ListView1.Selected) then begin 126 FormTable.Load(TTable(ListView1.Selected.Data)); 127 if FormTable.ShowModal = mrOk then begin 128 FormTable.Save(TTable(ListView1.Selected.Data)); 129 DbClient.Query('UPDATE Model SET Caption = ' + TTable(ListView1.Selected.Data).Caption + ' WHERE Name = ' + TTable(ListView1.Selected.Data).Name); 130 ReloadList; 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; 131 138 end; 132 139 end; … … 135 142 procedure TFormTables.ARemoveExecute(Sender: TObject); 136 143 begin 137 if Assigned(ListView 1.Selected) then begin138 if MessageDlg(SRemoveTable, Format(SRemoveTableConfirm, [TTable(ListView 1.Selected.Data).Caption]),144 if Assigned(ListViewTables.Selected) then begin 145 if MessageDlg(SRemoveTable, Format(SRemoveTableConfirm, [TTable(ListViewTables.Selected.Data).Caption]), 139 146 mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin 140 Tables.DbClient.Query('DELETE FROM Model WHERE Name = ' + TTable(ListView 1.Selected.Data).Name);147 Tables.DbClient.Query('DELETE FROM Model WHERE Name = ' + TTable(ListViewTables.Selected.Data).Name); 141 148 ReloadList; 142 149 end; … … 152 159 OldField: TField; 153 160 OldTable: TTable; 154 begin 155 if Assigned(ListView1.Selected) then begin 156 OldTable := TTable(ListView1.Selected.Data); 161 FormFields: TFormFields; 162 begin 163 if Assigned(ListViewTables.Selected) then begin 164 OldTable := TTable(ListViewTables.Selected.Data); 165 FormFields := TFormFields.Create(nil); 157 166 FormFields.TableName := OldTable.Caption; 158 167 FormFields.Fields := TFields.Create; … … 160 169 FormFields.Fields.Assign(OldTable.Fields); 161 170 for I := 0 to FormFields.Fields.Count - 1 do 162 TField(FormFields.Fields[I]).Table := TField(OldTable.Fields[I]).Table;171 FormFields.Fields[I].Table := OldTable.Fields[I].Table; 163 172 if FormFields.ShowModal = mrOk then begin 164 173 // Inefficient way to update table data. Copy original columns to new records and … … 168 177 for I := 0 to NewRecords.Count - 1 do begin 169 178 NewRecords.Items[I] := TRecord.Create; 170 TRecord(NewRecords.Items[I]).Values.Count := FormFields.Fields.Count;179 NewRecords[I].Values.Count := FormFields.Fields.Count; 171 180 for C := 0 to FormFields.Fields.Count - 1 do 172 TRecord(NewRecords.Items[I]).Values[C] := TField(FormFields.Fields[C]).GetValueClass.Create;181 NewRecords[I].Values[C] := FormFields.Fields[C].GetValueClass.Create; 173 182 end; 174 183 for C := 0 to FormFields.Fields.Count - 1 do begin 175 OldField := OldTable.Fields.SearchByName( TField(FormFields.Fields[C]).Name);184 OldField := OldTable.Fields.SearchByName(FormFields.Fields[C].Name); 176 185 if Assigned(OldField) then begin 177 186 FI := OldTable.Fields.IndexOf(OldField); 178 187 for I := 0 to NewRecords.Count - 1 do 179 TValue(TRecord(NewRecords[I]).Values[C]).Assign(TValue(TRecord(OldTable.Records[I]).Values[FI]));188 NewRecords[I].Values[C].Assign(OldTable.Records[I].Values[FI]); 180 189 end; 181 190 end; … … 186 195 end; 187 196 FormFields.Fields.Free; 197 FormFields.Free; 188 198 end; 189 199 end; … … 191 201 procedure TFormTables.AShowRecordsExecute(Sender: TObject); 192 202 begin 193 if Assigned(ListView1.Selected) then begin 194 FormRecords.Table := TTable(ListView1.Selected.Data); 195 FormRecords.ShowModal; 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; 196 211 ReloadList; 197 212 end; … … 227 242 if Assigned(DbClient) then begin 228 243 DbRows := TDbRows.Create; 229 DbClient.Query('SELECT * FROM Model', DbRows); 230 for I := 0 to DbRows.Count - 1 do begin 231 NewTable := Tables.AddNew(DbRows[I].Items['Name']); 232 NewTable.Caption := DbRows[I].Items['Caption']; 233 end; 234 DbRows.Free; 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; 235 253 end; 236 254 237 255 for I := 0 to Tables.Count - 1 do 238 T Table(Tables[I]).LoadRecordsCount;256 Tables[I].LoadRecordsCount; 239 257 if Assigned(Tables) then begin 240 ListView 1.Items.Count := Tables.Count;241 ListView 1.Repaint;242 end else ListView 1.Items.Count := 0;258 ListViewTables.Items.Count := Tables.Count; 259 ListViewTables.Repaint; 260 end else ListViewTables.Items.Count := 0; 243 261 UpdateInterface; 244 262 end;
Note:
See TracChangeset
for help on using the changeset viewer.