ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/oup/_old_/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 51 by alloc, Sat Dec 23 22:16:42 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 848 | Line 851 | begin
851    until FQuery.EOF;
852    FQuery.Close;
853  
854 +  UpdateListCache;
855 +
856    Result   := True;
857    FBackend := ODB_ADB;
858   end;
# Line 864 | Line 869 | end;
869  
870  
871  
872 + procedure TOniDataADB.UpdateListCache;
873 + var
874 +  i:     LongWord;
875 +  temps: String;
876 + begin
877 +  FQuery.SQL.Text := 'SELECT id,name,extension,[size],contenttype FROM datfiles ORDER BY id ASC;';
878 +  FQuery.Open;
879 +  if FQuery.RecordCount > 0 then
880 +  begin
881 +    FQuery.First;
882 +    SetLength(Fdat_files, FQuery.RecordCount);
883 +    i := 0;
884 +    repeat
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;
896 +      Inc(i);
897 +      FQuery.Next;
898 +    until FQuery.EOF;
899 +  end;
900 +  FQuery.Close;
901 +
902 +  SetLength(Fdat_extensionsmap, 0);
903 +  FQuery.SQL.Text :=
904 +    'SELECT extension,count(extension) AS x FROM datfiles GROUP BY extension ORDER BY extension ASC;';
905 +  FQuery.Open;
906 +  if FQuery.RecordCount > 0 then
907 +  begin
908 +    SetLength(Fdat_extensionsmap, FQuery.RecordCount);
909 +    i := 0;
910 +    repeat
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;
917 +      Inc(i);
918 +      FQuery.Next;
919 +    until FQuery.EOF;
920 +  end;
921 +  FQuery.Close;
922 + end;
923 +
924  
925   function TOniDataADB.GetFileInfo(fileid: LongWord): TFileInfo;
926 + var
927 +  i: Integer;
928   begin
929    if fileid < Self.GetFilesCount then
930    begin
931 <    FQuery.SQL.Text := 'SELECT * FROM datfiles WHERE id=' + IntToStr(
932 <      fileid) + ' ORDER BY id ASC;';
933 <    FQuery.Open;
934 <    if FQuery.RecordCount = 1 then
935 <    begin
936 <      FQuery.First;
937 <      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;
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
# Line 899 | Line 948 | end;
948   function TOniDataADB.GetFilesList(ext: String; pattern: String;
949    NoEmptyFiles: Boolean; sort: TSortType): TStringArray;
950   var
951 <  i:     LongWord;
952 <  where: String;
953 <  where_ext: String;
954 <  order: String;
955 < begin
956 <  where := '';
957 <  if Length(ext) > 0 then
958 <  begin
959 <    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
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 <      where := where + '(extension="' + ext + '")';
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 <  if Length(pattern) > 0 then
979 <  begin
980 <    if Length(where) > 0 then
981 <      where := where + ' AND ';
982 <    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
978 >
979 > begin
980 >  list := TStringList.Create;
981 >  list.Sorted := True;
982 >  for i := 0 to High(Fdat_files) do
983    begin
984 <    FQuery.First;
985 <    SetLength(Result, FQuery.RecordCount);
986 <    i := 0;
987 <    repeat
988 <      Result[i] := FormatNumber(FQuery.FieldByName('id').AsInteger, 5, '0') + '-' +
989 <        FQuery.FieldByName('name').AsString + '.' + FQuery.FieldByName('extension').AsString;
990 <      Inc(i);
991 <      FQuery.Next;
992 <    until FQuery.EOF;
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 <  FQuery.Close;
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;
1021   end;
1022  
1023  
# Line 971 | Line 1025 | end;
1025  
1026   function TOniDataADB.GetFilesCount: LongWord;
1027   begin
1028 <  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;
1028 >  Result := Length(Fdat_files);
1029   end;
1030  
1031  
# Line 990 | Line 1035 | function TOniDataADB.GetExtensionsList:
1035   var
1036    i: LongWord;
1037   begin
1038 <  SetLength(Result, 0);
1039 <  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
1038 >  SetLength(Result, Length(Fdat_extensionsmap));
1039 >  for i := 0 to High(Result) do
1040    begin
1041 <    SetLength(Result, FQuery.RecordCount);
1042 <    i := 0;
1043 <    repeat
1044 <      Result[i] := FQuery.FieldByName('extension').AsString + ' (' +
1045 <        IntToStr(FQuery.FieldByName('x').AsInteger) + ')';
1004 <      Inc(i);
1005 <      FQuery.Next;
1006 <    until FQuery.EOF;
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;
1008  FQuery.Close;
1047   end;
1048  
1049  
# Line 1138 | Line 1176 | begin
1176      mem.Write(Data[0], Length(Data));
1177      mem.Seek(0, soFromBeginning);
1178      FQuery.SQL.Text := 'UPDATE datfiles SET data=MimeToBin("' +
1179 <      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) + ';';
1181      FQuery.ExecSQL;
1182      mem.Free;
1183      mimecoder.Free;
1184    end;
1185 +  UpdateListCache;
1186   end;
1187  
1188  

Diff Legend

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