Exemplo n.º 1
0
 public void SetUp()
 {
     ctx =
         new XmlApplicationContext("assembly://Spring.Data.Integration.Tests/Spring.Data/templateTests.xml");
     dbProvider         = ctx["DbProvider"] as IDbProvider;
     transactionManager = ctx["adoTransactionManager"] as IPlatformTransactionManager;
     adoOperations      = ctx["adoTemplate"] as IAdoOperations;
 }
Exemplo n.º 2
0
 public void SetUp()
 {
     _mockery             = new MockRepository();
     _adoOperations       = _mockery.StrictMock <IAdoOperations>();
     _ordinalCache        = _mockery.StrictMock <IDataRecordOrdinalCache>();
     _rowCallback         = _mockery.StrictMock <IRowCallback>();
     _rowCallbackDelegate = _mockery.StrictMock <RowCallbackDelegate>();
 }
 public void CreateAdoTemplate()
 {
     IApplicationContext ctx =
         new XmlApplicationContext("assembly://Spring.Data.Integration.Tests/Spring.Data/adoTemplateTests.xml");
     Assert.IsNotNull(ctx);
     dbProvider = ctx["DbProvider"] as IDbProvider;
     Assert.IsNotNull(dbProvider);
     adoOperations = new AdoTemplate(dbProvider);
 }
Exemplo n.º 4
0
        public void CreateAdoTemplate()
        {
            IApplicationContext ctx =
                new XmlApplicationContext("assembly://Spring.Data.Integration.Tests/Spring.Data/adoTemplateTests.xml");

            Assert.IsNotNull(ctx);
            dbProvider = ctx["DbProvider"] as IDbProvider;
            Assert.IsNotNull(dbProvider);
            adoOperations = new AdoTemplate(dbProvider);
        }
        /// <summary>
        /// Query database with given SQL command and mapping each row to a
        /// object via a <see cref="RowMapperDelegate{T}"/> with help of optional
        /// ordinal cache and/or the rows expected value for better performance.
        /// </summary>
        /// <typeparam name="T">The type of object that each row maps to.</typeparam>
        /// <param name="operations">
        /// An <see cref="Spring.Data.Generic.IAdoOperations"/> object to
        /// perform database operation.
        /// </param>
        /// <param name="cmdType">
        /// The type of command.
        /// </param>
        /// <param name="cmdText">
        /// The text of command.
        /// </param>
        /// <param name="rowMapperDelegate">
        /// The row mapper delegate that maps each row to object of type
        /// <typeparamref name="T"/>.
        /// </param>
        /// <param name="ordinalCache">
        /// The <see cref="IDataRecordOrdinalCache"/> that caches the mapping
        /// from field name to field index.
        /// </param>
        /// <param name="rowsExpected">
        /// Number of rows this query is expected to return. This doesn't need
        /// to be accurate but estimating at the higer end for best performance.
        /// </param>
        /// <returns>
        /// A list of object of type <typeparamref name="T"/> that were mapped
        /// for the rows.
        /// </returns>
        public static IList <T> QueryWithRowMapperDelegate <T>(
            this IAdoOperations operations,
            CommandType cmdType,
            string cmdText,
            RowMapperDelegate <T> rowMapperDelegate,
            IDataRecordOrdinalCache ordinalCache,
            int rowsExpected)
        {
            var extractor = MakeResultSetExtractor(rowMapperDelegate, ordinalCache, rowsExpected);

            return(operations.QueryWithResultSetExtractor(cmdType, cmdText, extractor));
        }
        /// <summary>
        /// Query database with given SQL command and have each row processed
        /// by a given <see cref="IRowCallback"/> with help of optional
        /// ordinal cache and/or the rows expected value for better performance.
        /// </summary>
        ///
        /// <param name="operations">
        /// An <see cref="Spring.Data.Generic.IAdoOperations"/> object to
        /// perform database operation.
        /// </param>
        /// <param name="cmdType">
        /// The type of command.
        /// </param>
        /// <param name="cmdText">
        /// The text of command.
        /// </param>
        /// <param name="rowCallback">
        /// The row callback that processes each row.
        /// </param>
        /// <param name="ordinalCache">
        /// The <see cref="IDataRecordOrdinalCache"/> that caches the mapping
        /// from field name to field index.
        /// </param>
        /// <param name="rowsExpected">
        /// Number of rows this query is expected to return. This doesn't need
        /// to be accurate but estimating at the higer end for best performance.
        /// </param>
        public static void QueryWithRowCallback(
            this IAdoOperations operations,
            CommandType cmdType,
            string cmdText,
            IRowCallback rowCallback,
            IDataRecordOrdinalCache ordinalCache,
            int rowsExpected)
        {
            var extractor = MakeResultSetExtractor(rowCallback, ordinalCache, rowsExpected);

            operations.QueryWithResultSetExtractor(cmdType, cmdText, extractor);
        }
        public void SetUp()
        {
            ctx =
                new XmlApplicationContext("assembly://Spring.Data.Integration.Tests/Spring.Data/templateTests.xml");
            dbProvider = ctx["DbProvider"] as IDbProvider;
            transactionManager = ctx["transactionManager"] as IPlatformTransactionManager;
            adoOperations = ctx["adoTemplate"] as IAdoOperations;

            ITestObjectManager testObjectManager = ctx["testObjectManager"] as ITestObjectManager;
            testObjectManager.DeleteAllTestObjects();
            testObjectManager.SaveTwoTestObjects(new TestObject("Jack", 10), new TestObject("Jill", 20));
        }
