Ignore:
Timestamp:
Mar 5, 2010, 8:52:50 PM (14 years ago)
Author:
george
Message:
  • Upraveno: Aktualizována třída TSqlDatabase.
  • Upraveno: Binární sestavené soubory se nyní vytváří v podsložce bin.
Location:
branches/web
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/web

    • Property svn:ignore
      •  

        old new  
        33*.o
        44backup
        5 
        65index.cgi
        7 
        86index.compiled
        9 
        107UConfig.pas
         8bin
  • branches/web/USqlDatabase.pas

    r142 r279  
    11unit USqlDatabase;
    22
    3 {$mode delphi}{$H+}
     3{$mode Delphi}{$H+}
     4// Upraveno: 16.12.2009
    45
    56interface
    67
    78uses
    8   SysUtils, Classes, mysql50, 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,
     
    1619  TSetClientCapabilities = set of TClientCapabilities;
    1720
    18   TAssocArray = class(TStringList)
     21  TAssociativeArray = class(TStringList)
    1922  private
    2023    function GetValues(Index: string): string;
     
    3235  TDbRows = class(TList)
    3336  private
    34     function GetData(Index: Integer): TAssocArray;
    35     procedure SetData(Index: Integer; const Value: TAssocArray);
     37    function GetData(Index: Integer): TAssociativeArray;
     38    procedure SetData(Index: Integer; const Value: TAssociativeArray);
    3639  public
    37     property Data[Index: Integer]: TAssocArray read GetData write SetData; default;
     40    property Data[Index: Integer]: TAssociativeArray read GetData write SetData; default;
    3841    destructor Destroy; override;
    3942  end;
     
    4447    FSession: PMYSQL;
    4548    FConnected: Boolean;
    46     FOnError: TNotifyEvent;
    4749    FDatabase: string;
    4850    function GetConnected: Boolean;
     
    5254    function GetCharset: string;
    5355    procedure SetDatabase(const Value: string);
    54     { Private declarations }
    5556  public
    5657    Hostname: string;
    5758    UserName: string;
    5859    Password: string;
     60    Encoding: string;
    5961    Table: string;
    6062    RepeatLastAction: Boolean;
     
    6264    procedure CreateDatabase;
    6365    procedure CreateTable(Name: string);
    64     procedure CreateColumn(ATable, ColumnName: string; ColumnType: TTypeKind);
     66    procedure CreateColumn(Table, ColumnName: string; ColumnType: TTypeKind);
    6567    function Query(Data: string): TDbRows;
    6668    function Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows;
    6769    procedure Delete(ATable: string; Condition: string = '1');
    68     procedure Insert(ATable: string; Data: TAssocArray);
    69     procedure Update(ATable: string; Data: TAssocArray; Condition: string = '1');
    70     procedure Replace(ATable: string; Data: TAssocArray);
     70    procedure Insert(ATable: string; Data: TAssociativeArray);
     71    procedure Update(ATable: string; Data: TAssociativeArray; Condition: string = '1');
     72    procedure Replace(ATable: string; Data: TAssociativeArray);
    7173    procedure Connect;
    7274    procedure Disconnect;
     
    7577    property LastErrorNumber: Integer read GetLastErrorNumber;
    7678    property Connected: Boolean read GetConnected;
    77     property OnError: TNotifyEvent read FOnError write FOnError;
    7879    constructor Create;
    7980    destructor Destroy; override;
     
    8485  function MySQLFloatToStr(F: Real): string;
    8586  function MySQLStrToFloat(S: string): Real;
    86   function MySQLDateToDateTime(Date: string): TDateTime;
    8787
    8888implementation
     
    105105  CLIENT_TRANSACTIONS = 8192;    // Client knows about transactions
    106106
    107 { TDataModule2 }
    108 
    109 function MySQLDateToDateTime(Date: string): TDateTime;
    110 begin
    111   Result := 0;
    112 end;
    113 
    114107function MySQLFloatToStr(F: Real): string;
    115108var
     
    117110begin
    118111  S := FloatToStr(F);
    119   if Pos(',',S) > 0 then S[Pos(',',S)] := '.';
     112  if Pos(',', S) > 0 then S[Pos(',',S)] := '.';
    120113  Result := S;
    121114end;
     
    123116function MySQLStrToFloat(S: string): Real;
    124117begin
    125   if Pos('.',S) > 0 then  S[Pos('.',S)] := ',';
     118  if Pos('.', S) > 0 then  S[Pos('.',S)] := ',';
    126119  Result := StrToFloat(S);
    127120end;
     121
     122{ TSqlDatabase }
    128123
    129124procedure TSqlDatabase.Connect;
     
    143138  end else FConnected := False;
    144139  CheckError;
    145   Rows := Query('SET NAMES cp1250');
     140  Rows := Query('SET NAMES ' + Encoding);
    146141  Rows.Free;
    147142end;
    148143
    149 procedure TSqlDatabase.Insert(ATable: string; Data: TAssocArray);
     144procedure TSqlDatabase.Insert(ATable: string; Data: TAssociativeArray);
    150145var
    151146  DbNames: string;
     
    153148  I: Integer;
    154149  Value: string;
    155   DbRows: TDbRows;
     150  DbResult: TDbRows;
    156151begin
    157152  Table := ATable;
    158153  DbNames := '';
    159154  DbValues := '';
    160   for I := 0 to Data.Count-1 do begin
     155  for I := 0 to Data.Count - 1 do begin
    161156    Value := Data.ValuesAtIndex[I];
    162157    StringReplace(Value, '"', '\"', [rfReplaceAll]);
     
    167162  System.Delete(DbNames, 1, 1);
    168163  System.Delete(DbValues, 1, 1);
    169   DbRows := Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
    170   DbRows.Free;
     164  DbResult := Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
     165  DbResult.Free;
    171166end;
    172167
     
    176171  DbResult: PMYSQL_RES;
    177172  DbRow: MYSQL_ROW;
    178 type
    179   PMYSQL_ROW2 = ^TMYSQL_ROW2;  // return data as array of strings
    180   TMYSQL_ROW2 = array[0..MaxInt div SizeOf(pChar) - 1] of pChar;
    181173begin
    182174  //DebugLog('SqlDatabase query: '+Data);
     
    194186    if Assigned(DbResult) then begin
    195187      Result.Count := mysql_num_rows(DbResult);
    196       for I := 0 to Result.Count-1 do begin
     188      for I := 0 to Result.Count - 1 do begin
    197189        DbRow := mysql_fetch_row(DbResult);
    198         Result[I] := TAssocArray.Create;
     190        Result[I] := TAssociativeArray.Create;
    199191        with Result[I] do begin
    200           for II := 0 to mysql_num_fields(DbResult)-1 do begin
    201             Add(mysql_fetch_field_direct(DbResult, II)^.name + NameValueSeparator + PMYSQL_ROW2(DbRow)^[II]);
     192          for II := 0 to mysql_num_fields(DbResult) - 1 do begin
     193            Add(mysql_fetch_field_direct(DbResult, II)^.Name +
     194              NameValueSeparator + PChar((DbRow + II)^));
    202195          end;
    203196        end;
     
    215208end;
    216209
    217 procedure TSqlDatabase.Replace(ATable: string; Data: TAssocArray);
     210procedure TSqlDatabase.Replace(ATable: string; Data: TAssociativeArray);
    218211var
    219212  DbNames: string;
     
    221214  Value: string;
    222215  I: Integer;
    223   DbRows: TDbRows;
     216  DbResult: TDbRows;
    224217begin
    225218  Table := ATable;
    226219  DbNames := '';
    227220  DbValues := '';
    228   for I := 0 to Data.Count-1 do begin
     221  for I := 0 to Data.Count - 1 do begin
    229222    Value := Data.ValuesAtIndex[I];
    230223    StringReplace(Value, '"', '\"', [rfReplaceAll]);
     
    235228  System.Delete(DbNames, 1, 1);
    236229  System.Delete(DbValues, 1, 1);
    237   DbRows := Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
    238   DbRows.Free;
     230  DbResult := Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
     231  DbResult.Free;
    239232end;
    240233
     
    245238end;
    246239
    247 procedure TSqlDatabase.Update(ATable: string; Data: TAssocArray; Condition: string = '1');
     240procedure TSqlDatabase.Update(ATable: string; Data: TAssociativeArray; Condition: string = '1');
    248241var
    249242  DbValues: string;
    250243  Value: string;
    251244  I: Integer;
    252   DbRows: TDbRows;
     245  DbResult: TDbRows;
    253246begin
    254247  Table := ATable;
    255248  DbValues := '';
    256   for I := 0 to Data.Count-1 do begin
     249  for I := 0 to Data.Count - 1 do begin
    257250    Value := Data.ValuesAtIndex[I];
    258251    StringReplace(Value, '"', '\"', [rfReplaceAll]);
     
    261254  end;
    262255  System.Delete(DbValues, 1, 1);
    263   DbRows := Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition);
    264   DBRows.Free;
     256  DbResult := Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition);
     257  DbResult.Free;
    265258end;
    266259
     
    270263end;
    271264
    272 { TAssocArray }
    273 
    274 procedure TAssocArray.AddKeyValue(Key, Value: string);
    275 begin
    276   Add(Key + NameValueSeparator + Value);
    277 end;
    278 
    279 constructor TAssocArray.Create;
    280 begin
    281   NameValueSeparator := '|';
    282 end;
    283 
    284 destructor TAssocArray.Destroy;
    285 begin
    286   inherited;
    287 end;
    288 
    289 function TAssocArray.GetAllValues: string;
    290 var
    291   I: Integer;
    292 begin
    293   Result := '';
    294   for I := 0 to Count-1 do begin
    295     Result := Result + Names[I] + '=' + ValuesAtIndex[I] + ',';
    296   end;
    297 end;
    298 
    299 function TAssocArray.GetValues(Index: string): string;
    300 begin
    301   Result := inherited Values[Index];
    302 end;
    303 
    304 function TAssocArray.GetValuesAtIndex(Index: Integer): string;
    305 begin
    306   Result := inherited Values[Names[Index]];
    307 end;
    308 
    309265procedure TSqlDatabase.Delete(ATable: string; Condition: string = '1');
    310266var
    311   DbRows: TDbRows;
     267  DbResult: TDbRows;
    312268begin
    313269  Table := ATable;
    314   DbRows := Query('DELETE FROM `' + Table + '` WHERE ' + Condition);
    315   DbRows.Free;
     270  DbResult := Query('DELETE FROM `' + Table + '` WHERE ' + Condition);
     271  DbResult.Free;
    316272end;
    317273
     
    331287  inherited;
    332288  FSession := nil;
    333 end;
    334 
    335 procedure TAssocArray.SetValues(Index: string; const Value: string);
    336 begin
    337   inherited Values[Index] := Value;
    338 end;
    339 
    340 { TDbRows }
    341 
    342 destructor TDbRows.Destroy;
    343 var
    344   I: Integer;
    345 begin
    346   for I := 0 to Count - 1 do Data[I].Free;
    347   inherited;
    348 end;
    349 
    350 function TDbRows.GetData(Index: Integer): TAssocArray;
    351 begin
    352   Result := Items[Index];
    353 end;
    354 
    355 procedure TDbRows.SetData(Index: Integer; const Value: TAssocArray);
    356 begin
    357   Items[Index] := Value;
     289  Encoding := 'utf8';
    358290end;
    359291
     
    376308begin
    377309  Result := LastErrorNumber <> 0;
    378   if Result and Assigned(OnError) then OnError(Self);
     310  if Result then
     311    raise EQueryError.Create('Database query error: "' + LastErrorMessage + '"');
    379312end;
    380313
     
    394327begin
    395328  Query('CREATE TABLE `' + Name + '`' +
    396   ' (`id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`));');
    397 end;
    398 
    399 procedure TSqlDatabase.CreateColumn(ATable, ColumnName: string;
     329  ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));');
     330end;
     331
     332procedure TSqlDatabase.CreateColumn(Table, ColumnName: string;
    400333  ColumnType: TTypeKind);
    401334const
     
    423356end;
    424357
     358{ TAssociativeArray }
     359
     360procedure TAssociativeArray.AddKeyValue(Key, Value: string);
     361begin
     362  Add(Key + NameValueSeparator + Value);
     363end;
     364
     365constructor TAssociativeArray.Create;
     366begin
     367  NameValueSeparator := '|';
     368end;
     369
     370destructor TAssociativeArray.Destroy;
     371begin
     372  inherited;
     373end;
     374
     375function TAssociativeArray.GetAllValues: string;
     376var
     377  I: Integer;
     378begin
     379  Result := '';
     380  for I := 0 to Count - 1 do begin
     381    Result := Result + Names[I] + '=' + ValuesAtIndex[I] + ',';
     382  end;
     383end;
     384
     385function TAssociativeArray.GetValues(Index: string): string;
     386begin
     387  Result := inherited Values[Index];
     388end;
     389
     390function TAssociativeArray.GetValuesAtIndex(Index: Integer): string;
     391begin
     392  Result := inherited Values[Names[Index]];
     393end;
     394
     395procedure TAssociativeArray.SetValues(Index: string; const Value: string);
     396begin
     397  inherited Values[Index] := Value;
     398end;
     399
     400{ TDbRows }
     401
     402destructor TDbRows.Destroy;
     403var
     404  I: Integer;
     405begin
     406  for I := 0 to Count - 1 do
     407    Data[I].Free;
     408  inherited;
     409end;
     410
     411function TDbRows.GetData(Index: Integer): TAssociativeArray;
     412begin
     413  Result := Items[Index];
     414end;
     415
     416procedure TDbRows.SetData(Index: Integer; const Value: TAssociativeArray);
     417begin
     418  Items[Index] := Value;
     419end;
     420
    425421end.
    426422
Note: See TracChangeset for help on using the changeset viewer.