Changeset 35


Ignore:
Timestamp:
Jul 1, 2010, 2:11:01 PM (14 years ago)
Author:
george
Message:
  • Přidáno: Třídy pro běh web serveru, asociativní pole, dávkový přenos paketů.
Files:
14 added
1 edited

Legend:

Unmodified
Added
Removed
  • Network/MysqlClient/USqlDatabase.pas

    r33 r35  
    11unit USqlDatabase;
    22
    3 // Upraveno: 24.2.2008
     3{$mode Delphi}{$H+}
     4// Upraveno: 16.12.2009
    45
    56interface
    67
    78uses
    8   SysUtils, Classes, Dialogs, mysql, TypInfo;
     9  SysUtils, Classes, Dialogs, mysql50, TypInfo;
    910
    1011type
     12  EQueryError = Exception;
     13
    1114  TClientCapabilities = (_CLIENT_LONG_PASSWORD, _CLIENT_FOUND_ROWS,
    1215    _CLIENT_LONG_FLAG, _CLIENT_CONNECT_WITH_DB, _CLIENT_NO_SCHEMA,
     
    4447    FSession: PMYSQL;
    4548    FConnected: Boolean;
    46     FOnError: TNotifyEvent;
    4749    FDatabase: string;
    4850    function GetConnected: Boolean;
     
    5759    UserName: string;
    5860    Password: string;
     61    Encoding: string;
    5962    Table: string;
    6063    RepeatLastAction: Boolean;
     
    7578    property LastErrorNumber: Integer read GetLastErrorNumber;
    7679    property Connected: Boolean read GetConnected;
    77     property OnError: TNotifyEvent read FOnError write FOnError;
    7880    constructor Create;
    7981    destructor Destroy; override;
     
    111113begin
    112114  S := FloatToStr(F);
    113   if Pos(',',S) > 0 then S[Pos(',',S)] := '.';
     115  if Pos(',', S) > 0 then S[Pos(',',S)] := '.';
    114116  Result := S;
    115117end;
     
    117119function MySQLStrToFloat(S: string): Real;
    118120begin
    119   if Pos('.',S) > 0 then S[Pos('.',S)] := ',';
     121  if Pos('.', S) > 0 then S[Pos('.',S)] := ',';
    120122  Result := StrToFloat(S);
    121123end;
     
    137139  end else FConnected := False;
    138140  CheckError;
    139   Rows := Query('SET NAMES cp1250');
     141  Rows := Query('SET NAMES ' + Encoding);
    140142  Rows.Free;
    141143end;
     
    152154  DbNames := '';
    153155  DbValues := '';
    154   for I := 0 to Data.Count-1 do begin
     156  for I := 0 to Data.Count - 1 do begin
    155157    Value := Data.ValuesAtIndex[I];
    156158    StringReplace(Value, '"', '\"', [rfReplaceAll]);
     
    169171  I, II: Integer;
    170172  DbResult: PMYSQL_RES;
    171   DbRow: PMYSQL_ROW;
     173  DbRow: MYSQL_ROW;
    172174begin
    173175  //DebugLog('SqlDatabase query: '+Data);
     
    185187    if Assigned(DbResult) then begin
    186188      Result.Count := mysql_num_rows(DbResult);
    187       for I := 0 to Result.Count-1 do begin
     189      for I := 0 to Result.Count - 1 do begin
    188190        DbRow := mysql_fetch_row(DbResult);
    189191        Result[I] := TAssociativeArray.Create;
    190192        with Result[I] do begin
    191           for II := 0 to mysql_num_fields(DbResult)-1 do begin
    192             Add(mysql_fetch_field_direct(DbResult, II)^.name + NameValueSeparator + DbRow^[II]);
     193          for II := 0 to mysql_num_fields(DbResult) - 1 do begin
     194            Add(mysql_fetch_field_direct(DbResult, II)^.Name +
     195              NameValueSeparator + PChar((DbRow + II)^));
    193196          end;
    194197        end;
     
    217220  DbNames := '';
    218221  DbValues := '';
    219   for I := 0 to Data.Count-1 do begin
     222  for I := 0 to Data.Count - 1 do begin
    220223    Value := Data.ValuesAtIndex[I];
    221224    StringReplace(Value, '"', '\"', [rfReplaceAll]);
     
    245248  Table := ATable;
    246249  DbValues := '';
    247   for I := 0 to Data.Count-1 do begin
     250  for I := 0 to Data.Count - 1 do begin
    248251    Value := Data.ValuesAtIndex[I];
    249252    StringReplace(Value, '"', '\"', [rfReplaceAll]);
     
    283286begin
    284287  Result := '';
    285   for I := 0 to Count-1 do begin
     288  for I := 0 to Count - 1 do begin
    286289    Result := Result + Names[I] + '=' + ValuesAtIndex[I] + ',';
    287290  end;
     
    319322  inherited;
    320323  FSession := nil;
     324  Encoding := 'utf8';
    321325end;
    322326
     
    332336  I: Integer;
    333337begin
    334   for I := 0 to Count - 1 do Data[I].Free;
     338  for I := 0 to Count - 1 do
     339    Data[I].Free;
    335340  inherited;
    336341end;
     
    364369begin
    365370  Result := LastErrorNumber <> 0;
    366   if Result and Assigned(OnError) then OnError(Self);
     371  if Result then
     372    raise EQueryError.Create('Database query error: "' + LastErrorMessage + '"');
    367373end;
    368374
     
    380386
    381387procedure TSqlDatabase.CreateTable(Name: string);
    382 begin
    383   Query('CREATE TABLE `' + Name + '`' +
    384   ' (`id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`));');
     388var
     389  DbRows: TDbRows;
     390begin
     391  DbRows := Query('CREATE TABLE `' + Name + '`' +
     392  ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));');
     393  DbRows.Destroy;
    385394end;
    386395
Note: See TracChangeset for help on using the changeset viewer.