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

Diff Legend

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