toolsunit.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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(const s: ansistring): Variant;
  210. function StringToBytes(const s: ansistring): TBytes;
  211. implementation
  212. uses
  213. inifiles, FmtBCD, Variants;
  214. var DBConnectorRefCount: integer;
  215. { TDBConnector }
  216. constructor TDBConnector.Create;
  217. begin
  218. FFormatSettings.DecimalSeparator:='.';
  219. FFormatSettings.ThousandSeparator:=#0;
  220. FFormatSettings.DateSeparator:='-';
  221. FFormatSettings.TimeSeparator:=':';
  222. FFormatSettings.ShortDateFormat:='yyyy/mm/dd';
  223. FFormatSettings.LongTimeFormat:='hh:nn:ss.zzz';
  224. // Set up time format for logging output:
  225. // ISO 8601 type date string so logging is uniform across machines
  226. FLogTimeFormat.DecimalSeparator:='.';
  227. FLogTimeFormat.ThousandSeparator:=#0;
  228. FLogTimeFormat.DateSeparator:='-';
  229. FLogTimeFormat.TimeSeparator:=':';
  230. FLogTimeFormat.ShortDateFormat:='yyyy-mm-dd';
  231. FLogTimeFormat.LongTimeFormat:='hh:nn:ss';
  232. FUsedDatasets := TFPList.Create;
  233. CreateFieldDataset;
  234. CreateNDatasets;
  235. end;
  236. destructor TDBConnector.Destroy;
  237. begin
  238. if assigned(FUsedDatasets) then FUsedDatasets.Destroy;
  239. DropNDatasets;
  240. DropFieldDataset;
  241. Inherited;
  242. end;
  243. function TDBConnector.GetTestUniDirectional: boolean;
  244. begin
  245. result := false;
  246. end;
  247. procedure TDBConnector.SetTestUniDirectional(const AValue: boolean);
  248. begin
  249. raise exception.create('Connector does not support tests for unidirectional datasets');
  250. end;
  251. procedure TDBConnector.DataEvent(dataset : tdataset);
  252. begin
  253. DataEvents := DataEvents + 'DataEvent' + ';';
  254. end;
  255. procedure TDBConnector.ResetNDatasets;
  256. begin
  257. DropNDatasets;
  258. CreateNDatasets;
  259. end;
  260. procedure TDBConnector.ResetFieldDataset;
  261. begin
  262. DropFieldDataset;
  263. CreateFieldDataset;
  264. end;
  265. function TDBConnector.GetNDataset(n: integer): TDataset;
  266. begin
  267. Result := GetNDataset(False,n);
  268. end;
  269. function TDBConnector.GetNDataset(AChange : Boolean; n: integer): TDataset;
  270. begin
  271. if AChange then FChangedDatasets[n] := True;
  272. Result := InternalGetNDataset(n);
  273. FUsedDatasets.Add(Result);
  274. end;
  275. function TDBConnector.GetFieldDataset: TDataSet;
  276. begin
  277. Result := GetFieldDataset(False);
  278. end;
  279. function TDBConnector.GetFieldDataset(AChange: Boolean): TDataSet;
  280. begin
  281. if AChange then FChangedFieldDataset := True;
  282. Result := InternalGetFieldDataset;
  283. FUsedDatasets.Add(Result);
  284. end;
  285. function TDBConnector.GetTraceDataset(AChange: Boolean): TDataset;
  286. begin
  287. result := GetNDataset(AChange,NForTraceDataset);
  288. end;
  289. procedure TDBConnector.StartTest(TestName: string);
  290. begin
  291. // Log if necessary
  292. LogMessage('Test','Starting test '+TestName);
  293. end;
  294. procedure TDBConnector.StopTest(TestName: string);
  295. var i : integer;
  296. ds : TDataset;
  297. begin
  298. LogMessage('Test','Stopping test '+TestName);
  299. for i := 0 to FUsedDatasets.Count -1 do
  300. begin
  301. ds := tdataset(FUsedDatasets[i]);
  302. if ds.active then ds.Close;
  303. ds.Free;
  304. end;
  305. FUsedDatasets.Clear;
  306. if FChangedFieldDataset then ResetFieldDataset;
  307. for i := 0 to MaxDataSet do if FChangedDatasets[i] then
  308. begin
  309. ResetNDatasets;
  310. fillchar(FChangedDatasets,sizeof(FChangedDatasets),ord(False));
  311. break;
  312. end;
  313. end;
  314. procedure TDBConnector.LogMessage(Category,Message: string);
  315. begin
  316. if dblogfilename<>'' then //double check: only if logging enabled
  317. begin
  318. try
  319. Message:=StringReplace(Message, #9, '\t', [rfReplaceAll, rfIgnoreCase]);
  320. Message:=StringReplace(Message, LineEnding, '\n', [rfReplaceAll, rfIgnoreCase]);
  321. writeln(dbLogFile, TimeToStr(Now(), FLogTimeFormat) + #9 +
  322. Category + #9 +
  323. Message);
  324. Flush(dbLogFile); //in case tests crash
  325. except
  326. // ignore log file errors
  327. end;
  328. end;
  329. end;
  330. { TTestDataLink }
  331. procedure TTestDataLink.DataSetScrolled(Distance: Integer);
  332. begin
  333. DataEvents := DataEvents + 'DataSetScrolled' + ':' + inttostr(Distance) + ';';
  334. inherited DataSetScrolled(Distance);
  335. end;
  336. procedure TTestDataLink.DataSetChanged;
  337. begin
  338. DataEvents := DataEvents + 'DataSetChanged;';
  339. inherited DataSetChanged;
  340. end;
  341. {$IFDEF FPC}
  342. procedure TTestDataLink.DataEvent(Event: TDataEvent; Info: Ptrint);
  343. {$ELSE}
  344. procedure TTestDataLink.DataEvent(Event: TDataEvent; Info: Longint);
  345. {$ENDIF}
  346. begin
  347. if Event <> deFieldChange then
  348. DataEvents := DataEvents + DataEventnames[Event] + ':' + inttostr(info) + ';'
  349. else
  350. DataEvents := DataEvents + DataEventnames[Event] + ':' + TField(info).FieldName + ';';
  351. inherited DataEvent(Event, Info);
  352. end;
  353. { TDBBasicsTestSetup }
  354. procedure TDBBasicsTestSetup.OneTimeSetup;
  355. begin
  356. InitialiseDBConnector;
  357. end;
  358. procedure TDBBasicsTestSetup.OneTimeTearDown;
  359. begin
  360. FreeDBConnector;
  361. end;
  362. { TDBBasicsTestCase }
  363. procedure TDBBasicsTestCase.SetUp;
  364. begin
  365. inherited SetUp;
  366. DBConnector.StartTest(TestName);
  367. end;
  368. procedure TDBBasicsTestCase.TearDown;
  369. begin
  370. DBConnector.StopTest(TestName);
  371. inherited TearDown;
  372. end;
  373. procedure TDBBasicsTestCase.CheckFieldDatasetValues(ADataSet: TDataSet);
  374. var i: integer;
  375. begin
  376. with ADataSet do
  377. begin
  378. First;
  379. for i := 0 to testValuesCount-1 do
  380. begin
  381. CheckEquals(i, FieldByName('ID').AsInteger, 'ID');
  382. CheckEquals(testStringValues[i], FieldByName('FSTRING').AsString, 'FSTRING');
  383. CheckEquals(testIntValues[i], FieldByName('FINTEGER').AsInteger, 'FINTEGER');
  384. CheckEquals(testLargeIntValues[i], FieldByName('FLARGEINT').AsLargeInt, 'FLARGEINT');
  385. Next;
  386. end;
  387. CheckTrue(Eof, 'Eof');
  388. end;
  389. end;
  390. procedure TDBBasicsTestCase.CheckNDatasetValues(ADataSet: TDataSet; n: integer);
  391. var i: integer;
  392. begin
  393. with ADataSet do
  394. begin
  395. First;
  396. for i := 1 to n do
  397. begin
  398. CheckEquals(i, FieldByName('ID').AsInteger, 'ID');
  399. CheckEquals('TestName' + inttostr(i), FieldByName('NAME').AsString, 'NAME');
  400. Next;
  401. end;
  402. CheckTrue(Eof, 'Eof');
  403. end;
  404. end;
  405. procedure ReadIniFile;
  406. var IniFile : TIniFile;
  407. begin
  408. IniFile := TIniFile.Create(getcurrentdir + PathDelim + 'database.ini');
  409. dbtype:='';
  410. if Paramcount>0 then
  411. dbtype := ParamStr(1);
  412. if (dbtype='') or not inifile.SectionExists(dbtype) then
  413. dbtype := IniFile.ReadString('Database','Type','');
  414. dbconnectorname := IniFile.ReadString(dbtype,'Connector','');
  415. dbname := IniFile.ReadString(dbtype,'Name','');
  416. dbuser := IniFile.ReadString(dbtype,'User','');
  417. dbhostname := IniFile.ReadString(dbtype,'Hostname','');
  418. dbpassword := IniFile.ReadString(dbtype,'Password','');
  419. dbconnectorparams := IniFile.ReadString(dbtype,'ConnectorParams','');
  420. dblogfilename := IniFile.ReadString(dbtype,'LogFile','');
  421. dbquotechars := IniFile.ReadString(dbtype,'QuoteChars','"');
  422. IniFile.Free;
  423. end;
  424. procedure SetupLog;
  425. begin
  426. if dblogfilename<>'' then
  427. begin
  428. try
  429. AssignFile(dblogfile,dblogfilename);
  430. if not(FileExists(dblogfilename)) then
  431. begin
  432. ReWrite(dblogfile);
  433. CloseFile(dblogfile);
  434. end;
  435. Append(dblogfile);
  436. except
  437. dblogfilename:=''; //rest of code relies on this as a log switch
  438. end;
  439. end;
  440. end;
  441. procedure CloseLog;
  442. begin
  443. if dblogfilename<>'' then
  444. begin
  445. try
  446. CloseFile(dbLogFile);
  447. except
  448. // Ignore log file errors
  449. end;
  450. end;
  451. end;
  452. procedure InitialiseDBConnector;
  453. const B: array[boolean] of char=('0','1'); // should be exported from some main db unit, as SQL true/false?
  454. var DBConnectorClass : TPersistentClass;
  455. i : integer;
  456. FormatSettings : TFormatSettings;
  457. begin
  458. if DBConnectorRefCount>0 then exit;
  459. FormatSettings.DecimalSeparator:='.';
  460. FormatSettings.ThousandSeparator:=#0;
  461. testValues[ftString] := testStringValues;
  462. testValues[ftFixedChar] := testStringValues;
  463. testValues[ftTime] := testTimeValues;
  464. testValues[ftDate] := testDateValues;
  465. testValues[ftBlob] := testStringValues;
  466. testValues[ftMemo] := testStringValues;
  467. testValues[ftWideString] := testStringValues;
  468. testValues[ftWideMemo] := testStringValues;
  469. testValues[ftFMTBcd] := testFmtBCDValues;
  470. for i := 0 to testValuesCount-1 do
  471. begin
  472. testValues[ftBoolean,i] := B[testBooleanValues[i]];
  473. testValues[ftFloat,i] := FloatToStr(testFloatValues[i],FormatSettings);
  474. testValues[ftSmallint,i] := IntToStr(testSmallIntValues[i]);
  475. testValues[ftInteger,i] := IntToStr(testIntValues[i]);
  476. testValues[ftWord,i] := IntToStr(testWordValues[i]);
  477. testValues[ftLargeint,i] := IntToStr(testLargeIntValues[i]);
  478. testValues[ftCurrency,i] := CurrToStr(testCurrencyValues[i],FormatSettings);
  479. testValues[ftBCD,i] := CurrToStr(testCurrencyValues[i],FormatSettings);
  480. // For date '0001-01-01' other time-part like '00:00:00' causes "Invalid variant type cast", because of < MinDateTime constant
  481. if (testDateValues[i]>'0001-01-01') and (testTimeValues[i]>='00:00:01') and (testTimeValues[i]<'24:00:00') then
  482. testValues[ftDateTime,i] := testDateValues[i] + ' ' + testTimeValues[i]
  483. else
  484. testValues[ftDateTime,i] := testDateValues[i];
  485. end;
  486. if dbconnectorname = '' then raise Exception.Create('There is no db connector specified');
  487. DBConnectorClass := GetClass('T'+dbconnectorname+'DBConnector');
  488. if assigned(DBConnectorClass) then
  489. DBConnector := TDBConnectorClass(DBConnectorClass).create
  490. else Raise Exception.Create('Unknown db connector specified: ' + 'T'+dbconnectorname+'DBConnector');
  491. inc(DBConnectorRefCount);
  492. end;
  493. procedure FreeDBConnector;
  494. begin
  495. dec(DBConnectorRefCount);
  496. if DBConnectorRefCount=0 then
  497. FreeAndNil(DBConnector);
  498. end;
  499. function DateTimeToTimeString(d: tdatetime): string;
  500. var
  501. millisecond: word;
  502. second : word;
  503. minute : word;
  504. hour : word;
  505. begin
  506. // Format the datetime in the format hh:nn:ss.zzz, where the hours can be bigger then 23.
  507. DecodeTime(d,hour,minute,second,millisecond);
  508. hour := hour + (trunc(d) * 24);
  509. result := Format('%.2d:%.2d:%.2d.%.3d',[hour,minute,second,millisecond]);
  510. end;
  511. function TimeStringToDateTime(d: String): TDateTime;
  512. var
  513. millisecond: word;
  514. second : word;
  515. minute : word;
  516. hour : word;
  517. days : word;
  518. begin
  519. // Convert the string in the format hh:nn:ss.zzz to a datetime.
  520. hour := strtoint(copy(d,1,2));
  521. minute := strtoint(copy(d,4,2));
  522. second := strtoint(copy(d,7,2));
  523. millisecond := strtoint(copy(d,10,3));
  524. days := hour div 24;
  525. hour := hour mod 24;
  526. result := ComposeDateTime(days,EncodeTime(hour,minute,second,millisecond));
  527. end;
  528. function StringToByteArray(const s: ansistring): Variant;
  529. var P: Pointer;
  530. Len: integer;
  531. begin
  532. Len := Length(s) * SizeOf(AnsiChar);
  533. Result := VarArrayCreate([0, Len-1], varByte);
  534. P := VarArrayLock(Result);
  535. try
  536. Move(s[1], P^, Len);
  537. finally
  538. VarArrayUnlock(Result);
  539. end;
  540. end;
  541. function StringToBytes(const s: ansistring): TBytes;
  542. var Len: integer;
  543. begin
  544. Len := Length(s) * SizeOf(AnsiChar);
  545. SetLength(Result, Len);
  546. Move(s[1], Result[0], Len);
  547. end;
  548. initialization
  549. ReadIniFile;
  550. SetupLog;
  551. DBConnectorRefCount:=0;
  552. finalization
  553. CloseLog;
  554. end.