Ignore:
Timestamp:
Sep 9, 2022, 1:16:58 AM (22 months ago)
Author:
chronos
Message:
  • Added: Robots page.
  • Modified: Canonical URL for webcams.
  • Modified: Removed compiler mode delphi as it is already set in project.
  • Modified: Updated Common package.
  • Modified: Use Generics.Collections instead of fgl.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/CoolWeb/Persistence/USqlDatabase.pas

    r103 r137  
    11unit USqlDatabase;
    22
    3 {$mode Delphi}{$H+}
    4 
    5 // Modified: 2010-12-24
     3// Modified: 2022-09-08
    64
    75interface
    86
    97uses
    10   SysUtils, Classes, Dialogs, mysql50, TypInfo,
    11   SpecializedDictionary, SpecializedList;
     8  SysUtils, Classes, Dialogs, mysql50, TypInfo, SpecializedDictionary,
     9  SpecializedList;
    1210
    1311type
     
    6159    procedure CreateColumn(Table, ColumnName: string; ColumnType: TTypeKind);
    6260    procedure Query(DbRows: TDbRows; Data: string);
    63     procedure Select(DbRows: TDbRows; ATable: string; Filter: string = '*'; Condition: string = '1');
    64     procedure Delete(ATable: string; Condition: string = '1';
     61    procedure Select(DbRows: TDbRows; ATable: string; Filter: string = '*';
     62      Condition: string = '');
     63    procedure Delete(ATable: string; Condition: string = '';
    6564      Schema: string = '');
    6665    procedure Insert(ATable: string; Data: TDictionaryStringString;
    6766      Schema: string = '');
    6867    procedure Update(ATable: string; Data: TDictionaryStringString;
    69       Condition: string = '1'; Schema: string = '');
     68      Condition: string = ''; Schema: string = '');
    7069    procedure Replace(ATable: string; Data: TDictionaryStringString;
    7170      Schema: string = '');
     
    10099
    101100uses
    102   DateUtils, Math;
     101  DateUtils;
    103102
    104103resourcestring
     
    184183  Rows: TDbRows;
    185184begin
    186 //  mySQLClient1.Connect;
    187185  FSession := mysql_init(FSession);
    188 //  FSession.charset := 'latin2';
    189186  NewSession := mysql_real_connect(FSession, PChar(HostName), PChar(UserName),
    190187    PChar(Password), PChar(Database), FPort, nil, CLIENT_LONG_PASSWORD + CLIENT_CONNECT_WITH_DB);
     
    250247
    251248  DbResult := mysql_store_result(FSession);
    252   if Assigned(DbResult) then begin
    253     DbRows.Count := mysql_num_rows(DbResult);
    254     for I := 0 to DbRows.Count - 1 do begin
    255       DbRow := mysql_fetch_row(DbResult);
    256       DbRows[I] := TDictionaryStringString.Create;
    257       with DbRows[I] do begin
    258         for II := 0 to mysql_num_fields(DbResult) - 1 do begin
    259           Add(mysql_fetch_field_direct(DbResult, II)^.Name,
    260             PChar((DbRow + II)^));
     249  try
     250    if Assigned(DbResult) then begin
     251      DbRows.Count := mysql_num_rows(DbResult);
     252      for I := 0 to DbRows.Count - 1 do begin
     253        DbRow := mysql_fetch_row(DbResult);
     254        DbRows[I] := TDictionaryStringString.Create;
     255        with DbRows[I] do begin
     256          for II := 0 to mysql_num_fields(DbResult) - 1 do begin
     257            Add(mysql_fetch_field_direct(DbResult, II)^.Name,
     258              PChar((DbRow + II)^));
    261259          end;
    262260        end;
    263261      end;
    264   end;
    265   mysql_free_result(DbResult);
     262    end;
     263  finally
     264    mysql_free_result(DbResult);
     265  end;
    266266end;
    267267
     
    296296end;
    297297
    298 procedure TSqlDatabase.Select(DbRows: TDbRows; ATable: string; Filter: string = '*'; Condition: string = '1');
     298procedure TSqlDatabase.Select(DbRows: TDbRows; ATable: string; Filter: string = '*'; Condition: string = '');
     299var
     300  QueryText: string;
    299301begin
    300302  LastUsedTable := ATable;
    301   Query(DbRows, 'SELECT ' + Filter + ' FROM `' + ATable + '` WHERE ' + Condition);
     303  QueryText := 'SELECT ' + Filter + ' FROM `' + ATable + '`';
     304  if Condition <> '' then QueryText := QueryText + ' WHERE ' + Condition;
     305  Query(DbRows, QueryText);
    302306end;
    303307
    304308procedure TSqlDatabase.Update(ATable: string; Data: TDictionaryStringString;
    305   Condition: string = '1'; Schema: string = '');
    306 var
     309  Condition: string = ''; Schema: string = '');
     310var
     311  QueryText: string;
    307312  DbValues: string;
    308313  Value: string;
     
    322327    DbResult := TDbRows.Create;
    323328    if Schema <> '' then Schema := '`' + Schema + '`.';
    324     Query(DbResult, 'UPDATE ' + Schema + '`' + ATable + '` SET ' + DbValues + ' WHERE ' + Condition);
     329    QueryText := 'UPDATE ' + Schema + '`' + ATable + '` SET ' + DbValues;
     330    if Condition <> '' then QueryText := QueryText + ' WHERE ' + Condition;
     331    Query(DbResult, QueryText);
    325332  finally
    326333    DbResult.Free;
     
    333340end;
    334341
    335 procedure TSqlDatabase.Delete(ATable: string; Condition: string = '1';
     342procedure TSqlDatabase.Delete(ATable: string; Condition: string = '';
    336343  Schema: string = '');
    337344var
     345  QueryText: string;
    338346  DbResult: TDbRows;
    339347begin
     
    342350    DbResult := TDbRows.Create;
    343351    if Schema <> '' then Schema := '`' + Schema + '`.';
    344     Query(DbResult, 'DELETE FROM ' + Schema + '`' + ATable + '` WHERE ' + Condition);
     352    QueryText := 'DELETE FROM ' + Schema + '`' + ATable + '`';
     353    if Condition <> '' then QueryText := QueryText + ' WHERE ' + Condition;
     354    Query(DbResult, QueryText);
    345355  finally
    346356    DbResult.Free;
     
    497507end.
    498508
    499 
Note: See TracChangeset for help on using the changeset viewer.