parsesql.pas 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. {
  2. This file is part of the Free Component Library
  3. Copyright (c) 2019 by the Free Pascal development team
  4. Demo for SQL source syntax parser
  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 parsesql;
  12. uses sysutils, classes, fpsqlparser, fpsqlscanner,fpsqltree;
  13. Procedure parseScript(const aFilename:String; AScript :TStringList);
  14. var
  15. i: integer;
  16. Parser: TSQLParser;
  17. ResultList: TSQLElementList;
  18. ScriptStream:TFileStream;
  19. begin
  20. ScriptStream:=TFileStream.Create(aFilename, fmopenreadwrite or fmshareexclusive);
  21. try
  22. ScriptStream.Position:=0;
  23. Parser := TSQLParser.Create(ScriptStream);
  24. try
  25. ResultList := Parser.ParseScript([poAllowSetTerm]);
  26. for i:=0 to ResultList.Count-1 do
  27. AScript.Add(ResultList[i].GetAsSQL([sfoDoubleQuoteIdentifier]));
  28. finally
  29. Parser.Free;
  30. end;
  31. finally
  32. ScriptStream.Free;
  33. ResultList.Free;
  34. end;
  35. end;
  36. Var
  37. L : TStringList;
  38. S : String;
  39. begin
  40. if ParamCount<>1 then
  41. begin
  42. Writeln('Parse & Dump SQL');
  43. Writeln('Usage : parsesql <filename>');
  44. Halt(1);
  45. end;
  46. L:=TStringList.Create;
  47. try
  48. ParseScript(ParamStr(1),L);
  49. for S in L do Writeln(S);
  50. Finally
  51. L.Free;
  52. end;
  53. end.