dbconfig.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. {
  2. This file is part of the Free Pascal test suite.
  3. Copyright (c) 2002 by the Free Pascal development team.
  4. This program updates TESTCONFIG anf TESTRUNHISTORY tables
  5. with the last tests run.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}
  13. {$h+}
  14. {$ifndef win32}
  15. {$linklib pthread}
  16. {$endif}
  17. program dbconfig;
  18. uses
  19. sysutils,teststr,testu,tresults,
  20. sqldb,dbtests;
  21. Var
  22. StatusCount : Array[TTestStatus] of Integer;
  23. UnknownLines : integer;
  24. Procedure ExtractTestFileName(Var Line : string);
  25. Var I : integer;
  26. begin
  27. I:=Pos(' ',Line);
  28. If (I<>0) then
  29. Line:=Copy(Line,1,I-1);
  30. end;
  31. Function Analyse(Var Line : string; Var Status : TTestStatus) : Boolean;
  32. Var
  33. TS : TTestStatus;
  34. begin
  35. Result:=False;
  36. For TS:=FirstStatus to LastStatus do
  37. begin
  38. Result:=Pos(StatusText[TS],Line)=1;
  39. If Result then
  40. begin
  41. Status:=TS;
  42. Delete(Line,1,Length(StatusText[TS]));
  43. ExtractTestFileName(Line);
  44. Break;
  45. end;
  46. end;
  47. end;
  48. Type
  49. TConfigOpt = (
  50. coDatabaseName,
  51. coHost,
  52. coUserName,
  53. coPassword,
  54. coPort,
  55. coLogFile,
  56. coLongLogFile,
  57. coOS,
  58. coCPU,
  59. coCategory,
  60. coVersion,
  61. coDate,
  62. coSubmitter,
  63. coMachine,
  64. coComment,
  65. coTestSrcDir,
  66. coRelSrcDir,
  67. coVerbose,
  68. coOffset
  69. );
  70. { Additional options only for dbdigest.cfg file }
  71. TConfigAddOpt = (
  72. coCompilerDate,
  73. coCompilerFullVersion,
  74. coSvnCompilerRevision,
  75. coSvnTestsRevision,
  76. coSvnRTLRevision,
  77. coSvnPackagesRevision
  78. );
  79. Const
  80. ConfigStrings : Array [TConfigOpt] of string = (
  81. 'databasename',
  82. 'host',
  83. 'username',
  84. 'password',
  85. 'port',
  86. 'logfile',
  87. 'longlogfile',
  88. 'os',
  89. 'cpu',
  90. 'category',
  91. 'version',
  92. 'date',
  93. 'submitter',
  94. 'machine',
  95. 'comment',
  96. 'testsrcdir',
  97. 'relsrcdir',
  98. 'verbose',
  99. 'offset'
  100. );
  101. ConfigOpts : Array[TConfigOpt] of char =(
  102. 'd', { coDatabaseName }
  103. 'h', { coHost }
  104. 'u', { coUserName }
  105. 'p', { coPassword }
  106. 'P', { coPort }
  107. 'l', { coLogFile }
  108. 'L', { coLongLogFile }
  109. 'o', { coOS }
  110. 'c', { coCPU }
  111. 'a', { coCategory }
  112. 'v', { coVersion }
  113. 't', { coDate }
  114. 's', { coSubmitter }
  115. 'm', { coMachine }
  116. 'C', { coComment }
  117. 'S', { coTestSrcDir }
  118. 'r', { coRelSrcDir }
  119. 'V', { coVerbose }
  120. 'O' { coOffset }
  121. );
  122. ConfigAddStrings : Array [TConfigAddOpt] of string = (
  123. 'compilerdate',
  124. 'compilerfullversion',
  125. 'svncompilerrevision',
  126. 'svntestsrevision',
  127. 'svnrtlrevision',
  128. 'svnpackagesrevision'
  129. );
  130. ConfigAddCols : Array [TConfigAddOpt] of string = (
  131. 'TU_COMPILERDATE',
  132. 'TU_COMPILERFULLVERSION',
  133. 'TU_SVNCOMPILERREVISION',
  134. 'TU_SVNTESTSREVISION',
  135. 'TU_SVNRTLREVISION',
  136. 'TU_SVNPACKAGESREVISION'
  137. );
  138. Var
  139. TestOS,
  140. TestCPU,
  141. TestVersion,
  142. TestCategory,
  143. DatabaseName,
  144. HostName,
  145. UserName,
  146. Password,
  147. Port,
  148. LongLogFileName,
  149. LogFileName,
  150. Submitter,
  151. Machine,
  152. Comment,
  153. OffsetString : String;
  154. TestDate : TDateTime;
  155. TestCompilerDate,
  156. TestCompilerFullVersion,
  157. TestSvnCompilerRevision,
  158. TestSvnTestsRevision,
  159. TestSvnRTLRevision,
  160. TestSvnPackagesRevision : String;
  161. ConfigID : Integer;
  162. Procedure SetAddOpt (O : TConfigAddOpt; Value : string);
  163. begin
  164. Case O of
  165. coCompilerDate:
  166. TestCompilerDate:=Value;
  167. coCompilerFullVersion:
  168. TestCompilerFullVersion:=Value;
  169. coSvnCompilerRevision:
  170. TestSvnCompilerRevision:=Value;
  171. coSvnTestsRevision:
  172. TestSvnTestsRevision:=Value;
  173. coSvnRTLRevision:
  174. TestSvnRTLRevision:=Value;
  175. coSvnPackagesRevision:
  176. TestSvnPackagesRevision:=Value;
  177. end;
  178. end;
  179. Procedure SetOpt (O : TConfigOpt; Value : string);
  180. var
  181. year,month,day,min,hour : word;
  182. begin
  183. Case O of
  184. coDatabaseName : DatabaseName:=Value;
  185. coHost : HostName:=Value;
  186. coUserName : UserName:=Value;
  187. coPassword : Password:=Value;
  188. coPort : Port:=Value;
  189. coLogFile : LogFileName:=Value;
  190. coLongLogFile : LongLogFileName:=Value;
  191. coOS : TestOS:=Value;
  192. coCPU : TestCPU:=Value;
  193. coCategory : TestCategory:=Value;
  194. coVersion : TestVersion:=Value;
  195. coDate :
  196. begin
  197. { Formated like YYYYMMDDhhmm }
  198. if Length(value)=12 then
  199. begin
  200. year:=StrToInt(Copy(value,1,4));
  201. month:=StrToInt(Copy(value,5,2));
  202. day:=StrToInt(Copy(Value,7,2));
  203. hour:=StrToInt(Copy(Value,9,2));
  204. min:=StrToInt(Copy(Value,11,2));
  205. TestDate:=EncodeDate(year,month,day)+EncodeTime(hour,min,0,0);
  206. end
  207. else
  208. Verbose(V_Error,'Error in date format, use YYYYMMDDhhmm');
  209. end;
  210. coSubmitter : Submitter:=Value;
  211. coMachine : Machine:=Value;
  212. coComment : Comment:=Value;
  213. coOffset : OffsetString:=Value;
  214. coVerbose : DoVerbose:=true;
  215. coTestSrcDir :
  216. begin
  217. TestSrcDir:=Value;
  218. if (TestSrcDir<>'') and (TestSrcDir[length(TestSrcDir)]<>'/') then
  219. TestSrcDir:=TestSrcDir+'/';
  220. end;
  221. coRelSrcDir :
  222. begin
  223. RelSrcDir:=Value;
  224. if (RelSrcDir<>'') and (RelSrcDir[length(RelSrcDir)]<>'/') then
  225. RelSrcDir:=RelSrcDir+'/';
  226. if (RelSrcDir<>'') and (RelSrcDir[1]='/') then
  227. RelSrcDir:=copy(RelSrcDir,2,length(RelSrcDir)-1);
  228. end;
  229. end;
  230. end;
  231. Function ProcessOption(S: String) : Boolean;
  232. Var
  233. N : String;
  234. I : Integer;
  235. co : TConfigOpt;
  236. coa : TConfigAddOpt;
  237. begin
  238. Verbose(V_DEBUG,'Processing option: '+S);
  239. I:=Pos('=',S);
  240. Result:=(I<>0);
  241. If Result then
  242. begin
  243. N:=Copy(S,1,I-1);
  244. Delete(S,1,I);
  245. For co:=low(TConfigOpt) to high(TConfigOpt) do
  246. begin
  247. Result:=CompareText(ConfigStrings[co],N)=0;
  248. If Result then
  249. begin
  250. SetOpt(co,S);
  251. Exit;
  252. end;
  253. end;
  254. For coa:=low(TConfigAddOpt) to high(TConfigAddOpt) do
  255. begin
  256. Result:=CompareText(ConfigAddStrings[coa],N)=0;
  257. If Result then
  258. begin
  259. SetAddOpt(coa,S);
  260. Exit;
  261. end;
  262. end;
  263. end;
  264. Verbose(V_ERROR,'Unknown option : '+n+S);
  265. end;
  266. Procedure ProcessConfigfile(FN : String);
  267. Var
  268. F : Text;
  269. S : String;
  270. I : Integer;
  271. begin
  272. // Set the default value for old digests without RelSrcDir to the rtl/compiler
  273. // testsuite
  274. RelSrcDir:='tests/';
  275. If Not FileExists(FN) Then
  276. Exit;
  277. Verbose(V_DEBUG,'Parsing config file: '+FN);
  278. Assign(F,FN);
  279. {$i-}
  280. Reset(F);
  281. If IOResult<>0 then
  282. Exit;
  283. {$I+}
  284. While not(EOF(F)) do
  285. begin
  286. ReadLn(F,S);
  287. S:=trim(S);
  288. I:=Pos('#',S);
  289. If I<>0 then
  290. S:=Copy(S,1,I-1);
  291. If (S<>'') then
  292. ProcessOption(S);
  293. end;
  294. Close(F);
  295. end;
  296. Procedure ProcessCommandLine;
  297. Var
  298. I : Integer;
  299. O : String;
  300. c,co : TConfigOpt;
  301. ShortOptFound, Found : Boolean;
  302. begin
  303. I:=1;
  304. While I<=ParamCount do
  305. begin
  306. O:=Paramstr(I);
  307. ShortOptFound:=(Length(O)=2) and (O[1]='-');
  308. If ShortOptFound then
  309. For co:=low(TConfigOpt) to high(TConfigOpt) do
  310. begin
  311. Found:=(O[2]=ConfigOpts[co]);
  312. If Found then
  313. begin
  314. c:=co;
  315. Break;
  316. end;
  317. end;
  318. If not ShortOptFound then
  319. begin
  320. Found:=false;
  321. { accept long options }
  322. if (copy(O,1,2)='--') then
  323. begin
  324. { remove -- }
  325. O:=copy(O,3,length(O));
  326. For co:=low(TConfigOpt) to high(TConfigOpt) do
  327. begin
  328. Found:=(O=ConfigStrings[co]);
  329. If Found then
  330. begin
  331. c:=co;
  332. Break;
  333. end;
  334. end;
  335. end
  336. end;
  337. if not Found then
  338. Verbose(V_ERROR,'Illegal command-line option : '+O)
  339. else
  340. begin
  341. Found:=(I<ParamCount);
  342. If Not found then
  343. Verbose(V_ERROR,'Option requires argument : '+O)
  344. else
  345. begin
  346. inc(I);
  347. O:=Paramstr(I);
  348. SetOpt(c,o);
  349. end;
  350. end;
  351. Inc(I);
  352. end;
  353. end;
  354. function GetTestRunFieldID(const name : string; TestRunID : Integer) : Integer;
  355. begin
  356. GetTestRunFieldID:=IDQuery(
  357. format('SELECT %s FROM TESTRUN WHERE TU_ID=%d',[name,TestRunID]));
  358. end;
  359. function GetTestRunStringFieldID(const name : string; TestRunID : Integer) : String;
  360. begin
  361. GetTestRunStringFieldID:=StringQuery(
  362. format('SELECT %s FROM TESTRUN WHERE TU_ID=%d',[name,TestRunID]));
  363. end;
  364. function GetSubmitter(TestRunID:Integer) : String;
  365. begin
  366. GetSubmitter:=GetTestRunStringFieldID('TU_SUBMITTER',TestRunID);
  367. end;
  368. function GetComment(TestRunID:Integer) : String;
  369. begin
  370. GetComment:=GetTestRunStringFieldID('TU_COMMENT',TestRunID);
  371. end;
  372. function GetMachine(TestRunID:Integer) : String;
  373. begin
  374. GetMachine:=GetTestRunStringFieldID('TU_MACHINE',TestRunID);
  375. end;
  376. function GetDate(TestRunID:Integer) : String;
  377. begin
  378. GetDate:=GetTestRunStringFieldID('TU_DATE',TestRunID);
  379. end;
  380. function GetTestConfigId(TestRunID : Integer) : Integer;
  381. var
  382. qry : string;
  383. begin
  384. qry:='SELECT TCONF_ID FROM TESTCONFIG WHERE ' +
  385. 'TCONF_CPU_FK=%d AND ' +
  386. 'TCONF_OS_FK=%d AND ' +
  387. 'TCONF_VERSION_FK=%d AND ' +
  388. 'TCONF_CATEGORY_FK=%d AND ' +
  389. 'TCONF_SUBMITTER="%s" AND ' +
  390. 'TCONF_MACHINE="%s" AND ' +
  391. 'TCONF_COMMENT="%s" ';
  392. ConfigID:=IDQuery(format(qry,[
  393. GetTestRunFieldID('TU_CPU_FK',TestRunID),
  394. GetTestRunFieldID('TU_OS_FK',TestRunID),
  395. GetTestRunFieldID('TU_VERSION_FK',TestRunID),
  396. GetTestRunFieldID('TU_CATEGORY_FK',TestRunID),
  397. GetSubmitter(TestRunID),
  398. GetMachine(TestRunID),
  399. GetComment(TestRunID)]));
  400. GetTestConfigID:=ConfigID;
  401. end;
  402. function UpdateTestConfigID(TestRunID : Integer) : boolean;
  403. var
  404. qry : string;
  405. firstRunID, lastRunID,PrevRunID : Integer;
  406. RunCount : Integer;
  407. res : TSQLQuery;
  408. AddCount : boolean;
  409. begin
  410. AddCount:=false;
  411. UpdateTestConfigID:=false;
  412. qry:=format('SELECT TCONF_FIRST_RUN_FK FROM TESTCONFIG WHERE TCONF_ID=%d',[ConfigID]);
  413. FirstRunID:=IDQuery(qry);
  414. if TestRunID<FirstRunID then
  415. begin
  416. Verbose(V_Warning,format('FirstRunID changed from %d to %d',[FirstRunID,TestRunID]));
  417. qry:=format('UPDATE TESTCONFIG SET TCONF_FIRST_RUN_FK=%d WHERE TCONF_ID=%d',
  418. [TestRunID,ConfigID]);
  419. if OpenQuery(qry,res,false) then
  420. FreeQueryResult(res)
  421. else
  422. Verbose(V_Warning,'Update of LastRunID failed');
  423. end;
  424. qry:=format('SELECT TCONF_LAST_RUN_FK FROM TESTCONFIG WHERE TCONF_ID=%d',[ConfigID]);
  425. LastRunID:=IDQuery(qry);
  426. if TestRunID>LastRunID then
  427. begin
  428. qry:=format('UPDATE TESTCONFIG SET TCONF_LAST_RUN_FK=%d WHERE TCONF_ID=%d',
  429. [TestRunID,ConfigID]);
  430. if OpenQuery(qry,res,false) then
  431. FreeQueryResult(res)
  432. else
  433. Verbose(V_Warning,'Update of LastRunID failed');
  434. end
  435. else
  436. Verbose(V_Warning,format('LastRunID %di,new %d',[LastRunID,TestRunID]));
  437. qry:=format('SELECT TCONF_NEW_RUN_FK FROM TESTCONFIG WHERE TCONF_ID=%d',[ConfigID]);
  438. PrevRunID:=IDQuery(qry);
  439. if TestRunID<>PrevRunID then
  440. begin
  441. qry:=format('UPDATE TESTCONFIG SET TCONF_NEW_RUN_FK=%d WHERE TCONF_ID=%d',
  442. [TestRunID,ConfigID]);
  443. if OpenQuery(qry,res,false) then
  444. FreeQueryResult(res)
  445. else
  446. Verbose(V_Warning,'Update of LastRunID failed');
  447. AddTestHistoryEntry(TestRunID,PrevRunID);
  448. AddCount:=true;
  449. end
  450. else
  451. Verbose(V_Warning,'TestRunID is equal to last!');
  452. qry:=format('SELECT TCONF_COUNT_RUNS FROM TESTCONFIG WHERE TCONF_ID=%d',[ConfigID]);
  453. RunCount:=IDQuery(qry);
  454. { Add one to run count }
  455. if AddCount then
  456. begin
  457. Inc(RunCount);
  458. qry:=format('UPDATE TESTCONFIG SET TCONF_COUNT_RUNS=%d WHERE TCONF_ID=%d',
  459. [RunCount,ConfigID]);
  460. if OpenQuery(qry,res,false) then
  461. FreeQueryResult(res)
  462. else
  463. Verbose(V_Warning,'Update of TU_COUNT_RUNS failed');
  464. end;
  465. end;
  466. function InsertNewTestConfigId(TestRunID: Integer) : longint;
  467. var
  468. qry : string;
  469. TestDate : string;
  470. begin
  471. TestDate:=GetDate(TestRunID);
  472. qry:='INSERT INTO TESTCONFIG '+
  473. '(TCONF_NEW_RUN_FK,TCONF_FIRST_RUN_FK,TCONF_LAST_RUN_FK,' +
  474. 'TCONF_CPU_FK,TCONF_OS_FK,TCONF_VERSION_FK,TCONF_CATEGORY_FK,'+
  475. 'TCONF_SUBMITTER,TCONF_MACHINE,TCONF_COMMENT,'+
  476. 'TCONF_NEW_DATE,TCONF_FIRST_DATE,TCONF_LAST_DATE) ';
  477. qry:=qry+format(' VALUES(%d,%d,%d,%d,%d,%d,%d,"%s","%s","%s","%s","%s","%s") ',
  478. [TestRunID,TestRunID,TestRunID,
  479. GetTestRunFieldID('TU_CPU_FK',TestRunID),
  480. GetTestRunFieldID('TU_OS_FK',TestRunID),
  481. GetTestRunFieldID('TU_VERSION_FK',TestRunID),
  482. GetTestRunFieldID('TU_CATEGORY_FK',TestRunID),
  483. GetSubmitter(TestRunID),
  484. GetMachine(TestRunID),
  485. GetComment(TestRunID),
  486. TestDate,TestDate,TestDate]);
  487. Result:=InsertQuery(qry);
  488. AddTestHistoryEntry(TestRunID,0);
  489. end;
  490. Procedure InsertRunsIntoConfigAndHistory(var GlobalRes : TSQLQuery);
  491. var
  492. i,fid,num_fields,row_count : Integer;
  493. Row : Variant;
  494. s : string;
  495. runid,previd : Integer;
  496. begin
  497. with GlobalRes do
  498. begin
  499. num_fields:=FieldCount;
  500. First;
  501. Last; { be sure to read all }
  502. row_count:=RecordCount;
  503. Writeln('Row count=',row_count);
  504. First;
  505. for i:=0 to row_count-1 do
  506. begin
  507. row:=FieldValues['TR_ID'];
  508. runid:=StrToIntDef(Row,-1);
  509. previd:=GetTestPreviousRunHistoryID(RunID);
  510. if previd>=0 then
  511. begin
  512. Writeln(format('RunID=%d already handled prevID=%d',[runID,prevID]));
  513. continue;
  514. end
  515. else
  516. begin
  517. if GetTestConfigId(runid)=-1 then
  518. begin
  519. InsertNewTestConfigId(RunID);
  520. end
  521. else
  522. UpdateTestConfigID(RunID);
  523. end;
  524. Next;
  525. end;
  526. end;
  527. end;
  528. Procedure GetAllTestRuns(var GlobalRes : TSQLQuery);
  529. var
  530. qry : string;
  531. begin
  532. qry:='SELECT * FROM TESTRUN ORDER BY TU_ID';
  533. if OffsetString<>'' then
  534. qry:=qry+' LIMIT 1000 OFFSET '+OffsetString;
  535. if not OpenQuery(qry,GlobalRes,false) then
  536. Verbose(V_Warning,'Failed to fetch testrun content');
  537. end;
  538. var
  539. GlobalRes : TSQLQuery;
  540. begin
  541. ProcessConfigFile('dbdigest.cfg');
  542. ProcessCommandLine;
  543. ConnectToDatabase(DatabaseName,HostName,UserName,Password,Port);
  544. GetAllTestRuns(GlobalRes);
  545. InsertRunsIntoConfigAndHistory(GlobalRes);
  546. end.