toolsunit.pas 19 KB

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