Пример #1
0
        private static string PrepareUpdateData(DELock lockData, bool forceOverride)
        {
            ORMappingItemCollection mappingInfo = GetMappingInfo();

            UpdateSqlClauseBuilder uBuilder = ORMapping.GetUpdateSqlClauseBuilder(lockData, mappingInfo);
            WhereSqlClauseBuilder  wBuilder = ORMapping.GetWhereSqlClauseBuilderByPrimaryKey(lockData, mappingInfo);

            if (forceOverride == false)
            {
                wBuilder.AppendItem("LockTime", "DATEADD(SECOND, -EffectiveTime, GETDATE())", "<", true);
            }

            StringBuilder sql = new StringBuilder();

            sql.AppendFormat("UPDATE {0} SET {1} WHERE {2}",
                             mappingInfo.TableName,
                             uBuilder.ToSqlString(TSqlBuilder.Instance),
                             wBuilder.ToSqlString(TSqlBuilder.Instance));

            sql.Append(TSqlBuilder.Instance.DBStatementSeperator);

            sql.Append("SELECT @@ROWCOUNT");

            sql.Append(TSqlBuilder.Instance.DBStatementSeperator);

            sql.AppendFormat("SELECT * FROM {0} WHERE LockID = {1}",
                             GetMappingInfo().TableName,
                             TSqlBuilder.Instance.CheckUnicodeQuotationMark(lockData.LockID));

            return(sql.ToString());
        }
Пример #2
0
        /// <summary>
        /// 延长锁的时间
        /// </summary>
        /// <param name="lockData"></param>
        /// <returns></returns>
        public DECheckLockResult ExtendLockTime(DELock lockData)
        {
            DECheckLockResult result = null;

            lockData.LockTime = DateTime.MinValue;

            using (TransactionScope scope = TransactionScopeFactory.Create())
            {
                bool updated = false;

                DELock lockInDB = lockInDB = Update(lockData, true, out updated);

                if (updated == false)
                {
                    result = BuildNotAvailableResult(lockData);
                }
                else
                {
                    result = BuildAvailableResult(lockInDB, true);
                }

                scope.Complete();
            }

            return(result);
        }
Пример #3
0
        private static DELock Insert(DELock lockData)
        {
            StringBuilder sql = new StringBuilder();

            sql.Append(ORMapping.GetInsertSql(lockData, TSqlBuilder.Instance));
            sql.Append(TSqlBuilder.Instance.DBStatementSeperator);
            sql.AppendFormat("SELECT * FROM {0} WHERE LockID = {1}",
                             GetMappingInfo().TableName,
                             TSqlBuilder.Instance.CheckUnicodeQuotationMark(lockData.LockID));

            DELock result = null;

            try
            {
                DataTable table = DbHelper.RunSqlReturnDS(sql.ToString(), GetConnectionName()).Tables[0];

                result = new DELock();

                ORMapping.DataRowToObject(table.Rows[0], GetMappingInfo(), result);
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                if (ex.Number != 2627)
                {
                    throw;
                }
            }

            return(result);
        }
Пример #4
0
        private static DECheckLockResult BuildNotAvailableResult(DELock lockData)
        {
            DECheckLockResult result = new DECheckLockResult();

            result.Lock       = lockData;
            result.LockStatus = DECheckLockStatus.Locked;

            return(result);
        }
Пример #5
0
        /// <summary>
        /// 删除锁
        /// </summary>
        public void DeleteLock()
        {
            DELock lockData = this._Lock;

            if (lockData == null)
            {
                lockData = DELock.CreateDefaultDataOperationLock();
            }

            DELockAdapter.Instance.DeleteLock(lockData);
            this._Lock = null;
        }
Пример #6
0
        /// <summary>
        /// 延长锁的时间
        /// </summary>
        public void ExtendLock()
        {
            DELock lockData = this._Lock;

            if (lockData == null)
            {
                lockData = DELock.CreateDefaultDataOperationLock();
            }

            DECheckLockResult checkResult = DELockAdapter.Instance.ExtendLockTime(lockData);

            this._Lock = checkResult.Lock;
        }
