PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mojoPortal.Features.Data.MSSQL/Poll/DBPoll.cs

#
C# | 209 lines | 151 code | 38 blank | 20 comment | 3 complexity | 9fd2e4d4fec360749180c97ad58cd10c 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: Christian Fredh
  2. /// Created: 2007-07-01
  3. /// Last Modified: 2008-11-08
  4. ///
  5. ///
  6. /// The use and distribution terms for this software are covered by the
  7. /// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  8. /// which can be found in the file CPL.TXT at the root of this distribution.
  9. /// By using this software in any fashion, you are agreeing to be bound by
  10. /// the terms of this license.
  11. ///
  12. /// You must not remove this notice, or any other, from this software.
  13. using System;
  14. using System.Data;
  15. using System.Configuration;
  16. using mojoPortal.Data;
  17. namespace PollFeature.Data
  18. {
  19. public static class DBPoll
  20. {
  21. /// <summary>
  22. /// Gets the connection string for read.
  23. /// </summary>
  24. /// <returns></returns>
  25. private static string GetReadConnectionString()
  26. {
  27. return ConfigurationManager.AppSettings["MSSQLConnectionString"];
  28. }
  29. /// <summary>
  30. /// Gets the connection string for write.
  31. /// </summary>
  32. /// <returns></returns>
  33. private static string GetWriteConnectionString()
  34. {
  35. if (ConfigurationManager.AppSettings["MSSQLWriteConnectionString"] != null)
  36. {
  37. return ConfigurationManager.AppSettings["MSSQLWriteConnectionString"];
  38. }
  39. return ConfigurationManager.AppSettings["MSSQLConnectionString"];
  40. }
  41. public static String dbPlatform()
  42. {
  43. return "MSSQL";
  44. }
  45. public static IDataReader GetPolls(Guid siteGuid)
  46. {
  47. SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "mp_Polls_Select", 1);
  48. sph.DefineSqlParameter("@SiteGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, siteGuid);
  49. return sph.ExecuteReader();
  50. }
  51. public static IDataReader GetActivePolls(Guid siteGuid)
  52. {
  53. SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "mp_Polls_SelectActive", 2);
  54. sph.DefineSqlParameter("@SiteGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, siteGuid);
  55. sph.DefineSqlParameter("@CurrentTime", SqlDbType.DateTime, ParameterDirection.Input, DateTime.UtcNow);
  56. return sph.ExecuteReader();
  57. }
  58. public static IDataReader GetPollsByUserGuid(Guid userGuid)
  59. {
  60. SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "mp_Polls_SelectByUserGuid", 1);
  61. sph.DefineSqlParameter("@UserGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, userGuid);
  62. return sph.ExecuteReader();
  63. }
  64. public static int Add(
  65. Guid pollGuid,
  66. Guid siteGuid,
  67. String question,
  68. bool anonymousVoting,
  69. bool allowViewingResultsBeforeVoting,
  70. bool showOrderNumbers,
  71. bool showResultsWhenDeactivated,
  72. bool active,
  73. DateTime activeFrom,
  74. DateTime activeTo)
  75. {
  76. SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_Polls_Insert", 10);
  77. sph.DefineSqlParameter("@PollGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, pollGuid);
  78. sph.DefineSqlParameter("@SiteGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, siteGuid);
  79. sph.DefineSqlParameter("@Question", SqlDbType.NVarChar, 255, ParameterDirection.Input, question);
  80. sph.DefineSqlParameter("@AnonymousVoting", SqlDbType.Bit, ParameterDirection.Input, anonymousVoting);
  81. sph.DefineSqlParameter("@AllowViewingResultsBeforeVoting", SqlDbType.Bit, ParameterDirection.Input, allowViewingResultsBeforeVoting);
  82. sph.DefineSqlParameter("@ShowOrderNumbers", SqlDbType.Bit, ParameterDirection.Input, showOrderNumbers);
  83. sph.DefineSqlParameter("@ShowResultsWhenDeactivated", SqlDbType.Bit, ParameterDirection.Input, showResultsWhenDeactivated);
  84. sph.DefineSqlParameter("@Active", SqlDbType.Bit, ParameterDirection.Input, active);
  85. sph.DefineSqlParameter("@ActiveFrom", SqlDbType.DateTime, ParameterDirection.Input, activeFrom);
  86. sph.DefineSqlParameter("@ActiveTo", SqlDbType.DateTime, ParameterDirection.Input, activeTo);
  87. int rowsAffected = sph.ExecuteNonQuery();
  88. return rowsAffected;
  89. }
  90. public static bool Update(
  91. Guid pollGuid,
  92. String question,
  93. bool anonymousVoting,
  94. bool allowViewingResultsBeforeVoting,
  95. bool showOrderNumbers,
  96. bool showResultsWhenDeactivated,
  97. bool active,
  98. DateTime activeFrom,
  99. DateTime activeTo)
  100. {
  101. SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_Polls_Update", 9);
  102. sph.DefineSqlParameter("@PollGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, pollGuid);
  103. sph.DefineSqlParameter("@Question", SqlDbType.NVarChar, 255, ParameterDirection.Input, question);
  104. sph.DefineSqlParameter("@AnonymousVoting", SqlDbType.Bit, ParameterDirection.Input, anonymousVoting);
  105. sph.DefineSqlParameter("@AllowViewingResultsBeforeVoting", SqlDbType.Bit, ParameterDirection.Input, allowViewingResultsBeforeVoting);
  106. sph.DefineSqlParameter("@ShowOrderNumbers", SqlDbType.Bit, ParameterDirection.Input, showOrderNumbers);
  107. sph.DefineSqlParameter("@ShowResultsWhenDeactivated", SqlDbType.Bit, ParameterDirection.Input, showResultsWhenDeactivated);
  108. sph.DefineSqlParameter("@Active", SqlDbType.Bit, ParameterDirection.Input, active);
  109. sph.DefineSqlParameter("@ActiveFrom", SqlDbType.DateTime, ParameterDirection.Input, activeFrom);
  110. sph.DefineSqlParameter("@ActiveTo", SqlDbType.DateTime, ParameterDirection.Input, activeTo);
  111. int rowsAffected = sph.ExecuteNonQuery();
  112. return (rowsAffected > 0);
  113. }
  114. public static IDataReader GetPoll(Guid pollGuid)
  115. {
  116. SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "mp_Polls_SelectOne", 1);
  117. sph.DefineSqlParameter("@PollGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, pollGuid);
  118. return sph.ExecuteReader();
  119. }
  120. public static IDataReader GetPollByModuleID(int moduleID)
  121. {
  122. SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "mp_Polls_SelectOneByModuleID", 1);
  123. sph.DefineSqlParameter("@ModuleID", SqlDbType.Int, ParameterDirection.Input, moduleID);
  124. return sph.ExecuteReader();
  125. }
  126. public static bool ClearVotes(Guid pollGuid)
  127. {
  128. SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_Polls_ClearVotes", 1);
  129. sph.DefineSqlParameter("@PollGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, pollGuid);
  130. int rowsAffected = sph.ExecuteNonQuery();
  131. return (rowsAffected > 0);
  132. }
  133. public static bool Delete(Guid pollGuid)
  134. {
  135. SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_Polls_Delete", 1);
  136. sph.DefineSqlParameter("@PollGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, pollGuid);
  137. int rowsAffected = sph.ExecuteNonQuery();
  138. return (rowsAffected > 0);
  139. }
  140. public static bool DeleteBySite(int siteId)
  141. {
  142. SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_Polls_DeleteBySite", 1);
  143. sph.DefineSqlParameter("@SiteID", SqlDbType.Int, ParameterDirection.Input, siteId);
  144. int rowsAffected = sph.ExecuteNonQuery();
  145. return (rowsAffected > -1);
  146. }
  147. public static bool UserHasVoted(Guid pollGuid, Guid userGuid)
  148. {
  149. SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_Polls_UserHasVoted", 2);
  150. sph.DefineSqlParameter("@PollGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, pollGuid);
  151. sph.DefineSqlParameter("@UserGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, userGuid);
  152. int userHasVoted = Convert.ToInt32(sph.ExecuteScalar());
  153. return (userHasVoted == 1);
  154. }
  155. public static bool AddToModule(Guid pollGuid, int moduleID)
  156. {
  157. SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_Polls_AddToModule", 2);
  158. sph.DefineSqlParameter("@PollGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, pollGuid);
  159. sph.DefineSqlParameter("@ModuleID", SqlDbType.Int, ParameterDirection.Input, moduleID);
  160. int rowsAffected = sph.ExecuteNonQuery();
  161. return (rowsAffected > -1);
  162. }
  163. public static bool RemoveFromModule(int moduleID)
  164. {
  165. SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_Polls_RemoveFromModule", 1);
  166. sph.DefineSqlParameter("@ModuleID", SqlDbType.Int, ParameterDirection.Input, moduleID);
  167. int rowsAffected = sph.ExecuteNonQuery();
  168. return (rowsAffected > -1);
  169. }
  170. }
  171. }