Changeset 359 for tools


Ignore:
Timestamp:
Mar 5, 2010, 8:46:18 AM (15 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.
Location:
tools/dbc_export
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tools/dbc_export/DBCExport.lpi

    r348 r359  
    22<CONFIG>
    33  <ProjectOptions>
    4     <PathDelim Value="/"/>
    54    <Version Value="7"/>
    65    <General>
     
    1716      <Language Value=""/>
    1817      <CharSet Value=""/>
     18      <StringTable Comments="" CompanyName="" FileDescription="" FileVersion="0.0.0.0" InternalName="" LegalCopyright="" LegalTrademarks="" OriginalFilename="" ProductName="" ProductVersion="0.0.0.0"/>
    1919    </VersionInfo>
    2020    <PublishOptions>
     
    3131      </local>
    3232    </RunParams>
    33     <Units Count="15">
     33    <Units Count="17">
    3434      <Unit0>
    3535        <Filename Value="DBCExport.pas"/>
    3636        <IsPartOfProject Value="True"/>
    3737        <UnitName Value="DBCExport"/>
    38         <CursorPos X="1" Y="196"/>
    39         <TopLine Value="174"/>
     38        <CursorPos X="57" Y="16"/>
     39        <TopLine Value="144"/>
    4040        <EditorIndex Value="0"/>
    41         <UsageCount Value="79"/>
     41        <UsageCount Value="80"/>
    4242        <Loaded Value="True"/>
    4343        <SyntaxHighlighter Value="Delphi"/>
     
    115115        <IsPartOfProject Value="True"/>
    116116        <UnitName Value="USqlDatabase"/>
    117         <CursorPos X="1" Y="181"/>
    118         <TopLine Value="159"/>
     117        <CursorPos X="22" Y="166"/>
     118        <TopLine Value="276"/>
    119119        <EditorIndex Value="3"/>
    120         <UsageCount Value="70"/>
     120        <UsageCount Value="71"/>
    121121        <Loaded Value="True"/>
    122122        <SyntaxHighlighter Value="Delphi"/>
     
    129129        <TopLine Value="89"/>
    130130        <EditorIndex Value="1"/>
    131         <UsageCount Value="70"/>
     131        <UsageCount Value="71"/>
    132132        <Loaded Value="True"/>
    133133        <SyntaxHighlighter Value="Delphi"/>
     
    139139        <TopLine Value="1"/>
    140140        <EditorIndex Value="2"/>
    141         <UsageCount Value="70"/>
     141        <UsageCount Value="71"/>
    142142        <Loaded Value="True"/>
    143143        <SyntaxHighlighter Value="None"/>
     
    150150        <UsageCount Value="12"/>
    151151      </Unit14>
     152      <Unit15>
     153        <Filename Value="/usr/share/fpcsrc/rtl/objpas/classes/classesh.inc"/>
     154        <CursorPos X="14" Y="253"/>
     155        <TopLine Value="234"/>
     156        <UsageCount Value="10"/>
     157      </Unit15>
     158      <Unit16>
     159        <Filename Value="/usr/share/fpcsrc/rtl/objpas/classes/lists.inc"/>
     160        <CursorPos X="3" Y="544"/>
     161        <TopLine Value="542"/>
     162        <UsageCount Value="10"/>
     163      </Unit16>
    152164    </Units>
    153     <JumpHistory Count="30" HistoryIndex="29">
     165    <JumpHistory Count="28" HistoryIndex="27">
    154166      <Position1>
    155167        <Filename Value="DBCExport.pas"/>
    156         <Caret Line="203" Column="1" TopLine="189"/>
     168        <Caret Line="193" Column="16" TopLine="171"/>
    157169      </Position1>
    158170      <Position2>
    159171        <Filename Value="DBCExport.pas"/>
    160         <Caret Line="204" Column="1" TopLine="190"/>
     172        <Caret Line="198" Column="37" TopLine="171"/>
    161173      </Position2>
    162174      <Position3>
    163         <Filename Value="DBCExport.pas"/>
    164         <Caret Line="205" Column="1" TopLine="191"/>
     175        <Filename Value="USqlDatabase.pas"/>
     176        <Caret Line="4" Column="18" TopLine="1"/>
    165177      </Position3>
    166178      <Position4>
    167         <Filename Value="DBCExport.pas"/>
    168         <Caret Line="206" Column="1" TopLine="192"/>
     179        <Filename Value="USqlDatabase.pas"/>
     180        <Caret Line="52" Column="24" TopLine="30"/>
    169181      </Position4>
    170182      <Position5>
    171         <Filename Value="DBCExport.pas"/>
    172         <Caret Line="29" Column="5" TopLine="1"/>
     183        <Filename Value="USqlDatabase.pas"/>
     184        <Caret Line="80" Column="45" TopLine="58"/>
    173185      </Position5>
    174186      <Position6>
    175         <Filename Value="DBCExport.pas"/>
    176         <Caret Line="80" Column="18" TopLine="58"/>
     187        <Filename Value="USqlDatabase.pas"/>
     188        <Caret Line="137" Column="21" TopLine="115"/>
    177189      </Position6>
    178190      <Position7>
    179191        <Filename Value="DBCExport.pas"/>
    180         <Caret Line="81" Column="42" TopLine="59"/>
     192        <Caret Line="212" Column="30" TopLine="190"/>
    181193      </Position7>
    182194      <Position8>
    183         <Filename Value="USqlDatabase.pas"/>
    184         <Caret Line="34" Column="14" TopLine="8"/>
     195        <Filename Value="DBCExport.pas"/>
     196        <Caret Line="197" Column="32" TopLine="191"/>
    185197      </Position8>
    186198      <Position9>
    187         <Filename Value="USqlDatabase.pas"/>
    188         <Caret Line="52" Column="24" TopLine="30"/>
     199        <Filename Value="DBCExport.pas"/>
     200        <Caret Line="195" Column="1" TopLine="173"/>
    189201      </Position9>
    190202      <Position10>
    191203        <Filename Value="USqlDatabase.pas"/>
    192         <Caret Line="80" Column="45" TopLine="54"/>
     204        <Caret Line="181" Column="1" TopLine="159"/>
    193205      </Position10>
    194206      <Position11>
    195207        <Filename Value="USqlDatabase.pas"/>
    196         <Caret Line="137" Column="21" TopLine="115"/>
     208        <Caret Line="183" Column="1" TopLine="161"/>
    197209      </Position11>
    198210      <Position12>
    199         <Filename Value="DBCExport.pas"/>
    200         <Caret Line="96" Column="7" TopLine="55"/>
     211        <Filename Value="USqlDatabase.pas"/>
     212        <Caret Line="184" Column="1" TopLine="162"/>
    201213      </Position12>
    202214      <Position13>
    203         <Filename Value="DBCExport.pas"/>
    204         <Caret Line="193" Column="16" TopLine="171"/>
     215        <Filename Value="USqlDatabase.pas"/>
     216        <Caret Line="186" Column="1" TopLine="164"/>
    205217      </Position13>
    206218      <Position14>
    207         <Filename Value="DBCExport.pas"/>
    208         <Caret Line="198" Column="37" TopLine="171"/>
     219        <Filename Value="USqlDatabase.pas"/>
     220        <Caret Line="188" Column="1" TopLine="166"/>
    209221      </Position14>
    210222      <Position15>
    211223        <Filename Value="USqlDatabase.pas"/>
    212         <Caret Line="4" Column="18" TopLine="1"/>
     224        <Caret Line="190" Column="1" TopLine="168"/>
    213225      </Position15>
    214226      <Position16>
    215227        <Filename Value="USqlDatabase.pas"/>
    216         <Caret Line="52" Column="24" TopLine="30"/>
     228        <Caret Line="193" Column="1" TopLine="171"/>
    217229      </Position16>
    218230      <Position17>
    219231        <Filename Value="USqlDatabase.pas"/>
    220         <Caret Line="80" Column="45" TopLine="58"/>
     232        <Caret Line="194" Column="1" TopLine="172"/>
    221233      </Position17>
    222234      <Position18>
    223         <Filename Value="USqlDatabase.pas"/>
    224         <Caret Line="137" Column="21" TopLine="115"/>
     235        <Filename Value="DBCExport.pas"/>
     236        <Caret Line="196" Column="1" TopLine="174"/>
    225237      </Position18>
    226238      <Position19>
    227         <Filename Value="DBCExport.pas"/>
    228         <Caret Line="212" Column="30" TopLine="190"/>
     239        <Filename Value="USqlDatabase.pas"/>
     240        <Caret Line="179" Column="43" TopLine="1"/>
    229241      </Position19>
    230242      <Position20>
    231243        <Filename Value="DBCExport.pas"/>
    232         <Caret Line="197" Column="32" TopLine="191"/>
     244        <Caret Line="196" Column="37" TopLine="174"/>
    233245      </Position20>
    234246      <Position21>
    235247        <Filename Value="DBCExport.pas"/>
    236         <Caret Line="195" Column="1" TopLine="173"/>
     248        <Caret Line="29" Column="1" TopLine="10"/>
    237249      </Position21>
    238250      <Position22>
    239         <Filename Value="USqlDatabase.pas"/>
    240         <Caret Line="181" Column="1" TopLine="159"/>
     251        <Filename Value="DBCExport.pas"/>
     252        <Caret Line="82" Column="29" TopLine="61"/>
    241253      </Position22>
    242254      <Position23>
    243         <Filename Value="USqlDatabase.pas"/>
    244         <Caret Line="183" Column="1" TopLine="161"/>
     255        <Filename Value="DBCExport.pas"/>
     256        <Caret Line="194" Column="1" TopLine="176"/>
    245257      </Position23>
    246258      <Position24>
    247         <Filename Value="USqlDatabase.pas"/>
    248         <Caret Line="184" Column="1" TopLine="162"/>
     259        <Filename Value="DBCExport.pas"/>
     260        <Caret Line="206" Column="23" TopLine="185"/>
    249261      </Position24>
    250262      <Position25>
    251         <Filename Value="USqlDatabase.pas"/>
    252         <Caret Line="186" Column="1" TopLine="164"/>
     263        <Filename Value="DBCExport.pas"/>
     264        <Caret Line="234" Column="42" TopLine="217"/>
    253265      </Position25>
    254266      <Position26>
    255         <Filename Value="USqlDatabase.pas"/>
    256         <Caret Line="188" Column="1" TopLine="166"/>
     267        <Filename Value="DBCExport.pas"/>
     268        <Caret Line="238" Column="29" TopLine="219"/>
    257269      </Position26>
    258270      <Position27>
    259         <Filename Value="USqlDatabase.pas"/>
    260         <Caret Line="190" Column="1" TopLine="168"/>
     271        <Filename Value="DBCExport.pas"/>
     272        <Caret Line="6" Column="92" TopLine="1"/>
    261273      </Position27>
    262274      <Position28>
    263         <Filename Value="USqlDatabase.pas"/>
    264         <Caret Line="193" Column="1" TopLine="171"/>
     275        <Filename Value="DBCExport.pas"/>
     276        <Caret Line="53" Column="89" TopLine="19"/>
    265277      </Position28>
    266       <Position29>
    267         <Filename Value="USqlDatabase.pas"/>
    268         <Caret Line="194" Column="1" TopLine="172"/>
    269       </Position29>
    270       <Position30>
    271         <Filename Value="DBCExport.pas"/>
    272         <Caret Line="196" Column="1" TopLine="174"/>
    273       </Position30>
    274278    </JumpHistory>
    275279  </ProjectOptions>
     
    304308  </CompilerOptions>
    305309  <Debugging>
    306     <BreakPoints Count="3">
    307       <Item1>
    308         <Source Value="DBCExport.pas"/>
    309         <Line Value="212"/>
    310       </Item1>
    311       <Item2>
    312         <Source Value="DBCExport.pas"/>
    313         <Line Value="195"/>
    314       </Item2>
    315       <Item3>
    316         <Source Value="DBCExport.pas"/>
    317         <Line Value="196"/>
    318       </Item3>
    319     </BreakPoints>
    320310    <Watches Count="2">
    321311      <Item1>
  • tools/dbc_export/DBCExport.pas

    r348 r359  
    77  cthreads,
    88  {$ENDIF}{$ENDIF}
    9   Classes, SysUtils, CustApp
    10   { you can add units after this },
     9  Classes, SysUtils, CustApp,
    1110  USqlDatabase, UDBC, Variants;
    1211
     
    2726    IndexTable: array of integer;
    2827    SQLquery: string;
    29     CharSet: string;
    3028    DisplayData: Boolean;
    3129    constructor Create(TheOwner: TComponent); override;
     
    3735    procedure ReplaceText;
    3836    procedure CreateIndexTable(var DbRows: TDbRows);
    39     function GetIDbyEntry(entry: integer; var DBRows: TDBRows): integer;
     37    function GetIDbyEntry(Entry: Integer; var DBRows: TDBRows): Integer;
    4038  end;
    4139
     
    7977  end else Database.Hostname := 'localhost';
    8078  if HasOption('a', 'Charset') then begin
    81     Charset := GetOptionValue('a', 'Charset');
    82   end else Charset := 'utf8';
     79    Database.Encoding := GetOptionValue('a', 'Charset');
     80  end else Database.Encoding := 'utf8';
    8381  if HasOption('q', 'sql_query') then begin
    8482    SQLquery := GetOptionValue('q', 'sql_query');
     
    8987begin
    9088  ParseParameters;
    91 
    9289  LoadDBC;
    93 
    9490  ShowDBC;
    95 
    9691  ReplaceText;
    97 
    9892  SaveDBC;
    99 
    100   // Stop program loop
    101   Terminate;
     93  Terminate;   // Stop program loop
    10294end;
    10395
     
    128120  WriteLn('  -c --coltypedef  DBC column type definition string (u - uint32, s - string)');
    129121  WriteLn('  -d --display     Display DBC data');
    130   WriteLn('  -q --sql_query  SQL select when entry is ID first column in dbc file and text1,text2... is texts columns');
     122  WriteLn('  -q --sql_query   SQL select when entry is ID first column in dbc file and text1,text2... is texts columns');
    131123  // SELECT T.entry as entry,T.Text as text1 FROM (SELECT * FROM gametips WHERE (Complete = 1) AND ((Language = 1)) AND (User IN (459,670,602,462,1,400,638,592,624,610,769,331,131,704,2,499,641,660,578,337,304,277,208,613,768,754,590,606,26,618,739,503,601,607,585,596,765,320,547,687)) AND VersionStart <= 9947 AND VersionEnd >= 9947) AS T GROUP BY T.entry
    132124end;
     
    165157          LastIndexStringColumns:=I;
    166158        end;
    167 
    168 
    169159    end else WriteLn('File ' + DBCFileName + ' not exists.');
    170160  end;
     
    180170end;
    181171
    182 
    183 //function get data from database and replace text in dbc
     172// Function get data from database and replace text in DBC
    184173procedure TDBCExport.ReplaceText;
    185174var
    186175  DBRows : TDbRows;
    187   Text: String;
    188   X, Y, I,entry: Integer;
     176  Text: string;
     177  X, Y, I, Entry: Integer;
    189178  IndexRowData: Integer;
    190179begin
    191180  with DBC do begin
    192     if (SQLquery <> '') then begin;
    193 
     181    if (SQLquery <> '') then begin
    194182      Database.Connect;
    195 //      Database.Query('CHARACTER SET '+CharSet);
    196       Database.Query('SET NAMES '+CharSet);
    197183      DBRows := Database.Query(SQLquery);
    198       CharSet:=Database.Charset;
    199184
    200185      CreateIndexTable(DBRows);
    201186
    202 
    203187      for Y := 0 to Length(DBC.Cells) - 1 do begin
    204         entry :=Cells[Y,0];
    205         if (Length(IndexTable) > entry) then
    206            IndexRowData:= IndexTable[entry]
    207         else IndexRowData:= 0;
    208 
    209          I := 1;
    210 
     188        Entry := Cells[Y, 0];
     189        if (Length(IndexTable) > Entry) then
     190          IndexRowData := IndexTable[entry]
     191        else IndexRowData := 0;
     192
     193        I := 1;
    211194        if (IndexRowData <> 0) then begin
    212195          for X := 1 to Length(ColumnTypeDefinition) do begin
    213196            if ColumnTypeDefinition[X] = 's' then begin
    214               Text:=DBRows.Data[IndexRowData-1].Values['text'+IntToStr(I)];
    215               SetString(Y,X-1,Text);
    216               I:=I+1;
    217               if (LastIndexStringColumns = X) then
    218                 Break;
     197              Text := DBRows.Data[IndexRowData - 1].Values['text' + IntToStr(I)];
     198              SetString(Y, X - 1, Text);
     199              I := I + 1;
     200              if (LastIndexStringColumns = X) then Break;
    219201            end;
    220202          end;
    221203        end;
    222 
    223204      end;
    224 
    225 
    226 
     205      DBRows.Destroy;
    227206    end;
    228207  end;
    229208end;
    230209
    231 //create table index = entry, value = Index-1 DBRows, notfound = 0
     210// Create table index = entry, value = Index-1 DBRows, notfound = 0
    232211procedure TDBCExport.CreateIndexTable(var DbRows: TDbRows);
    233212var
    234  i: integer;
    235  Count,entry: integer;
     213  I: Integer;
     214  Count, Entry: Integer;
    236215begin
    237216  try
    238      Count:=DBRows.Count;
    239      for i:=1 to Count do begin
    240        entry:=StrToInt(DBRows.Data[i-1].Values['entry']);
    241 
    242        if (i =127) then
    243          SetLength(IndexTable,entry+1);
    244 
    245        if (Length(IndexTable)-1 < entry) then
    246          SetLength(IndexTable,entry+1);
    247 
    248 
    249        IndexTable[entry]:=i;
    250 
     217    Count := DBRows.Count;
     218    for I := 1 to Count do begin
     219      Entry := StrToInt(DBRows.Data[i - 1].Values['entry']);
     220
     221      if (I = 127) then SetLength(IndexTable, Entry + 1);
     222
     223      if (Length(IndexTable) - 1 < Entry) then
     224         SetLength(IndexTable, Entry + 1);
     225
     226       IndexTable[Entry] := I;
    251227    end;
    252228  except
     
    254230end;
    255231
    256 function TDBCExport.GetIDbyEntry(entry: integer; var DBRows: TDBRows): integer;
    257 var
    258  i: integer;
    259  Count: integer;
     232function TDBCExport.GetIDbyEntry(Entry: Integer; var DBRows: TDBRows): Integer;
     233var
     234  I: Integer;
    260235begin
    261236  try
    262      Count:=DBRows.Count;
    263      for i:=0 to DBRows.Count - 1 do begin
    264       if (StrToInt(DBRows.Data[i].Values['entry']) = entry) then begin
    265         Result := i;
    266         exit;
     237    for I := 0 to DBRows.Count - 1 do begin
     238      if (StrToInt(DBRows.Data[i].Values['entry']) = Entry) then begin
     239        Result := I;
     240        Exit;
    267241      end;
    268242    end;
  • 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.