コード例 #1
0
        /// <summary>
        /// 지정된 쿼리 문을 실행한 결과 셋의 레코드 갯수를 구한다. (SQL 문장이 SQL Count() 함수 문장으로 변환이 가능하다면 속도가 가장 빠르다.)
        /// </summary>
        /// <param name="repository">IAdoRepository 인스턴스</param>
        /// <param name="sqlString">실행할 Command 객체</param>
        /// <param name="parameters">Command 인자 정보</param>
        /// <returns>결과 셋의 레코드 수</returns>
        /// <remarks>
        /// 실제 SQL의 count(*) 함수로 변환할 수 있는 SQL문장이라면, 변환하여 Count 값을 가져오고,
        /// 불가능하다면 IDataReader를 이용하여 결과 셋을 가져와서 갯수만 센다.
        /// </remarks>
        /// <seealso cref="AdoTool.GetCountingSqlString"/>
        public static int CountBySqlString(this IAdoRepository repository, string sqlString, params IAdoParameter[] parameters)
        {
            sqlString.ShouldNotBeWhiteSpace("sqlString");

            // 1. SELECT Count(*) FROM 구문으로 변환이 가능하다면, 변환된 SQL 문장을 수행한다.
            try {
                var countSql = AdoTool.GetCountingSqlString(sqlString);
                var count    = repository.ExecuteScalarBySqlString(countSql, parameters).AsInt(-1);

                if (count > -1)
                {
                    return(count);
                }
            }
            catch (Exception ex) {
                if (log.IsInfoEnabled)
                {
                    log.Info("Count() SQL로 변환 작업이 지원되지 않는 문장입니다. DataReader를 이용하여, 결과 셋의 레코드 수를 알아봅니다.");
                    log.Info("sqlString=[{0}]", sqlString);
                    log.Info(ex);
                }
            }

            // 2. SQL Count() 함수를 이용하지 못할 경우라면, 그냥 DataReader를 이용하여 Count를 계산한다.
            //
            using (var cmd = repository.GetSqlStringCommand(sqlString))
                return(repository.Count(cmd, parameters));
        }
コード例 #2
0
ファイル: AdoToolFixture.cs プロジェクト: 15831944/NFramework
        public void ConvertAllFromDataTableByConverter(int firstResult, int maxResults)
        {
            Func <DataRow, CustomerOrderHistory> @mapFunc =
                row =>
                new CustomerOrderHistory {
                ProductName = row["PRODUCTNAME"].AsText(),
                Total       = row["TOTAL"].AsIntNullable()
            };

            using (var dt = NorthwindAdoRepository.ExecuteDataTableByProcedure("CustOrderHist",
                                                                               firstResult,
                                                                               maxResults,
                                                                               CustomerTestParameter)) {
                Assert.IsFalse(dt.HasErrors);
                Assert.Greater(dt.Rows.Count, 1);

                var orderHistories = AdoTool.Map(dt, @mapFunc);

                if (IsDebugEnabled)
                {
                    log.Debug(orderHistories.CollectionToString());
                }

                Assert.Greater(orderHistories.Count, 0);
            }
        }
コード例 #3
0
ファイル: AdoToolFixture.cs プロジェクト: 15831944/NFramework
        public void SetParameterValuesGeneric()
        {
            var category = new Category {
                CategoryName = "Test",
                Description  = "FluentUtil"
            };

            // delete exist category
            NorthwindAdoRepository.ExecuteNonQueryBySqlString(
                @"DELETE FROM Categories where CategoryName = @CategoryName",
                new AdoParameter("CategoryName", category.CategoryName, DbType.String, 255));

            // insert
            using (var command = NorthwindAdoRepository.GetProcedureCommand("SaveOrUpdateCategory", true)) {
                AdoTool.SetParameterValues(NorthwindAdoRepository.Db,
                                           command,
                                           category,
                                           command.Mapping(NameMappingUtil.CapitalizeMappingFunc('_', ' ')));


                category.CategoryId = NorthwindAdoRepository.ExecuteCommand(command).AsInt(-1);
                Assert.AreNotEqual(-1, category.CategoryId);
            }

            // update
            using (var command = NorthwindAdoRepository.GetProcedureCommand("SaveOrUpdateCategory", true)) {
                AdoTool.SetParameterValues(NorthwindAdoRepository.Db,
                                           command,
                                           category,
                                           command.Mapping(NameMappingUtil.CapitalizeMappingFunc('_', ' ')));

                category.CategoryId = NorthwindAdoRepository.ExecuteCommand(command).AsInt(-1);
                Assert.AreNotEqual(-1, category.CategoryId);
            }
        }
