示例#1
0
        /// <summary>
        /// Executes a transaction in a SQL database.
        /// </summary>
        /// <param name="operations">The list of operations to be executed.</param>
        public virtual void ExecuteTransaction(List <TransOperation> operations)
        {
            LoggerHelper.Info("Start");
            try
            {
                StringBuilder   query       = new StringBuilder();
                IList <DBParam> queryParams = new List <DBParam>();

                StringBuilder variables         = new StringBuilder();
                StringBuilder statementsQueries = new StringBuilder();

                IDictionary <string, string> defaultVals = new Dictionary <string, string>();

                IQueryBuilder queryBuilder = GetQueryBuilder();

                foreach (TransOperation operation in operations)
                {
                    Entity entity = operation.Entity;
                    if (operation.OperationType == TransOperation.OperType.Save)
                    {
                        if (string.IsNullOrEmpty(operation.Entity.GetEntityId())) // New entity Insert
                        {
                            if (operation.BindIds)
                            {
                                queryBuilder.BuildInsertStatement(entity, statementsQueries, queryParams, defaultVals);
                            }
                            else
                            {
                                queryBuilder.BuildInsertStatement(entity, statementsQueries, queryParams);
                            }

                            Field  id      = entity.GetFieldId();
                            string varName = GetVariableName(id);

                            if (!defaultVals.ContainsKey(id.Name))
                            {
                                defaultVals[id.Name] = varName;
                                variables.Append("DECLARE ").Append(varName).Append(" AS INT\n");
                            }

                            statementsQueries.Append("\nSET ").Append(varName).Append(" = scope_identity()\n");
                        }
                        else //update
                        {
                            queryBuilder.BuildUpdateStatement(entity, statementsQueries, queryParams);
                        }
                    }
                    else if (operation.OperationType == TransOperation.OperType.Update)
                    {
                        queryBuilder.BuildUpdateEntityStatement(entity, statementsQueries, queryParams);
                    }
                    else if (operation.OperationType == TransOperation.OperType.Delete)
                    {
                        queryBuilder.BuildDeleteStatement(entity, statementsQueries, queryParams);
                    }
                    else if (operation.OperationType == TransOperation.OperType.DeleteEntities)
                    {
                        queryBuilder.BuildDeleteEntitiesStatement(entity, statementsQueries, queryParams);
                    }

                    statementsQueries.Append("\n");
                }

                query.Append(variables).Append("\n");
                query.Append(statementsQueries);
                LoggerHelper.Debug(query.ToString());

                StatementWrapper stmtWrapper = new StatementWrapper(query, queryParams);

                IList <StatementWrapper> statements = new List <StatementWrapper>();
                statements.Add(stmtWrapper);

                GetQueryRunner().ExecuteTransaction(GetConnection(), statements);
            }
            catch (Exception e)
            {
                LoggerHelper.Error(e.Message);
                if (e.Message.Contains("FK") || e.Message.Contains("REFERENCE"))
                {
                    throw new Exception("Entity cannot be deleted, because is being used.");
                }
                else if (e.Message.Contains("UNIQUE KEY"))
                {
                    throw new Exception("Entity already exists.");
                }
                else
                {
                    throw new Exception("Unable to execute transactions.", e);
                }
            }
            finally
            {
                LoggerHelper.Info("End");
            }
        }