/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
- //=================================================================
- // ?????????(c) 2010?????
- // ????????????Sql Server???????
- // ?????2010?04?02?
- // ???v1.0 alpha
- // ??????
- //=================================================================
- using System;
- using System.Data;
- using System.Data.SqlClient;
-
- using UniqueStudio.Common.Config;
- using UniqueStudio.Common.DatabaseHelper;
- using UniqueStudio.Common.Model;
- using UniqueStudio.DAL.IDAL;
- using UniqueStudio.DAL.Permission;
-
- namespace UniqueStudio.DAL.Compenent
- {
- /// <summary>
- /// ???????Sql Server???????
- /// </summary>
- internal class SqlCompenentProvider : ICompenent
- {
- private const string CREATE_COMPENENT = "CreateCompenent";
- private const string DELETE_COMPENENT = "DeleteCompenent";
- private const string GET_ALL_COMPENENTS = "GetAllCompenents";
- private const string GET_COMPENENT = "GetCompenent";
- private const string GET_COMPENENT_CONFIG_BY_ID = "GetCompenentConfigByID";
- private const string GET_COMPENENT_CONFIG_BY_NAME = "GetCompenentConfigByName";
- private const string IS_COMPENENT_EXISTS = "IsCompenentExists";
- private const string UPDATE_COMPENENT_CONFIG_BY_ID = "UpdateCompenentConfigByID";
- private const string UPDATE_COMPENENT_CONFIG_BY_NAME = "UpdateCompenentConfigByName";
-
- /// <summary>
- /// ???<see cref="SqlCompenentProvider"/>?????
- /// </summary>
- public SqlCompenentProvider()
- {
- //??????
- }
-
- /// <summary>
- /// ?????
- /// </summary>
- /// <remarks>????????????</remarks>
- /// <param name="compenent">?????</param>
- /// <returns>????????????????????</returns>
- public CompenentInfo CreateCompenent(CompenentInfo compenent)
- {
- using (SqlConnection conn = new SqlConnection(GlobalConfig.SqlConnectionString))
- {
- using (SqlCommand cmd = new SqlCommand(CREATE_COMPENENT, conn))
- {
- cmd.CommandType = CommandType.StoredProcedure;
- conn.Open();
-
- using (SqlTransaction trans = conn.BeginTransaction())
- {
- cmd.Transaction = trans;
- try
- {
- cmd.Parameters.AddWithValue("@SiteID", compenent.SiteId);
- cmd.Parameters.AddWithValue("@CompenentName", compenent.CompenentName);
- cmd.Parameters.AddWithValue("@DisplayName", compenent.DisplayName);
- cmd.Parameters.AddWithValue("@CompenentAuthor", compenent.CompenentAuthor);
- cmd.Parameters.AddWithValue("@Description", compenent.Description);
- cmd.Parameters.AddWithValue("@ClassPath", compenent.ClassPath);
- cmd.Parameters.AddWithValue("@Assembly", compenent.Assembly);
- cmd.Parameters.AddWithValue("@WorkingPath", compenent.WorkingPath);
- cmd.Parameters.AddWithValue("@Config", compenent.Config);
- object o = cmd.ExecuteScalar();
- if (o != null && o != DBNull.Value)
- {
- compenent.CompenentId = Convert.ToInt32(o);
- if ((new SqlPermissionProvider()).CreatePermissions(conn, cmd, compenent.Permissions))
- {
- trans.Commit();
- return compenent;
- }
- else
- {
- trans.Rollback();
- return null;
- }
- }
- else
- {
- trans.Rollback();
- return null;
- }
- }
- catch
- {
- trans.Rollback();
- return null;
- }
- }//end of using transaction
- }
- }
- }
-
- /// <summary>
- /// ???????
- /// </summary>
- /// <param name="compenentId">?????ID?</param>
- /// <returns>???????</returns>
- public bool DeleteCompenent(int compenentId)
- {
- SqlParameter parm = new SqlParameter("@CompenentID", compenentId);
- return SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, DELETE_COMPENENT, parm) > 0;
- }
-
- /// <summary>
- /// ???????
- /// </summary>
- /// <param name="compenentId">?????ID????</param>
- /// <returns>???????</returns>
- public bool DeleteCompenents(int[] compenentIds)
- {
- using (SqlConnection conn = new SqlConnection(GlobalConfig.SqlConnectionString))
- {
- using (SqlCommand cmd = new SqlCommand(DELETE_COMPENENT, conn))
- {
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.Add("@CompenentID", SqlDbType.Int);
-
- conn.Open();
- using (SqlTransaction trans = conn.BeginTransaction())
- {
- cmd.Transaction = trans;
-
- try
- {
- foreach (int compenentId in compenentIds)
- {
- cmd.Parameters[0].Value = compenentId;
- cmd.ExecuteNonQuery();
- }
- trans.Commit();
- return true;
- }
- catch
- {
- trans.Rollback();
- return false;
- }
- }
- }
- }
- }
-
- /// <summary>
- /// ??????????
- /// </summary>
- /// <returns>????????????</returns>
- public CompenentCollection GetAllCompenents()
- {
- CompenentCollection collection = new CompenentCollection();
- using (SqlDataReader reader = SqlHelper.ExecuteReader(CommandType.StoredProcedure, GET_ALL_COMPENENTS, null))
- {
- while (reader.Read())
- {
- collection.Add(FillCompenentInfo(reader));
- }
- }
- return collection;
- }
-
- /// <summary>
- /// ????????????
- /// </summary>
- /// <param name="compenentId">??ID?</param>
- /// <returns>?????</returns>
- public string GetCompenentConfig(int compenentId)
- {
- SqlParameter parm = new SqlParameter("@CompenentID", compenentId);
- object o = SqlHelper.ExecuteScalar(CommandType.StoredProcedure, GET_COMPENENT_CONFIG_BY_ID, parm);
- if (o != null && o != DBNull.Value)
- {
- return (string)o;
- }
- else
- {
- return null;
- }
- }
-
- /// <summary>
- /// ????????????
- /// </summary>
- /// <param name="siteId">??ID?</param>
- /// <param name="compenentName">?????</param>
- /// <returns>?????</returns>
- public string GetCompenentConfig(int siteId, string compenentName)
- {
- SqlParameter[] parms = new SqlParameter[]{
- new SqlParameter("@SiteID",siteId),
- new SqlParameter("@CompenentName",compenentName)};
- object o = SqlHelper.ExecuteScalar(CommandType.StoredProcedure, GET_COMPENENT_CONFIG_BY_NAME, parms);
- if (o != null && o != DBNull.Value)
- {
- return (string)o;
- }
- else
- {
- return null;
- }
- }
-
- /// <summary>
- /// ???????
- /// </summary>
- /// <param name="compenentId">??ID?</param>
- /// <returns>?????</returns>
- public CompenentInfo GetCompenent(int compenentId)
- {
- SqlParameter parm = new SqlParameter("@CompenentID", compenentId);
- using (SqlDataReader reader = SqlHelper.ExecuteReader(CommandType.StoredProcedure, GET_COMPENENT, parm))
- {
- if (reader.Read())
- {
- return FillCompenentInfo(reader);
- }
- else
- {
- return null;
- }
- }
- }
-
- /// <summary>
- /// ???????????
- /// </summary>
- /// <param name="siteId">??ID?</param>
- /// <param name="compenentName">?????</param>
- /// <returns>?????</returns>
- public bool IsCompenentExists(int siteId, string compenentName)
- {
- SqlParameter[] parms = new SqlParameter[]{
- new SqlParameter("@SiteID",siteId),
- new SqlParameter("@CompenentName",compenentName)};
- object o = SqlHelper.ExecuteScalar(CommandType.StoredProcedure, IS_COMPENENT_EXISTS, parms);
- if (o != null && o != DBNull.Value)
- {
- return Convert.ToBoolean((int)o);
- }
- else
- {
- throw new Exception();
- }
- }
-
- /// <summary>
- /// ?????????
- /// </summary>
- /// <param name="compenentId">??ID?</param>
- /// <param name="config">?????</param>
- /// <returns>???????</returns>
- public bool UpdateCompenentConfig(int compenentId, string config)
- {
- SqlParameter[] parms = new SqlParameter[]{
- new SqlParameter("@CompenentID", compenentId),
- new SqlParameter("@Config",config)};
- return SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, UPDATE_COMPENENT_CONFIG_BY_ID, parms) > 0;
- }
-
- /// <summary>
- /// ?????????
- /// </summary>
- /// <param name="siteId">??ID?</param>
- /// <param name="compenentName">?????</param>
- /// <param name="config">?????</param>
- /// <returns>???????</returns>
- public bool UpdateCompenentConfig(int siteId, string compenentName, string config)
- {
- SqlParameter[] parms = new SqlParameter[]{
- new SqlParameter("@SiteID",siteId),
- new SqlParameter("@CompenentName",compenentName),
- new SqlParameter("@Config",config)};
- return SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, UPDATE_COMPENENT_CONFIG_BY_NAME, parms) > 0;
- }
-
- private CompenentInfo FillCompenentInfo(SqlDataReader reader)
- {
- CompenentInfo compenent = new CompenentInfo();
- compenent.CompenentId = (int)reader["CompenentID"];
- compenent.SiteId = (int)reader["SiteID"];
- compenent.CompenentName = reader["CompenentName"].ToString();
- compenent.DisplayName = reader["DisplayName"].ToString();
- compenent.CompenentAuthor = reader["CompenentAuthor"].ToString();
- compenent.Description = reader["Description"].ToString();
- compenent.WorkingPath = reader["WorkingPath"].ToString();
- compenent.SiteName = reader["SiteName"].ToString();
- return compenent;
- }
- }
- }