2
0

cmdclient.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. program cmdclient;
  2. {$mode objfpc}{$H+}
  3. uses
  4. cwstring,Classes, SysUtils, CustApp, fphttpclient, db, bufdataset, XMLDatapacketReader;
  5. type
  6. { TSQLDBRestClientApplication }
  7. TSQLDBRestClientApplication = class(TCustomApplication)
  8. Private
  9. FURL: String;
  10. FUserName: string;
  11. FPassword: string;
  12. FShowRaw : Boolean;
  13. protected
  14. procedure RunQuery(aDataset: TBufDataset);
  15. Procedure ShowData(aDataset: TDataset);
  16. procedure DoRun; override;
  17. public
  18. constructor Create(TheOwner: TComponent); override;
  19. destructor Destroy; override;
  20. procedure WriteHelp; virtual;
  21. end;
  22. { TSQLDBRestClientApplication }
  23. procedure TSQLDBRestClientApplication.RunQuery(aDataset : TBufDataset);
  24. Var
  25. C : TFPHTTPClient;
  26. S : TStringStream;
  27. U : String;
  28. begin
  29. U:=FURL;
  30. S:=Nil;
  31. C:=TFPHTTPClient.Create(Self);
  32. try
  33. C.UserName:=FUserName;
  34. C.Password:=FPassword;
  35. S:=TStringStream.Create;
  36. if Pos('?',U)=0 then
  37. U:=U+'?'
  38. else
  39. U:=U+'&';
  40. U:=U+'fmt=buf';
  41. C.Get(U,S);
  42. if FShowRaw then
  43. begin
  44. Writeln('Raw request data:');
  45. Writeln('---');
  46. Writeln(S.Datastring);
  47. Writeln('---');
  48. end;
  49. S.Position:=0;
  50. aDataset.LoadFromStream(S,dfXML);
  51. finally
  52. S.Free;
  53. C.Free;
  54. end;
  55. end;
  56. procedure TSQLDBRestClientApplication.ShowData(aDataset: TDataset);
  57. Var
  58. I : Integer;
  59. F : TField;
  60. FL : Integer;
  61. begin
  62. FL:=0;
  63. With aDataset do
  64. begin
  65. For I:=0 to FieldDefs.Count-1 do
  66. if Length(FieldDefs[I].Name)>FL then
  67. FL:=Length(FieldDefs[I].Name);
  68. While not EOF do
  69. begin
  70. Writeln(StringOfChar('-',FL));
  71. Writeln('Record: ',RecNo:4);
  72. Writeln(StringOfChar('-',FL));
  73. For F in Fields do
  74. With F do
  75. begin
  76. Write(FieldName:FL,': ');
  77. if F.IsNull then
  78. Writeln('<NULL>')
  79. else
  80. Writeln(F.AsString);
  81. end;
  82. Next;
  83. end;
  84. end;
  85. end;
  86. procedure TSQLDBRestClientApplication.DoRun;
  87. var
  88. ErrorMsg: String;
  89. D : TBufDataset;
  90. begin
  91. // quick check parameters
  92. ErrorMsg:=CheckOptions('hU:u:p:r', ['help','url:','username:','password:','raw']);
  93. if ErrorMsg<>'' then begin
  94. ShowException(Exception.Create(ErrorMsg));
  95. Terminate;
  96. Exit;
  97. end;
  98. // parse parameters
  99. if HasOption('h', 'help') then begin
  100. WriteHelp;
  101. Terminate;
  102. Exit;
  103. end;
  104. FURL:=GetOptionValue('U','url');
  105. FUserName:=GetOptionValue('u','username');
  106. FPassword:=GetOptionValue('p','password');
  107. FShowRaw:=HasOption('r','raw');
  108. D:=TBufDataset.Create(Self);
  109. try
  110. RunQuery(D);
  111. ShowData(D);
  112. Finally
  113. D.Free;
  114. end;
  115. // stop program loop
  116. Terminate;
  117. end;
  118. constructor TSQLDBRestClientApplication.Create(TheOwner: TComponent);
  119. begin
  120. inherited Create(TheOwner);
  121. StopOnException:=True;
  122. end;
  123. destructor TSQLDBRestClientApplication.Destroy;
  124. begin
  125. inherited Destroy;
  126. end;
  127. procedure TSQLDBRestClientApplication.WriteHelp;
  128. begin
  129. { add your help code here }
  130. writeln('Usage: ', ExeName, ' [options]');
  131. Writeln('Where options is one or more of:');
  132. Writeln('-h --help this message');
  133. Writeln('-p --password=PWD HTTP Basic authentication password.');
  134. Writeln('-r --raw Show raw request data');
  135. Writeln('-U --url=URL URL to get data from. Do not add format (fmt) parameter');
  136. Writeln('-u --username=User HTTP Basic authentication username');
  137. end;
  138. var
  139. Application: TSQLDBRestClientApplication;
  140. begin
  141. Application:=TSQLDBRestClientApplication.Create(nil);
  142. Application.Title:='SQLDB Rest Bridge client application';
  143. Application.Run;
  144. Application.Free;
  145. end.