コード例 #4
0
        public void TransactionScope_ShouldNotPromoteToDTC2()
        {
            TestTool.RunTasks(10,
                              () => {
                using (var txScope = AdoTool.CreateTransactionScope()) {
                    var count = TotalCount();

                    var ds = NorthwindAdoRepository.ExecuteDataSetBySqlString(SQL_REGION_SELECT, 1, 10);
                    var dt = NorthwindAdoRepository.ExecuteDataTableBySqlString(SQL_REGION_SELECT, 1, 10);

                    Assert.IsFalse(ds.Tables[0].HasErrors);
                    Assert.IsFalse(dt.HasErrors);

                    int rows = NorthwindAdoRepository.ExecuteNonQueryBySqlString(SQL_REGION_INSERT);
                    Assert.AreEqual(1, rows);
                    rows = NorthwindAdoRepository.ExecuteNonQueryBySqlString(SQL_REGION_INSERT2);
                    Assert.AreEqual(1, rows);

                    NorthwindAdoRepository.ExecuteNonQueryBySqlString(SQL_REGION_DELETE);

                    ds = NorthwindAdoRepository.ExecuteDataSetBySqlString(SQL_REGION_SELECT);
                    dt = NorthwindAdoRepository.ExecuteDataTableBySqlString(SQL_REGION_SELECT);

                    txScope.Complete();
                }
            });
        }
コード例 #5
0
        //! ===============================================================================================

        /// <summary>
        /// 지정된 Command를 수행하고, RETURN_VALUE를 반환합니다.
        /// </summary>
        /// <param name="repository">IAdoRepository 인스턴스</param>
        /// <param name="cmd">실행할 DbCommand</param>
        /// <param name="parameters">인자</param>
        /// <returns>Procedure인 경우 return value를 반환한다. 반환값이 없으면 0을 반환한다.</returns>
        public static object ExecuteCommand(this IAdoRepository repository, DbCommand cmd, params IAdoParameter[] parameters)
        {
            cmd.ShouldNotBeNull("cmd");

            repository.ExecuteNonQuery(cmd, parameters);

            return(AdoTool.GetReturnValue(repository.Db, cmd, 0));
        }
コード例 #6
0
        /// <summary>
        /// 지정된 Action 들을 하나의 Transaction Scope로 묶어서 처리합니다.
        /// </summary>
        /// <param name="actionToExecute">실행할 Action</param>
        /// <example>
        ///	<code>
        ///		// 한 Tx안에서 3개의 Action 를 수행합니다.
        ///		With.TransactionScope(TransactionScopeOption.Required,
        ///                           System.Transactions.IsolationLevel.ReadCommited,
        ///                           FindAll_By_DetachedCriteria,
        ///                           FindAll_By_Criterion,
        ///                           FindAll_By_Example);
        /// </code>
        /// </example>
        public static void TransactionScope(Action actionToExecute)
        {
            actionToExecute.ShouldNotBeNull("actionToExecute");

            using (var txScope = AdoTool.CreateTransactionScope()) {
                actionToExecute();
                txScope.Complete();
            }
        }
コード例 #7
0
        /// <summary>
        /// QueryProvider에서 제공하는 Query로 <see cref="DbCommand"/> 인스턴스를 생성합니다.
        /// </summary>
        /// <param name="repository">IAdoRepository 인스턴스</param>
        /// <param name="queryKey">[Section,] QueryName 형태의 쿼리 키</param>
        /// <param name="parameters">parameter collection</param>
        /// <returns>DbCommand instance</returns>
        /// <exception cref="InvalidOperationException">QueryProvider 속성이 null일때</exception>
        public static DbCommand GetNamedQueryCommand(this IAdoRepository repository, string queryKey, params IAdoParameter[] parameters)
        {
            var query = repository.QueryProvider.GetQuery(queryKey);

            Guard.Assert(query.IsNotWhiteSpace(), "QueryKey=[{0}] 에 해당하는 쿼리 문을 찾지 못했습니다.", queryKey);

            return((AdoTool.IsSqlString(query))
                       ? repository.GetSqlStringCommand(query, parameters)
                       : repository.GetProcedureCommand(query, AdoTool.DEFAULT_DISCOVER_PARAMETER, parameters));
        }
