1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| //----------------------------------------------------------------------------------------------------------------
function SynchronizeTableWithIndexOutOfDate(TableToRepare: TTable): Boolean;
var
PathDB, PathDBSansIndex, PathDBModele : String;
BackUpPathDBSansIndex : String;
SearchItem: TSearchRec;
TableWithoutIndex: TTable;
iField: Integer;
Compteur: Integer;
begin
Result := False;
try
PathDB := SetSlash(GetPathAlias());
if DirectoryExists(PathDB) then begin
// Suppression d'eventuel Fichier Temporaire
PathDBSansIndex := SetSlash(PathDB + DATABASE_TO_REPARE_DIRECTORY);
try
if FindFirst(PathDBSansIndex + '*', faAnyFile - faDirectory, SearchItem ) = 0 then begin
repeat
if (SearchItem.Name <> '.') and (SearchItem.Name <> '..') then begin
if DeleteFile( PChar(PathDBSansIndex + ExtractFileName(SearchItem.Name))) then begin
Beep;
end;
end;
until not ( FindNext(SearchItem) = 0 ); // Continuer tant que FindNext renvoi 0
end;
finally
FindClose(SearchItem);
end;
// Recopier les Fichiers de cette Table en dehors de la Base de Données
try
if FindFirst(PathDB + ChangeFileExt(TableToRepare.TableName, '*'), faAnyFile - faDirectory, SearchItem ) = 0 then begin
repeat
if (SearchItem.Name <> '.') and (SearchItem.Name <> '..') then begin
if MoveFile( PChar(PathDB + ExtractFileName(SearchItem.Name)), PChar(PathDBSansIndex + ExtractFileName(SearchItem.Name)) ) then begin
if UpperCase(ExtractFileExt(SearchItem.Name)) <> '.DB' then begin
RenameFile( PChar(PathDBSansIndex + ExtractFileName(SearchItem.Name)), PChar(PathDBSansIndex + ExtractFileName(SearchItem.Name) + '.stwiood'));
end;
end;
end;
until not ( FindNext(SearchItem) = 0 ); // Continuer tant que FindNext renvoi 0
end;
finally
FindClose(SearchItem);
end;
// Utiliser des fichiers Vierges
PathDBModele := SetSlash(PathDB + DATABASE_EMPTY_DIRECTORY );
try
if FindFirst(PathDBModele + ChangeFileExt(TableToRepare.TableName, '*'), faAnyFile - faDirectory, SearchItem ) = 0 then begin
repeat
if (SearchItem.Name <> '.') and (SearchItem.Name <> '..') then begin
if CopyFile( PChar(PathDBModele + ExtractFileName(SearchItem.Name)), PChar(PathDB + ExtractFileName(SearchItem.Name)), True ) then begin
SetFileAttributes( PChar(PathDB + ExtractFileName(SearchItem.Name)), FILE_ATTRIBUTE_NORMAL);
beep;
end;
end;
until not ( FindNext(SearchItem) = 0 ); // Continuer tant que FindNext renvoi 0
end;
finally
FindClose(SearchItem);
end;
// Création d'une Table Temporaire pour Lire les Données SANS Index
TableWithoutIndex := TTable.Create(DMServeur);
try
TableWithoutIndex.DatabaseName := PathDBSansIndex;
TableWithoutIndex.TableName := TableToRepare.TableName;
try
// Ouverture de la Table Vierge
TableToRepare.Close();
TableToRepare.Exclusive := True;
TableToRepare.EmptyTable();
TableToRepare.Open();
TableToRepare.First();
try
// Récupération des Données
try
TableWithoutIndex.Open();
TableWithoutIndex.IndexName := '';
TableWithoutIndex.First();
while not TableWithoutIndex.Eof do begin
TableToRepare.Append();
for iField := 0 to TableWithoutIndex.FieldCount - 1 do begin
TableToRepare.Fields[iField].AsVariant := TableWithoutIndex.Fields[iField].AsVariant;
end;
TableToRepare.Post();
TableWithoutIndex.Next();
end;
Result := True;
except
FichierLog('REPARATION', 'Except STWIOOD, Copy', Exception(ExceptObject).Message);
Result := False;
end;
finally
TableToRepare.Close();
TableToRepare.Exclusive := False;
end;
except
FichierLog('REPARATION', 'Except STWIOOD, Open', Exception(ExceptObject).Message);
Result := False;
end;
finally
TableWithoutIndex.Close();
TableWithoutIndex.Free();
end;
try
// Recopier les Fichiers de cette Table sans Index dans un BackUp
BackUpPathDBSansIndex := PathDBSansIndex + 'BackUp\DBOutOfDate\Table ' + FormatDateTime('yyyy-mm-dd (dddd d mmmm)',Now()) + '\';
Compteur := 1;
while DirectoryExists( BackUpPathDBSansIndex + 'No ' + IntToStr(Compteur) ) do begin
Compteur := Compteur + 1;
end;
BackUpPathDBSansIndex := BackUpPathDBSansIndex + 'No ' + IntToStr(Compteur) + '\';
if not DirectoryExists(BackUpPathDBSansIndex) then begin
ForceDirectories(BackUpPathDBSansIndex);
end;
try
if FindFirst(PathDBSansIndex + ChangeFileExt(TableToRepare.TableName, '*'), faAnyFile - faDirectory, SearchItem ) = 0 then begin
repeat
if (SearchItem.Name <> '.') and (SearchItem.Name <> '..') then begin
if MoveFile( PChar(PathDBSansIndex + ExtractFileName(SearchItem.Name)), PChar(BackUpPathDBSansIndex + ExtractFileName(SearchItem.Name)) ) then begin
if UpperCase(ExtractFileExt(SearchItem.Name)) = '.STWIOOD' then begin
// Suppression de l'extension spéciale sans Index
RenameFile( PChar(BackUpPathDBSansIndex + ExtractFileName(SearchItem.Name)), PChar(BackUpPathDBSansIndex + ChangeFileExt(ExtractFileName(SearchItem.Name),'')));
end;
end;
end;
until not ( FindNext(SearchItem) = 0 ); // Continuer tant que FindNext renvoi 0
end;
finally
FindClose(SearchItem);
end;
except
FichierLog('REPARATION', 'Except STWIOOD, BackUP', Exception(ExceptObject).Message);
end;
end;
except
FichierLog('REPARATION', 'Except STWIOOD, Global', Exception(ExceptObject).Message);
Result := False;
end;
end; |
Partager