Exemplo n.º 1
0
        /// <inheritdoc />
        public Task BulkInsertAsync <T>(
            IEnumerable <T> entities,
            IBulkInsertOptions options,
            CancellationToken cancellationToken = default)
            where T : class
        {
            var entityType = _ctx.Model.GetEntityType(typeof(T));

            return(BulkInsertAsync(entityType, entities, entityType.GetSchema(), entityType.GetTableName(), options, cancellationToken));
        }
Exemplo n.º 2
0
        private async Task BulkInsertAsync <T>(
            IEntityType entityType,
            IEnumerable <T> entities,
            string?schema,
            string tableName,
            IBulkInsertOptions options,
            CancellationToken cancellationToken = default)
            where T : class
        {
            if (entities == null)
            {
                throw new ArgumentNullException(nameof(entities));
            }
            if (tableName == null)
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (!(options is SqlServerBulkInsertOptions sqlServerOptions))
            {
                sqlServerOptions = new SqlServerBulkInsertOptions(options);
            }

            var factory    = _ctx.GetService <IEntityDataReaderFactory>();
            var properties = options.MembersToInsert.GetPropertiesForInsert(entityType);
            var sqlCon     = (SqlConnection)_ctx.Database.GetDbConnection();
            var sqlTx      = (SqlTransaction?)_ctx.Database.CurrentTransaction?.GetDbTransaction();

            using var reader   = factory.Create(_ctx, entities, properties);
            using var bulkCopy = CreateSqlBulkCopy(sqlCon, sqlTx, schema, tableName, sqlServerOptions);

            var columns = SetColumnMappings(bulkCopy, reader);

            await _ctx.Database.OpenConnectionAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                LogInserting(sqlServerOptions.SqlBulkCopyOptions, bulkCopy, columns);
                var stopwatch = Stopwatch.StartNew();

                await bulkCopy.WriteToServerAsync(reader, cancellationToken).ConfigureAwait(false);

                LogInserted(sqlServerOptions.SqlBulkCopyOptions, stopwatch.Elapsed, bulkCopy, columns);
            }
            finally
            {
                await _ctx.Database.CloseConnectionAsync().ConfigureAwait(false);
            }
        }
Exemplo n.º 3
0
        private static async Task BulkInsertAsync <T>(IBulkOperationExecutor bulkInsertExecutor,
                                                      IEnumerable <T> entities,
                                                      IBulkInsertOptions options,
                                                      CancellationToken cancellationToken)
            where T : class
        {
            if (bulkInsertExecutor == null)
            {
                throw new ArgumentNullException(nameof(bulkInsertExecutor));
            }

            await bulkInsertExecutor.BulkInsertAsync(entities, options, cancellationToken).ConfigureAwait(false);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Bulk insert datas
 /// </summary>
 /// <param name="server">Database server</param>
 /// <param name="dataTable">Data table</param>
 /// <param name="bulkInsertOptions">Bulk insert options</param>
 public async Task BulkInsertAsync(DatabaseServer server, DataTable dataTable, IBulkInsertOptions bulkInsertOptions = null)
 {
     if (server == null)
     {
         return;
     }
     var provider = DataManager.GetDatabaseProvider(server.ServerType);
     await provider.BulkInsertAsync(server, dataTable, bulkInsertOptions).ConfigureAwait(false);
 }
Exemplo n.º 5
0
        private async Task BulkInsertAsync <T>(
            IEntityType entityType,
            IEnumerable <T> entities,
            string?schema,
            string tableName,
            IBulkInsertOptions options,
            CancellationToken cancellationToken = default)
            where T : class
        {
            if (entities == null)
            {
                throw new ArgumentNullException(nameof(entities));
            }
            if (tableName == null)
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (!(options is SqliteBulkInsertOptions sqliteOptions))
            {
                sqliteOptions = new SqliteBulkInsertOptions(options);
            }

            var factory    = _ctx.GetService <IEntityDataReaderFactory>();
            var properties = sqliteOptions.MembersToInsert.GetPropertiesForInsert(entityType);
            var sqlCon     = (SqliteConnection)_ctx.Database.GetDbConnection();

            using var reader = factory.Create(_ctx, entities, properties);

            await _ctx.Database.OpenConnectionAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                using var command = sqlCon.CreateCommand();

                var tableIdentifier = _sqlGenerationHelper.DelimitIdentifier(tableName, schema);
#pragma warning disable CA2100
                command.CommandText = GetInsertStatement(reader, tableIdentifier);
#pragma warning restore CA2100
                var parameterInfos = CreateParameters(reader, command);

                try
                {
                    command.Prepare();
                }
                catch (SqliteException ex)
                {
                    throw new InvalidOperationException($"Cannot access destination table '{tableIdentifier}'.", ex);
                }

                LogInserting(command.CommandText);
                var stopwatch = Stopwatch.StartNew();

                while (reader.Read())
                {
                    for (var i = 0; i < reader.FieldCount; i++)
                    {
                        var    paramInfo = parameterInfos[i];
                        object?value     = reader.GetValue(i);

                        if (sqliteOptions.AutoIncrementBehavior == SqliteAutoIncrementBehavior.SetZeroToNull && paramInfo.IsAutoIncrementColumn && 0.Equals(value))
                        {
                            value = null;
                        }

                        paramInfo.Parameter.Value = value ?? DBNull.Value;
                    }

                    await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                }

                stopwatch.Stop();
                LogInserted(command.CommandText, stopwatch.Elapsed);
            }
            finally
            {
                await _ctx.Database.CloseConnectionAsync().ConfigureAwait(false);
            }
        }