Changeset 29 for trunk/UDatabase.pas
- Timestamp:
- Sep 10, 2022, 10:54:56 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UDatabase.pas
r28 r29 4 4 5 5 uses 6 Classes, SysUtils, ExtCtrls, dialogs, USqlDatabase, URegistry, UGenerics,6 Classes, SysUtils, ExtCtrls, Dialogs, USqlDatabase, URegistry, UGenerics, 7 7 Generics.Collections; 8 8 … … 76 76 procedure Assign(Source: TFields); 77 77 function AddNew(Name: string; DataType: TDataType): TField; 78 procedure Load; 78 79 end; 79 80 … … 81 82 82 83 TRecord = class 83 Parent: TTable;84 Table: TTable; 84 85 Values: TValues; 85 86 function Match(AValues: TStrings): Boolean; … … 93 94 94 95 TRecords = class(TObjectList<TRecord>) 95 Parent: TTable;96 Table: TTable; 96 97 procedure Assign(Source: TRecords); 97 98 function SearchByValue(Name, Value: string): TRecord; 98 99 function SearchByValues(Values: TStrings): TRecord; 99 100 function AddNew: TRecord; 101 procedure Load; 100 102 end; 101 103 … … 110 112 DbClient: TDbClient; 111 113 RecordsCount: Integer; 112 procedure LoadRecords;113 114 procedure LoadRecordsCount; 114 115 procedure Assign(Source: TTable); … … 594 595 begin 595 596 Result := nil; 596 Field := Parent.Fields.SearchByName(Name);597 Field := Table.Fields.SearchByName(Name); 597 598 if Assigned(Field) then begin 598 FieldIndex := Parent.Fields.IndexOf(Field);599 FieldIndex := Table.Fields.IndexOf(Field); 599 600 I := 0; 600 601 while (I < Count) and (Items[I].Values[FieldIndex].GetString <> Value) do Inc(I); … … 618 619 begin 619 620 Result := TRecord.Create; 620 Result. Parent := Parent;621 Result.Table := Table; 621 622 Result.InitValues; 622 623 Add(Result); 624 end; 625 626 procedure TRecords.Load; 627 var 628 DbRows: TDbRows; 629 I: Integer; 630 F: Integer; 631 NewRecord: TRecord; 632 NewValue: TValue; 633 Value: string; 634 begin 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; 623 656 end; 624 657 … … 658 691 end; 659 692 693 procedure TFields.Load; 694 var 695 DbRows: TDbRows; 696 NewField: TField; 697 I: Integer; 698 begin 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; 711 end; 712 660 713 { TRecord } 661 714 … … 668 721 Result := True; 669 722 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); 672 725 if Assigned(Field) then begin 673 726 if Values[FieldIndex].GetString <> AValues.ValueFromIndex[I] then begin … … 684 737 begin 685 738 Values.Clear; 686 for I := 0 to Parent.Fields.Count - 1 do687 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); 688 741 end; 689 742 … … 781 834 end; 782 835 783 { TTable }784 785 procedure TTable.LoadRecords;786 var787 DbRows: TDbRows;788 I: Integer;789 F: Integer;790 NewRecord: TRecord;791 NewValue: TValue;792 Value: string;793 begin794 Records.Clear;795 DbRows := TDbRows.Create;796 try797 DbClient.Query('SELECT * FROM ' + Name, DbRows);798 for I := 0 to DbRows.Count - 1 do begin799 NewRecord := TRecord.Create;800 for F := 0 to Fields.Count - 1 do begin801 NewValue := Fields[F].GetValueClass.Create;802 if DbRows[I].TryGetValue(Fields[F].Name, Value) then begin803 NewValue.SetString(Value);804 NewRecord.Values.Add(NewValue);805 end else begin806 //NewValue.SetString('');807 NewRecord.Values.Add(NewValue);808 end;809 end;810 Records.Add(NewRecord);811 end;812 finally813 DbRows.Free;814 end;815 end;816 817 836 procedure TTable.LoadRecordsCount; 818 837 var … … 842 861 begin 843 862 Records := TRecords.Create; 844 Records. Parent:= Self;863 Records.Table := Self; 845 864 Fields := TFields.Create; 846 865 Fields.Table := Self;
Note:
See TracChangeset
for help on using the changeset viewer.