Exemplo n.º 8
0
        public void SetUp()
        {
            ctx =
                new XmlApplicationContext("assembly://Spring.Data.Integration.Tests/Spring.Data/templateTests.xml");
            dbProvider         = ctx["DbProvider"] as IDbProvider;
            transactionManager = ctx["transactionManager"] as IPlatformTransactionManager;
            adoOperations      = ctx["adoTemplate"] as IAdoOperations;

            ITestObjectManager testObjectManager = ctx["testObjectManager"] as ITestObjectManager;

            testObjectManager.DeleteAllTestObjects();
            testObjectManager.SaveTwoTestObjects(new TestObject("Jack", 10), new TestObject("Jill", 20));
        }
        /// <summary>
        /// Query database with given SQL command and have each row processed
        /// by a given <see cref="RowCallbackDelegate"/> with help of optional
        /// ordinal cache and/or the rows expected value for better performance.
        /// </summary>
        ///
        /// <param name="operations">
        /// An <see cref="Spring.Data.Generic.IAdoOperations"/> object to
        /// perform database operation.
        /// </param>
        /// <param name="cmdType">
        /// The type of command.
        /// </param>
        /// <param name="cmdText">
        /// The text of command.
        /// </param>
        /// <param name="rowCallbackDelegate">
        /// The row callback delegate that processes each row.
        /// </param>
        /// <param name="parameters">
        /// The parameters with all neccessary values populated.
        /// </param>
        /// <param name="ordinalCache">
        /// The <see cref="IDataRecordOrdinalCache"/> that caches the mapping
        /// from field name to field index.
        /// </param>
        /// <param name="rowsExpected">
        /// Number of rows this query is expected to return. This doesn't need
        /// to be accurate but estimating at the higer end for best performance.
        /// </param>
        public static void QueryWithRowCallbackDelegate(
            this IAdoOperations operations,
            CommandType cmdType,
            string cmdText,
            RowCallbackDelegate rowCallbackDelegate,
            IDbParameters parameters,
            IDataRecordOrdinalCache ordinalCache,
            int rowsExpected)
        {
            var extractor = MakeResultSetExtractor(rowCallbackDelegate, ordinalCache, rowsExpected);

            operations.QueryWithResultSetExtractor(cmdType, cmdText, extractor, parameters);
        }
        /// <summary>
        /// Query database with given SQL command and mapping each row to a
        /// object via a <see cref="IRowMapper{T}"/> with help of optional
        /// ordinal cache and/or the rows expected value for better performance.
        /// </summary>
        /// <typeparam name="T">The type of object that each row maps to.</typeparam>
        /// <param name="operations">
        /// An <see cref="Spring.Data.Generic.IAdoOperations"/> object to
        /// perform database operation.
        /// </param>
        /// <param name="cmdType">
        /// The type of command.
        /// </param>
        /// <param name="cmdText">
        /// The text of command.
        /// </param>
        /// <param name="rowMapper">
        /// The row mapper that maps each row to object of type
        /// <typeparamref name="T"/>.
        /// </param>
        /// <param name="parameterValue">
        /// The value of the parameter.
        /// </param>
        /// <param name="ordinalCache">
        /// The <see cref="IDataRecordOrdinalCache"/> that caches the mapping
        /// from field name to field index.
        /// </param>
        /// <param name="parameterName">
        /// The name of the parameter.
        /// </param>
        /// <param name="rowsExpected">
        /// Number of rows this query is expected to return. This doesn't need
        /// to be accurate but estimating at the higer end for best performance.
        /// </param>
        /// <param name="dbType">
        /// Type type of the parameter.
        /// </param>
        /// <param name="size">
        /// The size of parameter.
        /// </param>
        /// <returns>
        /// A list of object of type <typeparamref name="T"/> that were mapped
        /// for the rows.
        /// </returns>
        public static IList <T> QueryWithRowMapper <T>(
            this IAdoOperations operations,
            CommandType cmdType,
            string cmdText,
            IRowMapper <T> rowMapper,
            string parameterName, Enum dbType, int size, object parameterValue,
            IDataRecordOrdinalCache ordinalCache,
            int rowsExpected)
        {
            var extractor = MakeResultSetExtractor(rowMapper, ordinalCache, rowsExpected);

            return(operations.QueryWithResultSetExtractor(
                       cmdType, cmdText, extractor, parameterName, dbType, size, parameterValue));
        }
        /// <summary>
        /// Query database with given SQL command and have each row processed
        /// by a given <see cref="IRowCallback"/> with help of optional
        /// ordinal cache and/or the rows expected value for better performance.
        /// </summary>
        ///
        /// <param name="operations">
        /// An <see cref="Spring.Data.Generic.IAdoOperations"/> object to
        /// perform database operation.
        /// </param>
        /// <param name="cmdType">
        /// The type of command.
        /// </param>
        /// <param name="cmdText">
        /// The text of command.
        /// </param>
        /// <param name="rowCallback">
        /// The row callback that processes each row.
        /// </param>
        /// <param name="parameterValue">
        /// The value of the parameter.
        /// </param>
        /// <param name="ordinalCache">
        /// The <see cref="IDataRecordOrdinalCache"/> that caches the mapping
        /// from field name to field index.
        /// </param>
        /// <param name="parameterName">
        /// The name of the parameter.
        /// </param>
        /// <param name="rowsExpected">
        /// Number of rows this query is expected to return. This doesn't need
        /// to be accurate but estimating at the higer end for best performance.
        /// </param>
        /// <param name="dbType">
        /// Type type of the parameter.
        /// </param>
        /// <param name="size">
        /// The size of parameter.
        /// </param>
        public static void QueryWithRowCallback(
            this IAdoOperations operations,
            CommandType cmdType,
            string cmdText,
            IRowCallback rowCallback,
            string parameterName, Enum dbType, int size, object parameterValue,
            IDataRecordOrdinalCache ordinalCache,
            int rowsExpected)
        {
            var extractor = MakeResultSetExtractor(rowCallback, ordinalCache, rowsExpected);

            operations.QueryWithResultSetExtractor(
                cmdType, cmdText, extractor, parameterName, dbType, size, parameterValue);
        }
        public static IList <T> QueryWithRowMapperDelegate <T>(
            this IAdoOperations operations,
            CommandType cmdType,
            string cmdText,
            RowMapperDelegate <T> rowMapperDelegate,
            Action <IDbCommand> commandSetterDelegate,
            IDataRecordOrdinalCache ordinalCache,
            int rowsExpected)
        {
            var commandSetter = new DelegateCommandSetter(commandSetterDelegate);

            return(QueryWithRowMapperDelegate(operations, cmdType, cmdText, rowMapperDelegate,
                                              commandSetter, ordinalCache, rowsExpected));
        }
