Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. program SQLWorkbenchExplain;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils, Classes, JclSysUtils, IniFiles;
  7.  
  8. type
  9. TConfig = record
  10. LogFile: string;
  11. WinscpPath: string;
  12. WinscpUser: string;
  13. WinscpPass: string;
  14. WinscpServer: string;
  15. WorkbenchPath: string;
  16. WorkbenchProfile: string;
  17. end;
  18.  
  19. var
  20. log: TextFile;
  21. config: TConfig;
  22.  
  23. procedure LoadConfig;
  24. var
  25. ini: TMemIniFile;
  26. section: TStrings;
  27. begin
  28. ini := TMemIniFile.Create('SqlWorkbenchExplain.ini');
  29. section := TStringList.Create;
  30. try
  31. ini.ReadSectionValues('Config', section);
  32. config.LogFile := section.Values['LogFile'];
  33. section.Clear;
  34. ini.ReadSectionValues('Winscp', section);
  35. config.WinscpPath := section.Values['Path'];
  36. config.WinscpUser := section.Values['User'];
  37. config.WinscpPass := section.Values['Pass'];
  38. config.WinscpServer := section.Values['Server'];
  39. section.Clear;
  40. ini.ReadSectionValues('Workbench', section);
  41. config.WorkbenchPath := section.Values['Path'];
  42. config.WorkbenchProfile := section.Values['Profile'];
  43. finally
  44. section.Free;
  45. ini.Free;
  46. end;
  47. end;
  48.  
  49. procedure SetExplainOn(pSqlFile, pOutputFile: string);
  50. var
  51. fInput: TStringList;
  52. begin
  53. fInput := TStringList.Create;
  54. try
  55. fInput.LoadFromFile(pSqlFile);
  56. fInput.Insert(0, 'set explain on avoid_execute;' + sLineBreak);
  57. fInput.Add(';' + sLineBreak + 'set explain off;');
  58. fInput.SaveToFile(pOutputFile);
  59. Writeln(log, fInput.Text);
  60. finally
  61. fInput.Free;
  62. end;
  63. end;
  64.  
  65. procedure ExecuteSql(pSqlFile: string);
  66. var
  67. commandOutput: string;
  68. command: string;
  69. begin
  70. command := config.WorkbenchPath + 'sqlwbconsole.exe -profile='''+ config.WorkbenchProfile + ''' -script=''' + pSqlFile + '''';
  71. Writeln(log, command);
  72. Execute(command, commandOutput, True);
  73. Writeln(log, commandOutput);
  74. end;
  75.  
  76. procedure DownloadExplainFile;
  77. var
  78. commandOutput: string;
  79. command: string;
  80. script: TStringList;
  81. begin
  82. script := TStringList.Create;
  83. try
  84. script.Add('open ' + config.WinscpUser + ':' + config.WinscpPass + '@' + config.WinscpServer);
  85. script.Add('get sqexplain.out sqexplain.out');
  86. script.Add('rm sqexplain.out');
  87. script.Add('exit');
  88. script.SaveToFile('winscpcommand.tmp');
  89. command := config.WinscpPath + 'WinScp.com /SCRIPT=winscpcommand.tmp';
  90. Writeln(log, command);
  91. Execute(command, commandOutput, True);
  92. Writeln(log, commandOutput);
  93. DeleteFile('winscpcommand.tmp');
  94. finally
  95. script.Free;
  96. end;
  97. end;
  98.  
  99. procedure SaveExplain(pSqlFile: string);
  100. var
  101. explainFile: TStringList;
  102. sqlFile: TStringList;
  103. begin
  104. sqlFile := TStringList.Create;
  105. explainFile := TStringList.Create;
  106. try
  107. sqlFile.LoadFromFile(pSqlFile);
  108. explainFile.LoadFromFile('sqexplain.out');
  109. Writeln(log, 'Explain output: ' + sLineBreak + explainFile.Text);
  110. sqlFile.Append('/*');
  111. sqlFile.Append(sLineBreak + explainFile.Text + sLineBreak);
  112. sqlFile.Append('*/');
  113. sqlFile.SaveToFile(pSqlFile);
  114. finally
  115. sqlFile.Free;
  116. explainFile.Free;
  117. end;
  118. end;
  119.  
  120. var
  121. fInput, fOutput: string;
  122. begin
  123. if ParamCount < 2 then
  124. begin
  125. fInput := 'test.sql';
  126. fOutput := 'test_out.sql';
  127. end
  128. else
  129. begin
  130. fInput := ParamStr(1);
  131. fOutput := ParamStr(2);
  132. end;
  133.  
  134. LoadConfig;
  135. AssignFile(log, config.LogFile);
  136. try
  137. try
  138. Rewrite(log);
  139. Writeln(log, fInput);
  140. Writeln(log, fOutput);
  141. SetExplainOn(fInput, fOutput);
  142. ExecuteSql(fOutput);
  143. DownloadExplainFile;
  144. SaveExplain(fOutput);
  145. except
  146. on E: Exception do
  147. Writeln(log, 'Exception: ' + E.Message);
  148. end;
  149. finally
  150. CloseFile(log);
  151. end;
  152. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement