PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/mojoPortal.Features.Data.MySql/Survey/DBSurveyResponse.cs

#
C# | 359 lines | 245 code | 63 blank | 51 comment | 5 complexity | 73023074888e9e369d79fb39921dccd0 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. /// Author: Rob Henry
  2. /// Created: 2007-11-26
  3. /// Last Modified: 2009-02-23
  4. ///
  5. /// This implementation is for MySQL.
  6. ///
  7. /// The use and distribution terms for this software are covered by the
  8. /// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  9. /// which can be found in the file CPL.TXT at the root of this distribution.
  10. /// By using this software in any fashion, you are agreeing to be bound by
  11. /// the terms of this license.
  12. ///
  13. /// You must not remove this notice, or any other, from this software.
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Globalization;
  17. using System.Text;
  18. using System.Data;
  19. using System.Configuration;
  20. using MySql.Data.MySqlClient;
  21. namespace SurveyFeature.Data
  22. {
  23. public static class DBSurveyResponse
  24. {
  25. private static String GetReadConnectionString()
  26. {
  27. return ConfigurationManager.AppSettings["MySqlConnectionString"];
  28. }
  29. private static String GetWriteConnectionString()
  30. {
  31. if (ConfigurationManager.AppSettings["MySqlWriteConnectionString"] != null)
  32. {
  33. return ConfigurationManager.AppSettings["MySqlWriteConnectionString"];
  34. }
  35. return ConfigurationManager.AppSettings["MySqlConnectionString"];
  36. }
  37. /// <summary>
  38. /// Inserts a row in the mp_SurveyResponses table. Returns rows affected count.
  39. /// </summary>
  40. /// <param name="responseGuid"> responseGuid </param>
  41. /// <param name="surveyGuid"> surveyGuid </param>
  42. /// <param name="userId"> userId </param>
  43. /// <param name="annonymous"> annonymous </param>
  44. /// <param name="complete"> complete </param>
  45. /// <returns>int</returns>
  46. public static int Add(
  47. Guid responseGuid,
  48. Guid surveyGuid,
  49. Guid userGuid,
  50. bool annonymous,
  51. bool complete)
  52. {
  53. #region Bit Conversion
  54. int intAnnonymous;
  55. if (annonymous)
  56. {
  57. intAnnonymous = 1;
  58. }
  59. else
  60. {
  61. intAnnonymous = 0;
  62. }
  63. int intComplete;
  64. if (complete)
  65. {
  66. intComplete = 1;
  67. }
  68. else
  69. {
  70. intComplete = 0;
  71. }
  72. #endregion
  73. StringBuilder sqlCommand = new StringBuilder();
  74. sqlCommand.Append("INSERT INTO mp_SurveyResponses (");
  75. sqlCommand.Append("ResponseGuid, ");
  76. sqlCommand.Append("SurveyGuid, ");
  77. sqlCommand.Append("UserGuid, ");
  78. //sqlCommand.Append("SubmissionDate, ");
  79. sqlCommand.Append("Annonymous, ");
  80. sqlCommand.Append("Complete )");
  81. sqlCommand.Append(" VALUES (");
  82. sqlCommand.Append("?ResponseGuid, ");
  83. sqlCommand.Append("?SurveyGuid, ");
  84. sqlCommand.Append("?UserGuid, ");
  85. //sqlCommand.Append(DateTime.Now.ToShortDateString() + ", ");
  86. sqlCommand.Append("?Annonymous, ");
  87. sqlCommand.Append("?Complete );");
  88. MySqlParameter[] arParams = new MySqlParameter[5];
  89. arParams[0] = new MySqlParameter("?ResponseGuid", MySqlDbType.VarChar, 36);
  90. arParams[0].Direction = ParameterDirection.Input;
  91. arParams[0].Value = responseGuid.ToString();
  92. arParams[1] = new MySqlParameter("?SurveyGuid", MySqlDbType.VarChar, 36);
  93. arParams[1].Direction = ParameterDirection.Input;
  94. arParams[1].Value = surveyGuid.ToString();
  95. arParams[2] = new MySqlParameter("?UserGuid", MySqlDbType.VarChar, 36);
  96. arParams[2].Direction = ParameterDirection.Input;
  97. arParams[2].Value = userGuid.ToString();
  98. arParams[3] = new MySqlParameter("?Annonymous", MySqlDbType.Int32);
  99. arParams[3].Direction = ParameterDirection.Input;
  100. arParams[3].Value = intAnnonymous;
  101. arParams[4] = new MySqlParameter("?Complete", MySqlDbType.Int32);
  102. arParams[4].Direction = ParameterDirection.Input;
  103. arParams[4].Value = intComplete;
  104. int rowsAffected = MySqlHelper.ExecuteNonQuery(
  105. GetWriteConnectionString(),
  106. sqlCommand.ToString(),
  107. arParams);
  108. return rowsAffected;
  109. }
  110. /// <summary>
  111. /// Updates the status of a response. Returns true if row updated.
  112. /// </summary>
  113. /// <param name="responseGuid"> responseGuid </param>
  114. /// <param name="complete"> complete </param>
  115. /// <returns>bool</returns>
  116. public static bool Update(
  117. Guid responseGuid,
  118. DateTime submissionDate,
  119. bool complete)
  120. {
  121. #region Bit Conversion
  122. int intComplete;
  123. if (complete)
  124. {
  125. intComplete = 1;
  126. }
  127. else
  128. {
  129. intComplete = 0;
  130. }
  131. #endregion
  132. StringBuilder sqlCommand = new StringBuilder();
  133. sqlCommand.Append("UPDATE mp_SurveyResponses SET ");
  134. sqlCommand.Append("Complete = ?Complete, ");
  135. sqlCommand.Append("SubmissionDate = ?SubmissionDate ");
  136. sqlCommand.Append("WHERE ResponseGuid = ?ResponseGuid");
  137. MySqlParameter[] arParams = new MySqlParameter[3];
  138. arParams[0] = new MySqlParameter("?ResponseGuid", MySqlDbType.VarChar, 36);
  139. arParams[0].Direction = ParameterDirection.Input;
  140. arParams[0].Value = responseGuid.ToString();
  141. arParams[1] = new MySqlParameter("?Complete", MySqlDbType.Int32);
  142. arParams[1].Direction = ParameterDirection.Input;
  143. arParams[1].Value = intComplete;
  144. arParams[2] = new MySqlParameter("?SubmissionDate", MySqlDbType.DateTime);
  145. arParams[2].Direction = ParameterDirection.Input;
  146. arParams[2].Value = submissionDate;
  147. int rowsAffected = MySqlHelper.ExecuteNonQuery(
  148. GetWriteConnectionString(),
  149. sqlCommand.ToString(),
  150. arParams);
  151. return (rowsAffected > 0);
  152. }
  153. /// <summary>
  154. /// Deletes a row from the mp_SurveyResponses table. Returns true if row deleted.
  155. /// </summary>
  156. /// <param name="responseGuid"> responseGuid </param>
  157. /// <returns>bool</returns>
  158. public static bool Delete(
  159. Guid responseGuid)
  160. {
  161. StringBuilder sqlCommand = new StringBuilder();
  162. sqlCommand.Append("DELETE FROM mp_SurveyResponses ");
  163. sqlCommand.Append("WHERE ");
  164. sqlCommand.Append("ResponseGuid = ?ResponseGuid ;");
  165. MySqlParameter[] arParams = new MySqlParameter[1];
  166. arParams[0] = new MySqlParameter("?ResponseGuid", MySqlDbType.VarChar, 36);
  167. arParams[0].Direction = ParameterDirection.Input;
  168. arParams[0].Value = responseGuid.ToString();
  169. int rowsAffected = MySqlHelper.ExecuteNonQuery(
  170. GetWriteConnectionString(),
  171. sqlCommand.ToString(),
  172. arParams);
  173. return (rowsAffected > 0);
  174. }
  175. /// <summary>
  176. /// Gets an IDataReader with one row from the mp_SurveyResponses table.
  177. /// </summary>
  178. /// <param name="responseGuid"> responseGuid </param>
  179. public static IDataReader GetOne(
  180. Guid responseGuid)
  181. {
  182. StringBuilder sqlCommand = new StringBuilder();
  183. sqlCommand.Append("SELECT * ");
  184. sqlCommand.Append("FROM mp_SurveyResponses ");
  185. sqlCommand.Append("WHERE ");
  186. sqlCommand.Append("ResponseGuid = ?ResponseGuid ;");
  187. MySqlParameter[] arParams = new MySqlParameter[1];
  188. arParams[0] = new MySqlParameter("?ResponseGuid", MySqlDbType.VarChar, 36);
  189. arParams[0].Direction = ParameterDirection.Input;
  190. arParams[0].Value = responseGuid.ToString();
  191. return MySqlHelper.ExecuteReader(
  192. GetReadConnectionString(),
  193. sqlCommand.ToString(),
  194. arParams);
  195. }
  196. /// <summary>
  197. /// Gets an IDataReader with all rows in the mp_SurveyResponses table.
  198. /// </summary>
  199. public static IDataReader GetAll(Guid surveyGuid)
  200. {
  201. StringBuilder sqlCommand = new StringBuilder();
  202. sqlCommand.Append("SELECT * ");
  203. sqlCommand.Append("FROM mp_SurveyResponses ");
  204. sqlCommand.Append("WHERE SurveyGuid = ?SurveyGuid; ");
  205. MySqlParameter[] arParams = new MySqlParameter[1];
  206. arParams[0] = new MySqlParameter("?SurveyGuid", MySqlDbType.VarChar, 36);
  207. arParams[0].Direction = ParameterDirection.Input;
  208. arParams[0].Value = surveyGuid.ToString();
  209. return MySqlHelper.ExecuteReader(
  210. GetReadConnectionString(),
  211. sqlCommand.ToString(),
  212. arParams);
  213. }
  214. /// <summary>
  215. /// Gets an IDataReader with the first response to a survey
  216. /// </summary>
  217. public static IDataReader GetFirst(Guid surveyGuid)
  218. {
  219. StringBuilder sqlCommand = new StringBuilder();
  220. sqlCommand.Append("SELECT * ");
  221. sqlCommand.Append("FROM mp_SurveyResponses ");
  222. sqlCommand.Append("WHERE SurveyGuid = ?SurveyGuid ");
  223. sqlCommand.Append("AND Complete = 1 ");
  224. sqlCommand.Append("ORDER BY SubmissionDate, ResponseGuid ");
  225. sqlCommand.Append("LIMIT 1; ");
  226. MySqlParameter[] arParams = new MySqlParameter[1];
  227. arParams[0] = new MySqlParameter("?SurveyGuid", MySqlDbType.VarChar, 36);
  228. arParams[0].Direction = ParameterDirection.Input;
  229. arParams[0].Value = surveyGuid.ToString();
  230. return MySqlHelper.ExecuteReader(
  231. GetReadConnectionString(),
  232. sqlCommand.ToString(),
  233. arParams);
  234. }
  235. /// <summary>
  236. /// Gets an IDataReader with the next response to a survey
  237. /// </summary>
  238. public static IDataReader GetNext(Guid responseGuid)
  239. {
  240. StringBuilder sqlCommand = new StringBuilder();
  241. sqlCommand.Append("SELECT * ");
  242. sqlCommand.Append("FROM mp_SurveyResponses ");
  243. sqlCommand.Append("WHERE SubmissionDate > (");
  244. sqlCommand.Append("SELECT SubmissionDate ");
  245. sqlCommand.Append("FROM mp_SurveyResponses ");
  246. sqlCommand.Append("WHERE ");
  247. sqlCommand.Append("ResponseGuid = ?ResponseGuid) ");
  248. sqlCommand.Append("AND ");
  249. sqlCommand.Append("SurveyGuid = (");
  250. sqlCommand.Append("SELECT SurveyGuid ");
  251. sqlCommand.Append("FROM mp_SurveyResponses ");
  252. sqlCommand.Append("WHERE ");
  253. sqlCommand.Append("ResponseGuid = ?ResponseGuid) ");
  254. sqlCommand.Append("AND Complete = 1 ");
  255. sqlCommand.Append("Order By SubmissionDate, ResponseGuid ");
  256. sqlCommand.Append("Limit 1; ");
  257. MySqlParameter[] arParams = new MySqlParameter[1];
  258. arParams[0] = new MySqlParameter("?ResponseGuid", MySqlDbType.VarChar, 36);
  259. arParams[0].Direction = ParameterDirection.Input;
  260. arParams[0].Value = responseGuid.ToString();
  261. return MySqlHelper.ExecuteReader(
  262. GetReadConnectionString(),
  263. sqlCommand.ToString(),
  264. arParams);
  265. }
  266. /// <summary>
  267. /// Gets an IDataReader with the next response to a survey
  268. /// </summary>
  269. public static IDataReader GetPrevious(Guid responseGuid)
  270. {
  271. StringBuilder sqlCommand = new StringBuilder();
  272. sqlCommand.Append("SELECT * ");
  273. sqlCommand.Append("FROM mp_SurveyResponses ");
  274. sqlCommand.Append("WHERE Complete = 1 AND SubmissionDate < (");
  275. sqlCommand.Append("SELECT SubmissionDate ");
  276. sqlCommand.Append("FROM mp_SurveyResponses ");
  277. sqlCommand.Append("WHERE ");
  278. sqlCommand.Append("ResponseGuid = ?ResponseGuid) ");
  279. sqlCommand.Append("AND ");
  280. sqlCommand.Append("SurveyGuid = (");
  281. sqlCommand.Append("SELECT SurveyGuid ");
  282. sqlCommand.Append("FROM mp_SurveyResponses ");
  283. sqlCommand.Append("WHERE ");
  284. sqlCommand.Append("ResponseGuid = ?ResponseGuid) ");
  285. sqlCommand.Append("Order By SubmissionDate DESC, ResponseGuid; ");
  286. MySqlParameter[] arParams = new MySqlParameter[1];
  287. arParams[0] = new MySqlParameter("?ResponseGuid", MySqlDbType.VarChar, 36);
  288. arParams[0].Direction = ParameterDirection.Input;
  289. arParams[0].Value = responseGuid.ToString();
  290. return MySqlHelper.ExecuteReader(
  291. GetReadConnectionString(),
  292. sqlCommand.ToString(),
  293. arParams);
  294. }
  295. }
  296. }