コード例 #8
0
        /// <summary>
        /// 지정된 쿼리 문을 실행한 결과 셋의 레코드 갯수를 구한다.
        /// </summary>
        /// <param name="repository">IAdoRepository 인스턴스</param>
        /// <param name="query">실행할 Command 객체</param>
        /// <param name="parameters">Command 인자 정보</param>
        /// <returns>결과 셋의 레코드 수</returns>
        /// <remarks>
        /// 실제 SQL의 count(*) 함수를 생성하는 것이 아니라, IDataReader를 이용하여 결과 셋을 가져와서 갯수만 센다.
        /// 장점은 DataSet을 이용하여 Paging하는 것보다 빠르고, Count 용 Query문을 따로 만들 필요가 없다.
        /// 단점은 SQL의 count(*) 함수보다는 당연히 느리다.
        /// </remarks>
        public static int Count(this IAdoRepository repository, string query, params IAdoParameter[] parameters)
        {
            query.ShouldNotBeWhiteSpace("query");

            if (AdoTool.IsSqlString(query))
            {
                return(repository.CountBySqlString(query, parameters));
            }

            using (var cmd = repository.GetCommand(query))
                return(repository.Count(cmd, parameters));
        }
コード例 #9
0
ファイル: AdoToolFixture.cs プロジェクト: 15831944/NFramework
        public void Can_GetCommonPropertyNames()
        {
            var propertyNames = AdoTool.GetCommonPropertyNames(typeof(Category), "CategoryID", "CATEGORYNAME").ToList();

            var ignoreCaseComparer = StringComparer.Create(CultureInfo.InvariantCulture, true);

            Assert.IsTrue(propertyNames.Contains("CategoryID", ignoreCaseComparer));
            Assert.IsTrue(propertyNames.Contains("CATEGORYNAME", ignoreCaseComparer));

            Assert.IsFalse(propertyNames.Contains("CategoryID"));
            Assert.IsFalse(propertyNames.Contains("CATEGORYNAME"));
        }
コード例 #10
0
        /// <summary>
        /// <paramref name="query"/>를 Paging 정보에 근거해서 실행하고, 결과를 DataTable로 반환한다.
        /// NOTE: DISTINCT, TOP N 조회는 Paging에서 지원하지 않습니다.
        /// </summary>
        /// <param name="query">조회용 쿼리 문</param>
        /// <param name="pageIndex">Page Index (0부터 시작).  null이면 0으로 간주</param>
        /// <param name="pageSize">Page Size. 한 페이지에 표현할 요소 수 (보통 10개). null이면 10으로 간주</param>
        /// <param name="parameters">조회용 쿼리의 Parameter 정보</param>
        /// <returns>Paging된 DataTable</returns>
        public PagingDataTable ExecutePagingDataTable(string query, int?pageIndex, int?pageSize,
                                                      params IAdoParameter[] parameters)
        {
            query.ShouldNotBeWhiteSpace("query");

            if (AdoTool.IsSqlString(query))
            {
                return(ExecutePagingDataTableBySqlString(query, pageIndex, pageSize, parameters));
            }

            using (var cmd = this.GetCommand(query))
                return(ExecutePagingDataTable(cmd, pageIndex, pageSize, parameters));
        }
コード例 #11
0
        //! ===============================================================================================

        /// <summary>
        /// 지정된 쿼리문을 CommendText로 가지는 <see cref="DbCommand"/> 인스턴스를 생성합니다.
        /// </summary>
        /// <param name="repository">IAdoRepository 인스턴스</param>
        /// <param name="query">simple query string or procedure name or queryKey of QueryProvider ([Section,] QueryName)</param>
        /// <param name="parameters">파라미터 컬렉션</param>
        /// <returns>instance of <see cref="DbCommand"/></returns>
        public static DbCommand GetCommand(this IAdoRepository repository, string query, params IAdoParameter[] parameters)
        {
            query.ShouldNotBeWhiteSpace("query");

            var cmd = repository.GetCommand(query, AdoTool.DEFAULT_DISCOVER_PARAMETER);

            if (parameters.IsNotEmptySequence())
            {
                AdoTool.SetParameterValues(repository.Db, cmd, parameters);
            }

            return(cmd);
        }
