2
0

toolsunit.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. 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);
  105. 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);
  106. 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');
  107. 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);
  108. 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);
  109. 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);
  110. 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);
  111. 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);
  112. 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);
  113. 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);
  114. 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);
  115. testStringValues : Array[0..testValuesCount-1] of string = (
  116. '',
  117. 'a',
  118. 'ab',
  119. 'abc',
  120. 'abcd',
  121. 'abcde',
  122. 'abcdef',
  123. 'abcdefg',
  124. 'abcdefgh',
  125. 'abcdefghi',
  126. 'abcdefghij',
  127. 'lMnOpQrStU',
  128. '1234567890',
  129. '_!@#$%^&*(',
  130. '_!@#$%^&*(',
  131. ' ''quotes'' ',
  132. ')-;:/?.<>',
  133. '~`|{}- =', // note that there's no \ (backslash) since some db's uses that as escape-character
  134. ' WRaP ',
  135. 'wRaP ',
  136. ' wRAP',
  137. 'this',
  138. // 'is',
  139. 'fun',
  140. 'VB7^',
  141. 'vdfbst'
  142. );
  143. testDateValues : Array[0..testValuesCount-1] of string = (
  144. '2000-01-01',
  145. '1999-12-31',
  146. '2004-02-29',
  147. '2004-03-01',
  148. '1991-02-28',
  149. '1991-03-01',
  150. '1997-11-29',
  151. '2040-10-16',
  152. '1977-09-29',
  153. '1977-12-31',
  154. '1917-12-29',
  155. '1900-01-01',
  156. '1899-12-31',
  157. '1899-12-30',
  158. '1899-12-29',
  159. '1800-03-30',
  160. '1754-06-04',
  161. '1753-01-01',
  162. '1650-05-10',
  163. '0904-04-12',
  164. '0199-07-09',
  165. '0079-11-29',
  166. '0031-11-02',
  167. '0001-12-31',
  168. '0001-01-01'
  169. );
  170. testTimeValues : Array[0..testValuesCount-1] of string = (
  171. '10:45:12.000',
  172. '00:00:00.000',
  173. '24:00:00.000',
  174. '33:25:15.000',
  175. '04:59:16.000',
  176. '05:45:59.000',
  177. '11:45:12.000',
  178. '12:45:12.000',
  179. '14:45:14.000',
  180. '14:45:52.000',
  181. '15:35:12.000',
  182. '16:35:42.000',
  183. '16:45:12.000',
  184. '18:45:22.000',
  185. '19:45:12.000',
  186. '16:45:12.010',
  187. '13:55:12.200',
  188. '13:46:12.543',
  189. '15:35:12.000',
  190. '17:25:12.530',
  191. '19:45:12.003',
  192. '10:54:12.999',
  193. '12:25:12.000',
  194. '20:15:12.758',
  195. '23:59:59.000'
  196. );
  197. var dbtype,
  198. dbconnectorname,
  199. dbconnectorparams,
  200. dbname,
  201. dbuser,
  202. dbhostname,
  203. dbpassword,
  204. dbcharset,
  205. dblogfilename,
  206. dbQuoteChars : string;
  207. dblogfile : TextFile;
  208. DataEvents : string;
  209. DBConnector : TDBConnector;
  210. testValues : Array [TFieldType,0..testvaluescount -1] of string;
  211. procedure InitialiseDBConnector;
  212. procedure FreeDBConnector;
  213. function DateTimeToTimeString(d: tdatetime) : string;
  214. function TimeStringToDateTime(d: String): TDateTime;
  215. function StringToByteArray(const s: ansistring): Variant;
  216. implementation
  217. uses
  218. inifiles, FmtBCD, Variants;
  219. var DBConnectorRefCount: integer;
  220. { TDBConnector }
  221. constructor TDBConnector.Create;
  222. begin
  223. FFormatSettings.DecimalSeparator:='.';
  224. FFormatSettings.ThousandSeparator:=#0;
  225. FFormatSettings.DateSeparator:='-';
  226. FFormatSettings.TimeSeparator:=':';
  227. FFormatSettings.ShortDateFormat:='yyyy/mm/dd';
  228. FFormatSettings.LongTimeFormat:='hh:nn:ss.zzz';
  229. // Set up time format for logging output:
  230. // ISO 8601 type date string so logging is uniform across machines
  231. FLogTimeFormat.DecimalSeparator:='.';
  232. FLogTimeFormat.ThousandSeparator:=#0;
  233. FLogTimeFormat.DateSeparator:='-';
  234. FLogTimeFormat.TimeSeparator:=':';
  235. FLogTimeFormat.ShortDateFormat:='yyyy-mm-dd';
  236. FLogTimeFormat.LongTimeFormat:='hh:nn:ss';
  237. FUsedDatasets := TFPList.Create;
  238. CreateFieldDataset;
  239. CreateNDatasets;
  240. end;
  241. destructor TDBConnector.Destroy;
  242. begin
  243. if assigned(FUsedDatasets) then FUsedDatasets.Destroy;
  244. DropNDatasets;
  245. DropFieldDataset;
  246. Inherited;
  247. end;
  248. function TDBConnector.GetTestUniDirectional: boolean;
  249. begin
  250. result := false;
  251. end;
  252. procedure TDBConnector.SetTestUniDirectional(const AValue: boolean);
  253. begin
  254. raise exception.create('Connector does not support tests for unidirectional datasets');
  255. end;
  256. procedure TDBConnector.DataEvent(dataset: TDataset);
  257. begin
  258. DataEvents := DataEvents + 'DataEvent' + ';';
  259. end;
  260. procedure TDBConnector.ResetNDatasets;
  261. begin
  262. DropNDatasets;
  263. CreateNDatasets;
  264. end;
  265. procedure TDBConnector.ResetFieldDataset;
  266. begin
  267. DropFieldDataset;
  268. CreateFieldDataset;
  269. end;
  270. function TDBConnector.GetNDataset(n: integer): TDataset;
  271. begin
  272. Result := GetNDataset(False,n);
  273. end;
  274. function TDBConnector.GetNDataset(AChange : Boolean; n: integer): TDataset;
  275. begin
  276. if AChange then FChangedDatasets[n] := True;
  277. Result := InternalGetNDataset(n);
  278. FUsedDatasets.Add(Result);
  279. end;
  280. function TDBConnector.GetFieldDataset: TDataSet;
  281. begin
  282. Result := GetFieldDataset(False);
  283. end;
  284. function TDBConnector.GetFieldDataset(AChange: Boolean): TDataSet;
  285. begin
  286. if AChange then FChangedFieldDataset := True;
  287. Result := InternalGetFieldDataset;
  288. FUsedDatasets.Add(Result);
  289. end;
  290. function TDBConnector.GetTraceDataset(AChange: Boolean): TDataset;
  291. begin
  292. result := GetNDataset(AChange,NForTraceDataset);
  293. end;
  294. procedure TDBConnector.StartTest(TestName: string);
  295. begin
  296. // Log if necessary
  297. LogMessage('Test','Starting test '+TestName);
  298. end;
  299. procedure TDBConnector.StopTest(TestName: string);
  300. var i : integer;
  301. ds : TDataset;
  302. begin
  303. LogMessage('Test','Stopping test '+TestName);
  304. for i := 0 to FUsedDatasets.Count -1 do
  305. begin
  306. ds := tdataset(FUsedDatasets[i]);
  307. if ds.active then ds.Close;
  308. ds.Free;
  309. end;
  310. FUsedDatasets.Clear;
  311. if FChangedFieldDataset then ResetFieldDataset;
  312. for i := 0 to MaxDataSet do if FChangedDatasets[i] then
  313. begin
  314. ResetNDatasets;
  315. fillchar(FChangedDatasets,sizeof(FChangedDatasets),ord(False));
  316. break;
  317. end;
  318. end;
  319. procedure TDBConnector.LogMessage(Category,Message: string);
  320. begin
  321. if dblogfilename<>'' then //double check: only if logging enabled
  322. begin
  323. try
  324. Message:=StringReplace(Message, #9, '\t', [rfReplaceAll, rfIgnoreCase]);
  325. Message:=StringReplace(Message, LineEnding, '\n', [rfReplaceAll, rfIgnoreCase]);
  326. writeln(dbLogFile, TimeToStr(Now(), FLogTimeFormat) + #9 +
  327. Category + #9 +
  328. Message);
  329. Flush(dbLogFile); //in case tests crash
  330. except
  331. // ignore log file errors
  332. end;
  333. end;
  334. end;
  335. function TDBConnector.GetCharSize: integer;
  336. begin
  337. case LowerCase(dbcharset) of
  338. 'utf8','utf-8','utf8mb4':
  339. Result := 4;
  340. else
  341. Result := 1;
  342. end;
  343. end;
  344. { TTestDataLink }
  345. procedure TTestDataLink.DataSetScrolled(Distance: Integer);
  346. begin
  347. DataEvents := DataEvents + 'DataSetScrolled' + ':' + inttostr(Distance) + ';';
  348. inherited DataSetScrolled(Distance);
  349. end;
  350. procedure TTestDataLink.DataSetChanged;
  351. begin
  352. DataEvents := DataEvents + 'DataSetChanged;';
  353. inherited DataSetChanged;
  354. end;
  355. {$IFDEF FPC}
  356. procedure TTestDataLink.DataEvent(Event: TDataEvent; Info: Ptrint);
  357. {$ELSE}
  358. procedure TTestDataLink.DataEvent(Event: TDataEvent; Info: Longint);
  359. {$ENDIF}
  360. begin
  361. if Event <> deFieldChange then
  362. DataEvents := DataEvents + DataEventnames[Event] + ':' + inttostr(info) + ';'
  363. else
  364. DataEvents := DataEvents + DataEventnames[Event] + ':' + TField(info).FieldName + ';';
  365. inherited DataEvent(Event, Info);
  366. end;
  367. { TDBBasicsTestSetup }
  368. procedure TDBBasicsTestSetup.OneTimeSetup;
  369. begin
  370. InitialiseDBConnector;
  371. end;
  372. procedure TDBBasicsTestSetup.OneTimeTearDown;
  373. begin
  374. FreeDBConnector;
  375. end;
  376. { TDBBasicsTestCase }
  377. procedure TDBBasicsTestCase.SetUp;
  378. begin
  379. inherited SetUp;
  380. DBConnector.StartTest(TestName);
  381. end;
  382. procedure TDBBasicsTestCase.TearDown;
  383. begin
  384. DBConnector.StopTest(TestName);
  385. inherited TearDown;
  386. end;
  387. procedure TDBBasicsTestCase.CheckFieldDatasetValues(ADataSet: TDataSet);
  388. var i: integer;
  389. begin
  390. with ADataSet do
  391. begin
  392. First;
  393. for i := 0 to testValuesCount-1 do
  394. begin
  395. CheckEquals(i, FieldByName('ID').AsInteger, 'ID');
  396. CheckEquals(testStringValues[i], FieldByName('FSTRING').AsString, 'FSTRING');
  397. CheckEquals(testIntValues[i], FieldByName('FINTEGER').AsInteger, 'FINTEGER');
  398. CheckEquals(testLargeIntValues[i], FieldByName('FLARGEINT').AsLargeInt, 'FLARGEINT');
  399. Next;
  400. end;
  401. CheckTrue(Eof, 'Eof');
  402. end;
  403. end;
  404. procedure TDBBasicsTestCase.CheckNDatasetValues(ADataSet: TDataSet; n: integer);
  405. var i: integer;
  406. begin
  407. with ADataSet do
  408. begin
  409. First;
  410. for i := 1 to n do
  411. begin
  412. CheckEquals(i, FieldByName('ID').AsInteger, 'ID');
  413. CheckEquals('TestName' + inttostr(i), FieldByName('NAME').AsString, 'NAME');
  414. Next;
  415. end;
  416. CheckTrue(Eof, 'Eof');
  417. end;
  418. end;
  419. procedure ReadIniFile;
  420. var IniFile : TIniFile;
  421. begin
  422. IniFile := TIniFile.Create(GetCurrentDir + PathDelim + 'database.ini');
  423. dbtype:='';
  424. if ParamCount>0 then
  425. dbtype := ParamStr(1);
  426. if (dbtype='') or not IniFile.SectionExists(dbtype) then
  427. dbtype := IniFile.ReadString('Database','Type','');
  428. dbconnectorname := IniFile.ReadString(dbtype,'Connector','');
  429. dbname := IniFile.ReadString(dbtype,'Name','');
  430. dbuser := IniFile.ReadString(dbtype,'User','');
  431. dbhostname := IniFile.ReadString(dbtype,'Hostname','');
  432. dbpassword := IniFile.ReadString(dbtype,'Password','');
  433. dbcharset := IniFile.ReadString(dbtype,'CharSet','');
  434. dbconnectorparams := IniFile.ReadString(dbtype,'ConnectorParams','');
  435. dblogfilename := IniFile.ReadString(dbtype,'LogFile','');
  436. dbquotechars := IniFile.ReadString(dbtype,'QuoteChars','"');
  437. IniFile.Free;
  438. end;
  439. procedure SetupLog;
  440. begin
  441. if dblogfilename<>'' then
  442. begin
  443. try
  444. AssignFile(dblogfile,dblogfilename);
  445. if not(FileExists(dblogfilename)) then
  446. begin
  447. ReWrite(dblogfile);
  448. CloseFile(dblogfile);
  449. end;
  450. Append(dblogfile);
  451. except
  452. dblogfilename:=''; //rest of code relies on this as a log switch
  453. end;
  454. end;
  455. end;
  456. procedure CloseLog;
  457. begin
  458. if dblogfilename<>'' then
  459. begin
  460. try
  461. CloseFile(dbLogFile);
  462. except
  463. // Ignore log file errors
  464. end;
  465. end;
  466. end;
  467. procedure InitialiseDBConnector;
  468. var DBConnectorClass : TPersistentClass;
  469. i : integer;
  470. FormatSettings : TFormatSettings;
  471. begin
  472. if DBConnectorRefCount>0 then exit;
  473. FormatSettings.DecimalSeparator:='.';
  474. FormatSettings.ThousandSeparator:=#0;
  475. testValues[ftString] := testStringValues;
  476. testValues[ftFixedChar] := testStringValues;
  477. testValues[ftTime] := testTimeValues;
  478. testValues[ftDate] := testDateValues;
  479. testValues[ftBlob] := testStringValues;
  480. testValues[ftMemo] := testStringValues;
  481. testValues[ftWideString] := testStringValues;
  482. testValues[ftWideMemo] := testStringValues;
  483. testValues[ftFMTBcd] := testFmtBCDValues;
  484. for i := 0 to testValuesCount-1 do
  485. begin
  486. testValues[ftBoolean,i] := BoolToStr(testBooleanValues[i], True);
  487. testValues[ftFloat,i] := FloatToStr(testFloatValues[i],FormatSettings);
  488. testValues[ftSmallint,i] := IntToStr(testSmallIntValues[i]);
  489. testValues[ftInteger,i] := IntToStr(testIntValues[i]);
  490. testValues[ftWord,i] := IntToStr(testWordValues[i]);
  491. testValues[ftLargeint,i] := IntToStr(testLargeIntValues[i]);
  492. testValues[ftLongWord,i] := IntToStr(testLongWordValues[i]);
  493. testValues[ftCurrency,i] := CurrToStr(testCurrencyValues[i],FormatSettings);
  494. testValues[ftBCD,i] := CurrToStr(testCurrencyValues[i],FormatSettings);
  495. // For date '0001-01-01' other time-part like '00:00:00' causes "Invalid variant type cast", because of < MinDateTime constant
  496. if (testDateValues[i]>'0001-01-01') and (testTimeValues[i]>='00:00:01') and (testTimeValues[i]<'24:00:00') then
  497. testValues[ftDateTime,i] := testDateValues[i] + ' ' + testTimeValues[i]
  498. else
  499. testValues[ftDateTime,i] := testDateValues[i];
  500. end;
  501. if dbconnectorname = '' then raise Exception.Create('There is no db connector specified');
  502. DBConnectorClass := GetClass('T'+dbconnectorname+'DBConnector');
  503. if assigned(DBConnectorClass) then
  504. DBConnector := TDBConnectorClass(DBConnectorClass).create
  505. else Raise Exception.Create('Unknown db connector specified: ' + 'T'+dbconnectorname+'DBConnector');
  506. inc(DBConnectorRefCount);
  507. end;
  508. procedure FreeDBConnector;
  509. begin
  510. dec(DBConnectorRefCount);
  511. if DBConnectorRefCount=0 then
  512. FreeAndNil(DBConnector);
  513. end;
  514. function DateTimeToTimeString(d: tdatetime): string;
  515. var
  516. millisecond: word;
  517. second : word;
  518. minute : word;
  519. hour : word;
  520. begin
  521. // Format the datetime in the format hh:nn:ss.zzz, where the hours can be bigger then 23.
  522. DecodeTime(d,hour,minute,second,millisecond);
  523. hour := hour + (trunc(d) * 24);
  524. result := Format('%.2d:%.2d:%.2d.%.3d',[hour,minute,second,millisecond]);
  525. end;
  526. function TimeStringToDateTime(d: String): TDateTime;
  527. var
  528. millisecond: word;
  529. second : word;
  530. minute : word;
  531. hour : word;
  532. days : word;
  533. begin
  534. // Convert the string in the format hh:nn:ss.zzz to a datetime.
  535. hour := strtoint(copy(d,1,2));
  536. minute := strtoint(copy(d,4,2));
  537. second := strtoint(copy(d,7,2));
  538. millisecond := strtoint(copy(d,10,3));
  539. days := hour div 24;
  540. hour := hour mod 24;
  541. result := ComposeDateTime(days,EncodeTime(hour,minute,second,millisecond));
  542. end;
  543. function StringToByteArray(const s: ansistring): Variant;
  544. var P: Pointer;
  545. Len: integer;
  546. begin
  547. Len := Length(s) * SizeOf(AnsiChar);
  548. Result := VarArrayCreate([0, Len-1], varByte);
  549. P := VarArrayLock(Result);
  550. try
  551. Move(s[1], P^, Len);
  552. finally
  553. VarArrayUnlock(Result);
  554. end;
  555. end;
  556. initialization
  557. ReadIniFile;
  558. SetupLog;
  559. DBConnectorRefCount:=0;
  560. finalization
  561. CloseLog;
  562. end.