toolsunit.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. unit ToolsUnit;
  2. {$IFDEF FPC}
  3. {$mode objfpc}{$H+}
  4. {$ENDIF}
  5. interface
  6. uses
  7. Classes, SysUtils, DB;
  8. Const MaxDataSet = 35;
  9. type
  10. { TDBConnector }
  11. TDBConnectorClass = class of TDBConnector;
  12. TDBConnector = class(TPersistent)
  13. private
  14. FChangedDatasets : array[0..MaxDataSet] of boolean;
  15. FUsedDatasets : TFPList;
  16. FChangedFieldDataset : boolean;
  17. protected
  18. // These methods should be implemented by any descendents
  19. // They are called eacht time a test need a TDataset descendent
  20. Function InternalGetNDataset(n : integer) : TDataset; virtual; abstract;
  21. Function InternalGetFieldDataset : TDataSet; virtual; abstract;
  22. // These methods should be implemented by any descendents
  23. // They are called only once in the constructor. They can be used
  24. // to create the tables on disk, or on a DB-Server
  25. procedure CreateNDatasets; virtual; abstract;
  26. procedure CreateFieldDataset; virtual; abstract;
  27. // These methods are called after each test in which a dataset is used
  28. // by calling GetXXXDataset with Achange=true
  29. // They should reset all data to their right/initial values.
  30. procedure ResetNDatasets; virtual;
  31. procedure ResetFieldDataset; virtual;
  32. // These methods are called only once in the destructor.
  33. // They should clean up all mess, like tables on disk or on a DB-server
  34. procedure DropNDatasets; virtual; abstract;
  35. procedure DropFieldDataset; virtual; abstract;
  36. public
  37. constructor create; virtual;
  38. destructor destroy; override;
  39. procedure DataEvent(dataset :TDataset);
  40. Function GetNDataset(n : integer) : TDataset; overload;
  41. Function GetNDataset(AChange : Boolean; n : integer) : TDataset; overload;
  42. Function GetFieldDataset : TDataSet; overload;
  43. Function GetFieldDataset(AChange : Boolean) : TDataSet; overload;
  44. Function GetTraceDataset(AChange : Boolean) : TDataset; virtual;
  45. procedure StartTest;
  46. procedure StopTest;
  47. end;
  48. { TTestDataLink }
  49. TTestDataLink = class(TDataLink)
  50. protected
  51. procedure DataSetScrolled(Distance: Integer); override;
  52. {$IFDEF fpc}
  53. procedure DataEvent(Event: TDataEvent; Info: Ptrint); override;
  54. {$ELSE}
  55. procedure DataEvent(Event: TDataEvent; Info: longint); override;
  56. {$ENDIF}
  57. end;
  58. const
  59. DataEventnames : Array [TDataEvent] of String[21] =
  60. ('deFieldChange', 'deRecordChange', 'deDataSetChange', 'deDataSetScroll',
  61. 'deLayoutChange', 'deUpdateRecord', 'deUpdateState', 'deCheckBrowseMode',
  62. 'dePropertyChange', 'deFieldListChange', 'deFocusControl' ,'deParentScroll',
  63. 'deConnectChange', 'deReconcileError', 'deDisabledStateChange');
  64. const
  65. testValuesCount = 25;
  66. testFloatValues : Array[0..testValuesCount-1] of double = (-maxSmallint-1,-maxSmallint,-256,-255,-128,-127,-1,0,1,127,128,255,256,maxSmallint,maxSmallint+1,0.123456,-0.123456,4.35,12.434E7,9.876e-5,123.45678,2.4,3.2,0.4,23);
  67. testCurrencyValues : Array[0..testValuesCount-1] of currency = (-100,-65.5,-54.34,-43.34,-2.50,-0.2,45.40,0.3,45.4,127,128,255,256,45,0.3,45.4,127,128,255,256,45,1234.56,43.23,43.43,99.88);
  68. testIntValues : Array[0..testValuesCount-1] of integer = (-maxInt,-maxInt+1,-maxSmallint-1,-maxSmallint,-256,-255,-128,-127,-1,0,1,127,128,255,256,maxSmallint,maxSmallint+1,MaxInt-1,MaxInt,100,130,150,-150,-132,234);
  69. testSmallIntValues : Array[0..testValuesCount-1] of smallint = (-maxSmallint,-maxSmallint+1,-256,-255,-128,-127,-1,0,1,127,128,255,256,maxSmallint,maxSmallint-1,100,110,120,130,150,-150,-132,234,231,42);
  70. testLargeIntValues : Array[0..testValuesCount-1] of LargeInt = ( -$7fffffffffffffff,-$7ffffffffffffffe,-maxInt-1,-maxInt+1,-maxSmallint,-maxSmallint+1,-256,-255,-128,-127,-1,0,1,127,128,255,256,maxSmallint,maxSmallint-1,maxSmallint+1,MaxInt-1,MaxInt,$7fffffffffffffff-1,$7fffffffffffffff,235253244);
  71. testBooleanValues : Array[0..testValuesCount-1] of boolean = (true,false,false,true,true,false,false,true,false,true,true,true,false,false,false,false,true,true,true,true,false,true,true,false,false);
  72. testStringValues : Array[0..testValuesCount-1] of string = (
  73. '',
  74. 'a',
  75. 'ab',
  76. 'abc',
  77. 'abcd',
  78. 'abcde',
  79. 'abcdef',
  80. 'abcdefg',
  81. 'abcdefgh',
  82. 'abcdefghi',
  83. 'abcdefghij',
  84. 'lMnOpQrStU',
  85. '1234567890',
  86. '_!@#$%^&*(',
  87. '_!@#$%^&*(',
  88. ' ''quotes'' ',
  89. ')-;:/?.<>',
  90. '~`|{}- =', // note that there's no \ (backslash) since some db's uses that as escape-character
  91. ' WRaP ',
  92. 'wRaP ',
  93. ' wRAP',
  94. 'this',
  95. // 'is',
  96. 'fun',
  97. 'VB7^',
  98. 'vdfbst'
  99. );
  100. testDateValues : Array[0..testValuesCount-1] of string = (
  101. '2000-01-01',
  102. '1999-12-31',
  103. '2004-02-29',
  104. '2004-03-01',
  105. '1991-02-28',
  106. '1991-03-01',
  107. '2040-10-16',
  108. '1977-09-29',
  109. '1800-03-30',
  110. '1650-05-10',
  111. '1754-06-04',
  112. '0904-04-12',
  113. '0199-07-09',
  114. '0001-01-01',
  115. '0031-11-02',
  116. '1899-12-29',
  117. '1899-12-30',
  118. '1899-12-31',
  119. '1977-09-29',
  120. '1917-12-29',
  121. '0079-11-29',
  122. '1997-11-29',
  123. '0001-01-01',
  124. '1997-11-29',
  125. '1900-01-01'
  126. );
  127. var dbtype,
  128. dbconnectorname,
  129. dbconnectorparams,
  130. dbname,
  131. dbuser,
  132. dbhostname,
  133. dbpassword : string;
  134. DataEvents : string;
  135. DBConnector : TDBConnector;
  136. testValues : Array [TFieldType,0..testvaluescount -1] of string;
  137. procedure InitialiseDBConnector;
  138. implementation
  139. uses
  140. sqldbtoolsunit,
  141. dbftoolsunit,
  142. memdstoolsunit,
  143. inifiles;
  144. constructor TDBConnector.create;
  145. begin
  146. CreateFieldDataset;
  147. CreateNDatasets;
  148. FUsedDatasets := TFPList.Create;
  149. end;
  150. destructor TDBConnector.destroy;
  151. begin
  152. if assigned(FUsedDatasets) then FUsedDatasets.Destroy;
  153. DropNDatasets;
  154. DropFieldDataset;
  155. end;
  156. procedure TDBConnector.ResetNDatasets;
  157. begin
  158. DropNDatasets;
  159. CreateNDatasets;
  160. end;
  161. procedure TDBConnector.ResetFieldDataset;
  162. begin
  163. DropFieldDataset;
  164. CreateFieldDataset;
  165. end;
  166. procedure TDBConnector.DataEvent(dataset : tdataset);
  167. begin
  168. DataEvents := DataEvents + 'DataEvent' + ';';
  169. end;
  170. function TDBConnector.GetNDataset(n: integer): TDataset;
  171. begin
  172. Result := GetNDataset(False,n);
  173. end;
  174. procedure ReadIniFile;
  175. var IniFile : TIniFile;
  176. begin
  177. IniFile := TIniFile.Create(getcurrentdir + PathDelim + 'database.ini');
  178. dbtype:='';
  179. if Paramcount>0 then
  180. dbtype := ParamStr(1);
  181. if (dbtype='') or not inifile.SectionExists(dbtype) then
  182. dbtype := IniFile.ReadString('Database','Type','');
  183. dbconnectorname := IniFile.ReadString(dbtype,'Connector','');
  184. dbname := IniFile.ReadString(dbtype,'Name','');
  185. dbuser := IniFile.ReadString(dbtype,'User','');
  186. dbhostname := IniFile.ReadString(dbtype,'Hostname','');
  187. dbpassword := IniFile.ReadString(dbtype,'Password','');
  188. dbconnectorparams := IniFile.ReadString(dbtype,'ConnectorParams','');
  189. IniFile.Free;
  190. end;
  191. procedure InitialiseDBConnector;
  192. var DBConnectorClass : TPersistentClass;
  193. i : integer;
  194. begin
  195. testValues[ftString] := testStringValues;
  196. testValues[ftDate] := testDateValues;
  197. for i := 0 to testValuesCount-1 do
  198. begin
  199. testValues[ftFloat,i] := FloatToStr(testFloatValues[i]);
  200. testValues[ftSmallint,i] := IntToStr(testSmallIntValues[i]);
  201. testValues[ftInteger,i] := IntToStr(testIntValues[i]);
  202. testValues[ftLargeint,i] := IntToStr(testLargeIntValues[i]);
  203. DecimalSeparator:=',';
  204. testValues[ftCurrency,i] := CurrToStr(testCurrencyValues[i]);
  205. DecimalSeparator:='.';
  206. testValues[ftBCD,i] := CurrToStr(testCurrencyValues[i]);
  207. end;
  208. if dbconnectorname = '' then raise Exception.Create('There is no db-connector specified');
  209. DBConnectorClass := GetClass('T'+dbconnectorname+'DBConnector');
  210. if assigned(DBConnectorClass) then
  211. DBConnector := TDBConnectorClass(DBConnectorClass).create
  212. else Raise Exception.Create('Unknown db-connector specified');
  213. end;
  214. { TTestDataLink }
  215. {$IFDEF FPC}
  216. procedure TTestDataLink.DataSetScrolled(Distance: Integer);
  217. begin
  218. DataEvents := DataEvents + 'DataSetScrolled' + ':' + inttostr(Distance) + ';';
  219. inherited DataSetScrolled(Distance);
  220. end;
  221. procedure TTestDataLink.DataEvent(Event: TDataEvent; Info: Ptrint);
  222. {$ELSE}
  223. procedure TTestDataLink.DataEvent(Event: TDataEvent; Info: Longint);
  224. {$ENDIF}
  225. begin
  226. if Event <> deFieldChange then
  227. DataEvents := DataEvents + DataEventnames[Event] + ':' + inttostr(info) + ';'
  228. else
  229. DataEvents := DataEvents + DataEventnames[Event] + ':' + TField(info).FieldName + ';';
  230. inherited DataEvent(Event, Info);
  231. end;
  232. { TDBConnector }
  233. function TDBConnector.GetNDataset(AChange : Boolean; n: integer): TDataset;
  234. begin
  235. if AChange then FChangedDatasets[n] := True;
  236. Result := InternalGetNDataset(n);
  237. FUsedDatasets.Add(Result);
  238. end;
  239. function TDBConnector.GetFieldDataset: TDataSet;
  240. begin
  241. Result := GetFieldDataset(False);
  242. end;
  243. function TDBConnector.GetFieldDataset(AChange: Boolean): TDataSet;
  244. begin
  245. if AChange then FChangedFieldDataset := True;
  246. Result := InternalGetFieldDataset;
  247. FUsedDatasets.Add(Result);
  248. end;
  249. function TDBConnector.GetTraceDataset(AChange: Boolean): TDataset;
  250. begin
  251. result := GetNDataset(AChange,15);
  252. end;
  253. procedure TDBConnector.StartTest;
  254. begin
  255. // Do nothing?
  256. end;
  257. procedure TDBConnector.StopTest;
  258. var i : integer;
  259. ds : TDataset;
  260. begin
  261. for i := 0 to FUsedDatasets.Count -1 do
  262. begin
  263. ds := tdataset(FUsedDatasets[i]);
  264. if ds.active then ds.Close;
  265. ds.Free;
  266. end;
  267. FUsedDatasets.Clear;
  268. if FChangedFieldDataset then ResetFieldDataset;
  269. for i := 0 to MaxDataSet do if FChangedDatasets[i] then
  270. begin
  271. ResetNDatasets;
  272. fillchar(FChangedDatasets,sizeof(FChangedDatasets),ord(False));
  273. break;
  274. end;
  275. end;
  276. initialization
  277. ReadIniFile;
  278. end.