コード例 #12
0
        /// <summary>
        /// ADO.NET의 기본 Transaction을 시작한다. (TransactionScope와는 달리 DTC를 이용하지 않는다.)
        /// </summary>
        /// <param name="isolationLevel">Transaction 격리수준 (기본적으로 ReadCommitted)</param>
        /// <returns>Return a new instance of DbTransaction</returns>
        /// <example>
        /// <code>
        /// AdoRepository.BeginTransaction(IsolationLevel.ReadUncommitted);
        /// try
        /// {
        ///     DbCommand insertCommand = AdoRepository.GetSqlStringCommand(InsertString);
        ///     DbCommand deleteCommand = AdoRepository.GetSqlStringCommand(DeleteString);
        ///     DbCommand countCommand = AdoRepository.GetSqlStringCommand(CountString);
        ///
        ///     var count = Convert.ToInt32(AdoRepository.ExecuteScalar(countCommand));
        ///
        ///     AdoRepository.ExecuteNonQuery(insertCommand);
        ///     AdoRepository.ExecuteNonQuery(deleteCommand);
        ///
        ///     AdoRepository.Commit();
        ///
        ///     Assert.AreEqual(4, count);
        /// }
        /// catch (Exception ex)
        /// {
        ///     if (IsErrorEnabled)
        ///         log.ErrorException(ex);
        ///
        ///     AdoRepository.Rollback();
        ///
        ///     throw;
        /// }
        /// </code>
        /// </example>
        public virtual DbTransaction BeginTransaction(System.Data.IsolationLevel isolationLevel)
        {
            var connection = AdoTool.CreateTransactionScopeConnection(Db);

            ActiveTransaction = connection.BeginTransaction(isolationLevel);

            if (IsDebugEnabled)
            {
                log.Debug("Database=[{0}]에 새로운 Transaction을 시작했습니다.  Transaction=[{1}]",
                          connection.Database, ActiveTransaction.GetType().FullName);
            }

            return(ActiveTransaction);
        }
コード例 #13
0
        /// <summary>
        /// Database 작업을 테스트하기 위한 Utility 함수이다.<br/>
        /// 지정된 테스트용 DB 작업이 실제 Database에서 실행은 되지만 Transaction이 Commit이 되지 않으므로, 테스트시에 유용한다.
        /// </summary>
        /// <param name="actionToTest">테스트할 Action</param>
        /// <param name="options">TransactionScopeOption</param>
        /// <example>
        /// // ForTesting 의 anonymous method가 실행되지만 실제 DB에 commit 되지는 않습니다.
        /// <code>
        /// var originalCount = Convert.ToInt32(AdoRepository.ExecuteScalar(CountString));
        /// AdoWith.ForTesting(delegate
        ///		{
        ///			AdoRepository.ExecuteNonQuery(InsertString);
        ///			AdoRepository.ExecuteNonQuery(InsertString2);
        ///			var count = Convert.ToInt32(AdoRepository.ExecuteScalar(CountString));
        ///
        ///			Assert.AreEqual(originalCount + 2, count);
        ///		});
        ///
        /// var rollbackCount = Convert.ToInt32(AdoRepository.ExecuteScalar(CountString));
        /// Assert.AreEqual(originalCount, rollbackCount);
        /// </code>
        /// </example>
        public static void ForTesting(this Action actionToTest, TransactionScopeOption options = TransactionScopeOption.RequiresNew)
        {
            actionToTest.ShouldNotBeNull("actionToTest");

            using (var scope = AdoTool.CreateTransactionScope(options)) {
                actionToTest();

                // NOTE: 테스트를 위해 수행한 것이므로, scope.Complete()를 호출하여 Transaciton을 Commit 하지 않는다. (즉 Rollback 한다)
            }

            if (IsDebugEnabled)
            {
                log.Debug("Execute {0}() for testing, and rollback!!!", actionToTest.Method.Name);
            }
        }
