toolsunit.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. unit ToolsUnit;
  2. {$IFDEF FPC}
  3. {$mode objfpc}{$H+}
  4. {$ENDIF}
  5. interface
  6. uses
  7. Classes, SysUtils, DB, testdecorator, fpcunit;
  8. Const
  9. // Number of "N" test datasets (as opposed to FieldDatasets) that will be created
  10. // The connectors should have these records prepared in their Create*Dataset procedures.
  11. MaxDataSet = 35;
  12. // Number of records in a trace dataset:
  13. NForTraceDataset = 15;
  14. type
  15. { TDBConnector }
  16. TDBConnectorClass = class of TDBConnector;
  17. TDBConnector = class(TPersistent)
  18. private
  19. FLogTimeFormat: TFormatSettings; //for error logging only
  20. FFormatSettings: TFormatSettings;
  21. FChangedFieldDataset : boolean;
  22. function GetCharSize: integer;
  23. protected
  24. FChangedDatasets : array[0..MaxDataSet] of boolean;
  25. FUsedDatasets : TFPList;
  26. procedure SetTestUniDirectional(const AValue: boolean); virtual;
  27. function GetTestUniDirectional: boolean; virtual;
  28. // These methods should be implemented by all descendents
  29. // They are called each time a test needs a TDataset descendent
  30. // n: the dataset index to return (also number of records in set)
  31. // Presupposes that Create*Dataset(s) has been called already.
  32. Function InternalGetNDataset(n : integer) : TDataset; virtual; abstract;
  33. Function InternalGetFieldDataset : TDataSet; virtual; abstract;
  34. // These methods should be implemented by all descendents
  35. // They are called e.g. in the constructor. They can be used
  36. // to create the tables on disk, or on a DB server
  37. procedure CreateNDatasets; virtual; abstract;
  38. procedure CreateFieldDataset; virtual; abstract;
  39. // These methods are called after each test in which a dataset is used
  40. // by calling GetXXXDataset with Achange=true
  41. // They should reset all data to their right/initial values.
  42. procedure ResetNDatasets; virtual;
  43. procedure ResetFieldDataset; virtual;
  44. // These methods are called e.g. in the destructor.
  45. // They should clean up all mess, like tables on disk or on a DB server
  46. procedure DropNDatasets; virtual; abstract;
  47. procedure DropFieldDataset; virtual; abstract;
  48. // If logging is enabled, writes Message to log file and flushes
  49. // Logging uses tab-separated columns
  50. procedure LogMessage(Category,Message: string);
  51. public
  52. constructor Create; virtual;
  53. destructor Destroy; override;
  54. procedure DataEvent(dataset :TDataset);
  55. Function GetNDataset(n : integer) : TDataset; overload;
  56. Function GetNDataset(AChange : Boolean; n : integer) : TDataset; overload;
  57. Function GetFieldDataset : TDataSet; overload;
  58. Function GetFieldDataset(AChange : Boolean) : TDataSet; overload;
  59. // Gets a dataset that tracks calculation of calculated fields etc.
  60. Function GetTraceDataset(AChange : Boolean) : TDataset; virtual;
  61. // Run before a test is started
  62. procedure StartTest(TestName: string);
  63. // Run after a test is stopped
  64. procedure StopTest(TestName: string);
  65. property TestUniDirectional: boolean read GetTestUniDirectional write SetTestUniDirectional;
  66. property FormatSettings: TFormatSettings read FFormatSettings;
  67. property CharSize: integer read GetCharSize;
  68. end;
  69. { TTestDataLink }
  70. TTestDataLink = class(TDataLink)
  71. protected
  72. procedure DataSetScrolled(Distance: Integer); override;
  73. procedure DataSetChanged; override;
  74. {$IFDEF fpc}
  75. procedure DataEvent(Event: TDataEvent; Info: Ptrint); override;
  76. {$ELSE}
  77. procedure DataEvent(Event: TDataEvent; Info: longint); override;
  78. {$ENDIF}
  79. end;
  80. { TDBBasicsTestSetup }
  81. TDBBasicsTestSetup = class(TTestSetup)
  82. protected
  83. procedure OneTimeSetup; override;
  84. procedure OneTimeTearDown; override;
  85. end;
  86. { TDBBasicsTestCase }
  87. TDBBasicsTestCase = class(TTestCase)
  88. protected
  89. procedure SetUp; override;
  90. procedure TearDown; override;
  91. // Verify whether all values in FieldDataset are present and correct
  92. procedure CheckFieldDatasetValues(ADataSet: TDataSet);
  93. // Verify whether all values in NDataset are present and correct
  94. procedure CheckNDatasetValues(ADataSet: TDataSet; n: integer);
  95. end;
  96. const
  97. DataEventnames : Array [TDataEvent] of String[21] =
  98. ('deFieldChange', 'deRecordChange', 'deDataSetChange', 'deDataSetScroll',
  99. 'deLayoutChange', 'deUpdateRecord', 'deUpdateState', 'deCheckBrowseMode',
  100. 'dePropertyChange', 'deFieldListChange', 'deFocusControl' ,'deParentScroll',
  101. 'deConnectChange', 'deReconcileError', 'deDisabledStateChange');
  102. const
  103. testValuesCount = 25;
  104. testSingleValues : Array[0..testValuesCount-1] of single = (-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.4567,2.4,3.2,0.4,-2.3);
  105. 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.4567,2.4,3.2,0.4,23);
  106. testCurrencyValues : Array[0..testValuesCount-1] of currency = (-MaxLongInt-1,-MaxSmallint-1,-256,-255,-43.34,-2.5,-0.21,0,0.32,45.45,256,45,1234.56,12.34,0.12,MaxSmallInt+1,MaxLongInt+1,-6871947.67,68719476736,2748779069.44,922337203685.47,-92233720368547,99999999999999,-9223372036854.25,-9223372036854.7);
  107. testFmtBCDValues : Array[0..testValuesCount-1] of string = ('-100','-65.5','-54.3333','-43.3334','-2.5','-0.234567','45.4','0.3','45.414585','127','128','255','256','45','0.3','45.4','127','128','255','256','45','1234.56789','43.23','43.500001','99.88');
  108. 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);
  109. testWordValues : Array[0..testValuesCount-1] of Word = (1,2,3,4,5,6,7,8,0,1,127,128,255,256,maxSmallint,maxSmallint+1,maxSmallInt-1,maxSmallInt,65535,100,130,150,151,132,234);
  110. 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);
  111. testByteValues: Array[0..testValuesCount-1] of Byte = (1,2,3,4,5,6,7,8,0,1,127,128,255,0,0,0,0,0,0,0,0,0,0,0,0);
  112. testShortintValues: Array[0..testValuesCount-1] of ShortInt = (1,2,3,4,5,6,7,8,0,1,-128,-127,-64,64,126,127,0,0,0,0,0,0,0,0,0);
  113. testLongWordValues : Array[0..testValuesCount-1] of LongWord = (1,2,3,4,5,6,7,8,0,1,$FF,$FFFF,$FFFFFF,$FFFFFFFF,0,0,0,0,0,0,0,0,0,0,0);
  114. 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);
  115. 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);
  116. testStringValues : Array[0..testValuesCount-1] of string = (
  117. '',
  118. 'a',
  119. 'ab',
  120. 'abc',
  121. 'abcd',
  122. 'abcde',
  123. 'abcdef',
  124. 'abcdefg',
  125. 'abcdefgh',
  126. 'abcdefghi',
  127. 'abcdefghij',
  128. 'lMnOpQrStU',
  129. '1234567890',
  130. '_!@#$%^&*(',
  131. '_!@#$%^&*(',
  132. ' ''quotes'' ',
  133. ')-;:/?.<>',
  134. '~`|{}- =', // note that there's no \ (backslash) since some db's uses that as escape-character
  135. ' WRaP ',
  136. 'wRaP ',
  137. ' wRAP',
  138. 'this',
  139. // 'is',
  140. 'fun',
  141. 'VB7^',
  142. 'vdfbst'
  143. );
  144. testDateValues : Array[0..testValuesCount-1] of string = (
  145. '2000-01-01',
  146. '1999-12-31',
  147. '2004-02-29',
  148. '2004-03-01',
  149. '1991-02-28',
  150. '1991-03-01',
  151. '1997-11-29',
  152. '2040-10-16',
  153. '1977-09-29',
  154. '1977-12-31',
  155. '1917-12-29',
  156. '1900-01-01',
  157. '1899-12-31',
  158. '1899-12-30',
  159. '1899-12-29',
  160. '1800-03-30',
  161. '1754-06-04',
  162. '1753-01-01',
  163. '1650-05-10',
  164. '0904-04-12',
  165. '0199-07-09',
  166. '0079-11-29',
  167. '0031-11-02',
  168. '0001-12-31',
  169. '0001-01-01'
  170. );
  171. testTimeValues : Array[0..testValuesCount-1] of string = (
  172. '10:45:12.000',
  173. '00:00:00.000',
  174. '24:00:00.000',
  175. '33:25:15.000',
  176. '04:59:16.000',
  177. '05:45:59.000',
  178. '11:45:12.000',
  179. '12:45:12.000',
  180. '14:45:14.000',
  181. '14:45:52.000',
  182. '15:35:12.000',
  183. '16:35:42.000',
  184. '16:45:12.000',
  185. '18:45:22.000',
  186. '19:45:12.000',
  187. '16:45:12.010',
  188. '13:55:12.200',
  189. '13:46:12.543',
  190. '15:35:12.000',
  191. '17:25:12.530',
  192. '19:45:12.003',
  193. '10:54:12.999',
  194. '12:25:12.000',
  195. '20:15:12.758',
  196. '23:59:59.000'
  197. );
  198. var dbtype,
  199. dbconnectorname,
  200. dbconnectorparams,
  201. dbname,
  202. dbuser,
  203. dbhostname,
  204. dbpassword,
  205. dbcharset,
  206. dblogfilename,
  207. dbQuoteChars : string;
  208. dblogfile : TextFile;
  209. DataEvents : string;
  210. DBConnector : TDBConnector;
  211. testValues : Array [TFieldType,0..testvaluescount -1] of string;
  212. procedure InitialiseDBConnector;
  213. procedure FreeDBConnector;
  214. function DateTimeToTimeString(d: tdatetime) : string;
  215. function TimeStringToDateTime(d: String): TDateTime;
  216. function StringToByteArray(const s: ansistring): Variant;
  217. implementation
  218. uses
  219. inifiles, FmtBCD, Variants;
  220. var DBConnectorRefCount: integer;
  221. { TDBConnector }
  222. constructor TDBConnector.Create;
  223. begin
  224. FFormatSettings.DecimalSeparator:='.';
  225. FFormatSettings.ThousandSeparator:=#0;
  226. FFormatSettings.DateSeparator:='-';
  227. FFormatSettings.TimeSeparator:=':';
  228. FFormatSettings.ShortDateFormat:='yyyy/mm/dd';
  229. FFormatSettings.LongTimeFormat:='hh:nn:ss.zzz';
  230. // Set up time format for logging output:
  231. // ISO 8601 type date string so logging is uniform across machines
  232. FLogTimeFormat.DecimalSeparator:='.';
  233. FLogTimeFormat.ThousandSeparator:=#0;
  234. FLogTimeFormat.DateSeparator:='-';
  235. FLogTimeFormat.TimeSeparator:=':';
  236. FLogTimeFormat.ShortDateFormat:='yyyy-mm-dd';
  237. FLogTimeFormat.LongTimeFormat:='hh:nn:ss';
  238. FUsedDatasets := TFPList.Create;
  239. CreateFieldDataset;
  240. CreateNDatasets;
  241. end;
  242. destructor TDBConnector.Destroy;
  243. begin
  244. if assigned(FUsedDatasets) then FUsedDatasets.Destroy;
  245. DropNDatasets;
  246. DropFieldDataset;
  247. Inherited;
  248. end;
  249. function TDBConnector.GetTestUniDirectional: boolean;
  250. begin
  251. result := false;
  252. end;
  253. procedure TDBConnector.SetTestUniDirectional(const AValue: boolean);
  254. begin
  255. raise exception.create('Connector does not support tests for unidirectional datasets');
  256. end;
  257. procedure TDBConnector.DataEvent(dataset: TDataset);
  258. begin
  259. DataEvents := DataEvents + 'DataEvent' + ';';
  260. end;
  261. procedure TDBConnector.ResetNDatasets;
  262. begin
  263. DropNDatasets;
  264. CreateNDatasets;
  265. end;
  266. procedure TDBConnector.ResetFieldDataset;
  267. begin
  268. DropFieldDataset;
  269. CreateFieldDataset;
  270. end;
  271. function TDBConnector.GetNDataset(n: integer): TDataset;
  272. begin
  273. Result := GetNDataset(False,n);
  274. end;
  275. function TDBConnector.GetNDataset(AChange : Boolean; n: integer): TDataset;
  276. begin
  277. if AChange then FChangedDatasets[n] := True;
  278. Result := InternalGetNDataset(n);
  279. FUsedDatasets.Add(Result);
  280. end;
  281. function TDBConnector.GetFieldDataset: TDataSet;
  282. begin
  283. Result := GetFieldDataset(False);
  284. end;
  285. function TDBConnector.GetFieldDataset(AChange: Boolean): TDataSet;
  286. begin
  287. if AChange then FChangedFieldDataset := True;
  288. Result := InternalGetFieldDataset;
  289. FUsedDatasets.Add(Result);
  290. end;
  291. function TDBConnector.GetTraceDataset(AChange: Boolean): TDataset;
  292. begin
  293. result := GetNDataset(AChange,NForTraceDataset);
  294. end;
  295. procedure TDBConnector.StartTest(TestName: string);
  296. begin
  297. // Log if necessary
  298. LogMessage('Test','Starting test '+TestName);
  299. end;
  300. procedure TDBConnector.StopTest(TestName: string);
  301. var i : integer;
  302. ds : TDataset;
  303. begin
  304. LogMessage('Test','Stopping test '+TestName);
  305. for i := 0 to FUsedDatasets.Count -1 do
  306. begin
  307. ds := tdataset(FUsedDatasets[i]);
  308. if ds.active then ds.Close;
  309. ds.Free;
  310. end;
  311. FUsedDatasets.Clear;
  312. if FChangedFieldDataset then ResetFieldDataset;
  313. for i := 0 to MaxDataSet do if FChangedDatasets[i] then
  314. begin
  315. ResetNDatasets;
  316. fillchar(FChangedDatasets,sizeof(FChangedDatasets),ord(False));
  317. break;
  318. end;
  319. end;
  320. procedure TDBConnector.LogMessage(Category,Message: string);
  321. begin
  322. if dblogfilename<>'' then //double check: only if logging enabled
  323. begin
  324. try
  325. Message:=StringReplace(Message, #9, '\t', [rfReplaceAll, rfIgnoreCase]);
  326. Message:=StringReplace(Message, LineEnding, '\n', [rfReplaceAll, rfIgnoreCase]);
  327. writeln(dbLogFile, TimeToStr(Now(), FLogTimeFormat) + #9 +
  328. Category + #9 +
  329. Message);
  330. Flush(dbLogFile); //in case tests crash
  331. except
  332. // ignore log file errors
  333. end;
  334. end;
  335. end;
  336. function TDBConnector.GetCharSize: integer;
  337. begin
  338. case LowerCase(dbcharset) of
  339. 'utf8','utf-8','utf8mb4':
  340. Result := 4;
  341. else
  342. Result := 1;
  343. end;
  344. end;
  345. { TTestDataLink }
  346. procedure TTestDataLink.DataSetScrolled(Distance: Integer);
  347. begin
  348. DataEvents := DataEvents + 'DataSetScrolled' + ':' + inttostr(Distance) + ';';
  349. inherited DataSetScrolled(Distance);
  350. end;
  351. procedure TTestDataLink.DataSetChanged;
  352. begin
  353. DataEvents := DataEvents + 'DataSetChanged;';
  354. inherited DataSetChanged;
  355. end;
  356. {$IFDEF FPC}
  357. procedure TTestDataLink.DataEvent(Event: TDataEvent; Info: Ptrint);
  358. {$ELSE}
  359. procedure TTestDataLink.DataEvent(Event: TDataEvent; Info: Longint);
  360. {$ENDIF}
  361. begin
  362. if Event <> deFieldChange then
  363. DataEvents := DataEvents + DataEventnames[Event] + ':' + inttostr(info) + ';'
  364. else
  365. DataEvents := DataEvents + DataEventnames[Event] + ':' + TField(info).FieldName + ';';
  366. inherited DataEvent(Event, Info);
  367. end;
  368. { TDBBasicsTestSetup }
  369. procedure TDBBasicsTestSetup.OneTimeSetup;
  370. begin
  371. InitialiseDBConnector;
  372. end;
  373. procedure TDBBasicsTestSetup.OneTimeTearDown;
  374. begin
  375. FreeDBConnector;
  376. end;
  377. { TDBBasicsTestCase }
  378. procedure TDBBasicsTestCase.SetUp;
  379. begin
  380. inherited SetUp;
  381. DBConnector.StartTest(TestName);
  382. end;
  383. procedure TDBBasicsTestCase.TearDown;
  384. begin
  385. DBConnector.StopTest(TestName);
  386. inherited TearDown;
  387. end;
  388. procedure TDBBasicsTestCase.CheckFieldDatasetValues(ADataSet: TDataSet);
  389. var i: integer;
  390. begin
  391. with ADataSet do
  392. begin
  393. First;
  394. for i := 0 to testValuesCount-1 do
  395. begin
  396. CheckEquals(i, FieldByName('ID').AsInteger, 'ID');
  397. CheckEquals(testStringValues[i], FieldByName('FSTRING').AsString, 'FSTRING');
  398. CheckEquals(testIntValues[i], FieldByName('FINTEGER').AsInteger, 'FINTEGER');
  399. CheckEquals(testLargeIntValues[i], FieldByName('FLARGEINT').AsLargeInt, 'FLARGEINT');
  400. Next;
  401. end;
  402. CheckTrue(Eof, 'Eof');
  403. end;
  404. end;
  405. procedure TDBBasicsTestCase.CheckNDatasetValues(ADataSet: TDataSet; n: integer);
  406. var i: integer;
  407. begin
  408. with ADataSet do
  409. begin
  410. First;
  411. for i := 1 to n do
  412. begin
  413. CheckEquals(i, FieldByName('ID').AsInteger, 'ID');
  414. CheckEquals('TestName' + inttostr(i), FieldByName('NAME').AsString, 'NAME');
  415. Next;
  416. end;
  417. CheckTrue(Eof, 'Eof');
  418. end;
  419. end;
  420. procedure ReadIniFile;
  421. var IniFile : TIniFile;
  422. begin
  423. IniFile := TIniFile.Create(GetCurrentDir + PathDelim + 'database.ini');
  424. dbtype:='';
  425. if ParamCount>0 then
  426. dbtype := ParamStr(1);
  427. if (dbtype='') or not IniFile.SectionExists(dbtype) then
  428. dbtype := IniFile.ReadString('Database','Type','');
  429. dbconnectorname := IniFile.ReadString(dbtype,'Connector','');
  430. dbname := IniFile.ReadString(dbtype,'Name','');
  431. dbuser := IniFile.ReadString(dbtype,'User','');
  432. dbhostname := IniFile.ReadString(dbtype,'Hostname','');
  433. dbpassword := IniFile.ReadString(dbtype,'Password','');
  434. dbcharset := IniFile.ReadString(dbtype,'CharSet','');
  435. dbconnectorparams := IniFile.ReadString(dbtype,'ConnectorParams','');
  436. dblogfilename := IniFile.ReadString(dbtype,'LogFile','');
  437. dbquotechars := IniFile.ReadString(dbtype,'QuoteChars','"');
  438. IniFile.Free;
  439. end;
  440. procedure SetupLog;
  441. begin
  442. if dblogfilename<>'' then
  443. begin
  444. try
  445. AssignFile(dblogfile,dblogfilename);
  446. if not(FileExists(dblogfilename)) then
  447. begin
  448. ReWrite(dblogfile);
  449. CloseFile(dblogfile);
  450. end;
  451. Append(dblogfile);
  452. except
  453. dblogfilename:=''; //rest of code relies on this as a log switch
  454. end;
  455. end;
  456. end;
  457. procedure CloseLog;
  458. begin
  459. if dblogfilename<>'' then
  460. begin
  461. try
  462. CloseFile(dbLogFile);
  463. except
  464. // Ignore log file errors
  465. end;
  466. end;
  467. end;
  468. procedure InitialiseDBConnector;
  469. var DBConnectorClass : TPersistentClass;
  470. i : integer;
  471. FormatSettings : TFormatSettings;
  472. begin
  473. if DBConnectorRefCount>0 then exit;
  474. FormatSettings.DecimalSeparator:='.';
  475. FormatSettings.ThousandSeparator:=#0;
  476. testValues[ftString] := testStringValues;
  477. testValues[ftFixedChar] := testStringValues;
  478. testValues[ftTime] := testTimeValues;
  479. testValues[ftDate] := testDateValues;
  480. testValues[ftBlob] := testStringValues;
  481. testValues[ftMemo] := testStringValues;
  482. testValues[ftWideString] := testStringValues;
  483. testValues[ftWideMemo] := testStringValues;
  484. testValues[ftFMTBcd] := testFmtBCDValues;
  485. for i := 0 to testValuesCount-1 do
  486. begin
  487. testValues[ftBoolean,i] := BoolToStr(testBooleanValues[i], True);
  488. testValues[ftFloat,i] := FloatToStr(testFloatValues[i],FormatSettings);
  489. testValues[ftSmallint,i] := IntToStr(testSmallIntValues[i]);
  490. testValues[ftInteger,i] := IntToStr(testIntValues[i]);
  491. testValues[ftWord,i] := IntToStr(testWordValues[i]);
  492. testValues[ftLargeint,i] := IntToStr(testLargeIntValues[i]);
  493. testValues[ftLongWord,i] := IntToStr(testLongWordValues[i]);
  494. testValues[ftCurrency,i] := CurrToStr(testCurrencyValues[i],FormatSettings);
  495. testValues[ftBCD,i] := CurrToStr(testCurrencyValues[i],FormatSettings);
  496. // For date '0001-01-01' other time-part like '00:00:00' causes "Invalid variant type cast", because of < MinDateTime constant
  497. if (testDateValues[i]>'0001-01-01') and (testTimeValues[i]>='00:00:01') and (testTimeValues[i]<'24:00:00') then
  498. testValues[ftDateTime,i] := testDateValues[i] + ' ' + testTimeValues[i]
  499. else
  500. testValues[ftDateTime,i] := testDateValues[i];
  501. testValues[ftShortInt,i] := IntToStr(testShortIntValues[i]);
  502. testValues[ftByte,i] := IntToStr(testByteValues[i]);
  503. testValues[ftExtended,i] := FloatToStr(testFloatValues[i]);
  504. testValues[ftSingle,i] := FloatToStr(testSingleValues[i]);
  505. end;
  506. if dbconnectorname = '' then raise Exception.Create('There is no db connector specified');
  507. DBConnectorClass := GetClass('T'+dbconnectorname+'DBConnector');
  508. if assigned(DBConnectorClass) then
  509. DBConnector := TDBConnectorClass(DBConnectorClass).create
  510. else Raise Exception.Create('Unknown db connector specified: ' + 'T'+dbconnectorname+'DBConnector');
  511. inc(DBConnectorRefCount);
  512. end;
  513. procedure FreeDBConnector;
  514. begin
  515. dec(DBConnectorRefCount);
  516. if DBConnectorRefCount=0 then
  517. FreeAndNil(DBConnector);
  518. end;
  519. function DateTimeToTimeString(d: tdatetime): string;
  520. var
  521. millisecond: word;
  522. second : word;
  523. minute : word;
  524. hour : word;
  525. begin
  526. // Format the datetime in the format hh:nn:ss.zzz, where the hours can be bigger then 23.
  527. DecodeTime(d,hour,minute,second,millisecond);
  528. hour := hour + (trunc(d) * 24);
  529. result := Format('%.2d:%.2d:%.2d.%.3d',[hour,minute,second,millisecond]);
  530. end;
  531. function TimeStringToDateTime(d: String): TDateTime;
  532. var
  533. millisecond: word;
  534. second : word;
  535. minute : word;
  536. hour : word;
  537. days : word;
  538. begin
  539. // Convert the string in the format hh:nn:ss.zzz to a datetime.
  540. hour := strtoint(copy(d,1,2));
  541. minute := strtoint(copy(d,4,2));
  542. second := strtoint(copy(d,7,2));
  543. millisecond := strtoint(copy(d,10,3));
  544. days := hour div 24;
  545. hour := hour mod 24;
  546. result := ComposeDateTime(days,EncodeTime(hour,minute,second,millisecond));
  547. end;
  548. function StringToByteArray(const s: ansistring): Variant;
  549. var P: Pointer;
  550. Len: integer;
  551. begin
  552. Len := Length(s) * SizeOf(AnsiChar);
  553. Result := VarArrayCreate([0, Len-1], varByte);
  554. P := VarArrayLock(Result);
  555. try
  556. Move(s[1], P^, Len);
  557. finally
  558. VarArrayUnlock(Result);
  559. end;
  560. end;
  561. initialization
  562. ReadIniFile;
  563. SetupLog;
  564. DBConnectorRefCount:=0;
  565. finalization
  566. CloseLog;
  567. end.