PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/LLBL Pro v3.1/AW.Helper.LLBL/QueryToSQL.cs

#
C# | 75 lines | 58 code | 7 blank | 10 comment | 4 complexity | 8987465b91a1fb364b04bc1abfb006b1 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, MIT, GPL-2.0, Apache-2.0
  1. using System;
  2. using System.Text;
  3. using SD.LLBLGen.Pro.ORMSupportClasses;
  4. namespace AW.Helper.LLBL
  5. {
  6. /// <summary>
  7. /// Methods to get an executable SQL string from a query for using in tracing etc.
  8. /// <see cref="http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=15420" />
  9. /// <see cref="http://cs.rthand.com/blogs/blog_with_righthand/pages/Implementing-more-useful-tracing-for-LLBLGenPro-2.0.aspx" />
  10. /// </summary>
  11. public static class QueryToSQL
  12. {
  13. /// <summary>
  14. /// Gets SQL from query such that it can be executed/run.
  15. /// </summary>
  16. /// <param name="query"> The query. </param>
  17. /// <returns> Executable SQL string </returns>
  18. public static string GetExecutableSQLFromQuery(IQuery query)
  19. {
  20. try
  21. {
  22. var sb = new StringBuilder();
  23. if (query is IActionQuery)
  24. {
  25. if (query is BatchActionQuery)
  26. {
  27. DumpBatchActionQuery(sb, query as BatchActionQuery);
  28. }
  29. else
  30. {
  31. DumpActionQuery(sb, (IActionQuery) query);
  32. }
  33. }
  34. else if (query is IRetrievalQuery)
  35. {
  36. CommandToExecutableSQL.GetExecutableSQLFromQueryCommand(sb, query.Command);
  37. }
  38. else
  39. {
  40. sb.Append("** Unknown query type **");
  41. }
  42. return sb.ToString();
  43. }
  44. catch (Exception ex)
  45. {
  46. return "** Exception occurred during GetExecutableSQLFromQuery: " + ex.Message + "**";
  47. }
  48. }
  49. private static void DumpBatchActionQuery(StringBuilder sb, BatchActionQuery batchActionQuery)
  50. {
  51. sb.AppendFormat("BatchAction Query (Count={0}):\r\n", batchActionQuery.Count);
  52. var part = 1;
  53. foreach (var actionQuery in batchActionQuery.ActionQueries)
  54. {
  55. sb.Append("\r\n Action Query #" + part++ + ":\r\n");
  56. DumpActionQuery(sb, actionQuery);
  57. }
  58. sb.AppendLine();
  59. }
  60. private static void DumpActionQuery(StringBuilder sb, IActionQuery query)
  61. {
  62. CommandToExecutableSQL.GetExecutableSQLFromActionCommand(sb, query.Command);
  63. var part = 1;
  64. foreach (var sequenceRetrievalQuery in query.SequenceRetrievalQueries)
  65. sb.Append("\r\n Sequence Retrieval Query #" + part++ + ":\r\n" + sequenceRetrievalQuery.SequenceRetrievalCommand.CommandText);
  66. }
  67. }
  68. }