コード例 #14
0
        /// <summary>
        /// 지정된 Entity의 속성 값을 이용하여 Command의 Parameter 값을 설정하고, 실행시킨다.
        /// 일반적으로 Save / Update시에 활용하면 좋다.
        /// </summary>
        /// <typeparam name="T">Persistent object 수형</typeparam>
        /// <param name="repository">IAdoRepository 인스턴스</param>
        /// <param name="cmd">수행할 Command 객체</param>
        /// <param name="entity">처리할 Persistent object</param>
        /// <param name="nameMaps">ParameterName of Procedure = Property Name of Persistent object 매핑 정보</param>
        /// <returns>Command 인자 중에 ParameterDirection이 ReturnValue인 인자의 값</returns>
        public static object ExecuteEntity <T>(this IAdoRepository repository, DbCommand cmd, T entity, INameMap nameMaps)
        {
            cmd.ShouldNotBeNull("cmd");
            entity.ShouldNotBeNull("entity");
            nameMaps.ShouldNotBeNull("nameMap");

            if (IsDebugEnabled)
            {
                log.Debug("Entity를 처리를 수행합니다. CommandText=[{0}], entity=[{1}], nameMaps=[{2}]",
                          cmd.CommandText, entity.ObjectToString(), nameMaps.DictionaryToString());
            }

            AdoTool.SetParameterValues(repository.Db, cmd, entity, nameMaps);
            return(repository.ExecuteCommand(cmd));
        }
コード例 #15
0
        /// <summary>
        /// Execute specified DbCommand, and return <see cref="AdoDataReader"/>
        /// </summary>
        /// <param name="cmd">DbCommand to execute</param>
        /// <param name="parameters">Command parameters</param>
        /// <returns>instance of <see cref="IDataReader"/></returns>
        public IDataReader ExecuteReader(DbCommand cmd, params IAdoParameter[] parameters)
        {
            cmd.ShouldNotBeNull("cmd");

            if (IsDebugEnabled)
            {
                log.Debug("DataReader를 얻습니다... command text=[{0}], parameters=[{1}]",
                          cmd.CommandText, parameters.CollectionToString());
            }

            if (parameters != null)
            {
                AdoTool.SetParameterValues(Db, cmd, parameters);
            }

            return(ExecuteReaderInternal(cmd));
        }
コード例 #16
0
        /// <summary>
        /// Execute specified DbCommand
        /// </summary>
        /// <param name="cmd">Instance of DbCommand to execute</param>
        /// <param name="parameters">Parameters for DbCommand to execute</param>
        /// <returns>affected row count</returns>
        public int ExecuteNonQuery(DbCommand cmd, params IAdoParameter[] parameters)
        {
            cmd.ShouldNotBeNull("cmd");

            if (IsDebugEnabled)
            {
                log.Debug("Command를 실행합니다... CommandText=[{0}], parameters=[{1}]",
                          cmd.CommandText, parameters.CollectionToString());
            }

            if (parameters.IsNotEmptySequence())
            {
                AdoTool.SetParameterValues(Db, cmd, parameters);
            }

            return(ExecuteNonQueryInternal(cmd));
        }
コード例 #17
0
        public void TransactionScope_ShouldNotPromoteToDTC()
        {
            int totalRows = TotalRows();
            int dtcCount  = 0;

            TransactionManager.DistributedTransactionStarted += delegate { Interlocked.Increment(ref dtcCount); };

            using (var ts = AdoTool.CreateTransactionScope()) {
                int rows = NorthwindAdoRepository.ExecuteNonQueryBySqlString(SQL_REGION_INSERT);
                Assert.AreEqual(1, rows);
                rows = NorthwindAdoRepository.ExecuteNonQueryBySqlString(SQL_REGION_INSERT2);
                Assert.AreEqual(1, rows);

                ts.Complete();
            }
            Assert.AreEqual(totalRows + 2, TotalRows());
            Assert.AreEqual(0, dtcCount);
        }
