Ignore:
Timestamp:
Oct 30, 2010, 7:36:25 PM (14 years ago)
Author:
george
Message:
  • Modified: TAssociativeArray replaced by specialized generic class TDictionaryStringString.
  • Modified: All TObjectList replaced by specialized generic class TListObject.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Common/USqlDatabase.pas

    r34 r37  
    88
    99uses
    10   SysUtils, Classes, Dialogs, mysql50, TypInfo, Contnrs, UStringListEx;
     10  SysUtils, Classes, Dialogs, mysql50, TypInfo, UStringListEx,
     11  ListObject, DictionaryStringString;
    1112
    1213type
     
    2021  TSetClientCapabilities = set of TClientCapabilities;
    2122
    22   TAssociativeArray = class(TStringList)
     23  TDbRows = class(TListObject)
    2324  private
    24     function GetValues(Index: string): string;
    25     function GetValuesAtIndex(Index: Integer): string;
    26     procedure SetValues(Index: string; const Value: string);
     25    function GetData(Index: Integer): TDictionaryStringString;
     26    procedure SetData(Index: Integer; const Value: TDictionaryStringString);
    2727  public
    28     constructor Create;
    29     destructor Destroy; override;
    30     function GetAllValues: string;
    31     procedure AddKeyValue(Key, Value: string);
    32     property ValuesAtIndex[Index: Integer]: string read GetValuesAtIndex;
    33     property Values[Index: string]: string read GetValues write SetValues; default;
    34   end;
    35 
    36   TDbRows = class(TObjectList)
    37   private
    38     function GetData(Index: Integer): TAssociativeArray;
    39     procedure SetData(Index: Integer; const Value: TAssociativeArray);
    40   public
    41     property Data[Index: Integer]: TAssociativeArray read GetData write SetData; default;
     28    property Data[Index: Integer]: TDictionaryStringString read GetData write SetData; default;
    4229    destructor Destroy; override;
    4330  end;
     
    6855    function Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows;
    6956    procedure Delete(ATable: string; Condition: string = '1');
    70     procedure Insert(ATable: string; Data: TAssociativeArray);
    71     procedure Update(ATable: string; Data: TAssociativeArray; Condition: string = '1');
    72     procedure Replace(ATable: string; Data: TAssociativeArray);
     57    procedure Insert(ATable: string; Data: TDictionaryStringString);
     58    procedure Update(ATable: string; Data: TDictionaryStringString; Condition: string = '1');
     59    procedure Replace(ATable: string; Data: TDictionaryStringString);
    7360    procedure Connect;
    7461    procedure Disconnect;
     
    186173end;
    187174
    188 procedure TSqlDatabase.Insert(ATable: string; Data: TAssociativeArray);
     175procedure TSqlDatabase.Insert(ATable: string; Data: TDictionaryStringString);
    189176var
    190177  DbNames: string;
     
    198185  DbValues := '';
    199186  for I := 0 to Data.Count - 1 do begin
    200     Value := Data.ValuesAtIndex[I];
     187    Value := Data.Items[I].Value;
    201188    StringReplace(Value, '"', '\"', [rfReplaceAll]);
    202189    if Value = 'NOW()' then DbValues := DbValues + ',' + Value
    203190    else DbValues := DbValues + ',"' + Value + '"';
    204     DbNames := DbNames + ',`' + Data.Names[I] + '`';
     191    DbNames := DbNames + ',`' + Data.Keys[I] + '`';
    205192  end;
    206193  System.Delete(DbNames, 1, 1);
     
    233220    for I := 0 to Result.Count - 1 do begin
    234221      DbRow := mysql_fetch_row(DbResult);
    235       Result[I] := TAssociativeArray.Create;
     222      Result[I] := TDictionaryStringString.Create;
    236223      with Result[I] do begin
    237224        for II := 0 to mysql_num_fields(DbResult) - 1 do begin
    238           Add(mysql_fetch_field_direct(DbResult, II)^.Name +
    239             NameValueSeparator + PChar((DbRow + II)^));
     225          Add(mysql_fetch_field_direct(DbResult, II)^.Name,
     226            PChar((DbRow + II)^));
    240227          end;
    241228        end;
     
    245232end;
    246233
    247 procedure TSqlDatabase.Replace(ATable: string; Data: TAssociativeArray);
     234procedure TSqlDatabase.Replace(ATable: string; Data: TDictionaryStringString);
    248235var
    249236  DbNames: string;
     
    257244  DbValues := '';
    258245  for I := 0 to Data.Count - 1 do begin
    259     Value := Data.ValuesAtIndex[I];
     246    Value := Data.Items[I].Value;
    260247    StringReplace(Value, '"', '\"', [rfReplaceAll]);
    261248    if Value = 'NOW()' then DbValues := DbValues + ',' + Value
    262249    else DbValues := DbValues + ',"' + Value + '"';
    263     DbNames := DbNames + ',`' + Data.Names[I] + '`';
     250    DbNames := DbNames + ',`' + Data.Keys[I] + '`';
    264251  end;
    265252  System.Delete(DbNames, 1, 1);
     
    278265end;
    279266
    280 procedure TSqlDatabase.Update(ATable: string; Data: TAssociativeArray; Condition: string = '1');
     267procedure TSqlDatabase.Update(ATable: string; Data: TDictionaryStringString; Condition: string = '1');
    281268var
    282269  DbValues: string;
     
    288275  DbValues := '';
    289276  for I := 0 to Data.Count - 1 do begin
    290     Value := Data.ValuesAtIndex[I];
     277    Value := Data.Items[I].Value;
    291278    StringReplace(Value, '"', '\"', [rfReplaceAll]);
    292279    if Value = 'NOW()' then DbValues := DbValues + ',' + Value
    293     else DbValues := DbValues + ',' + Data.Names[I] + '=' + '"' + Value + '"';
     280    else DbValues := DbValues + ',' + Data.Keys[I] + '=' + '"' + Value + '"';
    294281  end;
    295282  System.Delete(DbValues, 1, 1);
     
    395382end;
    396383
    397 { TAssociativeArray }
    398 
    399 procedure TAssociativeArray.AddKeyValue(Key, Value: string);
    400 begin
    401   Add(Key + NameValueSeparator + Value);
    402 end;
    403 
    404 constructor TAssociativeArray.Create;
    405 begin
    406   NameValueSeparator := '|';
    407 end;
    408 
    409 destructor TAssociativeArray.Destroy;
     384{ TDbRows }
     385
     386destructor TDbRows.Destroy;
    410387begin
    411388  inherited;
    412389end;
    413390
    414 function TAssociativeArray.GetAllValues: string;
    415 var
    416   I: Integer;
    417 begin
    418   Result := '';
    419   for I := 0 to Count - 1 do begin
    420     Result := Result + Names[I] + '=' + ValuesAtIndex[I] + ',';
    421   end;
    422 end;
    423 
    424 function TAssociativeArray.GetValues(Index: string): string;
    425 begin
    426   Result := inherited Values[Index];
    427 end;
    428 
    429 function TAssociativeArray.GetValuesAtIndex(Index: Integer): string;
    430 begin
    431   Result := inherited Values[Names[Index]];
    432 end;
    433 
    434 procedure TAssociativeArray.SetValues(Index: string; const Value: string);
    435 begin
    436   inherited Values[Index] := Value;
    437 end;
    438 
    439 { TDbRows }
    440 
    441 destructor TDbRows.Destroy;
    442 begin
    443   inherited;
    444 end;
    445 
    446 function TDbRows.GetData(Index: Integer): TAssociativeArray;
    447 begin
    448   Result := TAssociativeArray(Items[Index]);
    449 end;
    450 
    451 procedure TDbRows.SetData(Index: Integer; const Value: TAssociativeArray);
     391function TDbRows.GetData(Index: Integer): TDictionaryStringString;
     392begin
     393  Result := TDictionaryStringString(Items[Index]);
     394end;
     395
     396procedure TDbRows.SetData(Index: Integer; const Value: TDictionaryStringString);
    452397begin
    453398  Items[Index] := Value;
Note: See TracChangeset for help on using the changeset viewer.