dbadd.lpr 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. program dbadd;
  2. {$mode objfpc}
  3. {$H+}
  4. {$modeswitch typehelpers}
  5. uses
  6. Classes, SysUtils, CustApp, tsdb, tsutils, inifiles;
  7. type
  8. TValueType = (vtCategory,vtCPU,vtOS,vtVersion);
  9. { TDigestValueApplication }
  10. TDigestValueApplication = class(TCustomApplication)
  11. private
  12. procedure ConnectToDB;
  13. function ProcessParams: boolean;
  14. protected
  15. FDB : TTestSQL;
  16. FDatabaseName : String;
  17. FHostName : String;
  18. FUserName : string;
  19. FPassword : string;
  20. FPort : Word;
  21. FType : TValueType;
  22. FValue : String;
  23. FDate : TDateTime;
  24. procedure DoRun; override;
  25. function ReadConfig(const aFileName: String): boolean;
  26. public
  27. constructor Create(TheOwner: TComponent); override;
  28. destructor Destroy; override;
  29. procedure Usage(const aMsg : String);
  30. end;
  31. const
  32. ValueNames : Array[TValueType] of string = ('Category','CPU','OS','Version');
  33. { TDigestValueApplication }
  34. function TDigestValueApplication.ProcessParams : boolean;
  35. function GetDate(S : String) : TDateTime;
  36. var
  37. Y,M,d : Integer;
  38. begin
  39. if (s='') then
  40. exit(Date);
  41. Y:=StrToIntDef(Copy(S,1,4),0);
  42. M:=StrToIntDef(Copy(S,5,2),0);
  43. D:=StrToIntDef(Copy(S,7,2),0);
  44. if (Y>0) and (M>0) and (D>0) then
  45. if not TryEncodeDate(Y,M,D,Result) then
  46. Result:=0
  47. end;
  48. begin
  49. if not ReadConfig(GetOptionValue('c','config')) then
  50. Exit(False);
  51. Case LowerCase(GetOptionValue('t','type')) of
  52. 'category' : FType:=vtCategory;
  53. 'cpu' : FType:=vtCPU;
  54. 'os' : FType:=vtOS;
  55. 'version' : FType:=vtVersion;
  56. else
  57. Usage('Unknown value type : '+GetOptionValue('t','type'));
  58. Exit(False);
  59. end;
  60. FValue:=GetOptionValue('v','value');
  61. FDate:=GetDate(GetOptionValue('d','date'));
  62. if FDate=0 then
  63. begin
  64. Usage('Invalid date : '+GetOptionValue('d','date'));
  65. Exit(False);
  66. end;
  67. if FValue='' then
  68. begin
  69. Usage('Empty value is not allowed');
  70. Exit(False);
  71. end;
  72. Result:=True;
  73. end;
  74. procedure TDigestValueApplication.ConnectToDB;
  75. begin
  76. FDB:=TTestSQL.create(FDatabaseName,FHostName,FUserName,FPassword,FPort);
  77. if not FDB.ConnectToDatabase then
  78. Writeln('Failed to connect to database');
  79. end;
  80. procedure TDigestValueApplication.DoRun;
  81. var
  82. ErrorMsg: String;
  83. lID : Integer;
  84. begin
  85. Terminate;
  86. // quick check parameters
  87. ErrorMsg:=CheckOptions('hc:t:v:d:', ['help','type:','value:','date:','config:']);
  88. if (ErrorMsg<>'') or HasOption('h', 'help') then
  89. begin
  90. Usage(ErrorMsg);
  91. Exit;
  92. end;
  93. if not ProcessParams then
  94. exit;
  95. ConnectToDB;
  96. Case FType of
  97. vtCPU : lID:=FDB.AddCPU(FValue);
  98. vtOS : lID:=FDB.AddOS(FValue);
  99. vtCategory : lID:=FDB.AddCategory(FValue);
  100. vtVersion: lID:=FDB.AddVersion(FValue,FDate);
  101. end;
  102. Writeln('Inserted ',ValueNames[FType],' "',FValue,'" with ID: ',lID);
  103. end;
  104. function TDigestValueApplication.ReadConfig(const aFileName: String) : boolean;
  105. var
  106. lFileName : string;
  107. Ini : TCustomIniFile;
  108. begin
  109. lFilename:=aFileName;
  110. if lFileName='' then
  111. lFileName:='/etc/dbdigest.ini';
  112. Ini:=TMemIniFile.Create(lFileName);
  113. With Ini do
  114. try
  115. FDatabaseName:=ReadString(SSection,KeyName,'testsuite');
  116. FHostName:=ReadString(SSection,KeyHost,'localhost');
  117. FUserName:=ReadString(SSection,KeyUser,'');
  118. FPassword:=ReadString(SSection,KeyPassword,'');
  119. FPort:=ReadInteger(SSection,KeyPort,0);
  120. finally
  121. Free;
  122. end;
  123. Result:=False;
  124. if FDatabaseName='' then
  125. Usage('Database name not set')
  126. else if (FHostName='') then
  127. Usage('Database hostname not set')
  128. else if (FUserName='') then
  129. Usage('Database username not set')
  130. else if (FPassword='') then
  131. Usage('Database user password not set')
  132. else
  133. Result:=True;
  134. end;
  135. constructor TDigestValueApplication.Create(TheOwner: TComponent);
  136. begin
  137. inherited Create(TheOwner);
  138. StopOnException:=True;
  139. end;
  140. destructor TDigestValueApplication.Destroy;
  141. begin
  142. FreeAndNil(FDB);
  143. inherited Destroy;
  144. end;
  145. procedure TDigestValueApplication.Usage(const aMsg: String);
  146. begin
  147. if aMsg<>'' then
  148. Writeln('Error: ',aMsg);
  149. writeln('Usage: ', ExeName, '[options]');
  150. Writeln('Where options is one or more of:');
  151. Writeln('-d --date=YYYYMMDD Date for version. If omitted, today is used');
  152. Writeln('-c --config=FILE Config file with database connection info (.ini file).');
  153. Writeln('-h --help This message');
  154. Writeln('-t --type=ATYPE Type of value to insert in db. This is one of:');
  155. Writeln(' category');
  156. Writeln(' cpu');
  157. Writeln(' os');
  158. Writeln(' version');
  159. Writeln('-v --value=VALUE Value to insert in database');
  160. Writeln('');
  161. Writeln('If -h is not specified, options -c -t -v are required.');
  162. Writeln('Config file is an .ini file with the following keys:');
  163. Writeln('[Database]');
  164. Writeln('Name=name');
  165. Writeln('Host=hostname');
  166. Writeln('UserName=user');
  167. Writeln('Password=pwd');
  168. Writeln('Port=1234');
  169. ExitCode:=Ord(aMsg<>'');
  170. end;
  171. var
  172. Application: TDigestValueApplication;
  173. begin
  174. Application:=TDigestValueApplication.Create(nil);
  175. Application.Title:='Digest add value Application';
  176. Application.Run;
  177. Application.Free;
  178. end.