Exemplo n.º 13
0
        [Test] public void ExecuteNonQueryCallsBatchExecutor()
        {
            var collection = new[] { "x", "y", "c", "r" };

            _adoOperations = _mockery.StrictMultiMock <IAdoOperations>(typeof(IBatchExecutorFactory));
            var factory       = (IBatchExecutorFactory)_adoOperations;
            var batchExecutor = _mockery.StrictMock <IBatchExecutor>();

            Expect.Call(factory.GetExecutor()).Return(batchExecutor);
            Expect.Call(batchExecutor.ExecuteNonQuery(_adoOperations, CommandType.Text, _sql, collection, _converter)).Return(10);

            _mockery.ReplayAll();
            int result = AdoOperationsExtension.ExecuteNonQuery(_adoOperations, CommandType.Text, _sql, collection, _converter);

            Assert.That(result, Is.EqualTo(10));
            _mockery.VerifyAll();
        }
Exemplo n.º 14
0
            public int ExecuteNonQuery <T>(
                IAdoOperations operation,
                CommandType cmdType,
                string cmdText,
                ICollection <T> data,
                Converter <T, IDbParameters> dataToParamters)
            {
                int totalRows = data.Count;
                int batchSize = _odpTemplate.BatchSize;

                if (totalRows < batchSize)
                {
                    batchSize = totalRows;
                }

                int count = 0, bindCount = 0, result = 0;

                object[][] valueBuffer = null;

                foreach (T row in data)
                {
                    IDbParameters parameters = dataToParamters(row);
                    if (parameters != null)
                    {
                        if (valueBuffer == null)
                        {
                            valueBuffer = InitBatchParameters(parameters, batchSize);
                        }

                        string error = ValidateAndCopyParams(parameters, valueBuffer, bindCount++);
                        if (error != null)
                        {
                            throw new InvalidDataAccessApiUsageException(error + " for row: " + row);
                        }
                    }
                    ++count;
                    if (bindCount == batchSize || (count == totalRows) && bindCount > 0)
                    {
                        _bindCount = bindCount;
                        result    += operation.ExecuteNonQuery(cmdType, cmdText, this);
                        bindCount  = 0;
                    }
                }
                return(result);
            }
        /// <summary>
        /// Executes batch of non queries with common command and different
        /// parameters.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the data object.
        /// </typeparam>
        /// <param name="operation">
        /// An <see cref="Spring.Data.Generic.IAdoOperations"/> object to
        /// perform database operation.
        /// </param>
        /// <param name="cmdType">
        /// The type of command.
        /// </param>
        /// <param name="cmdText">
        /// The text of command.
        /// </param>
        /// <param name="data">
        /// A collection of data object to be updated in batch.
        /// </param>
        /// <param name="dataToParameters">
        /// Delegate that converts data object to parameters.
        /// </param>
        /// <returns>
        /// The total updated count if 0 or positive. When -1 is returned,
        /// it indicates that the update count cannot be obtained due the
        /// the limitation of the batch implementation.
        /// </returns>
        public static int ExecuteNonQuery <T>(
            this IAdoOperations operation,
            CommandType cmdType,
            string cmdText,
            ICollection <T> data,
            Converter <T, IDbParameters> dataToParameters)
        {
            // Argument checking
            if (data == null || data.Count == 0)
            {
                return(0);
            }
            if (cmdText == null)
            {
                throw new ArgumentNullException("cmdText");
            }
            if (dataToParameters == null)
            {
                throw new ArgumentNullException("dataToParameters");
            }

            if (data.Count > 1)
            {
                // Let's try batch
                IBatchExecutorFactory factory = operation as IBatchExecutorFactory;
                if (factory != null)
                {
                    return(factory.GetExecutor().ExecuteNonQuery(
                               operation, cmdType, cmdText, data, dataToParameters));
                }
            }

            ParameterSetter setter = new ParameterSetter();
            int             result = 0;

            foreach (T row in data)
            {
                setter.Parameters = dataToParameters(row);
                result           += operation.ExecuteNonQuery(cmdType, cmdText, setter);
            }
            return(result);
        }
        /// <summary>
        /// Execute the given script
        /// </summary>
        private static void ExecuteSqlScriptInternal(IAdoOperations adoTemplate, EncodedResource resource, bool continueOnError, params Regex[] blockDelimiter)
        {
            AssertUtils.ArgumentNotNull(adoTemplate, "adoTemplate");
            AssertUtils.ArgumentNotNull(resource, "resource");

            if (!CollectionUtils.HasElements(blockDelimiter))
            {
                blockDelimiter = BLOCKDELIM_ALL_EXP;
            }

            List <string> statements = new List <string>();

            try
            {
                GetScriptBlocks(resource, statements, blockDelimiter);
            }
            catch (Exception ex)
            {
                throw new DataAccessResourceFailureException("Failed to open SQL script from " + resource, ex);
            }

            foreach (string statement in statements)
            {
                try
                {
                    adoTemplate.ExecuteNonQuery(CommandType.Text, statement);
                }
                catch (DataAccessException dae)
                {
                    if (!continueOnError)
                    {
                        throw;
                    }
                    Log.Warn(string.Format("SQL statement failed:{0}", statement), dae);
                }
            }
        }
 /// <summary>
 /// Execute the given script
 /// </summary>
 public static void ExecuteSqlScript(IAdoOperations adoTemplate, string script, bool continueOnError, params Regex[] blockDelimiter)
 {
     ExecuteSqlScriptInternal(adoTemplate, new EncodedResource(new StringResource(script)), continueOnError, blockDelimiter);
 }
