Ignore:
Timestamp:
Mar 5, 2010, 8:46:18 AM (14 years ago)
Author:
george
Message:
  • Upraveno: V třída TSqlDatabase doplněn výběr kódování spojení, přidáno generování vyjímky při chybném dotazu.
  • Opraveno: Objekty vracející metodou Query je nutno uvolňovat.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/dbc_export/USqlDatabase.pas

    r335 r359  
    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;
     
    5759    UserName: string;
    5860    Password: string;
     61    Encoding: string;
    5962    Table: string;
    6063    RepeatLastAction: Boolean;
     
    6265    procedure CreateDatabase;
    6366    procedure CreateTable(Name: string);
    64     procedure CreateColumn(ATable, ColumnName: string; ColumnType: TTypeKind);
     67    procedure CreateColumn(Table, ColumnName: string; ColumnType: TTypeKind);
    6568    function Query(Data: string): TDbRows;
    6669    function Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows;
    6770    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);
     71    procedure Insert(ATable: string; Data: TAssociativeArray);
     72    procedure Update(ATable: string; Data: TAssociativeArray; Condition: string = '1');
     73    procedure Replace(ATable: string; Data: TAssociativeArray);
    7174    procedure Connect;
    7275    procedure Disconnect;
     
    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;
     
    8486  function MySQLFloatToStr(F: Real): string;
    8587  function MySQLStrToFloat(S: string): Real;
    86   function MySQLDateToDateTime(Date: string): TDateTime;
    8788
    8889implementation
     
    107108{ TDataModule2 }
    108109
    109 function MySQLDateToDateTime(Date: string): TDateTime;
    110 begin
    111   Result := 0;
    112 end;
    113 
    114110function MySQLFloatToStr(F: Real): string;
    115111var
     
    117113begin
    118114  S := FloatToStr(F);
    119   if Pos(',',S) > 0 then S[Pos(',',S)] := '.';
     115  if Pos(',', S) > 0 then S[Pos(',',S)] := '.';
    120116  Result := S;
    121117end;
     
    123119function MySQLStrToFloat(S: string): Real;
    124120begin
    125   if Pos('.',S) > 0 then  S[Pos('.',S)] := ',';
     121  if Pos('.', S) > 0 then  S[Pos('.',S)] := ',';
    126122  Result := StrToFloat(S);
    127123end;
     
    143139  end else FConnected := False;
    144140  CheckError;
    145   Rows := Query('SET NAMES cp1250');
     141  Rows := Query('SET NAMES ' + Encoding);
    146142  Rows.Free;
    147143end;
    148144
    149 procedure TSqlDatabase.Insert(ATable: string; Data: TAssocArray);
     145procedure TSqlDatabase.Insert(ATable: string; Data: TAssociativeArray);
    150146var
    151147  DbNames: string;
     
    153149  I: Integer;
    154150  Value: string;
    155   DbRows: TDbRows;
     151  DbResult: TDbRows;
    156152begin
    157153  Table := ATable;
    158154  DbNames := '';
    159155  DbValues := '';
    160   for I := 0 to Data.Count-1 do begin
     156  for I := 0 to Data.Count - 1 do begin
    161157    Value := Data.ValuesAtIndex[I];
    162158    StringReplace(Value, '"', '\"', [rfReplaceAll]);
     
    167163  System.Delete(DbNames, 1, 1);
    168164  System.Delete(DbValues, 1, 1);
    169   DbRows := Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
    170   DbRows.Free;
     165  DbResult := Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
     166  DbResult.Free;
    171167end;
    172168
     
    176172  DbResult: PMYSQL_RES;
    177173  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;
    181174begin
    182175  //DebugLog('SqlDatabase query: '+Data);
     
    194187    if Assigned(DbResult) then begin
    195188      Result.Count := mysql_num_rows(DbResult);
    196       for I := 0 to Result.Count-1 do begin
     189      for I := 0 to Result.Count - 1 do begin
    197190        DbRow := mysql_fetch_row(DbResult);
    198         Result[I] := TAssocArray.Create;
     191        Result[I] := TAssociativeArray.Create;
    199192        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]);
     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)^));
    202196          end;
    203197        end;
     
    215209end;
    216210
    217 procedure TSqlDatabase.Replace(ATable: string; Data: TAssocArray);
     211procedure TSqlDatabase.Replace(ATable: string; Data: TAssociativeArray);
    218212var
    219213  DbNames: string;
     
    221215  Value: string;
    222216  I: Integer;
    223   DbRows: TDbRows;
     217  DbResult: TDbRows;
    224218begin
    225219  Table := ATable;
    226220  DbNames := '';
    227221  DbValues := '';
    228   for I := 0 to Data.Count-1 do begin
     222  for I := 0 to Data.Count - 1 do begin
    229223    Value := Data.ValuesAtIndex[I];
    230224    StringReplace(Value, '"', '\"', [rfReplaceAll]);
     
    235229  System.Delete(DbNames, 1, 1);
    236230  System.Delete(DbValues, 1, 1);
    237   DbRows := Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
    238   DbRows.Free;
     231  DbResult := Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
     232  DbResult.Free;
    239233end;
    240234
     
    245239end;
    246240
    247 procedure TSqlDatabase.Update(ATable: string; Data: TAssocArray; Condition: string = '1');
     241procedure TSqlDatabase.Update(ATable: string; Data: TAssociativeArray; Condition: string = '1');
    248242var
    249243  DbValues: string;
    250244  Value: string;
    251245  I: Integer;
    252   DbRows: TDbRows;
     246  DbResult: TDbRows;
    253247begin
    254248  Table := ATable;
    255249  DbValues := '';
    256   for I := 0 to Data.Count-1 do begin
     250  for I := 0 to Data.Count - 1 do begin
    257251    Value := Data.ValuesAtIndex[I];
    258252    StringReplace(Value, '"', '\"', [rfReplaceAll]);
     
    261255  end;
    262256  System.Delete(DbValues, 1, 1);
    263   DbRows := Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition);
    264   DBRows.Free;
     257  DbResult := Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition);
     258  DbResult.Free;
    265259end;
    266260
     
    272266{ TAssocArray }
    273267
    274 procedure TAssocArray.AddKeyValue(Key, Value: string);
     268procedure TAssociativeArray.AddKeyValue(Key, Value: string);
    275269begin
    276270  Add(Key + NameValueSeparator + Value);
    277271end;
    278272
    279 constructor TAssocArray.Create;
     273constructor TAssociativeArray.Create;
    280274begin
    281275  NameValueSeparator := '|';
    282276end;
    283277
    284 destructor TAssocArray.Destroy;
     278destructor TAssociativeArray.Destroy;
    285279begin
    286280  inherited;
    287281end;
    288282
    289 function TAssocArray.GetAllValues: string;
     283function TAssociativeArray.GetAllValues: string;
    290284var
    291285  I: Integer;
    292286begin
    293287  Result := '';
    294   for I := 0 to Count-1 do begin
     288  for I := 0 to Count - 1 do begin
    295289    Result := Result + Names[I] + '=' + ValuesAtIndex[I] + ',';
    296290  end;
    297291end;
    298292
    299 function TAssocArray.GetValues(Index: string): string;
     293function TAssociativeArray.GetValues(Index: string): string;
    300294begin
    301295  Result := inherited Values[Index];
    302296end;
    303297
    304 function TAssocArray.GetValuesAtIndex(Index: Integer): string;
     298function TAssociativeArray.GetValuesAtIndex(Index: Integer): string;
    305299begin
    306300  Result := inherited Values[Names[Index]];
     
    309303procedure TSqlDatabase.Delete(ATable: string; Condition: string = '1');
    310304var
    311   DbRows: TDbRows;
     305  DbResult: TDbRows;
    312306begin
    313307  Table := ATable;
    314   DbRows := Query('DELETE FROM `' + Table + '` WHERE ' + Condition);
    315   DbRows.Free;
     308  DbResult = Query('DELETE FROM `' + Table + '` WHERE ' + Condition);
     309  DbResult.Free;
    316310end;
    317311
     
    331325  inherited;
    332326  FSession := nil;
    333 end;
    334 
    335 procedure TAssocArray.SetValues(Index: string; const Value: string);
     327  Encoding := 'utf8';
     328end;
     329
     330procedure TAssociativeArray.SetValues(Index: string; const Value: string);
    336331begin
    337332  inherited Values[Index] := Value;
     
    344339  I: Integer;
    345340begin
    346   for I := 0 to Count - 1 do Data[I].Free;
     341  for I := 0 to Count - 1 do
     342    Data[I].Free;
    347343  inherited;
    348344end;
    349345
    350 function TDbRows.GetData(Index: Integer): TAssocArray;
     346function TDbRows.GetData(Index: Integer): TAssociativeArray;
    351347begin
    352348  Result := Items[Index];
    353349end;
    354350
    355 procedure TDbRows.SetData(Index: Integer; const Value: TAssocArray);
     351procedure TDbRows.SetData(Index: Integer; const Value: TAssociativeArray);
    356352begin
    357353  Items[Index] := Value;
     
    376372begin
    377373  Result := LastErrorNumber <> 0;
    378   if Result and Assigned(OnError) then OnError(Self);
     374  if Result then
     375    raise EQueryError.Create('Database query error: "' + LastErrorMessage + '"');
    379376end;
    380377
     
    394391begin
    395392  Query('CREATE TABLE `' + Name + '`' +
    396   ' (`id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`));');
    397 end;
    398 
    399 procedure TSqlDatabase.CreateColumn(ATable, ColumnName: string;
     393  ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));');
     394end;
     395
     396procedure TSqlDatabase.CreateColumn(Table, ColumnName: string;
    400397  ColumnType: TTypeKind);
    401398const
Note: See TracChangeset for help on using the changeset viewer.