HisRestorePointDM.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. unit HisRestorePointDM;
  2. interface
  3. uses
  4. SysUtils, Classes, DB, DBClient, Windows, Provider, ADODB;
  5. type
  6. TDMHisRestorePoint = class(TDataModule)
  7. cdsOrgHisPoint: TClientDataSet;
  8. cdsUnFixedPoint: TClientDataSet;
  9. cdsFixedPoint: TClientDataSet;
  10. cdsOrgHisPointID: TIntegerField;
  11. cdsOrgHisPointFixed: TBooleanField;
  12. cdsOrgHisPointFileName: TWideStringField;
  13. cdsOrgHisPointFileDir: TWideStringField;
  14. cdsUnFixedPointID: TIntegerField;
  15. cdsUnFixedPointFixed: TBooleanField;
  16. cdsUnFixedPointFileName: TWideStringField;
  17. cdsUnFixedPointFileDir: TWideStringField;
  18. cdsFixedPointID: TIntegerField;
  19. cdsFixedPointFixed: TBooleanField;
  20. cdsFixedPointFileName: TWideStringField;
  21. cdsFixedPointFileDir: TWideStringField;
  22. cdsUnFixedPointIsExists: TWideStringField;
  23. cdsFixedPointIsExists: TWideStringField;
  24. cdsOrgHisPointCreateTime: TDateTimeField;
  25. cdsUnFixedPointCreateTime: TDateTimeField;
  26. cdsFixedPointCreateTime: TDateTimeField;
  27. atHisRestorePoint: TADOTable;
  28. dspHisRestorePoint: TDataSetProvider;
  29. procedure DataModuleCreate(Sender: TObject);
  30. procedure cdsUnFixedPointBeforeDelete(DataSet: TDataSet);
  31. private
  32. FFileDir: string;
  33. FProjectPath: string;
  34. FProjectName: string;
  35. function NewPointName(AID: Integer): string;
  36. function GetPointID(var aNew: Boolean; aFixed: Boolean): Integer;
  37. procedure DeleteCurFile(aFixed: Boolean);
  38. procedure SetProjectPath(const Value: string);
  39. function GetConnection: TADOConnection;
  40. procedure SetConnection(const Value: TADOConnection);
  41. procedure SetProjectName(const Value: string);
  42. procedure Save;
  43. // Added by GiLi 2012-4-23 获取当前程序路径
  44. function GetApplicationPath: string;
  45. public
  46. procedure SavePoint(aFixed: Boolean = False);
  47. procedure RefreshPoints;
  48. function GetCurPointPath(aFixed: Boolean): string;
  49. procedure DeleteCurPoint(aFixed: Boolean);
  50. procedure DeleteAllPoints;
  51. property ProjectPath: string read FProjectPath write SetProjectPath;
  52. property ProjectName: string read FProjectName write SetProjectName;
  53. property Connection: TADOConnection read GetConnection write SetConnection;
  54. end;
  55. implementation
  56. {$R *.dfm}
  57. uses ConstVarUnit, ConstMethodUnit;
  58. { TDMHisRestorePoint }
  59. procedure TDMHisRestorePoint.DeleteCurFile(aFixed: Boolean);
  60. var
  61. strPath: string;
  62. begin
  63. strPath := GetCurPointPath(aFixed);
  64. if FileExists(strPath) then
  65. Windows.DeleteFile(PChar(strPath));
  66. end;
  67. procedure TDMHisRestorePoint.DeleteCurPoint(aFixed: Boolean);
  68. begin
  69. DeleteCurFile(aFixed);
  70. if aFixed then
  71. cdsFixedPoint.Delete
  72. else
  73. cdsUnFixedPoint.Delete;
  74. end;
  75. function TDMHisRestorePoint.GetCurPointPath(aFixed: Boolean): string;
  76. begin
  77. if aFixed then
  78. Result := cdsFixedPointFileDir.Value + cdsFixedPointFileName.Value
  79. else
  80. Result := cdsUnFixedPointFileDir.Value + cdsUnFixedPointFileName.Value;
  81. end;
  82. procedure TDMHisRestorePoint.RefreshPoints;
  83. var
  84. strFileName: string;
  85. begin
  86. cdsUnFixedPoint.EmptyDataSet;
  87. cdsFixedPoint.EmptyDataSet;
  88. cdsOrgHisPoint.First;
  89. while not cdsOrgHisPoint.Eof do
  90. begin
  91. if cdsOrgHisPointFixed.Value then
  92. begin
  93. cdsFixedPoint.Append;
  94. cdsFixedPointID.Value := cdsOrgHisPointID.Value;
  95. cdsFixedPointFixed.Value := True;
  96. cdsFixedPointFileName.Value := cdsOrgHisPointFileName.Value;
  97. cdsFixedPointFileDir.Value := cdsOrgHisPointFileDir.Value;
  98. cdsFixedPointCreateTime.Value := cdsOrgHisPointCreateTime.Value;
  99. strFileName := cdsOrgHisPointFileDir.Value + cdsOrgHisPointFileName.Value;
  100. if FileExists(strFileName) then
  101. cdsFixedPointIsExists.Value := '存在'
  102. else
  103. cdsFixedPointIsExists.Value := '不存在';
  104. cdsFixedPoint.Post;
  105. end
  106. else
  107. begin
  108. cdsUnFixedPoint.Append;
  109. cdsUnFixedPointID.Value := cdsOrgHisPointID.Value;
  110. cdsUnFixedPointFixed.Value := False;
  111. cdsUnFixedPointCreateTime.Value := cdsOrgHisPointCreateTime.Value;
  112. cdsUnFixedPointFileName.Value := cdsOrgHisPointFileName.Value;
  113. cdsUnFixedPointFileDir.Value := cdsOrgHisPointFileDir.Value;
  114. strFileName := cdsOrgHisPointFileDir.Value + cdsOrgHisPointFileName.Value;
  115. if FileExists(strFileName) then
  116. cdsUnFixedPointIsExists.Value := '存在'
  117. else
  118. cdsUnFixedPointIsExists.Value := '不存在';
  119. cdsUnFixedPoint.Post;
  120. end;
  121. cdsOrgHisPoint.Next;
  122. end;
  123. end;
  124. procedure TDMHisRestorePoint.SavePoint(aFixed: Boolean);
  125. var
  126. iID: Integer;
  127. bNew: Boolean;
  128. strFile: string;
  129. begin
  130. iID := GetPointID(bNew, aFixed);
  131. if bNew then
  132. begin
  133. cdsOrgHisPoint.Append;
  134. cdsOrgHisPointID.Value := iID;
  135. cdsOrgHisPointFixed.Value := aFixed;
  136. cdsOrgHisPointCreateTime.Value := Now;
  137. cdsOrgHisPointFileName.Value := NewPointName(iID);
  138. cdsOrgHisPointFileDir.Value := FFileDir;
  139. cdsOrgHisPoint.Post;
  140. end
  141. else
  142. begin
  143. if cdsOrgHisPoint.Locate(SID, iID, []) then
  144. begin
  145. cdsOrgHisPoint.Edit;
  146. cdsOrgHisPointCreateTime.Value := Now;
  147. cdsOrgHisPointFileName.Value := NewPointName(iID);
  148. cdsOrgHisPointFileDir.Value := FFileDir;
  149. cdsOrgHisPoint.Post;
  150. end;
  151. end;
  152. Save;
  153. strFile := cdsOrgHisPointFileDir.Value + cdsOrgHisPointFileName.Value;
  154. if not DirectoryExists(FFileDir) then
  155. ForceDirectories(FFileDir);
  156. CopyFile(PChar(FProjectPath), PChar(strFile), False);
  157. end;
  158. procedure TDMHisRestorePoint.DataModuleCreate(Sender: TObject);
  159. begin
  160. cdsUnFixedPoint.IndexFieldNames := sCreateTime;
  161. cdsFixedPoint.IndexFieldNames := sCreateTime;
  162. FFileDir := ExtractFilePath(ParamStr(0));
  163. end;
  164. function TDMHisRestorePoint.GetPointID(var aNew: Boolean; aFixed: Boolean): Integer;
  165. var
  166. bFlag: Boolean;
  167. iRefCount, iFirstID: Integer;
  168. begin
  169. bFlag := False;
  170. iRefCount := 0;
  171. if aFixed then Result := 5 else Result := 0;
  172. cdsOrgHisPoint.First;
  173. while not cdsOrgHisPoint.Eof do
  174. begin
  175. if cdsOrgHisPointFixed.Value = aFixed then
  176. begin
  177. if not bFlag then
  178. begin
  179. iFirstID := cdsOrgHisPointID.Value;
  180. bFlag := True;
  181. end;
  182. Result := cdsOrgHisPointID.Value;
  183. Inc(iRefCount);
  184. end;
  185. cdsOrgHisPoint.Next;
  186. end;
  187. if iRefCount < MaxRPointCount then
  188. begin
  189. aNew := True;
  190. Result := Result + 1;
  191. end
  192. else
  193. begin
  194. aNew := False;
  195. Result := iFirstID;
  196. end;
  197. end;
  198. function TDMHisRestorePoint.NewPointName(AID: Integer): string;
  199. var
  200. strName: string;
  201. begin
  202. // DateTimeToString(strName, 'yyyy.m.d.h.m.s', Now);
  203. // strName := FormatDateTime('yyyy.m.d.h.m.s', Now);
  204. Result := Format('%s.bak', [IntToStr(AID)]);
  205. end;
  206. procedure TDMHisRestorePoint.SetProjectPath(const Value: string);
  207. begin
  208. FProjectPath := Value;
  209. // FProjectName := ExtractFileNameWithoutExt(FProjectPath);
  210. // FFileDir := Format('%s%s\%s\', [FFileDir, sBackUpFolder, FProjectName]);
  211. end;
  212. procedure TDMHisRestorePoint.cdsUnFixedPointBeforeDelete(
  213. DataSet: TDataSet);
  214. begin
  215. if cdsOrgHisPoint.Locate(SID, DataSet.FieldByName(SID).AsInteger, []) then
  216. cdsOrgHisPoint.Delete;
  217. end;
  218. function TDMHisRestorePoint.GetConnection: TADOConnection;
  219. begin
  220. Result := atHisRestorePoint.Connection;
  221. end;
  222. procedure TDMHisRestorePoint.SetConnection(const Value: TADOConnection);
  223. begin
  224. atHisRestorePoint.Connection := Value;
  225. if Assigned(Value) then
  226. begin
  227. cdsOrgHisPoint.Active := True;
  228. cdsOrgHisPoint.IndexFieldNames := 'CreateTime';
  229. cdsUnFixedPoint.Active := True;
  230. cdsFixedPoint.Active := True;
  231. end;
  232. end;
  233. procedure TDMHisRestorePoint.Save;
  234. begin
  235. cdsOrgHisPoint.ApplyUpdates(0);
  236. end;
  237. procedure TDMHisRestorePoint.SetProjectName(const Value: string);
  238. begin
  239. FProjectName := Value;
  240. FFileDir := Format('%s%s\%s\', [GetApplicationPath, sBackUpFolder, FProjectName]);
  241. end;
  242. function TDMHisRestorePoint.GetApplicationPath: string;
  243. begin
  244. Result := ExtractFilePath(ParamStr(0));
  245. end;
  246. procedure TDMHisRestorePoint.DeleteAllPoints;
  247. begin
  248. if not Assigned(cdsOrgHisPoint) then
  249. Exit;
  250. if not cdsOrgHisPoint.Active then
  251. Exit;
  252. cdsUnFixedPoint.First;
  253. while not cdsUnFixedPoint.Eof do
  254. begin
  255. DeleteCurPoint(False);
  256. cdsUnFixedPoint.First;
  257. end;
  258. cdsFixedPoint.First;
  259. while not cdsFixedPoint.Eof do
  260. begin
  261. DeleteCurPoint(True);
  262. cdsUnFixedPoint.First;
  263. end;
  264. cdsOrgHisPoint.ApplyUpdates(0);
  265. end;
  266. end.