Exemplo n.º 18
0
 /// <summary>
 /// Execute the given script
 /// </summary>
 public static void ExecuteSqlScript(IAdoOperations adoTemplate, EncodedResource resource, bool continueOnError, params Regex[] blockDelimiter)
 {
     ExecuteSqlScriptInternal(adoTemplate, resource, continueOnError, blockDelimiter);
 }
Exemplo n.º 19
0
 public void SetUp()
 {
     mocks = new MockRepository();
     adoTemplate = (IAdoOperations) mocks.CreateMock(typeof (IAdoOperations));
 }
 public void SetUp()
 {
     mocks       = new MockRepository();
     adoTemplate = (IAdoOperations)mocks.CreateMock(typeof(IAdoOperations));
 }
 /// <summary>
 /// Execute the given script
 /// </summary>
 public static void ExecuteSqlScript(IAdoOperations adoTemplate, IResource resource, params Regex[] blockDelimiter)
 {
     ExecuteSqlScriptInternal(adoTemplate, new EncodedResource(resource), false, blockDelimiter);
 }
Exemplo n.º 22
0
        /// <summary>
        /// Execute the given script
        /// </summary>
        private static void ExecuteSqlScriptInternal(IAdoOperations adoTemplate, EncodedResource resource, bool continueOnError, params Regex[] blockDelimiter)
        {
            AssertUtils.ArgumentNotNull(adoTemplate, "adoTemplate");
            AssertUtils.ArgumentNotNull(resource, "resource");

            if (!CollectionUtils.HasElements(blockDelimiter))
            {
                blockDelimiter = BLOCKDELIM_ALL_EXP;
            }

            List<string> statements = new List<string>();
            try
            {
                GetScriptBlocks(resource, statements, blockDelimiter);
            }
            catch (Exception ex)
            {
                throw new DataAccessResourceFailureException("Failed to open SQL script from " + resource, ex);
            }

            foreach (string statement in statements)
            {
                try
                {
                    adoTemplate.ExecuteNonQuery(CommandType.Text, statement);
                }
                catch (DataAccessException dae)
                {
                    if (!continueOnError)
                    {
                        throw;
                    }
                    Log.Warn(string.Format("SQL statement failed:{0}", statement), dae);
                }
            }
        }
