Changeset 51 for oup/current/Code


Ignore:
Timestamp:
Dec 23, 2006, 11:16:42 PM (18 years ago)
Author:
alloc
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • oup/current/Code/OniDataClass.pas

    r46 r51  
    22interface
    33uses Data, DataStructures, Classes, SysUtils, StrUtils,
    4   Dialogs, ABSDecUtil, ABSMain, DB;
     4  Dialogs, ABSDecUtil, ABSMain, DB, Windows;
    55
    66type
     
    103103    FDatabase: TABSDatabase;
    104104    FQuery:    TABSQuery;
     105    Fdat_files:    TFiles;
     106    Fdat_extensionsmap: TExtensionsMap;
    105107  protected
    106108  public
     
    108110    procedure Close; override;
    109111
     112    procedure UpdateListCache;
    110113    //      function GetDatLinks(srcid:LongWord):TDatLinks;
    111114    function GetFileInfo(fileid: LongWord): TFileInfo; override;
     
    849852  FQuery.Close;
    850853
     854  UpdateListCache;
     855
    851856  Result   := True;
    852857  FBackend := ODB_ADB;
     
    865870
    866871
    867 
    868 function TOniDataADB.GetFileInfo(fileid: LongWord): TFileInfo;
    869 begin
    870   if fileid < Self.GetFilesCount then
    871   begin
    872     FQuery.SQL.Text := 'SELECT * FROM datfiles WHERE id=' + IntToStr(
    873       fileid) + ' ORDER BY id ASC;';
    874     FQuery.Open;
    875     if FQuery.RecordCount = 1 then
    876     begin
    877       FQuery.First;
    878       Result.ID      := FQuery.FieldByName('id').AsInteger;
    879       Result.Name    := FQuery.FieldByName('name').AsString;
    880       Result.Extension := FQuery.FieldByName('extension').AsString;
    881       Result.FileName := FormatNumber(Result.ID, 5, '0') + '-' + Result.Name + '.' +
    882         Result.Extension;
    883       Result.Size    := FQuery.FieldByName('size').AsInteger;
    884       Result.FileType := HexToLong(FQuery.FieldByName('contenttype').AsString);
    885       Result.DatAddr := 0;
    886       Result.opened  := False;
    887     end;
    888     FQuery.Close;
    889   end
    890   else
    891   begin
    892     Result.ID := -1;
    893   end;
    894 end;
    895 
    896 
    897 
    898 
    899 function TOniDataADB.GetFilesList(ext: String; pattern: String;
    900   NoEmptyFiles: Boolean; sort: TSortType): TStringArray;
     872procedure TOniDataADB.UpdateListCache;
    901873var
    902874  i:     LongWord;
    903   where: String;
    904   where_ext: String;
    905   order: String;
    906 begin
    907   where := '';
    908   if Length(ext) > 0 then
    909   begin
    910     if Length(where) > 0 then
    911       where := where + ' AND ';
    912     if Pos(',', ext) > 0 then
    913     begin
    914       i := 1;
    915       where_ext := '';
    916       while i < Length(ext) do
    917       begin
    918         if Length(where_ext) > 0 then
    919           where_ext := where_ext + ' OR ';
    920         where_ext := where_ext + '(extension="' + MidStr(ext, i, 4) + '")';
    921         i := i + 5;
    922       end;
    923       where := where + '(' + where_ext + ')';
    924     end
    925     else
    926     begin
    927       where := where + '(extension="' + ext + '")';
    928     end;
    929   end;
    930   if Length(pattern) > 0 then
    931   begin
    932     if Length(where) > 0 then
    933       where := where + ' AND ';
    934     where := where + '(name LIKE "%' + pattern + '%")';
    935   end;
    936   if NoEmptyFiles then
    937   begin
    938     if Length(where) > 0 then
    939       where := where + ' AND ';
    940     where := where + '(contenttype<>2)';
    941   end;
    942   if Length(where) > 0 then
    943     where := ' WHERE ' + where;
    944   case sort of
    945     stIDAsc:    order := ' ORDER BY id ASC';
    946     stIDDesc:   order := ' ORDER BY id DESC';
    947     stNameAsc:  order := ' ORDER BY name ASC, id ASC';
    948     stNameDesc: order := ' ORDER BY name DESC, id ASC';
    949     stExtAsc:   order := ' ORDER BY extension ASC, id ASC';
    950     stExtDesc:  order := ' ORDER BY extension DESC, id ASC';
    951   end;
    952   FQuery.SQL.Text := 'SELECT id,name,extension FROM datfiles' + where + order + ';';
     875  temps: String;
     876begin
     877  FQuery.SQL.Text := 'SELECT id,name,extension,[size],contenttype FROM datfiles ORDER BY id ASC;';
    953878  FQuery.Open;
    954879  if FQuery.RecordCount > 0 then
    955880  begin
    956881    FQuery.First;
    957     SetLength(Result, FQuery.RecordCount);
     882    SetLength(Fdat_files, FQuery.RecordCount);
    958883    i := 0;
    959884    repeat
    960       Result[i] := FormatNumber(FQuery.FieldByName('id').AsInteger, 5, '0') + '-' +
    961         FQuery.FieldByName('name').AsString + '.' + FQuery.FieldByName('extension').AsString;
     885      Fdat_files[i].ID := FQuery.FieldByName('id').AsInteger;
     886      Fdat_files[i].Name := FQuery.FieldByName('name').AsString;
     887      Fdat_files[i].Extension := FQuery.FieldByName('extension').AsString;
     888      Fdat_files[i].FileName := FormatNumber(Fdat_files[i].ID, 5, '0') + '-' +
     889          Fdat_files[i].Name + '.' + Fdat_files[0].Extension;
     890      Fdat_files[i].FileNameHex := IntToHex(Fdat_files[i].ID, 4) + '-' +
     891          Fdat_files[i].Name + '.' + Fdat_files[0].Extension;
     892      Fdat_files[i].Size := FQuery.FieldByName('size').AsInteger;
     893      Fdat_files[i].FileType := HexToLong(FQuery.FieldByName('contenttype').AsString);
     894      Fdat_files[i].DatAddr := 0;
     895      Fdat_files[i].opened := False;
    962896      Inc(i);
    963897      FQuery.Next;
     
    965899  end;
    966900  FQuery.Close;
    967 end;
    968 
    969 
    970 
    971 
    972 function TOniDataADB.GetFilesCount: LongWord;
    973 begin
    974   FQuery.SQL.Text := 'SELECT Count(*) AS cnumber FROM datfiles;';
    975   FQuery.Open;
    976   if FQuery.RecordCount > 0 then
    977   begin
    978     FQuery.First;
    979     Result := FQuery.FieldByName('cnumber').AsInteger;
    980   end
    981   else
    982     Result := 0;
    983   FQuery.Close;
    984 end;
    985 
    986 
    987 
    988 
    989 function TOniDataADB.GetExtensionsList: TStringArray;
    990 var
    991   i: LongWord;
    992 begin
    993   SetLength(Result, 0);
     901
     902  SetLength(Fdat_extensionsmap, 0);
    994903  FQuery.SQL.Text :=
    995904    'SELECT extension,count(extension) AS x FROM datfiles GROUP BY extension ORDER BY extension ASC;';
     
    997906  if FQuery.RecordCount > 0 then
    998907  begin
    999     SetLength(Result, FQuery.RecordCount);
     908    SetLength(Fdat_extensionsmap, FQuery.RecordCount);
    1000909    i := 0;
    1001910    repeat
    1002       Result[i] := FQuery.FieldByName('extension').AsString + ' (' +
    1003         IntToStr(FQuery.FieldByName('x').AsInteger) + ')';
     911      temps := FQuery.FieldByName('extension').AsString[1];
     912      Fdat_extensionsmap[i].Extension[3] := temps[1];
     913      Fdat_extensionsmap[i].Extension[2] := temps[2];
     914      Fdat_extensionsmap[i].Extension[1] := temps[3];
     915      Fdat_extensionsmap[i].Extension[0] := temps[4];
     916      Fdat_extensionsmap[i].ExtCount := FQuery.FieldByName('x').AsInteger;
    1004917      Inc(i);
    1005918      FQuery.Next;
     
    1007920  end;
    1008921  FQuery.Close;
     922end;
     923
     924
     925function TOniDataADB.GetFileInfo(fileid: LongWord): TFileInfo;
     926var
     927  i: Integer;
     928begin
     929  if fileid < Self.GetFilesCount then
     930  begin
     931    for i := 0 to High(Fdat_files) do
     932      if Fdat_files[i].ID = fileid then
     933        Break;
     934    if i < Length(Fdat_files) then
     935      Result := Fdat_files[i]
     936    else
     937      Result.ID := -1;
     938  end
     939  else
     940  begin
     941    Result.ID := -1;
     942  end;
     943end;
     944
     945
     946
     947
     948function TOniDataADB.GetFilesList(ext: String; pattern: String;
     949  NoEmptyFiles: Boolean; sort: TSortType): TStringArray;
     950var
     951  i: LongWord;
     952  list: TStringList;
     953  id, name, extension: String;
     954  fields: TStrings;
     955
     956  procedure getfields;
     957  begin
     958     fields.CommaText := StringReplace(AnsiQuotedStr(list.Strings[i], '"'), ';', '","', [rfReplaceAll]);
     959    if sort in [stIDAsc, stIDDesc] then
     960    begin
     961      id := fields.Strings[0];
     962      name := fields.Strings[1];
     963      extension := fields.Strings[2];
     964    end;
     965    if sort in [stNameAsc, stNameDesc] then
     966    begin
     967      id := fields.Strings[1];
     968      name := fields.Strings[0];
     969      extension := fields.Strings[2];
     970    end;
     971    if sort in [stExtAsc, stExtDesc] then
     972    begin
     973      id := fields.Strings[1];
     974      name := fields.Strings[2];
     975      extension := fields.Strings[0];
     976    end;
     977  end;
     978
     979begin
     980  list := TStringList.Create;
     981  list.Sorted := True;
     982  for i := 0 to High(Fdat_files) do
     983  begin
     984    if ((Length(ext) = 0) or (Pos(Fdat_files[i].Extension, ext) > 0)) and
     985      ((Length(pattern) = 0) or
     986      (Pos(UpperCase(pattern), UpperCase(Fdat_files[i].Name)) > 0)) then
     987    begin
     988      if (NoEmptyFiles = False) or ((Fdat_files[i].FileType and $02) = 0) then
     989      begin
     990        if AppSettings.FilenumbersAsHex then
     991          id := IntToHex(Fdat_files[i].ID, 4)
     992        else
     993          id := FormatNumber(Fdat_files[i].ID, 5, '0');
     994        name := Fdat_files[i].Name;
     995        extension := Fdat_files[i].Extension;
     996
     997        case sort of
     998          stIDAsc, stIDDesc:     list.Add(id + ';' + name + ';' + extension);
     999          stNameAsc, stNameDesc: list.Add(name + ';' + id + ';' + extension);
     1000          stExtAsc, stExtDesc:   list.Add(extension + ';' + id + ';' + name);
     1001        end;
     1002      end;
     1003    end;
     1004  end;
     1005  SetLength(Result, list.Count);
     1006  fields := TStringList.Create;
     1007  if sort in [stIDAsc, stNameAsc, stExtAsc] then
     1008    for i := 0 to list.Count - 1 do
     1009    begin
     1010      getfields;
     1011      Result[i] := id + '-' + name + '.' + extension;
     1012    end
     1013  else
     1014    for i := list.Count - 1 downto 0 do
     1015    begin
     1016      getfields;
     1017      Result[list.Count - i - 1] := id + '-' + name + '.' + extension;
     1018    end;
     1019  list.Free;
     1020  fields.Free;
     1021end;
     1022
     1023
     1024
     1025
     1026function TOniDataADB.GetFilesCount: LongWord;
     1027begin
     1028  Result := Length(Fdat_files);
     1029end;
     1030
     1031
     1032
     1033
     1034function TOniDataADB.GetExtensionsList: TStringArray;
     1035var
     1036  i: LongWord;
     1037begin
     1038  SetLength(Result, Length(Fdat_extensionsmap));
     1039  for i := 0 to High(Result) do
     1040  begin
     1041    with Fdat_extensionsmap[i] do
     1042    begin
     1043      Result[i] := Extension[3] + Extension[2] + Extension[1] + Extension[0] +
     1044        ' (' + IntToStr(ExtCount) + ')';
     1045    end;
     1046  end;
    10091047end;
    10101048
     
    11391177    mem.Seek(0, soFromBeginning);
    11401178    FQuery.SQL.Text := 'UPDATE datfiles SET data=MimeToBin("' +
    1141       MimeCoder.StrTo(mem.Memory, mem.Size) + '") WHERE id=' + IntToStr(fileid) + ';';
     1179      MimeCoder.StrTo(mem.Memory, mem.Size) + '"), size=' + IntToStr(mem.Size) +
     1180      ' WHERE id=' + IntToStr(fileid) + ';';
    11421181    FQuery.ExecSQL;
    11431182    mem.Free;
    11441183    mimecoder.Free;
    11451184  end;
     1185  UpdateListCache;
    11461186end;
    11471187
Note: See TracChangeset for help on using the changeset viewer.