Пример #7
0
        private static DELock Update(DELock lockData, bool forceOverride, out bool updated)
        {
            DataSet ds = DbHelper.RunSqlReturnDS(PrepareUpdateData(lockData, forceOverride), GetConnectionName());

            (ds.Tables[1].Rows.Count > 0).FalseThrow("不能找到ID为{0}的锁信息", lockData.LockID);

            updated = (int)ds.Tables[0].Rows[0][0] > 0;

            DELock result = new DELock();

            ORMapping.DataRowToObject(ds.Tables[1].Rows[0], GetMappingInfo(), result);

            return(result);
        }
Пример #8
0
        public static DELock CreateDefaultDataOperationLock()
        {
            DELock result = new DELock();

            result.LockID   = DefaultDataOperationLockID;
            result.LockType = DELockType.DataOperation;

            if (DeluxePrincipal.IsAuthenticated)
            {
                result.LockPerson = DeluxeIdentity.CurrentUser;
            }

            return(result);
        }
Пример #9
0
        private static DELock GetLockInfo(string lockID)
        {
            string sql = string.Format("SELECT * FROM {0} WHERE LockID = {1}",
                                       GetMappingInfo().TableName, TSqlBuilder.Instance.CheckUnicodeQuotationMark(lockID));

            DataTable table = DbHelper.RunSqlReturnDS(sql, GetConnectionName()).Tables[0];

            (table.Rows.Count > 0).FalseThrow("不能根据\"{0}\"找到对应的锁信息", lockID);

            DELock result = new DELock();

            ORMapping.DataRowToObject(table.Rows[0], GetMappingInfo(), result);

            return(result);
        }
Пример #10
0
        public void AddLock(TimeSpan effectiveTime, string description)
        {
            DELock lockData = DELock.CreateDefaultDataOperationLock();

            lockData.EffectiveTime = effectiveTime;
            lockData.Description   = description;

            DECheckLockResult checkResult = DELockAdapter.Instance.AddLock(lockData);

            if (checkResult.Available == false)
            {
                throw new DECheckLockException(DECheckLockException.CheckLockResultToMessage(checkResult));
            }

            this._Lock = checkResult.Lock;
        }
Пример #11
0
        private static DECheckLockResult BuildAvailableResult(DELock lockData, bool overrideLock)
        {
            DECheckLockResult result = new DECheckLockResult();

            result.Lock = lockData;

            if (overrideLock)
            {
                result.LockStatus = DECheckLockStatus.LockExpired;
            }
            else
            {
                result.LockStatus = DECheckLockStatus.NotLocked;
            }

            return(result);
        }
Пример #12
0
        /// <summary>
        /// 加锁。返回新加的锁,或者原来锁的状态。成功与否,检查SCCheckLockResult的Available属性
        /// </summary>
        /// <param name="lockData"></param>
        /// <returns></returns>
        public DECheckLockResult AddLock(DELock lockData)
        {
            DECheckLockResult result = null;

            lockData.LockTime = DateTime.MinValue;

            using (TransactionScope scope = TransactionScopeFactory.Create())
            {
                //插入是否成功,判断锁是否已经存在
                DELock lockInDB = Insert(lockData);

                if (lockInDB == null)
                {
                    //更新是否成功,如果不成功,表示锁被占用。
                    bool updated = false;

                    lockInDB = Update(lockData, false, out updated);

                    if (updated == false)
                    {
                        result = BuildNotAvailableResult(lockInDB);
                    }
                    else
                    {
                        result = BuildAvailableResult(lockInDB, true);
                    }
                }
                else
                {
                    result = BuildAvailableResult(lockInDB, false);
                }

                scope.Complete();
            }

            return(result);
        }
Пример #13
0
        public void DeleteLock(DELock lockData)
        {
            lockData.NullCheck("lockData");

            DeleteLock(lockData.LockID);
        }