Changeset 26 for trunk/UDatabase.pas


Ignore:
Timestamp:
Sep 10, 2022, 8:03:08 PM (2 years ago)
Author:
chronos
Message:
  • Removed: TemplateGenerics as required package. Used Generics.Collections instead.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        77heaptrclog.trc
        88MyData.exe
         9MyData.dbg
         10
  • trunk/UDatabase.pas

    r24 r26  
    11unit UDatabase;
    22
    3 {$mode delphi}
    4 
    53interface
    64
    75uses
    8   Classes, SysUtils, Contnrs, ExtCtrls, StdCtrls, EditBtn, dialogs, USqlDatabase,
    9   SpecializedDictionary, URegistry;
     6  Classes, SysUtils, ExtCtrls, StdCtrls, EditBtn, dialogs, USqlDatabase,
     7  URegistry, UGenerics, Generics.Collections;
    108
    119type
     
    3331  { TValues }
    3432
    35   TValues = class(TObjectList)
     33  TValues = class(TObjectList<TValue>)
    3634    procedure Assign(Source: TValues);
    3735  end;
     
    7371  { TFields }
    7472
    75   TFields = class(TObjectList)
     73  TFields = class(TObjectList<TField>)
    7674    Table: TTable;
    7775    function SearchByName(Name: string): TField;
     
    9492  { TRecords }
    9593
    96   TRecords = class(TObjectList)
     94  TRecords = class(TObjectList<TRecord>)
    9795    Parent: TTable;
    9896    procedure Assign(Source: TRecords);
     
    121119  { TTables }
    122120
    123   TTables = class(TObjectList)
     121  TTables = class(TObjectList<TTable>)
    124122    DbClient: TDbClient;
    125123    function SearchByName(Name: string): TTable;
     
    158156  { TDbConnectProfiles }
    159157
    160   TDbConnectProfiles = class(TObjectList)
     158  TDbConnectProfiles = class(TObjectList<TDbConnectProfile>)
    161159    DbManager: TDbManager;
    162160    procedure LoadFromRegistry(Context: TRegistryContext);
     
    177175  { TDataTypes }
    178176
    179   TDataTypes = class(TObjectList)
     177  TDataTypes = class(TObjectList<TDataType>)
    180178    function RegisterType(Id: Integer; Name, Title: string;
    181179      FieldType: TFieldType; FieldTypeClass: TFieldTypeSpecificClass): TDataType;
     
    223221  { TDbClientTypes }
    224222
    225   TDbClientTypes = class(TObjectList)
     223  TDbClientTypes = class(TObjectList<TDbClientType>)
    226224    function RegisterClientType(Name: string; DatabaseClass: TDbClientClass;
    227225      ConnectParamsClass: TDbConnectParamsClass): TDbClientType;
     
    265263
    266264uses
    267   UDataTypes,
    268   UEngineXML, UEngineMySQL, UEngineSQLite, UDbClientRegistry;
     265  UDataTypes, UEngineXML, UEngineMySQL, UEngineSQLite, UDbClientRegistry;
    269266
    270267{ TDbManager }
     
    319316destructor TDbManager.Destroy;
    320317begin
    321   DataTypes.Free;
    322   ClientTypes.Free;
    323   ConnectProfiles.Free;
    324   inherited Destroy;
     318  FreeAndNil(DataTypes);
     319  FreeAndNil(ClientTypes);
     320  FreeAndNil(ConnectProfiles);
     321  inherited;
    325322end;
    326323
     
    378375    for I := 0 to Count - 1 do begin
    379376      OpenKey(Context.Key + '\Item' + IntToStr(I), True);
    380       SetValue('Name', TDbConnectProfile(Items[I]).Name);
    381       SetValue('ConnectionString', TDbConnectProfile(Items[I]).Params.ConnectionString);
    382       SetValue('ClientType', TDbConnectProfile(Items[I]).ClientType.Name);
     377      SetValue('Name', Items[I].Name);
     378      SetValue('ConnectionString', Items[I].Params.ConnectionString);
     379      SetValue('ClientType', Items[I].ClientType.Name);
    383380    end;
    384381  finally
     
    392389begin
    393390  I := 0;
    394   while (I < Count) and (TDbConnectProfile(Items[I]).Name <> Name) do Inc(I);
    395   if (I < Count) then Result := TDbConnectProfile(Items[I])
     391  while (I < Count) and (Items[I].Name <> Name) do Inc(I);
     392  if (I < Count) then Result := Items[I]
    396393    else Result := nil;
    397394end;
     
    415412  ClientType := nil;
    416413  if Assigned(Params) then Params.Free;
    417   inherited Destroy;
     414  inherited;
    418415end;
    419416
     
    432429begin
    433430  I := 0;
    434   while (I < Count) and (TTable(Items[I]).Name <> Name) do Inc(I);
    435   if I < Count then Result := TTable(Items[I])
     431  while (I < Count) and (Items[I].Name <> Name) do Inc(I);
     432  if I < Count then Result := Items[I]
    436433    else Result := nil;
    437434end;
     
    469466procedure TDbClient.Load;
    470467begin
    471 
    472468end;
    473469
    474470procedure TDbClient.Save;
    475471begin
    476 
    477472end;
    478473
     
    494489begin
    495490  I := 0;
    496   while (I < Count) and (TDbClientType(Items[I]).Name <> Name) do Inc(I);
    497   if I < Count then Result := TDbClientType(Items[I])
     491  while (I < Count) and (Items[I].Name <> Name) do Inc(I);
     492  if I < Count then Result := Items[I]
    498493    else Result := nil;
    499494end;
     
    518513begin
    519514  I := 0;
    520   while (I < Count) and (TDataType(Items[I]).FieldType <> FieldType) do Inc(I);
    521   if I < Count then Result := TDataType(Items[I])
     515  while (I < Count) and (Items[I].FieldType <> FieldType) do Inc(I);
     516  if I < Count then Result := Items[I]
    522517    else Result := nil;
    523518end;
     
    528523begin
    529524  I := 0;
    530   while (I < Count) and (TDataType(Items[I]).Name <> Name) do Inc(I);
    531   if I < Count then Result := TDataType(Items[I])
     525  while (I < Count) and (Items[I].Name <> Name) do Inc(I);
     526  if I < Count then Result := Items[I]
    532527    else Result := nil;
    533528end;
     
    552547destructor TDbClientType.Destroy;
    553548begin
    554   DataTypes.Free;
    555   inherited Destroy;
     549  FreeAndNil(DataTypes);
     550  inherited;
    556551end;
    557552
     
    567562  for I := OldCount to Count - 1 do
    568563    //ShowMessage(TValue(Source.Items[I]).ClassName);
    569     Items[I] := TValueClass(TValue(Source.Items[I]).ClassType).Create;
     564    Items[I] := TValueClass(Source.Items[I].ClassType).Create;
    570565  for I := 0 to Count - 1 do
    571     if TValue(Items[I]).ClassType <> TValue(Source.Items[I]).ClassType then begin
    572       Items[I] := TValueClass(TValue(Source.Items[I]).ClassType).Create;
     566    if TValue(Items[I]).ClassType <> Source.Items[I].ClassType then begin
     567      Items[I] := TValueClass(Source.Items[I].ClassType).Create;
    573568    end;
    574569  for I := 0 to Source.Count - 1 do begin
    575     TValue(Items[I]).Assign(TValue(Source.Items[I]));
     570    Items[I].Assign(Source.Items[I]);
    576571  end;
    577572end;
     
    589584    Items[I] := TRecord.Create;
    590585  for I := 0 to Source.Count - 1 do begin
    591     TRecord(Items[I]).Assign(TRecord(Source.Items[I]));
     586    Items[I].Assign(Source.Items[I]);
    592587  end;
    593588end;
     
    604599    FieldIndex := Parent.Fields.IndexOf(Field);
    605600    I := 0;
    606     while (I < Count) and (TValue(TRecord(Items[I]).Values[FieldIndex]).GetString <> Value) do Inc(I);
    607     if I < Count then Result := TRecord(Items[I])
     601    while (I < Count) and (Items[I].Values[FieldIndex].GetString <> Value) do Inc(I);
     602    if I < Count then Result := Items[I]
    608603      else Result := nil;
    609604  end;
     
    616611  Result := nil;
    617612  I := 0;
    618   while (I < Count) and (TRecord(Items[I]).Match(Values)) do Inc(I);
    619   if I < Count then Result := TRecord(Items[I])
     613  while (I < Count) and (Items[I].Match(Values)) do Inc(I);
     614  if I < Count then Result := Items[I]
    620615    else Result := nil;
    621616end;
     
    651646    Items[I] := TField.Create;
    652647  for I := 0 to Source.Count - 1 do begin
    653     TField(Items[I]).Assign(TField(Source.Items[I]));
     648    Items[I].Assign(Source.Items[I]);
    654649  end;
    655650end;
     
    706701destructor TRecord.Destroy;
    707702begin
    708   Values.Free;
    709   inherited Destroy;
     703  FreeAndNil(Values);
     704  inherited;
    710705end;
    711706
     
    723718procedure TValue.SetString(Value: string);
    724719begin
    725 
    726720end;
    727721
     
    739733procedure TFieldTypeSpecific.Assign(Source: TFieldTypeSpecific);
    740734begin
    741 
    742735end;
    743736
     
    786779begin
    787780  DataType := nil;
    788   inherited Destroy;
     781  inherited;
    789782end;
    790783
     
    798791  NewRecord: TRecord;
    799792  NewValue: TValue;
    800   Index: Integer;
     793  Value: string;
    801794begin
    802795  Records.Clear;
    803796  DbRows := TDbRows.Create;
    804   DbClient.Query('SELECT * FROM ' + Name, DbRows);
    805   for I := 0 to DbRows.Count - 1 do begin
    806     NewRecord := TRecord.Create;
    807     for F := 0 to Fields.Count - 1 do begin
    808       NewValue := TField(Fields[F]).GetValueClass.Create;
    809       Index := TDictionaryStringString(DbRows[I]).SearchKey(TField(Fields[F]).Name);
    810       if Index <> -1 then begin
    811         NewValue.SetString(TDictionaryStringString(DbRows[I]).Values[TField(Fields[F]).Name]);
    812         NewRecord.Values.Add(NewValue);
    813       end else begin
    814         //NewValue.SetString('');
    815         NewRecord.Values.Add(NewValue);
     797  try
     798    DbClient.Query('SELECT * FROM ' + Name, DbRows);
     799    for I := 0 to DbRows.Count - 1 do begin
     800      NewRecord := TRecord.Create;
     801      for F := 0 to Fields.Count - 1 do begin
     802        NewValue := Fields[F].GetValueClass.Create;
     803        if DbRows[I].TryGetValue(Fields[F].Name, Value) then begin
     804          NewValue.SetString(Value);
     805          NewRecord.Values.Add(NewValue);
     806        end else begin
     807          //NewValue.SetString('');
     808          NewRecord.Values.Add(NewValue);
     809        end;
    816810      end;
     811      Records.Add(NewRecord);
    817812    end;
    818     Records.Add(NewRecord);
    819   end;
    820   DbRows.Free;
     813  finally
     814    DbRows.Free;
     815  end;
    821816end;
    822817
     
    827822  Records.Clear;
    828823  DbRows := TDbRows.Create;
    829   DbClient.Query('SELECT COUNT(*) FROM ' + Name, DbRows);
    830   if DbRows.Count = 1 then begin
    831     RecordsCount := StrToInt(TDictionaryStringString(DbRows[0]).Items[0].Value);
    832   end else RecordsCount := 0;
    833   DbRows.Free;
     824  try
     825    DbClient.Query('SELECT COUNT(*) AS `Count` FROM ' + Name, DbRows);
     826    if DbRows.Count = 1 then begin
     827      RecordsCount := StrToInt(DbRows[0].Items['Count']);
     828    end else RecordsCount := 0;
     829  finally
     830    DbRows.Free;
     831  end;
    834832end;
    835833
     
    852850destructor TTable.Destroy;
    853851begin
    854   Fields.Free;
    855   Records.Free;
     852  FreeAndNil(Fields);
     853  FreeAndNil(Records);
    856854  inherited Destroy;
    857855end;
Note: See TracChangeset for help on using the changeset viewer.