/UniqueStudio.Core/DAL/Compenent/SqlCompenentProvider.cs

http://uniquestudiocms.googlecode.com/ · C# · 298 lines · 212 code · 17 blank · 69 comment · 19 complexity · d8676a661aa4ad02e93c9fb4169c19e3 MD5 · raw file

  1. //=================================================================
  2. // ?????????(c) 2010?????
  3. // ????????????Sql Server???????
  4. // ?????2010?04?02?
  5. // ???v1.0 alpha
  6. // ??????
  7. //=================================================================
  8. using System;
  9. using System.Data;
  10. using System.Data.SqlClient;
  11. using UniqueStudio.Common.Config;
  12. using UniqueStudio.Common.DatabaseHelper;
  13. using UniqueStudio.Common.Model;
  14. using UniqueStudio.DAL.IDAL;
  15. using UniqueStudio.DAL.Permission;
  16. namespace UniqueStudio.DAL.Compenent
  17. {
  18. /// <summary>
  19. /// ???????Sql Server???????
  20. /// </summary>
  21. internal class SqlCompenentProvider : ICompenent
  22. {
  23. private const string CREATE_COMPENENT = "CreateCompenent";
  24. private const string DELETE_COMPENENT = "DeleteCompenent";
  25. private const string GET_ALL_COMPENENTS = "GetAllCompenents";
  26. private const string GET_COMPENENT = "GetCompenent";
  27. private const string GET_COMPENENT_CONFIG_BY_ID = "GetCompenentConfigByID";
  28. private const string GET_COMPENENT_CONFIG_BY_NAME = "GetCompenentConfigByName";
  29. private const string IS_COMPENENT_EXISTS = "IsCompenentExists";
  30. private const string UPDATE_COMPENENT_CONFIG_BY_ID = "UpdateCompenentConfigByID";
  31. private const string UPDATE_COMPENENT_CONFIG_BY_NAME = "UpdateCompenentConfigByName";
  32. /// <summary>
  33. /// ???<see cref="SqlCompenentProvider"/>?????
  34. /// </summary>
  35. public SqlCompenentProvider()
  36. {
  37. //??????
  38. }
  39. /// <summary>
  40. /// ?????
  41. /// </summary>
  42. /// <remarks>????????????</remarks>
  43. /// <param name="compenent">?????</param>
  44. /// <returns>????????????????????</returns>
  45. public CompenentInfo CreateCompenent(CompenentInfo compenent)
  46. {
  47. using (SqlConnection conn = new SqlConnection(GlobalConfig.SqlConnectionString))
  48. {
  49. using (SqlCommand cmd = new SqlCommand(CREATE_COMPENENT, conn))
  50. {
  51. cmd.CommandType = CommandType.StoredProcedure;
  52. conn.Open();
  53. using (SqlTransaction trans = conn.BeginTransaction())
  54. {
  55. cmd.Transaction = trans;
  56. try
  57. {
  58. cmd.Parameters.AddWithValue("@SiteID", compenent.SiteId);
  59. cmd.Parameters.AddWithValue("@CompenentName", compenent.CompenentName);
  60. cmd.Parameters.AddWithValue("@DisplayName", compenent.DisplayName);
  61. cmd.Parameters.AddWithValue("@CompenentAuthor", compenent.CompenentAuthor);
  62. cmd.Parameters.AddWithValue("@Description", compenent.Description);
  63. cmd.Parameters.AddWithValue("@ClassPath", compenent.ClassPath);
  64. cmd.Parameters.AddWithValue("@Assembly", compenent.Assembly);
  65. cmd.Parameters.AddWithValue("@WorkingPath", compenent.WorkingPath);
  66. cmd.Parameters.AddWithValue("@Config", compenent.Config);
  67. object o = cmd.ExecuteScalar();
  68. if (o != null && o != DBNull.Value)
  69. {
  70. compenent.CompenentId = Convert.ToInt32(o);
  71. if ((new SqlPermissionProvider()).CreatePermissions(conn, cmd, compenent.Permissions))
  72. {
  73. trans.Commit();
  74. return compenent;
  75. }
  76. else
  77. {
  78. trans.Rollback();
  79. return null;
  80. }
  81. }
  82. else
  83. {
  84. trans.Rollback();
  85. return null;
  86. }
  87. }
  88. catch
  89. {
  90. trans.Rollback();
  91. return null;
  92. }
  93. }//end of using transaction
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// ???????
  99. /// </summary>
  100. /// <param name="compenentId">?????ID?</param>
  101. /// <returns>???????</returns>
  102. public bool DeleteCompenent(int compenentId)
  103. {
  104. SqlParameter parm = new SqlParameter("@CompenentID", compenentId);
  105. return SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, DELETE_COMPENENT, parm) > 0;
  106. }
  107. /// <summary>
  108. /// ???????
  109. /// </summary>
  110. /// <param name="compenentId">?????ID????</param>
  111. /// <returns>???????</returns>
  112. public bool DeleteCompenents(int[] compenentIds)
  113. {
  114. using (SqlConnection conn = new SqlConnection(GlobalConfig.SqlConnectionString))
  115. {
  116. using (SqlCommand cmd = new SqlCommand(DELETE_COMPENENT, conn))
  117. {
  118. cmd.CommandType = CommandType.StoredProcedure;
  119. cmd.Parameters.Add("@CompenentID", SqlDbType.Int);
  120. conn.Open();
  121. using (SqlTransaction trans = conn.BeginTransaction())
  122. {
  123. cmd.Transaction = trans;
  124. try
  125. {
  126. foreach (int compenentId in compenentIds)
  127. {
  128. cmd.Parameters[0].Value = compenentId;
  129. cmd.ExecuteNonQuery();
  130. }
  131. trans.Commit();
  132. return true;
  133. }
  134. catch
  135. {
  136. trans.Rollback();
  137. return false;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// ??????????
  145. /// </summary>
  146. /// <returns>????????????</returns>
  147. public CompenentCollection GetAllCompenents()
  148. {
  149. CompenentCollection collection = new CompenentCollection();
  150. using (SqlDataReader reader = SqlHelper.ExecuteReader(CommandType.StoredProcedure, GET_ALL_COMPENENTS, null))
  151. {
  152. while (reader.Read())
  153. {
  154. collection.Add(FillCompenentInfo(reader));
  155. }
  156. }
  157. return collection;
  158. }
  159. /// <summary>
  160. /// ????????????
  161. /// </summary>
  162. /// <param name="compenentId">??ID?</param>
  163. /// <returns>?????</returns>
  164. public string GetCompenentConfig(int compenentId)
  165. {
  166. SqlParameter parm = new SqlParameter("@CompenentID", compenentId);
  167. object o = SqlHelper.ExecuteScalar(CommandType.StoredProcedure, GET_COMPENENT_CONFIG_BY_ID, parm);
  168. if (o != null && o != DBNull.Value)
  169. {
  170. return (string)o;
  171. }
  172. else
  173. {
  174. return null;
  175. }
  176. }
  177. /// <summary>
  178. /// ????????????
  179. /// </summary>
  180. /// <param name="siteId">??ID?</param>
  181. /// <param name="compenentName">?????</param>
  182. /// <returns>?????</returns>
  183. public string GetCompenentConfig(int siteId, string compenentName)
  184. {
  185. SqlParameter[] parms = new SqlParameter[]{
  186. new SqlParameter("@SiteID",siteId),
  187. new SqlParameter("@CompenentName",compenentName)};
  188. object o = SqlHelper.ExecuteScalar(CommandType.StoredProcedure, GET_COMPENENT_CONFIG_BY_NAME, parms);
  189. if (o != null && o != DBNull.Value)
  190. {
  191. return (string)o;
  192. }
  193. else
  194. {
  195. return null;
  196. }
  197. }
  198. /// <summary>
  199. /// ???????
  200. /// </summary>
  201. /// <param name="compenentId">??ID?</param>
  202. /// <returns>?????</returns>
  203. public CompenentInfo GetCompenent(int compenentId)
  204. {
  205. SqlParameter parm = new SqlParameter("@CompenentID", compenentId);
  206. using (SqlDataReader reader = SqlHelper.ExecuteReader(CommandType.StoredProcedure, GET_COMPENENT, parm))
  207. {
  208. if (reader.Read())
  209. {
  210. return FillCompenentInfo(reader);
  211. }
  212. else
  213. {
  214. return null;
  215. }
  216. }
  217. }
  218. /// <summary>
  219. /// ???????????
  220. /// </summary>
  221. /// <param name="siteId">??ID?</param>
  222. /// <param name="compenentName">?????</param>
  223. /// <returns>?????</returns>
  224. public bool IsCompenentExists(int siteId, string compenentName)
  225. {
  226. SqlParameter[] parms = new SqlParameter[]{
  227. new SqlParameter("@SiteID",siteId),
  228. new SqlParameter("@CompenentName",compenentName)};
  229. object o = SqlHelper.ExecuteScalar(CommandType.StoredProcedure, IS_COMPENENT_EXISTS, parms);
  230. if (o != null && o != DBNull.Value)
  231. {
  232. return Convert.ToBoolean((int)o);
  233. }
  234. else
  235. {
  236. throw new Exception();
  237. }
  238. }
  239. /// <summary>
  240. /// ?????????
  241. /// </summary>
  242. /// <param name="compenentId">??ID?</param>
  243. /// <param name="config">?????</param>
  244. /// <returns>???????</returns>
  245. public bool UpdateCompenentConfig(int compenentId, string config)
  246. {
  247. SqlParameter[] parms = new SqlParameter[]{
  248. new SqlParameter("@CompenentID", compenentId),
  249. new SqlParameter("@Config",config)};
  250. return SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, UPDATE_COMPENENT_CONFIG_BY_ID, parms) > 0;
  251. }
  252. /// <summary>
  253. /// ?????????
  254. /// </summary>
  255. /// <param name="siteId">??ID?</param>
  256. /// <param name="compenentName">?????</param>
  257. /// <param name="config">?????</param>
  258. /// <returns>???????</returns>
  259. public bool UpdateCompenentConfig(int siteId, string compenentName, string config)
  260. {
  261. SqlParameter[] parms = new SqlParameter[]{
  262. new SqlParameter("@SiteID",siteId),
  263. new SqlParameter("@CompenentName",compenentName),
  264. new SqlParameter("@Config",config)};
  265. return SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, UPDATE_COMPENENT_CONFIG_BY_NAME, parms) > 0;
  266. }
  267. private CompenentInfo FillCompenentInfo(SqlDataReader reader)
  268. {
  269. CompenentInfo compenent = new CompenentInfo();
  270. compenent.CompenentId = (int)reader["CompenentID"];
  271. compenent.SiteId = (int)reader["SiteID"];
  272. compenent.CompenentName = reader["CompenentName"].ToString();
  273. compenent.DisplayName = reader["DisplayName"].ToString();
  274. compenent.CompenentAuthor = reader["CompenentAuthor"].ToString();
  275. compenent.Description = reader["Description"].ToString();
  276. compenent.WorkingPath = reader["WorkingPath"].ToString();
  277. compenent.SiteName = reader["SiteName"].ToString();
  278. return compenent;
  279. }
  280. }
  281. }