Posted By

vriesdewie on 02/16/11


Tagged

EntitySpaces


Versions (?)

Get SQL of Dynamic Query in EntitySpaces


 / Published in: C#
 

After having constructed a query, this piece of code can obtain the SQL statement before executing the query.

  1. private static Object GetClassInstance(Assembly assembly, String className, Object args)
  2. {
  3.  
  4. Object result = null;
  5. if (assembly == null || String.IsNullOrEmpty(className)) return null;
  6.  
  7. try
  8. {
  9. foreach (Type type in assembly.GetTypes())
  10. {
  11. if (type.IsClass && type.FullName.EndsWith("." + className))
  12. {
  13. if (args == null)
  14. {
  15. // Use the raw null value for the args parameter. A parameter
  16. // of type Object with a value of null causes an exception.
  17. // Also an empty list of parameters like Object args[] = new {};
  18. // causes an exception.
  19. result = Activator.CreateInstance(type, null);
  20. }
  21. else result = Activator.CreateInstance(type, args);
  22. break;
  23. }
  24. }
  25. }
  26. catch
  27. {
  28. result = null;
  29. }
  30. return result;
  31. }
  32.  
  33. private static String GetQuery(EntitySpaces.Interfaces.esDynamicQuery qry)
  34. {
  35.  
  36. Assembly assembly;
  37. esDataRequest r = new esDataRequest();
  38. Object[] args = new Object[] { r };
  39. String sql = String.Empty;
  40. String className = "QueryBuilder";
  41.  
  42. try
  43. {
  44. qry.GetType().InvokeMember("PopulateRequest", BindingFlags.NonPublic |
  45. BindingFlags.InvokeMethod | BindingFlags.Instance, null, qry, args);
  46. String assemblyName = qry.es.Connection.ProviderSignature.DataProviderName;
  47. args = new Object[] { qry, new SqlCommand(), 1 };
  48. assembly = Assembly.Load(assemblyName);
  49. Object qb = GetClassInstance(assembly, className, null);
  50. sql = qb.GetType().InvokeMember("BuildQuery", BindingFlags.NonPublic |
  51. BindingFlags.Static | BindingFlags.InvokeMethod, null, qb, args).ToString();
  52. }
  53. catch
  54. {
  55. sql = null;
  56. }
  57. return sql;
  58. }

Report this snippet  

You need to login to post a comment.