// ReSharper disable InconsistentNaming
        public void SqlBulkCopyWrapper_WriteToServer_WhenNullBulkCopyObject_ExpectException()
        // ReSharper restore InconsistentNaming
        {
            //------------Setup for test--------------------------
            var       sqlBulkCopyWrapper = new SqlBulkCopyWrapper(null);
            DataTable dataTable          = new DataTable("myTable");

            //------------Execute Test---------------------------
            sqlBulkCopyWrapper.WriteToServer(dataTable);
        }
Exemplo n.º 2
0
        public void SqlBulkCopyWrapper_WriteToServer_WhenNullBulkCopyObject_ExpectException()

        {
            //------------Setup for test--------------------------
            var sqlBulkCopyWrapper = new SqlBulkCopyWrapper(null);
            var dataTable          = new DataTable("myTable");

            //------------Execute Test---------------------------
            sqlBulkCopyWrapper.WriteToServer(dataTable);
        }
Exemplo n.º 3
0
        void DoInsertForSqlServer(DbSource runtimeDatabase, SqlBulkCopyOptions currentOptions, IDSFDataObject dataObject, ErrorResultTO allErrors, IWarewolfIterator batchItr, IWarewolfListIterator parametersIteratorCollection, IWarewolfIterator timeoutItr, ref ErrorResultTO errorResultTo, ref bool addExceptionToErrorList, int update)
        {
            SqlBulkCopy sqlBulkCopy;

            sqlBulkCopy = string.IsNullOrEmpty(BatchSize) && string.IsNullOrEmpty(Timeout) ? new SqlBulkCopy(runtimeDatabase.ConnectionString, currentOptions)
            {
                DestinationTableName = TableName
            } : SetupSqlBulkCopy(batchItr, parametersIteratorCollection, timeoutItr, runtimeDatabase, currentOptions);

            if (sqlBulkCopy != null)
            {
                var dataTableToInsert = BuildDataTableToInsert();

                var types   = GETTypesFromMappingTypes();
                var columns = GetNamesFromMappings();
                if (InputMappings != null && InputMappings.Count > 0)
                {
                    var iteratorCollection = new WarewolfListIterator();
                    var listOfIterators    = GetIteratorsFromInputMappings(dataObject, iteratorCollection, out errorResultTo, update);
                    iteratorCollection.Types = types;
                    iteratorCollection.Names = columns;
                    allErrors.MergeErrors(errorResultTo);
                    FillDataTableWithDataFromDataList(iteratorCollection, dataTableToInsert, listOfIterators);
                    // oh no, we have an issue, bubble it out ;)
                    if (allErrors.HasErrors())
                    {
                        addExceptionToErrorList = false;
                        throw new Exception(string.Format(ErrorResource.ProblemsWithIterators, "SQLBulkInsert"));
                    }

                    // emit options to debug as per acceptance test ;)
                    if (dataObject.IsDebugMode())
                    {
                        AddBatchSizeAndTimeOutToDebug(dataObject.Environment, update);
                        AddOptionsDebugItems();
                    }

                    sqlBulkCopy = AddInputMappings(sqlBulkCopy);
                    var wrapper = new SqlBulkCopyWrapper(sqlBulkCopy);
                    SqlBulkInserter.Insert(wrapper, dataTableToInsert);
                    dataObject.Environment.Assign(Result, "Success", update);
                    if (dataObject.IsDebugMode())
                    {
                        AddDebugOutputItem(new DebugItemWarewolfAtomResult("Success", Result, ""));
                    }
                }
                dataTableToInsert?.Dispose();
            }
        }
 public void AddSqlBulkCopyColumnMappingDoesNotThrow()
 {
     // Arrange
     using (var connection = new SqlConnection())
     {
         using (var sqlBulkCopy = new SqlBulkCopy(connection))
         {
             using (var sut = new SqlBulkCopyWrapper(sqlBulkCopy))
             {
                 // Act (should not throw)
                 sut.AddSqlBulkCopyColumnMapping("Column", "Column");
             }
         }
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// When overridden runs the activity's execution logic
        /// </summary>
        /// <param name="context">The context to be used.</param>
        protected override void OnExecute(NativeActivityContext context)
        {
            _debugInputs  = new List <DebugItem>();
            _debugOutputs = new List <DebugItem>();
            var dataObject = context.GetExtension <IDSFDataObject>();
            var compiler   = DataListFactory.CreateDataListCompiler();
            var toUpsert   = Dev2DataListBuilderFactory.CreateStringDataListUpsertBuilder();

            toUpsert.IsDebug    = dataObject.IsDebugMode();
            toUpsert.ResourceID = dataObject.ResourceID;
            var       errorResultTo           = new ErrorResultTO();
            var       allErrors               = new ErrorResultTO();
            var       executionId             = DataListExecutionID.Get(context);
            DataTable dataTableToInsert       = null;
            bool      addExceptionToErrorList = true;

            InitializeDebug(dataObject);
            try
            {
                IDev2DataListEvaluateIterator batchItr;
                IDev2DataListEvaluateIterator timeoutItr;
                var         parametersIteratorCollection = BuildParametersIteratorCollection(compiler, executionId, out batchItr, out timeoutItr);
                SqlBulkCopy sqlBulkCopy     = null;
                var         currentOptions  = BuildSqlBulkCopyOptions();
                var         runtimeDatabase = ResourceCatalog.Instance.GetResource <DbSource>(dataObject.WorkspaceID, Database.ResourceID);
                if (String.IsNullOrEmpty(BatchSize) && String.IsNullOrEmpty(Timeout))
                {
                    sqlBulkCopy = new SqlBulkCopy(runtimeDatabase.ConnectionString, currentOptions)
                    {
                        DestinationTableName = TableName
                    };
                }
                else
                {
                    while (parametersIteratorCollection.HasMoreData())
                    {
                        sqlBulkCopy = SetupSqlBulkCopy(batchItr, parametersIteratorCollection, timeoutItr, runtimeDatabase, currentOptions);
                    }
                }
                if (sqlBulkCopy != null)
                {
                    // BuiltUsingSingleRecset was very poorly put together it assumes a 1-1 mapping between target and destination columns ?! ;(
                    // And it forced a need for duplicate logic?!
                    dataTableToInsert = BuildDataTableToInsert();

                    if (InputMappings != null && InputMappings.Count > 0)
                    {
                        var iteratorCollection = Dev2ValueObjectFactory.CreateIteratorCollection();
                        var listOfIterators    = GetIteratorsFromInputMappings(compiler, executionId, dataObject, iteratorCollection, out errorResultTo);
                        allErrors.MergeErrors(errorResultTo);

                        // oh no, we have an issue, bubble it out ;)
                        if (allErrors.HasErrors())
                        {
                            addExceptionToErrorList = false;
                            throw new Exception("Problems with Iterators for SQLBulkInsert");
                        }

                        // emit options to debug as per acceptance test ;)
                        if (dataObject.IsDebugMode())
                        {
                            AddBatchSizeAndTimeOutToDebug(compiler, executionId);
                            AddOptionsDebugItems();
                        }

                        FillDataTableWithDataFromDataList(iteratorCollection, dataTableToInsert, listOfIterators);

                        foreach (var dataColumnMapping in InputMappings)
                        {
                            if (!String.IsNullOrEmpty(dataColumnMapping.InputColumn))
                            {
                                sqlBulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(dataColumnMapping.OutputColumn.ColumnName, dataColumnMapping.OutputColumn.ColumnName));
                            }
                        }
                    }

                    // Pass in wrapper now ;)
                    var wrapper = new SqlBulkCopyWrapper(sqlBulkCopy);
                    SqlBulkInserter.Insert(wrapper, dataTableToInsert);

                    toUpsert.Add(Result, "Success");
                    compiler.Upsert(executionId, toUpsert, out errorsTo);
                    allErrors.MergeErrors(errorResultTo);
                    if (toUpsert.IsDebug)
                    {
                        foreach (var debugOutputTo in toUpsert.DebugOutputs)
                        {
                            AddDebugOutputItem(new DebugItemVariableParams(debugOutputTo));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (addExceptionToErrorList)
                {
                    allErrors.AddError(e.Message);
                }
                // ReSharper disable InvokeAsExtensionMethod
                Dev2Logger.Log.Error(this, e);
                // ReSharper restore InvokeAsExtensionMethod
            }
            finally
            {
                // Handle Errors
                if (allErrors.HasErrors())
                {
                    if (toUpsert.IsDebug)
                    {
                        foreach (var debugOutputTo in toUpsert.DebugOutputs)
                        {
                            AddDebugOutputItem(new DebugItemVariableParams(debugOutputTo));
                        }
                    }

                    DisplayAndWriteError("DsfSqlBulkInsertActivity", allErrors);
                    compiler.UpsertSystemTag(dataObject.DataListID, enSystemTag.Dev2Error, allErrors.MakeDataListReady(), out errorsTo);
                    compiler.Upsert(executionId, Result, (string)null, out errorResultTo);
                }
                if (toUpsert.IsDebug)
                {
                    DispatchDebugState(context, StateType.Before);
                    DispatchDebugState(context, StateType.After);
                }
                if (dataTableToInsert != null)
                {
                    dataTableToInsert.Dispose();
                }
            }
        }