testsqlfiles.lpr 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. {
  2. This file is part of the Free Component Library
  3. Copyright (c) 2010 by the Free Pascal development team
  4. SQL source syntax parser test program
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program testsqlfiles;
  12. {$mode objfpc}{$H+}
  13. uses
  14. {$IFDEF UNIX}{$IFDEF UseCThreads}
  15. cthreads,
  16. {$ENDIF}{$ENDIF}
  17. Classes,
  18. SysUtils, fpsqltree, fpsqlparser, fpsqlscanner, sqlscript,
  19. CustApp;
  20. { you can add units after this }
  21. type
  22. { TTestSQLFilesApplication }
  23. TTestSQLFilesApplication = class(TCustomApplication)
  24. private
  25. procedure ParseStatement(Sender: TObject; Statement: TStrings;
  26. var StopExecution: Boolean);
  27. procedure ProcessFile(const AFileName: String);
  28. protected
  29. FStatementCount : integer;
  30. FFileCount : Integer;
  31. FErrorCount : Integer;
  32. FCurrentFile : String;
  33. FWriteSQL : Boolean; // Set to true to write SQL to screen.
  34. procedure DoRun; override;
  35. public
  36. constructor Create(TheOwner: TComponent); override;
  37. destructor Destroy; override;
  38. end;
  39. { TTestSQLFilesApplication }
  40. Procedure TTestSQLFilesApplication.ParseStatement(Sender: TObject; Statement: TStrings; var StopExecution: Boolean);
  41. Var
  42. P : TSQLParser;
  43. D : TStringStream;
  44. S : TSQLElement;
  45. I : Integer;
  46. begin
  47. Inc(FStatementCount);
  48. D:=TStringStream.Create(Statement.Text);
  49. try
  50. P:=TSQLParser.Create(D);
  51. try
  52. try
  53. S:=P.Parse;
  54. If FWriteSQL then
  55. writeln(S.GetasSql([],0));
  56. S.Free;
  57. except
  58. On E : Exception do
  59. begin
  60. Inc(FErrorCount);
  61. Writeln('Error ',FErrorCount,' processing: ',FCurrentFile,' statement after line : ',(Sender as TEventSQLScript).Line);
  62. for I:=0 to Statement.Count-1 do
  63. begin
  64. Writeln(I+1:5,':',Statement[i]);
  65. end;
  66. Writeln('Exception message: ',E.Message);
  67. If (Sender as TEventSQLScript).Terminator<>';' then
  68. begin
  69. Statement.Insert(0,'SET TERM ^ ;');
  70. Statement.Add('^');
  71. end
  72. else
  73. Statement.Add(';');
  74. Statement.SaveToFile(Format('error-%d.sql',[FErrorCount]));
  75. end;
  76. end;
  77. finally
  78. P.Free;
  79. end;
  80. finally
  81. D.Free;
  82. end;
  83. end;
  84. Procedure TTestSQLFilesApplication.ProcessFile(Const AFileName : String);
  85. Var
  86. I : TEventSQLScript;
  87. begin
  88. try
  89. Inc(FFileCount);
  90. FCurrentFile:=AFileName;
  91. I:=TEventSQLScript.Create(Nil);
  92. try
  93. I.Script.LoadFromFile(AFileName);;
  94. I.OnSQLStatement:=@ParseStatement;
  95. I.UseSetTerm:=True;
  96. I.UseCommit:=True;
  97. I.Directives.Add('DISPLAY');
  98. I.Directives.Add('SET SQL DIALECT');
  99. I.Directives.Add('TRAP');
  100. I.Execute;
  101. finally
  102. I.Free;
  103. end;
  104. except
  105. On E : Exception do
  106. Writeln('Error processing ',AFIleName,' : ',E.Message);
  107. end;
  108. end;
  109. procedure TTestSQLFilesApplication.DoRun;
  110. var
  111. ErrorMsg: String;
  112. I : Integer;
  113. begin
  114. For I:=1 to ParamCount do
  115. ProcessFile(Paramstr(i));
  116. Writeln('Processed ',FFileCount,' files.');
  117. Writeln('Processed ',FStatementCount,' statements.');
  118. Writeln(FErrorCount,' statements had errors');
  119. Writeln(FStatementCount-FErrorCount,' statements processed correctly');
  120. // stop program loop
  121. Terminate;
  122. end;
  123. constructor TTestSQLFilesApplication.Create(TheOwner: TComponent);
  124. begin
  125. inherited Create(TheOwner);
  126. StopOnException:=True;
  127. end;
  128. destructor TTestSQLFilesApplication.Destroy;
  129. begin
  130. inherited Destroy;
  131. end;
  132. var
  133. Application: TTestSQLFilesApplication;
  134. begin
  135. Application:=TTestSQLFilesApplication.Create(nil);
  136. Application.Title:='Test SQL Files';
  137. Application.Run;
  138. Application.Free;
  139. end.