コード例 #18
0
        /// <summary>
        /// Get <see cref="DbCommand"/> which CommandText is sqlString, build up parameters by parameters
        /// </summary>
        /// <param name="repository">IAdoRepository 인스턴스</param>
        /// <param name="sqlString">simple query string</param>
        /// <param name="parameters">collection of parameter</param>
        /// <returns>instance of <see cref="DbCommand"/></returns>
        public static DbCommand GetSqlStringCommand(this IAdoRepository repository, string sqlString, params IAdoParameter[] parameters)
        {
            sqlString.ShouldNotBeWhiteSpace("sqlString");

            if (IsDebugEnabled)
            {
                log.Debug("다음 SQL 문을 수행할 DbCommand를 생성합니다... sqlString=[{0}]", sqlString);
            }

            var cmd = repository.Db.GetSqlStringCommand(sqlString);

            if (parameters.IsNotEmptySequence())
            {
                AdoTool.SetParameterValues(repository.Db, cmd, parameters);
            }

            return(cmd);
        }
コード例 #19
0
        /// <summary>
        /// Command를 실행시켜, 결과 셋을 DataSet에 저장한다.<br/>
        /// 여러 ResultSet을 반환하는 경우 각각의 ResultSet에 대응하는 TableName을 제공해야 합니다.
        /// </summary>
        /// <param name="repository">IAdoRepository 인스턴스</param>
        /// <param name="cmd">실행할 Command</param>
        /// <param name="targetDataSet"></param>
        /// <param name="tableNames"></param>
        /// <example>
        /// <code>
        /// // Region 전체 정보 및 갯수를 가져옵니다.
        /// string query = "select * from Region; select count(*) from Region";
        ///
        /// DataSet ds = new DataSet();
        /// using (DbCommand cmd = Impl.GetSqlStringCommand(query))
        /// {
        ///     Impl.LoadDataSet(cmd, ds, new string[] { "Region", "CountOfRegion" });
        ///     Assert.AreEqual(2, ds.Tables.Count);
        ///     Assert.AreEqual(4, ds.Tables[1].Rows[0][0]);
        /// }
        /// </code>
        /// </example>
        public static void LoadDataSet(this IAdoRepository repository, DbCommand cmd, DataSet targetDataSet, string[] tableNames)
        {
            cmd.ShouldNotBeNull("cmd");
            tableNames.ShouldNotBeEmpty("tableNames");

            if (IsDebugEnabled)
            {
                log.Debug("DbCommand를 실행하여 DataSet에 로등합니다... cmd.CommandText=[{0}], tableNames=[{1}]",
                          cmd.CommandText, tableNames.CollectionToString());
            }

            for (int i = 0; i < tableNames.Length; i++)
            {
                tableNames[i].ShouldNotBeWhiteSpace("index=" + i);
            }

            if (repository.IsActiveTransaction)
            {
                AdoTool.EnlistCommandToActiveTransaction(repository, cmd);
            }

            var newConnectionCreated = false;

            if (cmd.Connection == null)
            {
                cmd.Connection = AdoTool.CreateTransactionScopeConnection(repository.Db, ref newConnectionCreated);
            }

            using (var adapter = repository.GetDataAdapter()) {
                for (var j = 0; j < tableNames.Length; j++)
                {
                    var sourceTable = (j == 0) ? AdoTool.DefaultTableName : (AdoTool.DefaultTableName + j);
                    adapter.TableMappings.Add(sourceTable, tableNames[j]);
                }

                adapter.SelectCommand = cmd;
                adapter.Fill(targetDataSet);

                if (newConnectionCreated)
                {
                    AdoTool.ForceCloseConnection(adapter.SelectCommand);
                }
            }
        }
コード例 #20
0
        /// <summary>
        /// Stored Procedure를 실행하고, Parameter의 Direction이 INPUT이 아닌 Parameter들을 반환한다. (InputOutput, Output, ReturnValue)
        /// </summary>
        /// <param name="repository">IAdoRepository 인스턴스</param>
        /// <param name="spName">실행할 Procedure 이름</param>
        /// <param name="parameters">인자</param>
        /// <returns>INPUT을 제외한 Oupput, InputOutput, ReturnValue에 해당하는 Parameter 값을 반환한다.</returns>
        public static IAdoParameter[] ExecuteProcedure(this IAdoRepository repository, string spName, params IAdoParameter[] parameters)
        {
            if (IsDebugEnabled)
            {
                log.Debug("Procedure를 수행합니다... spName=[{0}], parameters=[{1}]", spName, parameters.CollectionToString());
            }

            using (var cmd = repository.GetProcedureCommand(spName)) {
                repository.ExecuteCommand(cmd, parameters);
                var result = AdoTool.GetOutputParameters(repository.Db, cmd);

                if (IsDebugEnabled)
                {
                    log.Debug("Procedure 수행 후 결과 값입니다. spName=[{0}], output parameters=[{1}]", spName, result.CollectionToString());
                }

                return(result);
            }
        }
