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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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;
Note: See TracChangeset for help on using the changeset viewer.