コード例 #1
0
ファイル: StormConnection.cs プロジェクト: fedtes/storm
        /// <summary>
        /// Open a new StormTransaction to execute queries in a transactional scope. If AutoCommit is set to True then when the Dispose method is call the transaction is automattically commited else is automatically rollbacked (unless rollback and commit are called explicitally). StormTransaction implements IDisposable and should be disposed.
        /// </summary>
        /// <param name="AutoCommit"></param>
        /// <returns></returns>
        public StormTransaction BeginTransaction(bool AutoCommit = false)
        {
            EnsureTransaction();
            var t = connection.BeginTransaction();

            currentTransaction = new StormTransaction(this, t, AutoCommit);
            navigator.GetLogger().Info("Connection", $"{{\"Action\":\"Begin Transaction\",\"AutoCommit\":\"{AutoCommit}\"}}", this.connectionId);
            return(currentTransaction);
        }
コード例 #2
0
ファイル: BaseCommand.cs プロジェクト: fedtes/storm
        public Object Execute()
        {
            this.CommandLog(LogLevel.Info, "Command", $"{{\"Action\":\"Begin\"}}");
            IDbCommand cmd;

            sw.Start();

            try
            {
                compiler = compiler == null ? new SqlServerCompiler() : compiler;
                this.CommandLog(LogLevel.Info, "Command", $"{{\"Action\":\"Selected Compiler\", \"Time\":\"{sw.ElapsedMilliseconds}\", \"Compiler\":\"{compiler.GetType().Name}\" }}");

                this.ParseSQL();
                this.CommandLog(LogLevel.Info, "Command", $"{{\"Action\":\"Parsed SQL\", \"Time\":\"{sw.ElapsedMilliseconds}\" }}");

                SqlResult result = compiler.Compile(query);

                cmd             = connection.connection.CreateCommand();
                cmd.CommandText = result.Sql;


                foreach (var binding in result.NamedBindings)
                {
                    var p = cmd.CreateParameter();
                    p.ParameterName = binding.Key;
                    p.Value         = binding.Value;
                    cmd.Parameters.Add(p);
                }

                this.CommandLog(LogLevel.Info, "Command", $"{{\"Action\":\"Compiled SQL\", \"Time\":\"{sw.ElapsedMilliseconds}\" }}");
                this.CommandLog(LogLevel.Debug, "Command", $"{{\"SQL\":\"{result.Sql}\", \"Params\":\"{result.NamedBindings.Select(nb => nb.Key + "=" + nb.Value)}\" }}");
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error parsing query", ex);
            }

            bool isLocalTransaction = transaction == null;

            transaction = isLocalTransaction ? connection.BeginTransaction(true) : transaction;

            try
            {
                this.CommandLog(LogLevel.Info, "Command", $"{{\"Action\":\"Execute\", \"Transaction\":\"{(isLocalTransaction ? "Local" : "External")}\"}}");
                cmd.Transaction = transaction.transaction;
                using (var reader = cmd.ExecuteReader())
                {
                    this.CommandLog(LogLevel.Info, "Command", $"{{\"Action\":\"Executed\", \"Time\":\"{sw.ElapsedMilliseconds}\" }}");
                    return(Read(reader));
                }
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw new ApplicationException("Error executing query", ex);
            }
            finally
            {
                if (isLocalTransaction)
                {
                    transaction.Dispose();
                }
            }
        }