Exemple #1
0
        /// <summary>
        /// A parancsok előrehaladottságának jelzése
        /// </summary>
        /// <param name="command">A parancsot leíró objektum</param>
        /// <param name="progressText">Az aktuális előrehaladottsági fázis</param>
        /// <param name="commandProgress">Az előrehaladás mértéke</param>
        public void ReportProgress(Command command, string progressText, int commandProgress = 0)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var index = _commands.IndexOf(command);

            if (index == -1)
            {
                throw new ArgumentException(
                          "Internal error: DeploymentTransaction.ReportProgress called with an unknown command.");
            }

            DeploymentUserInterface.Current.SetProgress(
                ProgressHelper.GetProgress(index, _commands.Count, commandProgress),
                progressText);
        }
        /// <summary>
        /// Supports <see cref="Microsoft.Data.SqlClient.SqlBulkCopy"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sqlBulkCopy"></param>
        /// <param name="entities"></param>
        /// <param name="setColumnMapping"></param>
        /// <param name="progress"></param>
        public void SetSqlBulkCopyConfig <T>(Microsoft.Data.SqlClient.SqlBulkCopy sqlBulkCopy, IList <T> entities, bool setColumnMapping, Action <decimal> progress)
        {
            sqlBulkCopy.DestinationTableName = InsertToTempTable ? FullTempTableName : FullTableName;
            sqlBulkCopy.BatchSize            = BulkConfig.BatchSize;
            sqlBulkCopy.NotifyAfter          = BulkConfig.NotifyAfter ?? BulkConfig.BatchSize;
            sqlBulkCopy.SqlRowsCopied       += (sender, e) =>
            {
                progress?.Invoke(ProgressHelper.GetProgress(entities.Count, e.RowsCopied)); // round to 4 decimal places
            };
            sqlBulkCopy.BulkCopyTimeout = BulkConfig.BulkCopyTimeout ?? sqlBulkCopy.BulkCopyTimeout;
            sqlBulkCopy.EnableStreaming = BulkConfig.EnableStreaming;

            if (setColumnMapping)
            {
                foreach (var element in PropertyColumnNamesDict)
                {
                    sqlBulkCopy.ColumnMappings.Add(element.Key, element.Value);
                }
            }
        }
        /// <summary>
        /// A parancsok előrehaladottságának jelzése
        /// </summary>
        /// <param name="command">A parancsot leíró objektum</param>
        /// <param name="progressText">Az aktuális előrehaladottsági fázis</param>
        /// <param name="commandProgress">Az előrehaladás mértéke</param>
        public void ReportProgress(Command command, string progressText, int commandProgress = 0)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var index = Commands.IndexOf(command);

            if (index == -1)
            {
                throw new ArgumentException(
                          "Internal error: ContainerCommand.ReportProgress called with an unknown command.");
            }

            if (Container == null)
            {
                throw new InvalidOperationException(
                          "Internal error: ContainerCommand.ReportProgress called on a container that is not a member of a deployment transaction.");
            }

            Container.ReportProgress(this, progressText,
                                     ProgressHelper.GetProgress(index, Commands.Count, commandProgress));
        }
        protected async Task InsertAsync <T>(DbContext context, Type type, IList <T> entities, TableInfo tableInfo, Action <decimal> progress, CancellationToken cancellationToken, bool isAsync)
        {
            NpgsqlConnection connection = tableInfo.NpgsqlConnection;
            bool             closeConnectionInternally = false;

            if (connection == null)
            {
                (connection, closeConnectionInternally) =
                    isAsync ? await OpenAndGetNpgsqlConnectionAsync(context, tableInfo.BulkConfig, cancellationToken).ConfigureAwait(false)
                            : OpenAndGetNpgsqlConnection(context, tableInfo.BulkConfig);
            }

            try
            {
                string sqlCopy = SqlQueryBuilderPostgreSql.InsertIntoTable(tableInfo, tableInfo.InsertToTempTable ? OperationType.InsertOrUpdate : OperationType.Insert);

                using var writer = isAsync ? await connection.BeginBinaryImportAsync(sqlCopy, cancellationToken).ConfigureAwait(false)
                                           : connection.BeginBinaryImport(sqlCopy);

                var uniqueColumnName = tableInfo.PrimaryKeysPropertyColumnNameDict.Values.ToList().FirstOrDefault();

                var doKeepIdentity       = tableInfo.BulkConfig.SqlBulkCopyOptions == SqlBulkCopyOptions.KeepIdentity;
                var propertiesColumnDict = ((tableInfo.InsertToTempTable || doKeepIdentity) && tableInfo.IdentityColumnName == uniqueColumnName)
                    ? tableInfo.PropertyColumnNamesDict
                    : tableInfo.PropertyColumnNamesDict.Where(a => a.Value != tableInfo.IdentityColumnName);

                var propertiesNames     = propertiesColumnDict.Select(a => a.Key).ToList();
                var entitiesCopiedCount = 0;
                foreach (var entity in entities)
                {
                    if (isAsync)
                    {
                        await writer.StartRowAsync(cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        writer.StartRow();
                    }

                    foreach (var propertyName in propertiesNames)
                    {
                        if (tableInfo.DefaultValueProperties.Contains(propertyName) && !tableInfo.PrimaryKeysPropertyColumnNameDict.ContainsKey(propertyName))
                        {
                            continue;
                        }

                        var propertyValue      = tableInfo.FastPropertyDict.ContainsKey(propertyName) ? tableInfo.FastPropertyDict[propertyName].Get(entity) : null;
                        var propertyColumnName = tableInfo.PropertyColumnNamesDict.ContainsKey(propertyName) ? tableInfo.PropertyColumnNamesDict[propertyName] : string.Empty;

                        var columnType = tableInfo.ColumnNamesTypesDict[propertyColumnName];

                        // string is 'text' which works fine
                        if (columnType.StartsWith("character")) // when MaxLength is defined: 'character(1)' or 'character varying'
                        {
                            columnType = "character";           // 'character' is like 'string'
                        }
                        else if (columnType.StartsWith("varchar"))
                        {
                            columnType = "varchar";
                        }
                        else if (columnType.StartsWith("numeric"))
                        {
                            columnType = "numeric";
                        }

                        var convertibleDict = tableInfo.ConvertibleColumnConverterDict;
                        if (convertibleDict.ContainsKey(propertyColumnName) && convertibleDict[propertyColumnName].ModelClrType.IsEnum)
                        {
                            if (propertyValue != null)
                            {
                                var clrType = tableInfo.ConvertibleColumnConverterDict[propertyColumnName].ProviderClrType;
                                if (clrType == typeof(byte)) // columnType == "smallint"
                                {
                                    propertyValue = (byte)propertyValue;
                                }
                                if (clrType == typeof(short))
                                {
                                    propertyValue = (short)propertyValue;
                                }
                                if (clrType == typeof(Int32))
                                {
                                    propertyValue = (int)propertyValue;
                                }
                                if (clrType == typeof(Int64))
                                {
                                    propertyValue = (long)propertyValue;
                                }
                                if (clrType == typeof(string))
                                {
                                    propertyValue = propertyValue.ToString();
                                }
                            }
                        }

                        if (isAsync)
                        {
                            await writer.WriteAsync(propertyValue, columnType, cancellationToken).ConfigureAwait(false);
                        }
                        else
                        {
                            writer.Write(propertyValue, columnType);
                        }
                    }
                    entitiesCopiedCount++;
                    if (progress != null && entitiesCopiedCount % tableInfo.BulkConfig.NotifyAfter == 0)
                    {
                        progress?.Invoke(ProgressHelper.GetProgress(entities.Count, entitiesCopiedCount));
                    }
                }
                if (isAsync)
                {
                    await writer.CompleteAsync(cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    writer.Complete();
                }
            }
            finally
            {
                if (closeConnectionInternally)
                {
                    //connection.Close();
                    if (isAsync)
                    {
                        await connection.CloseAsync();

                        //await context.Database.CloseConnectionAsync().ConfigureAwait(false);
                    }
                    else
                    {
                        connection.Close();
                        //context.Database.CloseConnection();
                    }
                }
            }
        }