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

/mojoPortal.Data.SqlCe/DBSystemLog.cs

#
C# | 393 lines | 238 code | 76 blank | 79 comment | 8 complexity | e031ba824d81d317a703d32b9ec1f011 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: Joe Audette
  2. // Created: 2011-07-23
  3. // Last Modified: 2011-07-28
  4. //
  5. // The use and distribution terms for this software are covered by the
  6. // Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. // which can be found in the file CPL.TXT at the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by
  9. // the terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. using System;
  13. using System.Data;
  14. using System.Data.SqlServerCe;
  15. using System.Globalization;
  16. using System.Text;
  17. namespace mojoPortal.Data
  18. {
  19. public static class DBSystemLog
  20. {
  21. private static String GetConnectionString()
  22. {
  23. return DBPortal.GetConnectionString();
  24. }
  25. /// <summary>
  26. /// Inserts a row in the mp_SystemLog table. Returns new integer id.
  27. /// </summary>
  28. /// <param name="logDate"> logDate </param>
  29. /// <param name="ipAddress"> ipAddress </param>
  30. /// <param name="culture"> culture </param>
  31. /// <param name="url"> url </param>
  32. /// <param name="shortUrl"> shortUrl </param>
  33. /// <param name="thread"> thread </param>
  34. /// <param name="logLevel"> logLevel </param>
  35. /// <param name="logger"> logger </param>
  36. /// <param name="message"> message </param>
  37. /// <returns>int</returns>
  38. public static int Create(
  39. DateTime logDate,
  40. string ipAddress,
  41. string culture,
  42. string url,
  43. string shortUrl,
  44. string thread,
  45. string logLevel,
  46. string logger,
  47. string message)
  48. {
  49. StringBuilder sqlCommand = new StringBuilder();
  50. sqlCommand.Append("INSERT INTO mp_SystemLog ");
  51. sqlCommand.Append("(");
  52. sqlCommand.Append("LogDate, ");
  53. sqlCommand.Append("IpAddress, ");
  54. sqlCommand.Append("Culture, ");
  55. sqlCommand.Append("Url, ");
  56. sqlCommand.Append("ShortUrl, ");
  57. sqlCommand.Append("Thread, ");
  58. sqlCommand.Append("LogLevel, ");
  59. sqlCommand.Append("Logger, ");
  60. sqlCommand.Append("Message ");
  61. sqlCommand.Append(")");
  62. sqlCommand.Append(" VALUES ");
  63. sqlCommand.Append("(");
  64. sqlCommand.Append("@LogDate, ");
  65. sqlCommand.Append("@IpAddress, ");
  66. sqlCommand.Append("@Culture, ");
  67. sqlCommand.Append("@Url, ");
  68. sqlCommand.Append("@ShortUrl, ");
  69. sqlCommand.Append("@Thread, ");
  70. sqlCommand.Append("@LogLevel, ");
  71. sqlCommand.Append("@Logger, ");
  72. sqlCommand.Append("@Message ");
  73. sqlCommand.Append(")");
  74. sqlCommand.Append(";");
  75. SqlCeParameter[] arParams = new SqlCeParameter[9];
  76. arParams[0] = new SqlCeParameter("@LogDate", SqlDbType.DateTime);
  77. arParams[0].Direction = ParameterDirection.Input;
  78. arParams[0].Value = logDate;
  79. arParams[1] = new SqlCeParameter("@IpAddress", SqlDbType.NVarChar, 50);
  80. arParams[1].Direction = ParameterDirection.Input;
  81. arParams[1].Value = ipAddress;
  82. arParams[2] = new SqlCeParameter("@Culture", SqlDbType.NVarChar, 10);
  83. arParams[2].Direction = ParameterDirection.Input;
  84. arParams[2].Value = culture;
  85. arParams[3] = new SqlCeParameter("@Url", SqlDbType.NText);
  86. arParams[3].Direction = ParameterDirection.Input;
  87. arParams[3].Value = url;
  88. arParams[4] = new SqlCeParameter("@ShortUrl", SqlDbType.NVarChar, 255);
  89. arParams[4].Direction = ParameterDirection.Input;
  90. arParams[4].Value = shortUrl;
  91. arParams[5] = new SqlCeParameter("@Thread", SqlDbType.NVarChar, 255);
  92. arParams[5].Direction = ParameterDirection.Input;
  93. arParams[5].Value = thread;
  94. arParams[6] = new SqlCeParameter("@LogLevel", SqlDbType.NVarChar, 20);
  95. arParams[6].Direction = ParameterDirection.Input;
  96. arParams[6].Value = logLevel;
  97. arParams[7] = new SqlCeParameter("@Logger", SqlDbType.NVarChar, 255);
  98. arParams[7].Direction = ParameterDirection.Input;
  99. arParams[7].Value = logger;
  100. arParams[8] = new SqlCeParameter("@Message", SqlDbType.NText);
  101. arParams[8].Direction = ParameterDirection.Input;
  102. arParams[8].Value = message;
  103. int newId = Convert.ToInt32(SqlHelper.DoInsertGetIdentitiy(
  104. GetConnectionString(),
  105. CommandType.Text,
  106. sqlCommand.ToString(),
  107. arParams));
  108. return newId;
  109. }
  110. /// <summary>
  111. /// Deletes rows from the mp_SystemLog table. Returns true if rows deleted.
  112. /// </summary>
  113. public static void DeleteAll()
  114. {
  115. StringBuilder sqlCommand = new StringBuilder();
  116. sqlCommand.Append("DELETE FROM mp_SystemLog ");
  117. //sqlCommand.Append("WHERE ");
  118. //sqlCommand.Append("ID = @ID ");
  119. sqlCommand.Append(";");
  120. //SqlCeParameter[] arParams = new SqlCeParameter[1];
  121. //arParams[0] = new SqlCeParameter("@ID", SqlDbType.Int);
  122. //arParams[0].Direction = ParameterDirection.Input;
  123. //arParams[0].Value = id;
  124. SqlHelper.ExecuteNonQuery(
  125. GetConnectionString(),
  126. CommandType.Text,
  127. sqlCommand.ToString(),
  128. null);
  129. }
  130. /// <summary>
  131. /// Deletes a row from the mp_SystemLog table. Returns true if row deleted.
  132. /// </summary>
  133. /// <param name="id"> id </param>
  134. /// <returns>bool</returns>
  135. public static bool Delete(int id)
  136. {
  137. StringBuilder sqlCommand = new StringBuilder();
  138. sqlCommand.Append("DELETE FROM mp_SystemLog ");
  139. sqlCommand.Append("WHERE ");
  140. sqlCommand.Append("ID = @ID ");
  141. sqlCommand.Append(";");
  142. SqlCeParameter[] arParams = new SqlCeParameter[1];
  143. arParams[0] = new SqlCeParameter("@ID", SqlDbType.Int);
  144. arParams[0].Direction = ParameterDirection.Input;
  145. arParams[0].Value = id;
  146. int rowsAffected = SqlHelper.ExecuteNonQuery(
  147. GetConnectionString(),
  148. CommandType.Text,
  149. sqlCommand.ToString(),
  150. arParams);
  151. return (rowsAffected > -1);
  152. }
  153. /// <summary>
  154. /// Deletes rows from the mp_SystemLog table. Returns true if rows deleted.
  155. /// </summary>
  156. /// <param name="id"> id </param>
  157. /// <returns>bool</returns>
  158. public static bool DeleteOlderThan(DateTime cutoffDate)
  159. {
  160. StringBuilder sqlCommand = new StringBuilder();
  161. sqlCommand.Append("DELETE FROM mp_SystemLog ");
  162. sqlCommand.Append("WHERE ");
  163. sqlCommand.Append("LogDate < @CutoffDate ");
  164. sqlCommand.Append(";");
  165. SqlCeParameter[] arParams = new SqlCeParameter[1];
  166. arParams[0] = new SqlCeParameter("@CutoffDate", SqlDbType.DateTime);
  167. arParams[0].Direction = ParameterDirection.Input;
  168. arParams[0].Value = cutoffDate;
  169. int rowsAffected = SqlHelper.ExecuteNonQuery(
  170. GetConnectionString(),
  171. CommandType.Text,
  172. sqlCommand.ToString(),
  173. arParams);
  174. return (rowsAffected > -1);
  175. }
  176. /// <summary>
  177. /// Deletes rows from the mp_SystemLog table. Returns true if rows deleted.
  178. /// </summary>
  179. /// <param name="id"> id </param>
  180. /// <returns>bool</returns>
  181. public static bool DeleteByLevel(string logLevel)
  182. {
  183. StringBuilder sqlCommand = new StringBuilder();
  184. sqlCommand.Append("DELETE FROM mp_SystemLog ");
  185. sqlCommand.Append("WHERE ");
  186. sqlCommand.Append("LogLevel = @LogLevel ");
  187. sqlCommand.Append(";");
  188. SqlCeParameter[] arParams = new SqlCeParameter[1];
  189. arParams[0] = new SqlCeParameter("@LogLevel", SqlDbType.NVarChar, 20);
  190. arParams[0].Direction = ParameterDirection.Input;
  191. arParams[0].Value = logLevel;
  192. int rowsAffected = SqlHelper.ExecuteNonQuery(
  193. GetConnectionString(),
  194. CommandType.Text,
  195. sqlCommand.ToString(),
  196. arParams);
  197. return (rowsAffected > -1);
  198. }
  199. /// <summary>
  200. /// Gets a count of rows in the mp_SystemLog table.
  201. /// </summary>
  202. public static int GetCount()
  203. {
  204. StringBuilder sqlCommand = new StringBuilder();
  205. sqlCommand.Append("SELECT Count(*) ");
  206. sqlCommand.Append("FROM mp_SystemLog ");
  207. sqlCommand.Append(";");
  208. //SqlCeParameter[] arParams = new SqlCeParameter[1];
  209. //arParams[0] = new SqlCeParameter("@ApplicationID", SqlDbType.UniqueIdentifier);
  210. //arParams[0].Direction = ParameterDirection.Input;
  211. //arParams[0].Value = applicationId;
  212. return Convert.ToInt32(SqlHelper.ExecuteScalar(
  213. GetConnectionString(),
  214. CommandType.Text,
  215. sqlCommand.ToString(),
  216. null));
  217. }
  218. /// <summary>
  219. /// Gets a page of data from the mp_SystemLog table.
  220. /// </summary>
  221. /// <param name="pageNumber">The page number.</param>
  222. /// <param name="pageSize">Size of the page.</param>
  223. /// <param name="totalPages">total pages</param>
  224. public static IDataReader GetPageAscending(
  225. int pageNumber,
  226. int pageSize,
  227. out int totalPages)
  228. {
  229. int pageLowerBound = (pageSize * pageNumber) - pageSize;
  230. totalPages = 1;
  231. int totalRows = GetCount();
  232. if (pageSize > 0) totalPages = totalRows / pageSize;
  233. if (totalRows <= pageSize)
  234. {
  235. totalPages = 1;
  236. }
  237. else
  238. {
  239. int remainder;
  240. Math.DivRem(totalRows, pageSize, out remainder);
  241. if (remainder > 0)
  242. {
  243. totalPages += 1;
  244. }
  245. }
  246. int offset = 0;
  247. if (pageNumber > 1) { offset = (pageSize * pageNumber) - pageSize; }
  248. StringBuilder sqlCommand = new StringBuilder();
  249. sqlCommand.Append("SELECT * ");
  250. sqlCommand.Append("FROM mp_SystemLog ");
  251. //sqlCommand.Append("WHERE ");
  252. sqlCommand.Append("ORDER BY ID ");
  253. //order by is required if using fetch and offset or an error will occur, uncomment it and put at least one column to sort by
  254. sqlCommand.Append("OFFSET " + offset.ToString(CultureInfo.InvariantCulture) + " ROWS ");
  255. sqlCommand.Append("FETCH NEXT " + pageSize.ToString(CultureInfo.InvariantCulture) + "ROWS ONLY ");
  256. sqlCommand.Append(";");
  257. //SqlCeParameter[] arParams = new SqlCeParameter[1];
  258. //arParams[0] = new SqlCeParameter("@ApplicationID", SqlDbType.UniqueIdentifier);
  259. //arParams[0].Direction = ParameterDirection.Input;
  260. //arParams[0].Value = applicationId;
  261. return SqlHelper.ExecuteReader(
  262. GetConnectionString(),
  263. CommandType.Text,
  264. sqlCommand.ToString(),
  265. null);
  266. }
  267. /// <summary>
  268. /// Gets a page of data from the mp_SystemLog table.
  269. /// </summary>
  270. /// <param name="pageNumber">The page number.</param>
  271. /// <param name="pageSize">Size of the page.</param>
  272. /// <param name="totalPages">total pages</param>
  273. public static IDataReader GetPageDescending(
  274. int pageNumber,
  275. int pageSize,
  276. out int totalPages)
  277. {
  278. int pageLowerBound = (pageSize * pageNumber) - pageSize;
  279. totalPages = 1;
  280. int totalRows = GetCount();
  281. if (pageSize > 0) totalPages = totalRows / pageSize;
  282. if (totalRows <= pageSize)
  283. {
  284. totalPages = 1;
  285. }
  286. else
  287. {
  288. int remainder;
  289. Math.DivRem(totalRows, pageSize, out remainder);
  290. if (remainder > 0)
  291. {
  292. totalPages += 1;
  293. }
  294. }
  295. int offset = 0;
  296. if (pageNumber > 1) { offset = (pageSize * pageNumber) - pageSize; }
  297. StringBuilder sqlCommand = new StringBuilder();
  298. sqlCommand.Append("SELECT * ");
  299. sqlCommand.Append("FROM mp_SystemLog ");
  300. //sqlCommand.Append("WHERE ");
  301. sqlCommand.Append("ORDER BY ID DESC ");
  302. //order by is required if using fetch and offset or an error will occur, uncomment it and put at least one column to sort by
  303. sqlCommand.Append("OFFSET " + offset.ToString(CultureInfo.InvariantCulture) + " ROWS ");
  304. sqlCommand.Append("FETCH NEXT " + pageSize.ToString(CultureInfo.InvariantCulture) + "ROWS ONLY ");
  305. sqlCommand.Append(";");
  306. //SqlCeParameter[] arParams = new SqlCeParameter[1];
  307. //arParams[0] = new SqlCeParameter("@ApplicationID", SqlDbType.UniqueIdentifier);
  308. //arParams[0].Direction = ParameterDirection.Input;
  309. //arParams[0].Value = applicationId;
  310. return SqlHelper.ExecuteReader(
  311. GetConnectionString(),
  312. CommandType.Text,
  313. sqlCommand.ToString(),
  314. null);
  315. }
  316. }
  317. }