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

/DICK.B1/B1QueryTools.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 231 lines | 192 code | 11 blank | 28 comment | 30 complexity | 3b6c09326fa1cce4c41fde1a35339e04 MD5 | raw file
  1. /* This file is part of DI Construction Kit.
  2. *
  3. * DI Construction Kit is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Lesser General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * DI Construction Kit is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public License
  14. * along with DI Construction Kit. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Text;
  20. using SAPbobsCOM;
  21. using DICK.Core;
  22. using System.Data;
  23. namespace DICK.B1
  24. {
  25. /// <summary>
  26. /// This library contains static helper methods that help using the DI API query resultsets.
  27. /// </summary>
  28. public class B1QueryTools
  29. {
  30. /// <summary>
  31. /// This is a library class not meant to be instantiated.
  32. /// </summary>
  33. private B1QueryTools() { }
  34. /// <summary>
  35. /// Retrieves the first record from the query resultset and converts it to an instance of Dictionary<string,object>.
  36. /// </summary>
  37. /// <param name="session">CompanyClass object</param>
  38. /// <param name="query">The query to be executed</param>
  39. /// <returns></returns>
  40. public static Dictionary<string,object> QueryRecordToDictionary(CompanyClass session, string query)
  41. {
  42. Dictionary<string, object> result = new Dictionary<string, object>();
  43. SAPbobsCOM.Recordset rec;
  44. rec = (Recordset)session.GetBusinessObject(BoObjectTypes.BoRecordset);
  45. rec.DoQuery(query);
  46. if (rec.RecordCount > 0)
  47. {
  48. rec.MoveFirst();
  49. for (int fieldIndex = 0; fieldIndex < rec.Fields.Count; fieldIndex++)
  50. {
  51. Field field = rec.Fields.Item(fieldIndex);
  52. result.Add(field.Name, field.Value);
  53. }
  54. }
  55. if (result.Count == 0)
  56. {
  57. Engine.GetInstance().LogWarn("query:" + query + "returned an empty recordset!");
  58. }
  59. return result;
  60. }
  61. public static List<object> QueryColumnToList(CompanyClass session, string query)
  62. {
  63. return QueryColumnToList(session, query,0);
  64. }
  65. public static void ExecuteQuery(string query)
  66. {
  67. CompanyClass sess = B1Engine.GetInstance().session;
  68. SAPbobsCOM.Recordset rec;
  69. rec = (Recordset)sess.GetBusinessObject(BoObjectTypes.BoRecordset);
  70. rec.DoQuery(query);
  71. }
  72. public static List<object> QueryColumnToList(CompanyClass session, string query,int columnno)
  73. {
  74. List<object> result = new List<object>();
  75. SAPbobsCOM.Recordset rec;
  76. rec = (Recordset)session.GetBusinessObject(BoObjectTypes.BoRecordset);
  77. rec.DoQuery(query);
  78. if (rec.RecordCount > 0)
  79. {
  80. rec.MoveFirst();
  81. for (int rowindex = 0; rowindex < rec.RecordCount; rowindex++)
  82. {
  83. Field field = rec.Fields.Item(columnno);
  84. result.Add(field.Value.ToString());
  85. rec.MoveNext();
  86. }
  87. }
  88. return result;
  89. }
  90. public static System.Data.DataSet QueryToDataSet(string dataTableName, CompanyClass session, string query, out MessageToken res)
  91. {
  92. Engine app = Engine.GetInstance();
  93. app.LogDebug("QueryToDataSet:b " + query);
  94. DataSet dataSet = new DataSet();
  95. dataSet.Tables.Add(QueryToDataTable(dataTableName, session, query, out res));
  96. return dataSet;
  97. }
  98. public static System.Data.DataTable QueryToDataTable(string dataTableName, string query, out MessageToken res)
  99. {
  100. return QueryToDataTable(dataTableName, B1Engine.GetInstance().session, query, out res);
  101. }
  102. public static System.Data.DataTable QueryToDataTable(string dataTableName, CompanyClass session, string query, out MessageToken res)
  103. {
  104. res = new MessageToken();
  105. res.OK = true;
  106. Engine app = Engine.GetInstance();
  107. app.LogDebug("Calling getQueryTable with " + query);
  108. if (session == null)
  109. {
  110. res.OK = false;
  111. res.AddTextToBody("Session is null");
  112. app.LogError("session is null");
  113. }
  114. DataTable result = new DataTable(dataTableName);
  115. SAPbobsCOM.Recordset rec;
  116. rec = (Recordset)session.GetBusinessObject(BoObjectTypes.BoRecordset);
  117. if (rec == null)
  118. {
  119. res.OK = false;
  120. res.AddTextToBody("Recordset is null");
  121. app.LogError("Recordset is null");
  122. }
  123. rec.DoQuery(query);
  124. if (rec != null && rec.RecordCount > 0)
  125. {
  126. rec.MoveFirst();
  127. foreach (Field f in rec.Fields)
  128. {
  129. result.Columns.Add(f.Name, f.Value.GetType());
  130. }
  131. for (int rowindex = 0; rowindex < rec.RecordCount; rowindex++)
  132. {
  133. object[] row = new object[rec.Fields.Count];
  134. int col = 0;
  135. foreach (Field f in rec.Fields)
  136. {
  137. app.LogDebug("FIELDINFO: " + f.Name + ":" + f.Type + "/" + f.Value);
  138. row[col] = f.Value;
  139. col++;
  140. }
  141. result.Rows.Add(row);
  142. rec.MoveNext();
  143. }
  144. }
  145. return result;
  146. }
  147. public static List<Dictionary<string,object>> QueryTableToDictionaryList(CompanyClass session, string query)
  148. {
  149. Engine app = Engine.GetInstance();
  150. app.LogDebug("Calling getQueryTable with " + query);
  151. if (session == null)
  152. {
  153. app.LogError("session is null");
  154. }
  155. List<Dictionary<string,object>> result = new List<Dictionary<string,object>>();
  156. SAPbobsCOM.Recordset rec;
  157. rec = (Recordset)session.GetBusinessObject(BoObjectTypes.BoRecordset);
  158. if (rec == null)
  159. {
  160. app.LogError("recordset is null!");
  161. }
  162. rec.DoQuery(query);
  163. if (rec != null && rec.RecordCount > 0)
  164. {
  165. rec.MoveFirst();
  166. for (int rowindex = 0; rowindex < rec.RecordCount; rowindex++)
  167. {
  168. Dictionary<string,object> row = new Dictionary<string, object>();
  169. for (int columnindex = 0; columnindex < rec.Fields.Count; columnindex++)
  170. {
  171. Field field = rec.Fields.Item(columnindex);
  172. app.LogDebug("FIELDINFO: " + field.Name + ":" + field.Type + "/" + field.Value);
  173. row.Add(field.Name, field.Value.ToString());
  174. }
  175. result.Add(row);
  176. rec.MoveNext();
  177. }
  178. }
  179. return result;
  180. }
  181. public static object QuerySingleValue(string qry, string returnkey)
  182. {
  183. Engine app = Engine.GetInstance();
  184. app.LogDebug("Calling dynamic retrieval");
  185. if (qry != null && qry.Trim() != "")
  186. {
  187. app.LogDebug("querying " + qry);
  188. Dictionary<string,object> qryresult = QueryRecordToDictionary(B1Engine.GetInstance().session, qry);
  189. object result = null;
  190. bool ok=qryresult.TryGetValue(returnkey, out result);
  191. if (ok)
  192. {
  193. return result;
  194. }
  195. else
  196. {
  197. app.LogError("B1.B1QueryTools.QuerySingleValue: failed to get value from Dictionary with key " + returnkey);
  198. return null;
  199. }
  200. }
  201. else
  202. {
  203. app.LogError("ERROR WHILE CONSTRUCTING QUERY FOR DYNAMIC CONVERSION. QUERY=" + qry);
  204. return null;
  205. }
  206. }
  207. public static object QuerySingleValue(string query, string returnkey, string format)
  208. {
  209. object o = QuerySingleValue(query, returnkey);
  210. if (o != null)
  211. {
  212. return B1Engine.FormatValue(o, format);
  213. }
  214. else
  215. {
  216. return "";
  217. }
  218. }
  219. }
  220. }