コード例 #21
0
ファイル: AdoToolFixture.cs プロジェクト: 15831944/NFramework
        public void ConvertAllFromDataTable(int firstResult, int maxResults)
        {
            using (var dt = NorthwindAdoRepository.ExecuteDataTableByProcedure("CustOrderHist",
                                                                               firstResult,
                                                                               maxResults,
                                                                               CustomerTestParameter)) {
                Assert.IsFalse(dt.HasErrors);
                Assert.Greater(dt.Rows.Count, 1);

                var orderHistories = AdoTool.Map <CustomerOrderHistory>(dt);

                if (IsDebugEnabled)
                {
                    log.Debug("OrderHistories:" + orderHistories.CollectionToString());
                }

                Assert.Greater(orderHistories.Count, 0);
            }
        }
コード例 #22
0
        /// <summary>
        /// 지정된 쿼리문을 CommendText로 가지는 <see cref="DbCommand"/> 인스턴스를 생성합니다.
        /// </summary>
        /// <param name="repository">IAdoRepository 인스턴스</param>
        /// <param name="query">simple query string or procedure name or queryKey of QueryProvider ([Section,] QueryName)</param>
        /// <param name="discoverParams">discover parameters</param>
        /// <returns>instance of <see cref="DbCommand"/></returns>
        public static DbCommand GetCommand(this IAdoRepository repository, string query, bool discoverParams)
        {
            query.ShouldNotBeWhiteSpace("query");

            if (repository.QueryProvider != null &&
                query.Contains(InIQueryProviderBase.SECTION_DELIMITER) &&
                AdoTool.IsSqlString(query) == false)
            {
                string namedQuery;
                if (repository.QueryProvider.TryGetQuery(query, out namedQuery))
                {
                    query = namedQuery;
                }
            }

            return((AdoTool.IsSqlString(query))
                       ? repository.GetSqlStringCommand(query)
                       : repository.GetProcedureCommand(query, discoverParams));
        }
コード例 #23
0
        /// <summary>
        /// 지정된 Action 들을 하나의 Transaction Scope로 묶어서 처리합니다.
        /// </summary>
        /// <param name="scopeOption">TransactionScopeOption 값</param>
        /// <param name="isolationLevel">Transaction 격리수준</param>
        /// <param name="actionsToExecute">TransactionScope 안에서 실행할 Action(s).</param>
        /// <example>
        ///	<code>
        ///		// 한 Tx안에서 3개의 Action 를 수행합니다.
        ///		With.TransactionScope(TransactionScopeOption.Required,
        ///                           System.Transactions.IsolationLevel.ReadCommited,
        ///                           FindAll_By_DetachedCriteria,
        ///                           FindAll_By_Criterion,
        ///                           FindAll_By_Example);
        /// </code>
        /// </example>
        public static void TransactionScope(TransactionScopeOption scopeOption,
                                            IsolationLevel isolationLevel,
                                            params Action[] actionsToExecute)
        {
            if (IsDebugEnabled)
            {
                log.Debug("TransactionScope 안에서, 지정한 Action 들을 실행하기 위해 준비합니다.");
            }

            if (actionsToExecute == null)
            {
                if (IsDebugEnabled)
                {
                    log.Debug("실행할 Action이 제공되지 않아 반환합니다.");
                }

                return;
            }

            int dtcCount = 0;

            TransactionManager.DistributedTransactionStarted += delegate { dtcCount++; };

            using (var txScope = AdoTool.CreateTransactionScope(scopeOption, isolationLevel)) {
                foreach (Action action in actionsToExecute)
                {
                    if (action != null)
                    {
                        action();
                    }
                }

                txScope.Complete();
            }

            if (IsDebugEnabled)
            {
                log.Debug("TransactionScope 내에서 Action 들을 실행에 성공했습니다. DTC count=[{0}]", dtcCount);
            }
        }
