ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/oup/releases/0.33a/Code/OniDataClass.pas
(Generate patch)

Comparing oup/current/Code/OniDataClass.pas (file contents):
Revision 46 by alloc, Sat Dec 23 16:26:43 2006 UTC vs.
Revision 56 by alloc, Mon Dec 25 18:36:41 2006 UTC

# Line 1 | Line 1
1   unit OniDataClass;
2   interface
3   uses Data, DataStructures, Classes, SysUtils, StrUtils,
4 <  Dialogs, ABSDecUtil, ABSMain, DB;
4 >  Dialogs, ABSDecUtil, ABSMain, DB, Windows;
5  
6   type
7    TOniData = class
# Line 102 | Line 102 | type
102    private
103      FDatabase: TABSDatabase;
104      FQuery:    TABSQuery;
105 +    Fdat_files:    TFiles;
106 +    Fdat_extensionsmap: TExtensionsMap;
107    protected
108    public
109      constructor Create(OLDBFilename: String; var Result: Boolean); override;
110      procedure Close; override;
111  
112 +    procedure UpdateListCache;
113      //      function GetDatLinks(srcid:LongWord):TDatLinks;
114      function GetFileInfo(fileid: LongWord): TFileInfo; override;
115      function GetFilesList(ext: String; pattern: String;
# Line 408 | Line 411 | begin
411      end;
412    end;
413    SetLength(Result, list.Count);
414 <  fields := TStringList.Create;
415 <  if sort in [stIDAsc, stNameAsc, stExtAsc] then
416 <    for i := 0 to list.Count - 1 do
417 <    begin
418 <      getfields;
419 <      Result[i] := id + '-' + name + '.' + extension;
420 <    end
421 <  else
422 <    for i := list.Count - 1 downto 0 do
423 <    begin
424 <      getfields;
425 <      Result[list.Count - i - 1] := id + '-' + name + '.' + extension;
426 <    end;
414 >  if Length(Result) > 0 then
415 >  begin
416 >    fields := TStringList.Create;
417 >    if sort in [stIDAsc, stNameAsc, stExtAsc] then
418 >      for i := 0 to list.Count - 1 do
419 >      begin
420 >        getfields;
421 >        Result[i] := id + '-' + name + '.' + extension;
422 >      end
423 >    else
424 >      for i := list.Count - 1 downto 0 do
425 >      begin
426 >        getfields;
427 >        Result[list.Count - i - 1] := id + '-' + name + '.' + extension;
428 >      end;
429 >    fields.Free;
430 >  end;
431    list.Free;
425  fields.Free;
432   end;
433  
434  
# Line 848 | Line 854 | begin
854    until FQuery.EOF;
855    FQuery.Close;
856  
857 +  UpdateListCache;
858 +
859    Result   := True;
860    FBackend := ODB_ADB;
861   end;
# Line 864 | Line 872 | end;
872  
873  
874  
875 + procedure TOniDataADB.UpdateListCache;
876 + var
877 +  i:     LongWord;
878 +  temps: String;
879 + begin
880 +  FQuery.SQL.Text := 'SELECT id,name,extension,[size],contenttype FROM datfiles ORDER BY id ASC;';
881 +  FQuery.Open;
882 +  if FQuery.RecordCount > 0 then
883 +  begin
884 +    FQuery.First;
885 +    SetLength(Fdat_files, FQuery.RecordCount);
886 +    i := 0;
887 +    repeat
888 +      Fdat_files[i].ID := FQuery.FieldByName('id').AsInteger;
889 +      Fdat_files[i].Name := FQuery.FieldByName('name').AsString;
890 +      Fdat_files[i].Extension := FQuery.FieldByName('extension').AsString;
891 +      Fdat_files[i].FileName := FormatNumber(Fdat_files[i].ID, 5, '0') + '-' +
892 +          Fdat_files[i].Name + '.' + Fdat_files[0].Extension;
893 +      Fdat_files[i].FileNameHex := IntToHex(Fdat_files[i].ID, 4) + '-' +
894 +          Fdat_files[i].Name + '.' + Fdat_files[0].Extension;
895 +      Fdat_files[i].Size := FQuery.FieldByName('size').AsInteger;
896 +      Fdat_files[i].FileType := HexToLong(FQuery.FieldByName('contenttype').AsString);
897 +      Fdat_files[i].DatAddr := 0;
898 +      Fdat_files[i].opened := False;
899 +      Inc(i);
900 +      FQuery.Next;
901 +    until FQuery.EOF;
902 +  end;
903 +  FQuery.Close;
904 +
905 +  SetLength(Fdat_extensionsmap, 0);
906 +  FQuery.SQL.Text :=
907 +    'SELECT extension,count(extension) AS x FROM datfiles GROUP BY extension ORDER BY extension ASC;';
908 +  FQuery.Open;
909 +  if FQuery.RecordCount > 0 then
910 +  begin
911 +    SetLength(Fdat_extensionsmap, FQuery.RecordCount);
912 +    i := 0;
913 +    repeat
914 +      temps := FQuery.FieldByName('extension').AsString[1];
915 +      Fdat_extensionsmap[i].Extension[3] := temps[1];
916 +      Fdat_extensionsmap[i].Extension[2] := temps[2];
917 +      Fdat_extensionsmap[i].Extension[1] := temps[3];
918 +      Fdat_extensionsmap[i].Extension[0] := temps[4];
919 +      Fdat_extensionsmap[i].ExtCount := FQuery.FieldByName('x').AsInteger;
920 +      Inc(i);
921 +      FQuery.Next;
922 +    until FQuery.EOF;
923 +  end;
924 +  FQuery.Close;
925 + end;
926 +
927  
928   function TOniDataADB.GetFileInfo(fileid: LongWord): TFileInfo;
929 + var
930 +  i: Integer;
931   begin
932    if fileid < Self.GetFilesCount then
933    begin
934 <    FQuery.SQL.Text := 'SELECT * FROM datfiles WHERE id=' + IntToStr(
935 <      fileid) + ' ORDER BY id ASC;';
936 <    FQuery.Open;
937 <    if FQuery.RecordCount = 1 then
938 <    begin
939 <      FQuery.First;
940 <      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;
934 >    for i := 0 to High(Fdat_files) do
935 >      if Fdat_files[i].ID = fileid then
936 >        Break;
937 >    if i < Length(Fdat_files) then
938 >      Result := Fdat_files[i]
939 >    else
940 >      Result.ID := -1;
941    end
942    else
943    begin
# Line 899 | Line 951 | end;
951   function TOniDataADB.GetFilesList(ext: String; pattern: String;
952    NoEmptyFiles: Boolean; sort: TSortType): TStringArray;
953   var
954 <  i:     LongWord;
955 <  where: String;
956 <  where_ext: String;
957 <  order: String;
958 < begin
959 <  where := '';
960 <  if Length(ext) > 0 then
961 <  begin
962 <    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
954 >  i: LongWord;
955 >  list: TStringList;
956 >  id, name, extension: String;
957 >  fields: TStrings;
958 >
959 >  procedure getfields;
960 >  begin
961 >     fields.CommaText := StringReplace(AnsiQuotedStr(list.Strings[i], '"'), ';', '","', [rfReplaceAll]);
962 >    if sort in [stIDAsc, stIDDesc] then
963      begin
964 <      where := where + '(extension="' + ext + '")';
964 >      id := fields.Strings[0];
965 >      name := fields.Strings[1];
966 >      extension := fields.Strings[2];
967 >    end;
968 >    if sort in [stNameAsc, stNameDesc] then
969 >    begin
970 >      id := fields.Strings[1];
971 >      name := fields.Strings[0];
972 >      extension := fields.Strings[2];
973 >    end;
974 >    if sort in [stExtAsc, stExtDesc] then
975 >    begin
976 >      id := fields.Strings[1];
977 >      name := fields.Strings[2];
978 >      extension := fields.Strings[0];
979      end;
980    end;
981 <  if Length(pattern) > 0 then
982 <  begin
983 <    if Length(where) > 0 then
984 <      where := where + ' AND ';
985 <    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 + ';';
953 <  FQuery.Open;
954 <  if FQuery.RecordCount > 0 then
981 >
982 > begin
983 >  list := TStringList.Create;
984 >  list.Sorted := True;
985 >  for i := 0 to High(Fdat_files) do
986    begin
987 <    FQuery.First;
988 <    SetLength(Result, FQuery.RecordCount);
989 <    i := 0;
990 <    repeat
991 <      Result[i] := FormatNumber(FQuery.FieldByName('id').AsInteger, 5, '0') + '-' +
992 <        FQuery.FieldByName('name').AsString + '.' + FQuery.FieldByName('extension').AsString;
993 <      Inc(i);
994 <      FQuery.Next;
995 <    until FQuery.EOF;
987 >    if ((Length(ext) = 0) or (Pos(Fdat_files[i].Extension, ext) > 0)) and
988 >      ((Length(pattern) = 0) or
989 >      (Pos(UpperCase(pattern), UpperCase(Fdat_files[i].Name)) > 0)) then
990 >    begin
991 >      if (NoEmptyFiles = False) or ((Fdat_files[i].FileType and $02) = 0) then
992 >      begin
993 >        if AppSettings.FilenumbersAsHex then
994 >          id := IntToHex(Fdat_files[i].ID, 4)
995 >        else
996 >          id := FormatNumber(Fdat_files[i].ID, 5, '0');
997 >        name := Fdat_files[i].Name;
998 >        extension := Fdat_files[i].Extension;
999 >
1000 >        case sort of
1001 >          stIDAsc, stIDDesc:     list.Add(id + ';' + name + ';' + extension);
1002 >          stNameAsc, stNameDesc: list.Add(name + ';' + id + ';' + extension);
1003 >          stExtAsc, stExtDesc:   list.Add(extension + ';' + id + ';' + name);
1004 >        end;
1005 >      end;
1006 >    end;
1007    end;
1008 <  FQuery.Close;
1008 >  SetLength(Result, list.Count);
1009 >  fields := TStringList.Create;
1010 >  if sort in [stIDAsc, stNameAsc, stExtAsc] then
1011 >    for i := 0 to list.Count - 1 do
1012 >    begin
1013 >      getfields;
1014 >      Result[i] := id + '-' + name + '.' + extension;
1015 >    end
1016 >  else
1017 >    for i := list.Count - 1 downto 0 do
1018 >    begin
1019 >      getfields;
1020 >      Result[list.Count - i - 1] := id + '-' + name + '.' + extension;
1021 >    end;
1022 >  list.Free;
1023 >  fields.Free;
1024   end;
1025  
1026  
# Line 971 | Line 1028 | end;
1028  
1029   function TOniDataADB.GetFilesCount: LongWord;
1030   begin
1031 <  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;
1031 >  Result := Length(Fdat_files);
1032   end;
1033  
1034  
# Line 990 | Line 1038 | function TOniDataADB.GetExtensionsList:
1038   var
1039    i: LongWord;
1040   begin
1041 <  SetLength(Result, 0);
1042 <  FQuery.SQL.Text :=
995 <    'SELECT extension,count(extension) AS x FROM datfiles GROUP BY extension ORDER BY extension ASC;';
996 <  FQuery.Open;
997 <  if FQuery.RecordCount > 0 then
1041 >  SetLength(Result, Length(Fdat_extensionsmap));
1042 >  for i := 0 to High(Result) do
1043    begin
1044 <    SetLength(Result, FQuery.RecordCount);
1045 <    i := 0;
1046 <    repeat
1047 <      Result[i] := FQuery.FieldByName('extension').AsString + ' (' +
1048 <        IntToStr(FQuery.FieldByName('x').AsInteger) + ')';
1004 <      Inc(i);
1005 <      FQuery.Next;
1006 <    until FQuery.EOF;
1044 >    with Fdat_extensionsmap[i] do
1045 >    begin
1046 >      Result[i] := Extension[3] + Extension[2] + Extension[1] + Extension[0] +
1047 >        ' (' + IntToStr(ExtCount) + ')';
1048 >    end;
1049    end;
1008  FQuery.Close;
1050   end;
1051  
1052  
# Line 1138 | Line 1179 | begin
1179      mem.Write(Data[0], Length(Data));
1180      mem.Seek(0, soFromBeginning);
1181      FQuery.SQL.Text := 'UPDATE datfiles SET data=MimeToBin("' +
1182 <      MimeCoder.StrTo(mem.Memory, mem.Size) + '") WHERE id=' + IntToStr(fileid) + ';';
1182 >      MimeCoder.StrTo(mem.Memory, mem.Size) + '"), size=' + IntToStr(mem.Size) +
1183 >      ' WHERE id=' + IntToStr(fileid) + ';';
1184      FQuery.ExecSQL;
1185      mem.Free;
1186      mimecoder.Free;
1187    end;
1188 +  UpdateListCache;
1189   end;
1190  
1191  

Diff Legend

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