Changeset 13 for trunk/UDatabase.pas


Ignore:
Timestamp:
Mar 17, 2015, 12:09:11 AM (10 years ago)
Author:
chronos
Message:
  • Modified: Now records are loaded from SQL database using SQL query. The query is parsed also by XML database client to load records from XML file.
  • Added: Packages Network and Synapse.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UDatabase.pas

    r12 r13  
    66
    77uses
    8   Classes, SysUtils, Contnrs, ExtCtrls, StdCtrls, EditBtn, dialogs;
     8  Classes, SysUtils, Contnrs, ExtCtrls, StdCtrls, EditBtn, dialogs, USqlDatabase,
     9  SpecializedDictionary;
    910
    1011type
     
    2324    procedure Assign(Source: TValue); virtual;
    2425    function GetString: string; virtual;
     26    function SetString(Value: string): string; virtual;
    2527  end;
    2628
     
    9496
    9597  TTable = class
     98    Id: Integer;
    9699    Name: string;
    97100    Caption: string;
     
    99102    Fields: TFields;
    100103    Database: TDatabase;
     104    RecordsCount: Integer;
     105    procedure LoadRecords;
     106    procedure LoadRecordsCount;
    101107    procedure Assign(Source: TTable);
    102108    constructor Create;
     
    104110  end;
    105111
     112  { TTables }
     113
    106114  TTables = class(TObjectList)
    107115    Database: TDatabase;
     116    function SearchByName(Name: string): TTable;
    108117  end;
    109118
     
    140149
    141150  TDataType = class
     151    Id: Integer;
    142152    Name: string;
    143153    FieldType: TFieldType;
     
    148158
    149159  TDataTypes = class(TObjectList)
    150     function RegisterType(Name: string; FieldType: TFieldType; FieldTypeClass: TFieldTypeSpecificClass): TDataType;
     160    function RegisterType(Id: Integer; Name: string; FieldType: TFieldType; FieldTypeClass: TFieldTypeSpecificClass): TDataType;
    151161    function FindByType(FieldType: TFieldType): TDataType;
    152162  end;
     
    156166  TDatabaseClient = class
    157167    Database: TDatabase;
     168    procedure Query(DbRows: TDbRows; Text: string); virtual;
    158169    constructor Create; virtual;
    159170    procedure Load; virtual;
     
    194205  UDataTypes;
    195206
     207{ TTables }
     208
     209function TTables.SearchByName(Name: string): TTable;
     210var
     211  I: Integer;
     212begin
     213  I := 0;
     214  while (I < Count) and (TTable(Items[I]).Name <> Name) do Inc(I);
     215  if I < Count then Result := TTable(Items[I])
     216    else Result := nil;
     217end;
     218
    196219{ TDatabases }
    197220
     
    208231{ TDatabaseClient }
    209232
     233procedure TDatabaseClient.Query(DbRows: TDbRows; Text: string);
     234begin
     235end;
     236
    210237constructor TDatabaseClient.Create;
    211238begin
     
    245272{ TDataTypes }
    246273
    247 function TDataTypes.RegisterType(Name: string; FieldType: TFieldType;
     274function TDataTypes.RegisterType(Id: Integer; Name: string; FieldType: TFieldType;
    248275  FieldTypeClass: TFieldTypeSpecificClass): TDataType;
    249276begin
    250277  Result := TDataType.Create;
     278  Result.Id := Id;
    251279  Result.Name := Name;
    252280  Result.FieldType := FieldType;
     
    380408end;
    381409
     410function TValue.SetString(Value: string): string;
     411begin
     412
     413end;
     414
    382415{ TFieldTypeSpecific }
    383416
     
    430463{ TTable }
    431464
     465procedure TTable.LoadRecords;
     466var
     467  DbRows: TDbRows;
     468  I: Integer;
     469  F: Integer;
     470  NewRecord: TRecord;
     471  NewValue: TValue;
     472begin
     473  Records.Clear;
     474  DbRows := TDbRows.Create;
     475  Database.Client.Query(DbRows, 'SELECT * FROM ' + Name);
     476  for I := 0 to DbRows.Count - 1 do begin
     477    NewRecord := TRecord.Create;
     478    for F := 0 to Fields.Count - 1 do begin
     479      NewValue := TField(Fields[F]).GetValueClass.Create;
     480      NewValue.SetString(TDictionaryStringString(DbRows[I]).Values[TField(Fields[F]).Name]);
     481      NewRecord.Values.Add(NewValue);
     482    end;
     483    Records.Add(NewRecord);
     484  end;
     485  DbRows.Free;
     486end;
     487
     488procedure TTable.LoadRecordsCount;
     489var
     490  DbRows: TDbRows;
     491  I: Integer;
     492  F: Integer;
     493  NewRecord: TRecord;
     494  NewValue: TValue;
     495begin
     496  Records.Clear;
     497  DbRows := TDbRows.Create;
     498  Database.Client.Query(DbRows, 'SELECT COUNT(*) FROM ' + Name);
     499  if DbRows.Count = 1 then begin
     500    RecordsCount := StrToInt(TDictionaryStringString(DbRows[0]).Items[0].Value);
     501  end else RecordsCount := 0;
     502  DbRows.Free;
     503end;
     504
    432505procedure TTable.Assign(Source: TTable);
    433506begin
Note: See TracChangeset for help on using the changeset viewer.