Ignore:
Timestamp:
Dec 31, 2010, 10:03:27 PM (14 years ago)
Author:
george
Message:
  • Modified: TStringListEx replaced by TListString in TSqlDatabase unit.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Network/MysqlClient/USqlDatabase.pas

    r81 r106  
    33{$mode Delphi}{$H+}
    44
    5 // Upraveno: 28.10.2010
     5// Modified: 2010-12-24
    66
    77interface
    88
    99uses
    10   SysUtils, Classes, Dialogs, mysql50, TypInfo, UStringListEx,
    11   ListObject, DictionaryStringString;
     10  SysUtils, Classes, Dialogs, mysql50, TypInfo,
     11  SpecializedDictionary, SpecializedList;
    1212
    1313type
     
    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);
     
    113113end;
    114114
     115function StrToStr(Value: string): string;
     116begin
     117  Result := Value;
     118end;
     119
    115120function SQLToDateTime(Value: string): TDateTime;
    116121var
    117   Parts: TStringListEx;
    118   DateParts: TStringListEx;
    119   TimeParts: TStringListEx;
    120 begin
    121   try
    122     Parts := TStringListEx.Create;
    123     DateParts := TStringListEx.Create;
    124     TimeParts := TStringListEx.Create;
    125 
    126     Parts.Explode(' ', Value);
    127     DateParts.Explode('-', Parts[0]);
     122  Parts: TListString;
     123  DateParts: TListString;
     124  TimeParts: TListString;
     125begin
     126  try
     127    Parts := TListString.Create;
     128    DateParts := TListString.Create;
     129    TimeParts := TListString.Create;
     130
     131    Parts.Explode(Value, ' ', StrToStr);
     132    DateParts.Explode(Parts[0], '-', StrToStr);
    128133    Result := EncodeDate(StrToInt(DateParts[0]), StrToInt(DateParts[1]),
    129134      StrToInt(DateParts[2]));
    130135    if Parts.Count > 1 then begin
    131       TimeParts.Explode(':', Parts[1]);
     136      TimeParts.Explode(Parts[1], ':', StrToStr);
    132137      Result := Result + EncodeTime(StrToInt(TimeParts[0]), StrToInt(TimeParts[1]),
    133138        StrToInt(TimeParts[2]), 0);
     
    167172
    168173  try
    169     Rows := Query('SET NAMES ' + Encoding);
     174    Rows := TDbRows.Create;
     175    Query(Rows, 'SET NAMES ' + Encoding);
    170176  finally
    171177    Rows.Free;
     
    194200  System.Delete(DbValues, 1, 1);
    195201  try
    196     DbResult := Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
     202    DbResult := TDbRows.Create;
     203    Query(DbResult, 'INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
    197204  finally
    198205    DbResult.Free;
     
    200207end;
    201208
    202 function TSqlDatabase.Query(Data: string): TDbRows;
     209procedure TSqlDatabase.Query(DbRows: TDbRows; Data: string);
    203210var
    204211  I, II: Integer;
     
    206213  DbRow: MYSQL_ROW;
    207214begin
     215  DbRows.Clear;
    208216  //DebugLog('SqlDatabase query: '+Data);
    209217  RepeatLastAction := False;
    210218  LastQuery := Data;
    211   Result := TDbRows.Create;
    212219  mysql_query(FSession, PChar(Data));
    213220  if LastErrorNumber <> 0 then begin
     
    217224  DbResult := mysql_store_result(FSession);
    218225  if Assigned(DbResult) then begin
    219     Result.Count := mysql_num_rows(DbResult);
    220     for I := 0 to Result.Count - 1 do begin
     226    DbRows.Count := mysql_num_rows(DbResult);
     227    for I := 0 to DbRows.Count - 1 do begin
    221228      DbRow := mysql_fetch_row(DbResult);
    222       Result[I] := TDictionaryStringString.Create;
    223       with Result[I] do begin
     229      DbRows[I] := TDictionaryStringString.Create;
     230      with DbRows[I] do begin
    224231        for II := 0 to mysql_num_fields(DbResult) - 1 do begin
    225232          Add(mysql_fetch_field_direct(DbResult, II)^.Name,
     
    253260  System.Delete(DbValues, 1, 1);
    254261  try
    255     DbResult := Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
     262    DbResult := TDbRows.Create;
     263    Query(DbResult, 'REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
    256264  finally
    257265    DbResult.Free;
     
    259267end;
    260268
    261 function TSqlDatabase.Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows;
     269procedure TSqlDatabase.Select(DbRows: TDbRows; ATable: string; Filter: string = '*'; Condition: string = '1');
    262270begin
    263271  Table := ATable;
    264   Result := Query('SELECT ' + Filter + ' FROM `' + Table + '` WHERE ' + Condition);
     272  Query(DbRows, 'SELECT ' + Filter + ' FROM `' + Table + '` WHERE ' + Condition);
    265273end;
    266274
     
    282290  System.Delete(DbValues, 1, 1);
    283291  try
    284     DbResult := Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition);
     292    DbResult := TDbRows.Create;
     293    Query(DbResult, 'UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition);
    285294  finally
    286295    DbResult.Free;
     
    299308  Table := ATable;
    300309  try
    301     DbResult := Query('DELETE FROM `' + Table + '` WHERE ' + Condition);
     310    DbResult := TDbRows.Create;
     311    Query(DbResult, 'DELETE FROM `' + Table + '` WHERE ' + Condition);
    302312  finally
    303313    DbResult.Free;
     
    341351var
    342352  TempDatabase: string;
     353  DbRows: TDbRows;
    343354begin
    344355  TempDatabase := Database;
    345356  Database := 'mysql';
    346357  Connect;
    347   Query('CREATE DATABASE ' + TempDatabase);
     358  try
     359    DbRows := TDbRows.Create;
     360    Query(DbRows, 'CREATE DATABASE ' + TempDatabase);
     361  finally
     362    DbRows.Free;
     363  end;
    348364  Disconnect;
    349365  Database := TempDatabase;
     
    351367
    352368procedure TSqlDatabase.CreateTable(Name: string);
    353 begin
    354   Query('CREATE TABLE `' + Name + '`' +
    355   ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));');
     369var
     370  DbRows: TDbRows;
     371begin
     372  try
     373    DbRows := TDbRows.Create;
     374    Query(DbRows, 'CREATE TABLE `' + Name + '`' +
     375    ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));');
     376  finally
     377    DbRows.Free;
     378  end;
    356379end;
    357380
     
    361384  ColTypes: array[0..17] of string = ('', 'INT', 'CHAR', 'INT', 'DOUBLE',
    362385  'VARCHAR(255)', 'SET', 'INT', '', '', 'TEXT', 'TEXT', '', '', '', '', '', '');
    363 begin
    364   Query('ALTER TABLE `' + Table + '` ADD `' + ColumnName + '` ' +
    365     ColTypes[Integer(ColumnType)] + ' NOT NULL');
     386var
     387  DbRows: TDbRows;
     388begin
     389  try
     390    DbRows := TDbRows.Create;
     391    Query(DbRows, 'ALTER TABLE `' + Table + '` ADD `' + ColumnName + '` ' +
     392      ColTypes[Integer(ColumnType)] + ' NOT NULL');
     393  finally
     394    DbRows.Free;
     395  end;
    366396end;
    367397
Note: See TracChangeset for help on using the changeset viewer.