Ignore:
Timestamp:
Nov 13, 2010, 4:32:15 PM (14 years ago)
Author:
george
Message:
  • Přidáno: Zobrazení seznamu hostovaných projektů.
  • Upraveno: Funkce Query jednotky SQLDatabase nyní nevrací novou instanci, ale výsledné řádky vrací v parametru. Bezpečnější metoda předcházející únikům paměti.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Common/USqlDatabase.pas

    r37 r38  
    33{$mode Delphi}{$H+}
    44
    5 // Upraveno: 8.9.2010
     5// Upraveno: 28.10.2010
    66
    77interface
     
    5252    procedure CreateTable(Name: string);
    5353    procedure CreateColumn(Table, ColumnName: string; ColumnType: TTypeKind);
    54     function Query(Data: string): TDbRows;
    55     function Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows;
     54    procedure Query(DbRows: TDbRows; Data: string);
     55    procedure Select(DbRows: TDbRows; ATable: string; Filter: string = '*'; Condition: string = '1');
    5656    procedure Delete(ATable: string; Condition: string = '1');
    5757    procedure Insert(ATable: string; Data: TDictionaryStringString);
     
    167167
    168168  try
    169     Rows := Query('SET NAMES ' + Encoding);
     169    Rows := TDbRows.Create;
     170    Query(Rows, 'SET NAMES ' + Encoding);
    170171  finally
    171172    Rows.Free;
     
    194195  System.Delete(DbValues, 1, 1);
    195196  try
    196     DbResult := Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
     197    DbResult := TDbRows.Create;
     198    Query(DbResult, 'INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
    197199  finally
    198200    DbResult.Free;
     
    200202end;
    201203
    202 function TSqlDatabase.Query(Data: string): TDbRows;
     204procedure TSqlDatabase.Query(DbRows: TDbRows; Data: string);
    203205var
    204206  I, II: Integer;
     
    206208  DbRow: MYSQL_ROW;
    207209begin
     210  DbRows.Clear;
    208211  //DebugLog('SqlDatabase query: '+Data);
    209212  RepeatLastAction := False;
    210213  LastQuery := Data;
    211   Result := TDbRows.Create;
    212214  mysql_query(FSession, PChar(Data));
    213215  if LastErrorNumber <> 0 then begin
     
    217219  DbResult := mysql_store_result(FSession);
    218220  if Assigned(DbResult) then begin
    219     Result.Count := mysql_num_rows(DbResult);
    220     for I := 0 to Result.Count - 1 do begin
     221    DbRows.Count := mysql_num_rows(DbResult);
     222    for I := 0 to DbRows.Count - 1 do begin
    221223      DbRow := mysql_fetch_row(DbResult);
    222       Result[I] := TDictionaryStringString.Create;
    223       with Result[I] do begin
     224      DbRows[I] := TDictionaryStringString.Create;
     225      with DbRows[I] do begin
    224226        for II := 0 to mysql_num_fields(DbResult) - 1 do begin
    225227          Add(mysql_fetch_field_direct(DbResult, II)^.Name,
     
    253255  System.Delete(DbValues, 1, 1);
    254256  try
    255     DbResult := Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
     257    DbResult := TDbRows.Create;
     258    Query(DbResult, 'REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
    256259  finally
    257260    DbResult.Free;
     
    259262end;
    260263
    261 function TSqlDatabase.Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows;
     264procedure TSqlDatabase.Select(DbRows: TDbRows; ATable: string; Filter: string = '*'; Condition: string = '1');
    262265begin
    263266  Table := ATable;
    264   Result := Query('SELECT ' + Filter + ' FROM `' + Table + '` WHERE ' + Condition);
     267  Query(DbRows, 'SELECT ' + Filter + ' FROM `' + Table + '` WHERE ' + Condition);
    265268end;
    266269
     
    282285  System.Delete(DbValues, 1, 1);
    283286  try
    284     DbResult := Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition);
     287    DbResult := TDbRows.Create;
     288    Query(DbResult, 'UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition);
    285289  finally
    286290    DbResult.Free;
     
    299303  Table := ATable;
    300304  try
    301     DbResult := Query('DELETE FROM `' + Table + '` WHERE ' + Condition);
     305    DbResult := TDbRows.Create;
     306    Query(DbResult, 'DELETE FROM `' + Table + '` WHERE ' + Condition);
    302307  finally
    303308    DbResult.Free;
     
    341346var
    342347  TempDatabase: string;
     348  DbRows: TDbRows;
    343349begin
    344350  TempDatabase := Database;
    345351  Database := 'mysql';
    346352  Connect;
    347   Query('CREATE DATABASE ' + TempDatabase);
     353  try
     354    DbRows := TDbRows.Create;
     355    Query(DbRows, 'CREATE DATABASE ' + TempDatabase);
     356  finally
     357    DbRows.Free;
     358  end;
    348359  Disconnect;
    349360  Database := TempDatabase;
     
    351362
    352363procedure TSqlDatabase.CreateTable(Name: string);
    353 begin
    354   Query('CREATE TABLE `' + Name + '`' +
    355   ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));');
     364var
     365  DbRows: TDbRows;
     366begin
     367  try
     368    DbRows := TDbRows.Create;
     369    Query(DbRows, 'CREATE TABLE `' + Name + '`' +
     370    ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));');
     371  finally
     372    DbRows.Free;
     373  end;
    356374end;
    357375
     
    361379  ColTypes: array[0..17] of string = ('', 'INT', 'CHAR', 'INT', 'DOUBLE',
    362380  'VARCHAR(255)', 'SET', 'INT', '', '', 'TEXT', 'TEXT', '', '', '', '', '', '');
    363 begin
    364   Query('ALTER TABLE `' + Table + '` ADD `' + ColumnName + '` ' +
    365     ColTypes[Integer(ColumnType)] + ' NOT NULL');
     381var
     382  DbRows: TDbRows;
     383begin
     384  try
     385    DbRows := TDbRows.Create;
     386    Query(DbRows, 'ALTER TABLE `' + Table + '` ADD `' + ColumnName + '` ' +
     387      ColTypes[Integer(ColumnType)] + ' NOT NULL');
     388  finally
     389    DbRows.Free;
     390  end;
    366391end;
    367392
Note: See TracChangeset for help on using the changeset viewer.