Exemplo n.º 23
0
 public void SetUp()
 {
     mocks       = new MockRepository();
     adoTemplate = mocks.StrictMock <IAdoOperations>();
 }
Exemplo n.º 24
0
 public void SetUp()
 {
     adoTemplate = A.Fake <IAdoOperations>();
 }
Exemplo n.º 25
0
 /// <summary>
 /// Execute the given script
 /// </summary>
 public static void ExecuteSqlScript(IAdoOperations adoTemplate, string script, params Regex[] blockDelimiter)
 {
     ExecuteSqlScriptInternal(adoTemplate, new EncodedResource(new StringResource(script)), false, blockDelimiter);
 }
 /// <summary>
 /// Execute the given script
 /// </summary>
 public static void ExecuteSqlScript(IAdoOperations adoTemplate, EncodedResource resource, bool continueOnError, params Regex[] blockDelimiter)
 {
     ExecuteSqlScriptInternal(adoTemplate, resource, continueOnError, blockDelimiter);
 }
Exemplo n.º 27
0
 [SetUp] public void SetUp()
 {
     _mockery       = new MockRepository();
     _adoOperations = _mockery.StrictMock <IAdoOperations>();
     _converter     = _mockery.StrictMock <Converter <string, IDbParameters> >();
 }
Exemplo n.º 28
0
 public void SetUp()
 {
     ctx =
         new XmlApplicationContext("assembly://Spring.Data.Integration.Tests/Spring.Data/templateTests.xml");
     dbProvider = ctx["DbProvider"] as IDbProvider;
     transactionManager = ctx["adoTransactionManager"] as IPlatformTransactionManager;
     adoOperations = ctx["adoTemplate"] as IAdoOperations;
 }
 /// <summary>
 /// Execute the given script
 /// </summary>
 public static void ExecuteSqlScript(IAdoOperations adoTemplate, IResourceLoader resourceLoader, string scriptResourcePath, bool continueOnError, params Regex[] blockDelimiter)
 {
     ExecuteSqlScriptInternal(adoTemplate, new EncodedResource(resourceLoader.GetResource(scriptResourcePath)), continueOnError, blockDelimiter);
 }
Exemplo n.º 30
0
 /// <summary>
 /// Execute the given script
 /// </summary>
 public static void ExecuteSqlScript(IAdoOperations adoTemplate, IResource resource, params Regex[] blockDelimiter)
 {
     ExecuteSqlScriptInternal(adoTemplate, new EncodedResource(resource), false, blockDelimiter);
 }
Exemplo n.º 31
0
 public void SetUp()
 {
     mocks = new MockRepository();
     adoTemplate = mocks.StrictMock<IAdoOperations>();
 }
Exemplo n.º 32
0
 /// <summary>
 /// Execute the given script
 /// </summary>
 public static void ExecuteSqlScript(IAdoOperations adoTemplate, IResourceLoader resourceLoader, string scriptResourcePath, bool continueOnError, params Regex[] blockDelimiter)
 {
     ExecuteSqlScriptInternal(adoTemplate, new EncodedResource(resourceLoader.GetResource(scriptResourcePath)), continueOnError, blockDelimiter);
 }