ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/oup/current/DataAccess/Access_OUP_ADB.pas
(Generate patch)

Comparing:
oup/rewrite/DataAccess/Access_OUP_ADB.pas (file contents), Revision 93 by alloc, Thu Jan 18 17:15:59 2007 UTC vs.
oup/current/DataAccess/Access_OUP_ADB.pas (file contents), Revision 155 by alloc, Sun Apr 1 21:14:29 2007 UTC

# Line 1 | Line 1
1   unit Access_OUP_ADB;
2   interface
3  
4 < uses DataAccess;
4 > uses DataAccess, ABSMain, TypeDefs, Classes;
5  
6   type
7    TAccess_OUP_ADB = class(TDataAccess)
8    private
9 < {    FDatabase: TABSDatabase;
10 <    FQuery:    TABSQuery;
11 <    Fdat_files:    TFiles;
9 >    FDatabase:          TABSDatabase;
10 >    FQuery:             TABSQuery;
11 >    Fdat_files:         TFiles;
12      Fdat_extensionsmap: TExtensionsMap;
13    protected
14    public
15 <    constructor Create(OLDBFilename: String; var Result: Boolean); override;
15 >    constructor Create(DBFilename: String; ConnectionID: Integer; var Msg: TStatusMessages); override;
16      procedure Close; override;
17  
18      procedure UpdateListCache;
19 <    //      function GetDatLinks(srcid:LongWord):TDatLinks;
20 <    function GetFileInfo(fileid: Integer): TFileInfo; override;
21 <    function GetFilesList(ext: String; pattern: String;
22 <      NoEmptyFiles: Boolean; sort: TSortType): TStringArray; override;
23 <    function GetFilesCount: LongWord; override;
24 <    function GetExtensionsList: TStringArray; override;
25 <    function GetExtendedExtensionsList: TExtensionsMap; override;
26 <    function GetNamedFilesMap: TNamedFilesMap;
27 <
28 <    function LoadDatFile(fileid: LongWord): Tdata; override;
29 <    procedure UpdateDatFile(fileid: LongWord; Data: Tdata); override;
30 <    procedure LoadDatFilePart(fileid, offset, size: LongWord; target: Pointer); override;
31 <    procedure UpdateDatFilePart(fileid, offset, size: LongWord; target: Pointer); override;
32 <
33 <    function GetRawList(fileid: LongWord): TRawList; override;
34 <    procedure LoadRawFile(fileid, dat_offset: LongWord; target: Pointer); override;
35 <    procedure UpdateRawFile(fileid, dat_offset: LongWord; size: LongWord;
36 <      target: Pointer); override;
37 <    procedure LoadRawFilePart(fileid, dat_offset: LongWord;
38 <      offset, size: LongWord; target: Pointer); override;
39 <    procedure UpdateRawFilePart(fileid, dat_offset: LongWord;
40 <      offset, size: LongWord; target: Pointer); override;
19 >
20 >    function GetLinksToFile(FileID: Integer): TLinks;
21 >
22 >    function GetFileInfo(FileID: Integer): TFileInfo; override;
23 >    function GetFilesList(Ext: String; Pattern: String;
24 >      NoEmptyFiles: Boolean; SortType: TSortType): TStrings; override;
25 >    function GetFileCount: Integer; override;
26 >    function GetExtensionsList(ExtListFormat: TExtensionFormat): TStrings; override;
27 >
28 >    procedure LoadDatFile(FileID: Integer; var Target: TStream); overload; override;
29 >    procedure UpdateDatFile(FileID: Integer; Src: TStream); overload; override;
30 >    procedure LoadDatFilePart(FileID, Offset, Size: Integer; var Target: TStream); overload; override;
31 >    procedure UpdateDatFilePart(FileID, Offset, Size: Integer; Src: TStream); overload; override;
32 >
33 >    function GetDatLinks(FileID: Integer): TDatLinkList; override;
34 >    function GetDatLink(FileID, DatOffset: Integer): TDatLink; override;
35 >    function GetRawList(FileID: Integer): TRawDataList; override;
36 >    function GetRawInfo(FileID, DatOffset: Integer): TRawDataInfo; override;
37 >
38 >    procedure LoadRawFile(FileID, DatOffset: Integer; var Target: TStream); overload; override;
39 >    procedure UpdateRawFile(FileID, DatOffset: Integer; Src: TStream); overload; override;
40 >    procedure LoadRawFilePart(FileID, DatOffset, Offset, Size: Integer; var Target: TStream); overload; override;
41 >    procedure UpdateRawFilePart(FileID, DatOffset, Offset, Size: Integer; Src: TStream); overload; override;
42    published
43 < }  end;
43 >  end;
44  
45  
46   implementation
47  
48 + uses
49 +  SysUtils, Data, Functions, ABSDecUtil, DB, DatLinks;
50 +
51  
52   (*
53   ================================================================================
54                       Implementation of  TOniDataADB
55   *)
56  
57 < {
58 < constructor TOniDataADB.Create(OLDBFilename: String; var Result: Boolean);
55 < var
56 <  i, j:  Byte;
57 <  temps: String;
57 >
58 > constructor TAccess_OUP_ADB.Create(DBFilename: String; ConnectionID: Integer; var Msg: TStatusMessages);
59   begin
60 <  if not FileExists(OLDBFilename) then
60 >  Msg := SM_UnknownError;
61 >  if not FileExists(DBFilename) then
62    begin
63 <    ShowMessage('File doesn''t exist!!!');
62 <    Result := False;
63 >    Msg := SM_FileNotFound;
64      Exit;
65    end;
66 <  FFileName := OLDBFilename;
66 >  FFileName := DBFilename;
67 >
68    FDatabase := TABSDatabase.Create(nil);
69 <  FDatabase.DatabaseName := 'OLDBcon';
70 <  FDatabase.DatabaseFileName := OLDBFilename;
69 >  FDatabase.Exclusive := True;
70 >  FDatabase.MultiUser := False;
71 >  FDatabase.DatabaseName := 'OLDBcon' + IntToStr(ConnectionID);
72 >  FDatabase.DatabaseFileName := DBFilename;
73    FDatabase.Open;
74    FQuery := TABSQuery.Create(FDatabase);
75 <  FQuery.DatabaseName := 'OLDBcon';
75 >  FQuery.DisableControls;
76 >  FQuery.RequestLive := False;
77 >  FQuery.DatabaseName := 'OLDBcon' + IntToStr(ConnectionID);
78    FQuery.SQL.Text := 'SELECT [name],[value] FROM globals ORDER BY [name] ASC';
79    FQuery.Open;
80    FQuery.First;
# Line 77 | Line 83 | begin
83      begin
84        if FQuery.FieldByName('value').AsString <> DBversion then
85        begin
86 <        ShowMessage('Database-file ' + #13 + #10 +
81 <          '"' + OLDBFilename + '"' + #13 + #10 +
82 <          'has wrong version. (Required: ' + DBversion + '; found: ' +
83 <          FQuery.FieldByName('value').AsString + ')');
86 >        Msg := SM_IncompatibleDBVersion;
87          FQuery.Close;
85        Result := False;
88          Exit;
89        end;
90      end;
91      if FQuery.FieldByName('name').AsString = 'lvl' then
92 +      FLevelNumber := StrToInt(FQuery.FieldByName('value').AsString);
93 +    if FQuery.FieldByName('name').AsString = 'DataOS' then
94      begin
95 <      FLevelInfo.LevelNumber := StrToInt(FQuery.FieldByName('value').AsString);
96 <    end;
97 <    if FQuery.FieldByName('name').AsString = 'ident' then
98 <    begin
99 <      temps := FQuery.FieldByName('value').AsString;
100 <      for i := 0 to High(FLevelInfo.Ident) do
101 <      begin
102 <        j := i * 2 + 1;
99 <        case temps[j] of
100 <          '0'..'9':
101 <            FLevelInfo.Ident[i] := Ord(temps[j]) - 48;
102 <          'A'..'F':
103 <            FLevelInfo.Ident[i] := Ord(temps[j]) - 55;
104 <        end;
105 <        FLevelInfo.Ident[i] := FLevelInfo.Ident[i] * 16;
106 <        case temps[j + 1] of
107 <          '0'..'9':
108 <            FLevelInfo.Ident[i] := FLevelInfo.Ident[i] + Ord(temps[j + 1]) - 48;
109 <          'A'..'F':
110 <            FLevelInfo.Ident[i] := FLevelInfo.Ident[i] + Ord(temps[j + 1]) - 55;
111 <        end;
112 <      end;
113 <    end;
114 <    if FQuery.FieldByName('name').AsString = 'ident' then
115 <    begin
116 <      temps   := FQuery.FieldByName('value').AsString;
117 <      Fos_mac := temps = 'MAC';
95 >      if FQuery.FieldByName('value').AsString = 'WIN' then
96 >        FDataOS := DOS_WIN
97 >      else if FQuery.FieldByName('value').AsString = 'WINDEMO' then
98 >        FDataOS := DOS_WINDEMO
99 >      else if FQuery.FieldByName('value').AsString = 'MAC' then
100 >        FDataOS := DOS_MAC
101 >      else if FQuery.FieldByName('value').AsString = 'MACBETA' then
102 >        FDataOS := DOS_MACBETA;
103      end;
104      FQuery.Next;
105    until FQuery.EOF;
106    FQuery.Close;
107  
108 <  UpdateListCache;
108 >  Msg := SM_OK;
109 >  FBackend := DB_ADB;
110 >
111 >  FConnectionID := ConnectionID;
112 >  FChangeRights := [CR_EditDat, CR_EditRaw, CR_ResizeDat, CR_ResizeRaw];
113  
114 <  Result   := True;
126 <  FBackend := ODB_ADB;
114 >  UpdateListCache;
115   end;
116  
117  
118  
119  
120 < procedure TOniDataADB.Close;
120 > procedure TAccess_OUP_ADB.Close;
121   begin
122 +  FQuery.Free;
123    FDatabase.Close;
124    FDatabase.Free;
125    Self.Free;
# Line 138 | Line 127 | end;
127  
128  
129  
130 < procedure TOniDataADB.UpdateListCache;
130 > procedure TAccess_OUP_ADB.UpdateListCache;
131   var
132 <  i:     LongWord;
132 >  i:     Integer;
133    temps: String;
134   begin
135    FQuery.SQL.Text := 'SELECT id,name,extension,[size],contenttype FROM datfiles ORDER BY id ASC;';
136    FQuery.Open;
137 +  SetLength(Fdat_files, FQuery.RecordCount);
138    if FQuery.RecordCount > 0 then
139    begin
140      FQuery.First;
151    SetLength(Fdat_files, FQuery.RecordCount);
141      i := 0;
142      repeat
143        Fdat_files[i].ID := FQuery.FieldByName('id').AsInteger;
144        Fdat_files[i].Name := FQuery.FieldByName('name').AsString;
145        Fdat_files[i].Extension := FQuery.FieldByName('extension').AsString;
157      Fdat_files[i].FileName := FormatNumber(Fdat_files[i].ID, 5, '0') + '-' +
158          Fdat_files[i].Name + '.' + Fdat_files[0].Extension;
159      Fdat_files[i].FileNameHex := IntToHex(Fdat_files[i].ID, 4) + '-' +
160          Fdat_files[i].Name + '.' + Fdat_files[0].Extension;
146        Fdat_files[i].Size := FQuery.FieldByName('size').AsInteger;
147 <      Fdat_files[i].FileType := HexToLong(FQuery.FieldByName('contenttype').AsString);
147 >      Fdat_files[i].FileType := StrToInt('$'+FQuery.FieldByName('contenttype').AsString);
148        Fdat_files[i].DatAddr := 0;
164      Fdat_files[i].opened := False;
149        Inc(i);
150        FQuery.Next;
151      until FQuery.EOF;
152    end;
153    FQuery.Close;
154  
171  SetLength(Fdat_extensionsmap, 0);
155    FQuery.SQL.Text :=
156      'SELECT extension,count(extension) AS x FROM datfiles GROUP BY extension ORDER BY extension ASC;';
157    FQuery.Open;
158 +  SetLength(Fdat_extensionsmap, FQuery.RecordCount);
159    if FQuery.RecordCount > 0 then
160    begin
177    SetLength(Fdat_extensionsmap, FQuery.RecordCount);
161      i := 0;
162      repeat
163 <      temps := FQuery.FieldByName('extension').AsString[1];
163 >      temps := FQuery.FieldByName('extension').AsString;
164        Fdat_extensionsmap[i].Extension[3] := temps[1];
165        Fdat_extensionsmap[i].Extension[2] := temps[2];
166        Fdat_extensionsmap[i].Extension[1] := temps[3];
# Line 191 | Line 174 | begin
174   end;
175  
176  
177 < function TOniDataADB.GetFileInfo(fileid: Integer): TFileInfo;
177 >
178 > function TAccess_OUP_ADB.GetLinksToFile(FileID: Integer): TLinks;
179   var
180    i: Integer;
181   begin
182 +  SetLength(Result.ByName, 0);
183 +  FQuery.SQL.Text := 'SELECT src_link_offset, src_id FROM linkmap WHERE target_id = ' + IntToStr(FileID) + ' ORDER BY src_id ASC;';
184 +  FQuery.Open;
185 +  SetLength(Result.ByID, FQuery.RecordCount);
186 +  if FQuery.RecordCount > 0 then
187 +  begin
188 +    i := 0;
189 +    repeat
190 +      Result.ByID[i].SrcOffset := FQuery.FieldByName('src_link_offset').AsInteger;
191 +      Result.ByID[i].Destination := FQuery.FieldByName('src_id').AsInteger;
192 +      Inc(i);
193 +      FQuery.Next;
194 +    until FQuery.EOF;
195 +  end;
196 +  FQuery.Close;
197 + end;
198 +
199 +
200 +
201 + function TAccess_OUP_ADB.GetFileInfo(fileid: Integer): TFileInfo;
202 + begin
203    if fileid = -1 then
204    begin
205      Result := inherited GetFileInfo(fileid);
206      Exit;
207    end;
208 <  if fileid < Self.GetFilesCount then
209 <  begin
205 <    for i := 0 to High(Fdat_files) do
206 <      if Fdat_files[i].ID = fileid then
207 <        Break;
208 <    if i < Length(Fdat_files) then
209 <      Result := Fdat_files[i]
210 <    else
211 <      Result.ID := -1;
212 <  end
208 >  if fileid < Self.GetFileCount then
209 >    Result    := Fdat_files[fileid]
210    else
214  begin
211      Result.ID := -1;
216  end;
212   end;
213  
214  
215  
216  
217 < function TOniDataADB.GetFilesList(ext: String; pattern: String;
218 <  NoEmptyFiles: Boolean; sort: TSortType): TStringArray;
217 > function TAccess_OUP_ADB.GetFilesList(ext: String; pattern: String;
218 >  NoEmptyFiles: Boolean; SortType: TSortType): TStrings;
219   var
220 <  i: LongWord;
221 <  list: TStringList;
220 >  i:      Integer;
221 >  list:   TStringList;
222    id, name, extension: String;
223    fields: TStrings;
224  
225    procedure getfields;
226    begin
227 <     fields.CommaText := StringReplace(AnsiQuotedStr(list.Strings[i], '"'), ';', '","', [rfReplaceAll]);
228 <    if sort in [stIDAsc, stIDDesc] then
227 >    fields.CommaText := StringReplace(AnsiQuotedStr(list.Strings[i], '"'), ';', '","', [rfReplaceAll]);
228 >    if SortType in [ST_IDAsc, ST_IDDesc] then
229      begin
230        id := fields.Strings[0];
231        name := fields.Strings[1];
232        extension := fields.Strings[2];
233      end;
234 <    if sort in [stNameAsc, stNameDesc] then
234 >    if SortType in [ST_NameAsc, ST_NameDesc] then
235      begin
236        id := fields.Strings[1];
237        name := fields.Strings[0];
238        extension := fields.Strings[2];
239      end;
240 <    if sort in [stExtAsc, stExtDesc] then
240 >    if SortType in [ST_ExtAsc, ST_ExtDesc] then
241      begin
242        id := fields.Strings[1];
243        name := fields.Strings[2];
244        extension := fields.Strings[0];
245      end;
246 +    if SortType in [ST_ExtNameAsc, ST_ExtNameDesc] then
247 +    begin
248 +      id := fields.Strings[2];
249 +      name := fields.Strings[1];
250 +      extension := fields.Strings[0];
251 +    end;
252    end;
253  
254   begin
255    list := TStringList.Create;
256 +  list.CaseSensitive := True;
257    list.Sorted := True;
258 <  for i := 0 to High(Fdat_files) do
258 >  for i := 0 to GetFileCount - 1 do
259    begin
260      if ((Length(ext) = 0) or (Pos(Fdat_files[i].Extension, ext) > 0)) and
261        ((Length(pattern) = 0) or
# Line 261 | Line 263 | begin
263      begin
264        if (NoEmptyFiles = False) or ((Fdat_files[i].FileType and $02) = 0) then
265        begin
266 <        if AppSettings.FilenumbersAsHex then
265 <          id := IntToHex(Fdat_files[i].ID, 4)
266 <        else
267 <          id := FormatNumber(Fdat_files[i].ID, 5, '0');
266 >        id := FormatNumber(Fdat_files[i].ID, 5, '0');
267          name := Fdat_files[i].Name;
268          extension := Fdat_files[i].Extension;
269  
270 <        case sort of
271 <          stIDAsc, stIDDesc:     list.Add(id + ';' + name + ';' + extension);
272 <          stNameAsc, stNameDesc: list.Add(name + ';' + id + ';' + extension);
273 <          stExtAsc, stExtDesc:   list.Add(extension + ';' + id + ';' + name);
270 >        case SortType of
271 >          ST_IDAsc, ST_IDDesc:     list.Add(id + ';' + name + ';' + extension);
272 >          ST_NameAsc, ST_NameDesc: list.Add(name + ';' + id + ';' + extension);
273 >          ST_ExtAsc, ST_ExtDesc:   list.Add(extension + ';' + id + ';' + name);
274 >          ST_ExtNameAsc, ST_ExtNameDesc: list.Add(extension + ';' + name + ';' + id);
275          end;
276        end;
277      end;
278    end;
279 <  SetLength(Result, list.Count);
280 <  fields := TStringList.Create;
281 <  if sort in [stIDAsc, stNameAsc, stExtAsc] then
282 <    for i := 0 to list.Count - 1 do
283 <    begin
284 <      getfields;
285 <      Result[i] := id + '-' + name + '.' + extension;
286 <    end
287 <  else
288 <    for i := list.Count - 1 downto 0 do
289 <    begin
290 <      getfields;
291 <      Result[list.Count - i - 1] := id + '-' + name + '.' + extension;
292 <    end;
279 >  if not Assigned(Result) then
280 >    Result := TStringList.Create;
281 >  if list.Count > 0 then
282 >  begin
283 >    fields := TStringList.Create;
284 >    if SortType in [ST_IDAsc, ST_NameAsc, ST_ExtAsc, ST_ExtNameAsc] then
285 >      for i := 0 to list.Count - 1 do
286 >      begin
287 >        getfields;
288 >        Result.Add(id + '-' + name + '.' + extension);
289 >      end
290 >    else
291 >      for i := list.Count - 1 downto 0 do
292 >      begin
293 >        getfields;
294 >        Result.Add(id + '-' + name + '.' + extension);
295 >      end;
296 >    fields.Free;
297 >  end;
298    list.Free;
294  fields.Free;
299   end;
300  
301  
302  
303  
304 < function TOniDataADB.GetFilesCount: LongWord;
304 > function TAccess_OUP_ADB.GetFileCount: Integer;
305   begin
306    Result := Length(Fdat_files);
307   end;
308  
309  
310 <
307 <
308 < function TOniDataADB.GetExtensionsList: TStringArray;
310 > function TAccess_OUP_ADB.GetExtensionsList(ExtListFormat: TExtensionFormat): TStrings;
311   var
312 <  i: LongWord;
312 >  i: Integer;
313   begin
314 <  SetLength(Result, Length(Fdat_extensionsmap));
315 <  for i := 0 to High(Result) do
314 >  if not Assigned(Result) then
315 >    Result := TStringList.Create;
316 >  if Result is TStringList then
317 >    TStringList(Result).Sorted := True;
318 >  for i := 0 to Length(Fdat_extensionsmap) - 1 do
319    begin
320      with Fdat_extensionsmap[i] do
321      begin
322 <      Result[i] := Extension[3] + Extension[2] + Extension[1] + Extension[0] +
323 <        ' (' + IntToStr(ExtCount) + ')';
322 >      case ExtListFormat of
323 >        EF_ExtOnly:
324 >          Result.Add(Extension[3] + Extension[2] + Extension[1] + Extension[0]);
325 >        EF_ExtCount:
326 >          Result.Add(Extension[3] + Extension[2] + Extension[1] + Extension[0] +
327 >                ' (' + IntToStr(ExtCount) + ')');
328 >      end;
329      end;
330    end;
331   end;
332  
333  
334 <
325 <
326 < function TOniDataADB.GetExtendedExtensionsList: TExtensionsMap;
327 < var
328 <  i, j:  LongWord;
329 <  temps: String;
330 <  Data:  Tdata;
331 < begin
332 <  SetLength(Result, 0);
333 <  FQuery.SQL.Text := 'SELECT ext,ident FROM extlist ORDER BY ext ASC;';
334 <  FQuery.Open;
335 <  if FQuery.RecordCount > 0 then
336 <  begin
337 <    SetLength(Result, FQuery.RecordCount);
338 <    i := 0;
339 <    repeat
340 <      temps := FQuery.FieldByName('ext').AsString;
341 <      for j := 0 to 3 do
342 <        Result[i].Extension[j] := temps[4 - j];
343 <      Data := DecodeHexString(FQuery.FieldByName('ident').AsString);
344 <      for j := 0 to 7 do
345 <        Result[i].Ident[j] := Data[j];
346 <      Inc(i);
347 <      FQuery.Next;
348 <    until FQuery.EOF;
349 <  end;
350 <  FQuery.Close;
351 < end;
352 <
353 <
354 <
355 <
356 < function TOniDataADB.GetNamedFilesMap: TNamedFilesMap;
334 > procedure TAccess_OUP_ADB.LoadDatFile(FileID: Integer; var Target: TStream);
335   var
336 <  i:     LongWord;
337 <  temp:  Integer;
360 <  temps: String;
361 <  temparray: array of record
362 <    id: Integer;
363 <    fullname: String[50];
364 <  end;
336 >  mem: TStream;
337 >  streampos: Integer;
338   begin
339 <  SetLength(temparray, 0);
367 <  FQuery.SQL.Text :=
368 <    'SELECT id,(extension+name) AS xname FROM datfiles WHERE Length(name)>0 ORDER BY extension,name ASC;';
369 <  FQuery.Open;
370 <  if FQuery.RecordCount > 0 then
371 <  begin
372 <    repeat
373 <      temp  := FQuery.FieldByName('id').AsInteger;
374 <      temps := FQuery.FieldByName('xname').AsString;
375 <
376 <      SetLength(temparray, Length(temparray) + 1);
377 <      if Length(temparray) > 1 then
378 <      begin
379 <        for i := High(temparray) - 1 downto 0 do
380 <        begin
381 <          if StringSmaller(temps, temparray[i].fullname) then
382 <          begin
383 <            temparray[i + 1] := temparray[i];
384 <            if i = 0 then
385 <            begin
386 <              temparray[i].id := temp;
387 <              temparray[i].fullname := temps;
388 <            end;
389 <          end
390 <          else
391 <          begin
392 <            temparray[i + 1].id := temp;
393 <            temparray[i + 1].fullname := temps;
394 <            Break;
395 <          end;
396 <        end;
397 <      end
398 <      else
399 <      begin
400 <        temparray[0].id := temp;
401 <        temparray[0].fullname := temps;
402 <      end;
403 <      FQuery.Next;
404 <    until FQuery.EOF;
405 <  end;
406 <  FQuery.Close;
407 <  SetLength(Result, Length(temparray));
408 <  for i := 0 to High(temparray) do
339 >  if fileid < GetFileCount then
340    begin
341 <    Result[i].FileNumber := temparray[i].id;
342 <    Result[i].blubb      := 0;
412 <  end;
413 < end;
414 <
415 <
341 >    if not Assigned(Target) then
342 >      Target := TMemoryStream.Create;
343  
344 +    streampos := Target.Position;
345  
418 function TOniDataADB.LoadDatFile(fileid: LongWord): Tdata;
419 var
420  mem: TStream;
421 begin
422  if fileid < Self.GetFilesCount then
423  begin
346      FQuery.SQL.Text := 'SELECT data FROM datfiles WHERE id=' + IntToStr(fileid) + ';';
347      FQuery.Open;
348      if FQuery.RecordCount > 0 then
349      begin
350        mem := FQuery.CreateBlobStream(FQuery.FieldByName('data'), bmRead);
429      SetLength(Result, mem.Size);
351        mem.Seek(0, soFromBeginning);
352 <      mem.Read(Result[0], mem.Size);
352 >      Target.CopyFrom(mem, mem.Size);
353        mem.Free;
354      end;
355      FQuery.Close;
356 +
357 +    Target.Seek(streampos, soFromBeginning);
358    end;
359   end;
360  
361 <
439 <
440 <
441 < procedure TOniDataADB.UpdateDatFile(fileid: LongWord; Data: Tdata);
361 > procedure TAccess_OUP_ADB.UpdateDatFile(FileID: Integer; Src: TStream);
362   var
363    MimeCoder: TStringFormat_MIME64;
364    mem: TMemoryStream;
365   begin
366 <  if fileid < Self.GetFilesCount then
366 >  if fileid < GetFileCount then
367    begin
368      mimecoder := TStringFormat_MIME64.Create;
369      mem := TMemoryStream.Create;
370 <    mem.Write(Data[0], Length(Data));
451 <    mem.Seek(0, soFromBeginning);
370 >    mem.CopyFrom(Src, Src.Size);
371      FQuery.SQL.Text := 'UPDATE datfiles SET data=MimeToBin("' +
372        MimeCoder.StrTo(mem.Memory, mem.Size) + '"), size=' + IntToStr(mem.Size) +
373        ' WHERE id=' + IntToStr(fileid) + ';';
# Line 456 | Line 375 | begin
375      mem.Free;
376      mimecoder.Free;
377    end;
459  UpdateListCache;
378   end;
379  
380  
381  
382 <
465 < procedure TOniDataADB.LoadDatFilePart(fileid, offset, size: LongWord; target: Pointer);
382 > procedure TAccess_OUP_ADB.LoadDatFilePart(FileID, Offset, Size: Integer; var Target: TStream);
383   var
384 +  streampos: Integer;
385    mem: TStream;
386   begin
387 <  if fileid < Self.GetFilesCount then
387 >  if fileid < GetFileCount then
388    begin
389 +    if not Assigned(Target) then
390 +      Target := TMemoryStream.Create;
391 +    streampos := Target.Position;
392 +
393      FQuery.SQL.Text := 'SELECT data FROM datfiles WHERE id=' + IntToStr(fileid) + ';';
394      FQuery.Open;
395      if FQuery.RecordCount > 0 then
396      begin
397        mem := FQuery.CreateBlobStream(FQuery.FieldByName('data'), bmRead);
398        mem.Seek(offset, soFromBeginning);
399 <      mem.Read(target^, size);
399 >      Target.CopyFrom(mem, size);
400        mem.Free;
401      end;
402      FQuery.Close;
403 +    Target.Seek(streampos, soFromBeginning);
404    end;
405   end;
406  
407  
408  
409 <
487 < procedure TOniDataADB.UpdateDatFilePart(fileid, offset, size: LongWord; target: Pointer);
409 > procedure TAccess_OUP_ADB.UpdateDatFilePart(FileID, Offset, Size: Integer; Src: TStream);
410   var
411    MimeCoder: TStringFormat_MIME64;
412    mem:  TMemoryStream;
491  Data: Tdata;
413   begin
414 <  if fileid < Self.GetFilesCount then
414 >  if fileid < GetFileCount then
415    begin
416 <    Data := Self.LoadDatFile(fileid);
417 <    mimecoder := TStringFormat_MIME64.Create;
418 <    mem := TMemoryStream.Create;
419 <    mem.Write(Data[0], Length(Data));
499 <    mem.Seek(offset, soFromBeginning);
500 <    mem.Write(target^, size);
416 >    mem := nil;
417 >    LoadDatFile(fileid, TStream(mem));
418 >    mem.Seek(Offset, soFromBeginning);
419 >    mem.CopyFrom(Src, Size);
420      mem.Seek(0, soFromBeginning);
421 +    mimecoder := TStringFormat_MIME64.Create;
422      FQuery.SQL.Text := 'UPDATE datfiles SET data=MimeToBin("' +
423        MimeCoder.StrTo(mem.Memory, mem.Size) + '") WHERE id=' + IntToStr(fileid) + ';';
424      FQuery.ExecSQL;
# Line 509 | Line 429 | end;
429  
430  
431  
432 + function TAccess_OUP_ADB.GetDatLink(FileID, DatOffset: Integer): TDatLink;
433 + begin
434 +  Result := DatLinksManager.GetDatLink(FConnectionID, FileID, DatOffset);
435 +  FQuery.SQL.Text := 'SELECT target_id FROM linkmap WHERE src_id = ' + IntToStr(FileID) + ' and src_link_offset = ' + IntToStr(DatOffset) + ';';
436 +  FQuery.Open;
437 +  if FQuery.RecordCount > 0 then
438 +    Result.DestID := FQuery.FieldByName('target_id').AsInteger;
439 +  FQuery.Close;
440 + end;
441  
442 < function TOniDataADB.GetRawList(fileid: LongWord): TRawList;
442 >
443 > function TAccess_OUP_ADB.GetDatLinks(FileID: Integer): TDatLinkList;
444   var
445 <  i: LongWord;
445 >  i: Integer;
446 >  SrcOffset, DestID: Integer;
447 > begin
448 >  Result := DatLinksManager.GetDatLinks(FConnectionID, FileID);
449 >  if Length(Result) > 0 then
450 >  begin
451 >    FQuery.SQL.Text := 'SELECT src_link_offset, target_id FROM linkmap WHERE src_id = ' + IntToStr(FileID) + ' ORDER BY src_link_offset ASC;';
452 >    FQuery.Open;
453 >    if FQuery.RecordCount > 0 then
454 >    begin
455 >      repeat
456 >        SrcOffset := FQuery.FieldByName('src_link_offset').AsInteger;
457 >        DestID := FQuery.FieldByName('target_id').AsInteger;
458 >        for i := 0 to High(Result) do
459 >          if Result[i].SrcOffset = SrcOffset then
460 >            Break;
461 >        if i < Length(Result) then
462 >          Result[i].DestID := DestID
463 >        else
464 >          Result[i].DestID := -1;
465 >        FQuery.Next;
466 >      until FQuery.EOF;
467 >    end;
468 >    FQuery.Close;
469 >  end;
470 > end;
471 >
472 >
473 > function TAccess_OUP_ADB.GetRawList(FileID: Integer): TRawDataList;
474 > var
475 >  i: Integer;
476   begin
477    SetLength(Result, 0);
478    FQuery.SQL.Text := 'SELECT [src_link_offset],[size],[sep] FROM rawmap WHERE [src_id]=' +
# Line 524 | Line 484 | begin
484      SetLength(Result, FQuery.RecordCount);
485      i := 0;
486      repeat
487 <      Result[i].src_id     := fileid;
488 <      Result[i].src_offset := FQuery.FieldByName('src_link_offset').AsInteger;
489 <      Result[i].raw_addr   := 0;
490 <      Result[i].raw_size   := FQuery.FieldByName('size').AsInteger;
491 <      Result[i].loc_sep    := FQuery.FieldByName('sep').AsBoolean;
487 >      Result[i].SrcID     := fileid;
488 >      Result[i].SrcOffset := FQuery.FieldByName('src_link_offset').AsInteger;
489 >      Result[i].RawAddr   := 0;
490 >      Result[i].RawSize   := FQuery.FieldByName('size').AsInteger;
491 >      Result[i].LocSep    := FQuery.FieldByName('sep').AsBoolean;
492        Inc(i);
493        FQuery.Next;
494      until FQuery.EOF;
# Line 537 | Line 497 | begin
497   end;
498  
499  
500 + function TAccess_OUP_ADB.GetRawInfo(FileID, DatOffset: Integer): TRawDataInfo;
501 + var
502 +  i: Integer;
503 +  rawlist: TRawDataList;
504 + begin
505 +  rawlist := GetRawList(FileID);
506 +  if Length(rawlist) > 0 then
507 +  begin
508 +    for i := 0 to High(rawlist) do
509 +      if rawlist[i].SrcOffset = DatOffset then
510 +        Break;
511 +    if i < Length(rawlist) then
512 +      Result := rawlist[i]
513 +    else begin
514 +      Result.SrcID     := -1;
515 +      Result.SrcOffset := -1;
516 +      Result.RawAddr   := -1;
517 +      Result.RawSize   := -1;
518 +    end;
519 +  end;
520 + end;
521  
522  
523 < procedure TOniDataADB.LoadRawFile(fileid, dat_offset: LongWord; target: Pointer);
523 >
524 > procedure TAccess_OUP_ADB.LoadRawFile(FileID, DatOffset: Integer; var Target: TStream);
525   var
526    mem: TStream;
527 +  streampos: Integer;
528   begin
529 <  if fileid < Self.GetFilesCount then
529 >  if fileid < GetFileCount then
530    begin
531 +    if not Assigned(Target) then
532 +      Target := TMemoryStream.Create;
533 +    streampos := Target.Position;
534      FQuery.SQL.Text := 'SELECT data FROM rawmap WHERE (src_id=' +
535 <      IntToStr(fileid) + ') AND (src_link_offset=' + IntToStr(dat_offset) + ');';
535 >      IntToStr(FileID) + ') AND (src_link_offset=' + IntToStr(DatOffset) + ');';
536      FQuery.Open;
537      if FQuery.RecordCount > 0 then
538      begin
539        mem := FQuery.CreateBlobStream(FQuery.FieldByName('data'), bmRead);
540        mem.Seek(0, soFromBeginning);
541 <      mem.Read(target^, mem.size);
541 >      Target.CopyFrom(mem, mem.Size);
542        mem.Free;
543      end;
544      FQuery.Close;
545 +    Target.Seek(streampos, soFromBeginning);
546    end;
547   end;
548  
549  
550 <
564 <
565 < procedure TOniDataADB.UpdateRawFile(fileid, dat_offset: LongWord;
566 <  size: LongWord; target: Pointer);
550 > procedure TAccess_OUP_ADB.UpdateRawFile(FileID, DatOffset: Integer; Src: TStream);
551   var
552    MimeCoder: TStringFormat_MIME64;
553    mem: TMemoryStream;
554   begin
555 <  if fileid < Self.GetFilesCount then
555 >  if fileid < GetFileCount then
556    begin
557      mimecoder := TStringFormat_MIME64.Create;
558      mem := TMemoryStream.Create;
559 <    mem.Write(target^, size);
559 >    mem.CopyFrom(Src, Src.Size);
560      mem.Seek(0, soFromBeginning);
561      FQuery.SQL.Text := 'UPDATE rawmap SET data=MimeToBin("' + MimeCoder.StrTo(
562 <      mem.Memory, mem.Size) + '") WHERE (src_id=' + IntToStr(fileid) +
563 <      ') AND (src_link_offset=' + IntToStr(dat_offset) + ');';
562 >      mem.Memory, mem.Size) + '") WHERE (src_id=' + IntToStr(FileID) +
563 >      ') AND (src_link_offset=' + IntToStr(DatOffset) + ');';
564      FQuery.ExecSQL;
565      mem.Free;
566      mimecoder.Free;
# Line 586 | Line 570 | end;
570  
571  
572  
573 < procedure TOniDataADB.LoadRawFilePart(fileid, dat_offset: LongWord;
590 <  offset, size: LongWord; target: Pointer);
573 > procedure TAccess_OUP_ADB.LoadRawFilePart(FileID, DatOffset, Offset, Size: Integer; var Target: TStream);
574   var
592  Data: Tdata;
575    mem:  TMemoryStream;
576 +  streampos: Integer;
577   begin
578 <  if fileid < Self.GetFilesCount then
578 >  if fileid < GetFileCount then
579    begin
580 <    SetLength(Data, Self.GetRawInfo(fileid, dat_offset).raw_size);
581 <    Self.LoadRawFile(fileid, dat_offset, @Data[0]);
582 <    mem := TMemoryStream.Create;
583 <    mem.Write(Data[offset], size);
584 <    mem.Read(target^, size);
580 >    if not Assigned(Target) then
581 >      Target := TMemoryStream.Create;
582 >    streampos := Target.Position;
583 >    mem := nil;
584 >    LoadRawFile(FileID, DatOffset, TStream(mem));
585 >    mem.Seek(Offset, soFromBeginning);
586 >    Target.CopyFrom(mem, Size);
587      mem.Free;
588 +    Target.Seek(streampos, soFromBeginning);
589    end;
590   end;
591  
592  
593  
594  
595 < procedure TOniDataADB.UpdateRawFilePart(fileid, dat_offset: LongWord;
610 <  offset, size: LongWord; target: Pointer);
595 > procedure TAccess_OUP_ADB.UpdateRawFilePart(FileID, DatOffset, Offset, Size: Integer; Src: TStream);
596   var
597    MimeCoder: TStringFormat_MIME64;
598    mem:  TMemoryStream;
614  Data: Tdata;
599   begin
600 <  if fileid < Self.GetFilesCount then
600 >  if fileid < GetFileCount then
601    begin
602 <    SetLength(Data, Self.GetRawInfo(fileid, offset).raw_size);
603 <    Self.LoadRawFile(fileid, offset, @Data[0]);
620 <    mimecoder := TStringFormat_MIME64.Create;
621 <    mem := TMemoryStream.Create;
622 <    mem.Write(Data[0], Length(Data));
602 >    mem := nil;
603 >    LoadRawFile(fileid, offset, TStream(mem));
604      mem.Seek(offset, soFromBeginning);
605 <    mem.Write(target^, size);
605 >    mem.CopyFrom(Src, Size);
606      mem.Seek(0, soFromBeginning);
607 +    mimecoder := TStringFormat_MIME64.Create;
608      FQuery.SQL.Text := 'UPDATE rawmap SET data=MimeToBin("' + MimeCoder.StrTo(
609        mem.Memory, mem.Size) + '") WHERE (src_id=' + IntToStr(fileid) +
610 <      ') AND (src_link_offset=' + IntToStr(dat_offset) + ');';
610 >      ') AND (src_link_offset=' + IntToStr(DatOffset) + ');';
611      FQuery.ExecSQL;
612      mem.Free;
613      mimecoder.Free;
614    end;
615   end;
616  
617 < }
617 >
618  
619  
620   end.

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)