Ignore:
Timestamp:
Jan 18, 2015, 11:29:26 PM (10 years ago)
Author:
chronos
Message:
  • Moved: Field and value data type declaration moved to separate unit.
  • Modified: Fields edit form is now modal and made need changes need to be accepted.
  • Added: 32x32 icons to actions.
  • Fixed: If fileds count or type changed then table values are updated.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormTables.pas

    r3 r4  
    6868  if (Item.Index >= 0) and (Item.Index < Database.Tables.Count) then
    6969  with TTable(Database.Tables[Item.Index]) do begin
    70     Item.Caption := Name;
     70    Item.Caption := Caption;
    7171    Item.SubItems.Add(IntToStr(Records.Count));
    7272    Item.Data := Database.Tables[Item.Index];
     
    116116begin
    117117  if Assigned(ListView1.Selected) then begin
    118     Database.Tables.Remove(ListView1.Selected.Data);
    119     ReloadList;
     118    if MessageDlg('Remove table', 'Do you want to really remove table ' +
     119    TTable(ListView1.Selected.Data).Name, mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
     120      Database.Tables.Remove(ListView1.Selected.Data);
     121      ReloadList;
     122    end;
    120123  end;
    121124end;
    122125
    123126procedure TFormMain.AShowFieldsExecute(Sender: TObject);
    124 begin
    125   if Assigned(ListView1.Selected) then begin
    126     FormFields.Table := TTable(ListView1.Selected.Data);
    127     FormFields.ShowModal;
    128     ReloadList;
     127var
     128  NewRecords: TRecords;
     129  I: Integer;
     130  FI: Integer;
     131  C: Integer;
     132  OldField: TField;
     133  OldTable: TTable;
     134begin
     135  if Assigned(ListView1.Selected) then begin
     136    OldTable := TTable(ListView1.Selected.Data);
     137    FormFields.TableName := OldTable.Caption;
     138    FormFields.Fields := TFields.Create;
     139    FormFields.Fields.Assign(OldTable.Fields);
     140    if FormFields.ShowModal = mrOk then begin
     141      // Inefficient way to update table data. Copy original columns to new records and
     142      // then replace original table records
     143      NewRecords := TRecords.Create;
     144      NewRecords.Count := OldTable.Records.Count;
     145      for I := 0 to NewRecords.Count - 1 do begin
     146        NewRecords.Items[I] := TRecord.Create;
     147        TRecord(NewRecords.Items[I]).Values.Count := FormFields.Fields.Count;
     148        for C := 0 to FormFields.Fields.Count - 1 do
     149          TRecord(NewRecords.Items[I]).Values[C] := TField(FormFields.Fields[C]).GetValueClass.Create;
     150      end;
     151      for C := 0 to FormFields.Fields.Count - 1 do begin
     152        OldField := OldTable.Fields.FindByName(TField(FormFields.Fields[C]).Name);
     153        if Assigned(OldField) then begin
     154          FI := OldTable.Fields.IndexOf(OldField);
     155          for I := 0 to NewRecords.Count - 1 do
     156            TValue(TRecord(NewRecords[I]).Values[C]).Assign(TValue(TRecord(OldTable.Records[I]).Values[FI]));
     157        end;
     158      end;
     159      OldTable.Fields.Assign(FormFields.Fields);
     160      OldTable.Records.Assign(NewRecords);
     161      NewRecords.Free;
     162      ReloadList;
     163    end;
     164    FormFields.Fields.Free;
    129165  end;
    130166end;
Note: See TracChangeset for help on using the changeset viewer.