Changeset 29


Ignore:
Timestamp:
Sep 10, 2022, 10:54:56 PM (2 years ago)
Author:
chronos
Message:
  • Fixed: Load table fields in records list and record edit form.
  • Fixed: Record add needs to insert new row into database.
Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/DbEngines/UEngineXML.pas

    r28 r29  
    9898  I: Integer;
    9999begin
    100   Row.Values.Count := Row.Parent.Fields.Count;
     100  Row.Values.Count := Row.Table.Fields.Count;
    101101  for I := 0 to Row.Values.Count - 1 do
    102     Row.Values[I] := TValueClass(Row.Parent.Fields[I].GetValueClass).Create;
     102    Row.Values[I] := TValueClass(Row.Table.Fields[I].GetValueClass).Create;
    103103
    104104  Node2 := Node.FirstChild;
    105105  I := 0;
    106106  while Assigned(Node2) and (Node2.NodeName = 'Value') and (I < Row.Values.Count) do begin
    107     case Row.Parent.Fields[I].DataType.FieldType of
     107    case Row.Table.Fields[I].DataType.FieldType of
    108108      ftString: TValueString(Row.Values[I]).Value := string(Node2.TextContent);
    109109      ftInteger: TValueInteger(Row.Values[I]).Value := StrToInt(string(Node2.TextContent));
     
    125125    NewNode := Node.OwnerDocument.CreateElement('Value');
    126126    Node.AppendChild(NewNode);
    127     case Row.Parent.Fields[I].DataType.FieldType of
     127    case Row.Table.Fields[I].DataType.FieldType of
    128128      ftString: NewNode.TextContent := UnicodeString(TValueString(Row.Values[I]).Value);
    129129      ftInteger: NewNode.TextContent := UnicodeString(IntToStr(TValueInteger(Row.Values[I]).Value));
     
    144144  while Assigned(Node2) and (Node2.NodeName = 'Record') do begin
    145145    NewRecord := TRecord.Create;
    146     NewRecord.Parent := Records.Parent;
     146    NewRecord.Table := Records.Table;
    147147    LoadNodeRecord(NewRecord, Node2);
    148148    Records.Add(NewRecord);
     
    492492        if Assigned(Field) then begin
    493493          FieldIndex := Table.Fields.IndexOf(Field);
    494           TValue(Row.Values[FieldIndex]).SetString(InsertValues.ValueFromIndex[ValueIndex]);
     494          Row.Values[FieldIndex].SetString(InsertValues.ValueFromIndex[ValueIndex]);
    495495        end else raise Exception.Create(Format(SColumnNotFoundInTable,
    496496          [InsertValues.Names[ValueIndex], TableName]));
  • trunk/Forms/UFormConnect.lfm

    r28 r29  
    8787        Left = 110
    8888        Height = 43
    89         Top = 25
     89        Top = 16
    9090        Width = 446
    9191        Anchors = [akTop, akLeft, akRight]
     
    107107      ClientWidth = 693
    108108      object Label4: TLabel
    109         Left = 17
     109        Left = 19
    110110        Height = 26
    111111        Top = 19
     
    124124        Left = 19
    125125        Height = 26
    126         Top = 58
     126        Top = 72
    127127        Width = 40
    128128        Caption = 'Port:'
     
    130130      end
    131131      object SpinEditPort: TSpinEdit
    132         Left = 150
     132        Left = 152
    133133        Height = 43
    134         Top = 58
     134        Top = 64
    135135        Width = 133
    136136        MaxValue = 65535
  • trunk/Forms/UFormFields.pas

    r28 r29  
    160160
    161161procedure TFormFields.ReloadList;
    162 var
    163   DbRows: TDbRows;
    164   NewField: TField;
    165   I: Integer;
    166162begin
    167   Fields.Clear;
    168   DbRows := TDbRows.Create;
    169   try
    170     Fields.Table.DbClient.Query('SELECT * FROM ModelField WHERE Model = ' + Fields.Table.Name, DbRows);
    171     for I := 0 to DbRows.Count - 1 do begin
    172       NewField := Fields.AddNew(DbRows[I].Items['Name'],
    173         Fields.Table.DbClient.DbManager.DataTypes.SearchByName(DbRows[I].Items['DataType']));
    174       NewField.TextBefore := DbRows[I].Items['Caption'];
    175     end;
    176   finally
    177     DbRows.Free;
    178   end;
     163  Fields.Load;
    179164
    180165  ListView1.Items.Count := Fields.Count;
  • trunk/Forms/UFormRecord.pas

    r28 r29  
    3737
    3838uses
    39   UDataTypes;
     39  UDataTypes, UCore;
    4040
    4141{$R *.lfm}
     
    8686const
    8787  LabelWidth = 300;
    88 begin
     88var
     89  LineHeight: Integer;
     90begin
     91  LineHeight := Core.ScaleDPI1.ScaleY(35, Core.ScaleDPI1.DesignDPI.Y);
     92
    8993  Row := DataRecord;
    9094  Controls.Clear;
    9195  Labels.Clear;
    9296  for I := 0 to Table.Fields.Count - 1 do begin
    93     CellRect := Rect(10, 10 + I * 35, Panel1.Width - 20, (I + 1) * 35);
     97    CellRect := Rect(10, 10 + I * LineHeight, Panel1.Width - 20, (I + 1) * LineHeight);
    9498    NewLabel := TLabel.Create(Panel1);
    95     NewLabel.Caption := TField(Table.Fields[I]).TextBefore;
     99    NewLabel.Caption := Table.Fields[I].TextBefore;
    96100    NewLabel.Parent := Panel1;
    97101    NewLabel.Left := CellRect.Left;
     
    99103    NewLabel.Visible := True;
    100104    Labels.Add(NewLabel);
    101     case TField(Table.Fields[I]).DataType.FieldType of
     105    case Table.Fields[I].DataType.FieldType of
    102106      ftString: begin
    103107        NewControl := TEdit.Create(Panel1);
     
    183187begin
    184188  for I := 0 to Table.Fields.Count - 1 do begin
    185     case TField(Table.Fields[I]).DataType.FieldType of
     189    case Table.Fields[I].DataType.FieldType of
    186190      ftString: TValueString(Row.Values[I]).Value := TEdit(Controls[I]).Text;
    187191      ftInteger: TValueInteger(Row.Values[I]).Value := TSpinEdit(Controls[I]).Value;
  • trunk/Forms/UFormRecords.pas

    r28 r29  
    6060    ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
    6161  Caption := STable + ' - ' + Table.Caption;
     62  Table.Fields.Load;
    6263  ReloadList;
    6364end;
     
    6869begin
    6970  if (Item.Index >= 0) and (Item.Index < Table.Records.Count) then
    70   with TRecord(Table.Records[Item.Index]) do begin
     71  with Table.Records[Item.Index] do begin
    7172    for I := 0 to Table.Fields.Count - 1 do begin
    72       if I = 0 then Item.Caption := TValue(Values[0]).GetString
    73         else Item.SubItems.Add(TValue(Values[I]).GetString);
     73      if I = 0 then Item.Caption := Values[0].GetString
     74        else Item.SubItems.Add(Values[I].GetString);
    7475    end;
    7576    Item.Data := Table.Records[Item.Index];
     
    116117  I: Integer;
    117118  NewValue: TValue;
     119  Columns: string;
     120  Values: string;
    118121begin
    119122  NewRecord := TRecord.Create;
    120   NewRecord.Parent := Table;
     123  NewRecord.Table := Table;
    121124  NewRecord.Values.Count := Table.Fields.Count;
    122125  for I := 0 to Table.Fields.Count - 1 do begin
    123     ValueClass := TField(Table.Fields[I]).GetValueClass;
     126    ValueClass := Table.Fields[I].GetValueClass;
    124127    NewValue := ValueClass.Create;
    125128    NewRecord.Values[I] := NewValue;
     
    132135    if ShowModal = mrOk then begin
    133136      Save(NewRecord);
    134       Table.Records.Add(NewRecord);
     137
     138      Values := '';
     139      Columns := '';
     140      for I := 0 to Table.Fields.Count - 1 do begin
     141        Columns := Columns + Table.Fields[I].Name;
     142        Values := Values + '"' + NewRecord.Values[I].GetString + '"';
     143        if I < Table.Fields.Count - 1 then begin
     144          Columns := Columns + ' , ';
     145          Values := Values + ' , ';
     146        end;
     147      end;
     148      Table.DbClient.Query('INSERT INTO ' + Table.Name + ' ( ' + Columns + ' )' +
     149        ' VALUES ( ' + Values + ' )');
     150
     151      NewRecord.Free;
    135152      ReloadList;
    136153    end else NewRecord.Free;
     
    150167  I: Integer;
    151168begin
    152   Table.LoadRecords;
     169  Table.Records.Load;
    153170
    154171  ListViewRecords.Columns.BeginUpdate;
    155   while ListViewRecords.Columns.Count > Table.Fields.Count do
    156     ListViewRecords.Columns[ListViewRecords.ColumnCount - 1].Free;
    157   while ListViewRecords.Columns.Count < Table.Fields.Count do
    158     ListViewRecords.Columns.Add;
    159   for I := 0 to Table.Fields.Count - 1 do begin
    160     ListViewRecords.Columns[I].Caption := TField(Table.Fields[I]).TextBefore;
    161     ListViewRecords.Columns[I].Width := 200;
     172  try
     173    while ListViewRecords.Columns.Count > Table.Fields.Count do
     174      ListViewRecords.Columns[ListViewRecords.ColumnCount - 1].Free;
     175    while ListViewRecords.Columns.Count < Table.Fields.Count do
     176      ListViewRecords.Columns.Add;
     177    for I := 0 to Table.Fields.Count - 1 do begin
     178      ListViewRecords.Columns[I].Caption := Table.Fields[I].TextBefore;
     179      ListViewRecords.Columns[I].Width := 200;
     180    end;
     181  finally
     182    ListViewRecords.Columns.EndUpdate;
    162183  end;
    163   ListViewRecords.Columns.EndUpdate;
    164184
    165185  ListViewRecords.Items.Count := Table.Records.Count;
  • trunk/Forms/UFormTables.pas

    r28 r29  
    7373  with TTable(FTables[Item.Index]) do begin
    7474    Item.Caption := Caption;
    75     Item.SubItems.Add(IntToStr(RecordsCount) + ' ' + IntToStr(Fields.Count));
     75    Item.SubItems.Add(IntToStr(RecordsCount));
    7676    Item.Data := FTables[Item.Index];
    7777  end
  • trunk/UDatabase.pas

    r28 r29  
    44
    55uses
    6   Classes, SysUtils, ExtCtrls, dialogs, USqlDatabase, URegistry, UGenerics,
     6  Classes, SysUtils, ExtCtrls, Dialogs, USqlDatabase, URegistry, UGenerics,
    77  Generics.Collections;
    88
     
    7676    procedure Assign(Source: TFields);
    7777    function AddNew(Name: string; DataType: TDataType): TField;
     78    procedure Load;
    7879  end;
    7980
     
    8182
    8283  TRecord = class
    83     Parent: TTable;
     84    Table: TTable;
    8485    Values: TValues;
    8586    function Match(AValues: TStrings): Boolean;
     
    9394
    9495  TRecords = class(TObjectList<TRecord>)
    95     Parent: TTable;
     96    Table: TTable;
    9697    procedure Assign(Source: TRecords);
    9798    function SearchByValue(Name, Value: string): TRecord;
    9899    function SearchByValues(Values: TStrings): TRecord;
    99100    function AddNew: TRecord;
     101    procedure Load;
    100102  end;
    101103
     
    110112    DbClient: TDbClient;
    111113    RecordsCount: Integer;
    112     procedure LoadRecords;
    113114    procedure LoadRecordsCount;
    114115    procedure Assign(Source: TTable);
     
    594595begin
    595596  Result := nil;
    596   Field := Parent.Fields.SearchByName(Name);
     597  Field := Table.Fields.SearchByName(Name);
    597598  if Assigned(Field) then begin
    598     FieldIndex := Parent.Fields.IndexOf(Field);
     599    FieldIndex := Table.Fields.IndexOf(Field);
    599600    I := 0;
    600601    while (I < Count) and (Items[I].Values[FieldIndex].GetString <> Value) do Inc(I);
     
    618619begin
    619620  Result := TRecord.Create;
    620   Result.Parent := Parent;
     621  Result.Table := Table;
    621622  Result.InitValues;
    622623  Add(Result);
     624end;
     625
     626procedure TRecords.Load;
     627var
     628  DbRows: TDbRows;
     629  I: Integer;
     630  F: Integer;
     631  NewRecord: TRecord;
     632  NewValue: TValue;
     633  Value: string;
     634begin
     635  Clear;
     636  DbRows := TDbRows.Create;
     637  try
     638    Table.DbClient.Query('SELECT * FROM ' + Table.Name, DbRows);
     639    for I := 0 to DbRows.Count - 1 do begin
     640      NewRecord := TRecord.Create;
     641      for F := 0 to Table.Fields.Count - 1 do begin
     642        NewValue := Table.Fields[F].GetValueClass.Create;
     643        if DbRows[I].TryGetValue(Table.Fields[F].Name, Value) then begin
     644          NewValue.SetString(Value);
     645          NewRecord.Values.Add(NewValue);
     646        end else begin
     647          //NewValue.SetString('');
     648          NewRecord.Values.Add(NewValue);
     649        end;
     650      end;
     651      Add(NewRecord);
     652    end;
     653  finally
     654    DbRows.Free;
     655  end;
    623656end;
    624657
     
    658691end;
    659692
     693procedure TFields.Load;
     694var
     695  DbRows: TDbRows;
     696  NewField: TField;
     697  I: Integer;
     698begin
     699  Clear;
     700  DbRows := TDbRows.Create;
     701  try
     702    Table.DbClient.Query('SELECT * FROM ModelField WHERE Model = ' + Table.Name, DbRows);
     703    for I := 0 to DbRows.Count - 1 do begin
     704      NewField := AddNew(DbRows[I].Items['Name'],
     705        Table.DbClient.DbManager.DataTypes.SearchByName(DbRows[I].Items['DataType']));
     706      NewField.TextBefore := DbRows[I].Items['Caption'];
     707    end;
     708  finally
     709    DbRows.Free;
     710  end;
     711end;
     712
    660713{ TRecord }
    661714
     
    668721  Result := True;
    669722  for I := 0 to aValues.Count - 1 do begin
    670     Field := Parent.Fields.SearchByName(AValues.Names[I]);
    671     FieldIndex := Parent.Fields.IndexOf(Field);
     723    Field := Table.Fields.SearchByName(AValues.Names[I]);
     724    FieldIndex := Table.Fields.IndexOf(Field);
    672725    if Assigned(Field) then begin
    673726      if Values[FieldIndex].GetString <> AValues.ValueFromIndex[I] then begin
     
    684737begin
    685738  Values.Clear;
    686   for I := 0 to Parent.Fields.Count - 1 do
    687     Values.Add(Parent.Fields[I].GetValueClass.Create);
     739  for I := 0 to Table.Fields.Count - 1 do
     740    Values.Add(Table.Fields[I].GetValueClass.Create);
    688741end;
    689742
     
    781834end;
    782835
    783 { TTable }
    784 
    785 procedure TTable.LoadRecords;
    786 var
    787   DbRows: TDbRows;
    788   I: Integer;
    789   F: Integer;
    790   NewRecord: TRecord;
    791   NewValue: TValue;
    792   Value: string;
    793 begin
    794   Records.Clear;
    795   DbRows := TDbRows.Create;
    796   try
    797     DbClient.Query('SELECT * FROM ' + Name, DbRows);
    798     for I := 0 to DbRows.Count - 1 do begin
    799       NewRecord := TRecord.Create;
    800       for F := 0 to Fields.Count - 1 do begin
    801         NewValue := Fields[F].GetValueClass.Create;
    802         if DbRows[I].TryGetValue(Fields[F].Name, Value) then begin
    803           NewValue.SetString(Value);
    804           NewRecord.Values.Add(NewValue);
    805         end else begin
    806           //NewValue.SetString('');
    807           NewRecord.Values.Add(NewValue);
    808         end;
    809       end;
    810       Records.Add(NewRecord);
    811     end;
    812   finally
    813     DbRows.Free;
    814   end;
    815 end;
    816 
    817836procedure TTable.LoadRecordsCount;
    818837var
     
    842861begin
    843862  Records := TRecords.Create;
    844   Records.Parent := Self;
     863  Records.Table := Self;
    845864  Fields := TFields.Create;
    846865  Fields.Table := Self;
Note: See TracChangeset for help on using the changeset viewer.