コード例 #24
0
ファイル: AdoToolFixture.cs プロジェクト: 15831944/NFramework
        public void ConvertAllFromDataTableByNameMappingFuncs(int firstResult, int maxResults)
        {
            // CustOrderHist2 Procedure를 만들어야 한다.  (Column 명을 대문자, '_'로 바꾼다. 즉 PRODUCT_NAME, TOTAL 로 column명만 바꾼다
            using (var dt = NorthwindAdoRepository.ExecuteDataTableByProcedure("CustOrderHist2",
                                                                               firstResult,
                                                                               maxResults,
                                                                               CustomerTestParameter)) {
                Assert.IsFalse(dt.HasErrors);
                Assert.Greater(dt.Rows.Count, 1);

                var orderHistories =
                    AdoTool.Map <CustomerOrderHistory>(dt,
                                                       dt.Mapping(NameMappingUtil
                                                                  .CapitalizeMappingFunc('_', ' ')));

                if (IsDebugEnabled)
                {
                    log.Debug(orderHistories.CollectionToString());
                }

                Assert.Greater(orderHistories.Count, 0);
            }
        }
コード例 #25
0
        /// <summary>
        /// 지정된 Stored Procedure를 실행할 <see cref="DbCommand"/> 인스턴스를 생성합니다.
        /// </summary>
        /// <param name="repository">IAdoRepository 인스턴스</param>
        /// <param name="spName">프로시저 명</param>
        /// <param name="discoverParams">DbCommand 인자를 DB에서 확인할 것인지 여부</param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static DbCommand GetProcedureCommand(this IAdoRepository repository, string spName, bool discoverParams,
                                                    params IAdoParameter[] parameters)
        {
            spName.ShouldNotBeWhiteSpace("spName");

            if (IsDebugEnabled)
            {
                log.Debug("Procedure 실행을 위한 DbCommand를 생성합니다.. spName=[{0}], discoverParams=[{1}], parameters=[{2}]",
                          spName, discoverParams, parameters.CollectionToString());
            }

            var cmd = repository.Db.GetStoredProcCommand(spName);

            if (discoverParams)
            {
                repository.Db.DiscoverParameters(cmd);
            }

            // command parameter 를 discover하지 않더라도 새로 정의할 parameter가 있다면...
            AdoTool.SetParameterValues(repository.Db, cmd, parameters);

            return(cmd);
        }
コード例 #26
0
        /// <summary>
        /// Database 작업을 테스트하기 위한 Utility 함수이다.<br/>
        /// 지정된 테스트용 DB 작업이 실제 Database에서 실행은 되지만 Transaction이 Commit이 되지 않으므로, 테스트시에 유용한다.
        /// </summary>
        /// <remarks>
        /// Database 작업에 대해, 실제 DB에 적용은 되지 않고, 테스트만을 하기 원할 때 사용합니다.
        /// </remarks>
        /// <param name="actionsToTest">테스트할 Action 시퀀스</param>
        /// <param name="isolationLevel">Transaction isolation level</param>
        /// <example>
        /// <code>
        /// // ForTesting 의 anonymous method가 실행되지만 실제 DB에 commit 되지는 않습니다.
        /// IList{Action} actions = new List{Action}();
        ///
        /// actions.Add(DeleteActionTest);
        /// actions.Add(InsertActionTest);
        /// actions.Add(DeleteActionTest);
        /// actions.Add(InsertActionTest);
        ///
        /// var originalCount = TotalCount();
        ///
        /// actions.ForTesting(AdoTool.AdoIsolationLevel);
        ///
        /// Assert.AreEqual(originalCount, TotalCount());
        /// </code>
        /// </example>
        public static void ForTesting(this IEnumerable <Action> actionsToTest, IsolationLevel isolationLevel)
        {
            actionsToTest.ShouldNotBeNull("actions");

            using (var scope = AdoTool.CreateTransactionScope(TransactionScopeOptionForTesting, isolationLevel)) {
                foreach (var action in actionsToTest.Where(action => action != null))
                {
                    if (IsDebugEnabled)
                    {
                        log.Debug("Execute {0}() for testing...", action.Method.Name);
                    }

                    action();
                }

                //! NOTE: 테스트를 위해 수행한 것이므로, scope.Complete()를 호출하여 Transaciton을 Commit 하지 않는다. (즉 Rollback 한다)
            }

            if (IsDebugEnabled)
            {
                log.Debug("Execute {0} actions for testing and rollback.", actionsToTest.Count());
            }
        }