コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="batchSize"></param>
        /// <param name="fields"></param>
        /// <param name="hints"></param>
        /// <param name="transaction"></param>
        /// <param name="statementBuilder"></param>
        /// <returns></returns>
        public static InsertAllExecutionContext <TEntity> Create <TEntity>(IDbConnection connection,
                                                                           string tableName,
                                                                           int batchSize,
                                                                           IEnumerable <Field> fields,
                                                                           string hints = null,
                                                                           IDbTransaction transaction         = null,
                                                                           IStatementBuilder statementBuilder = null)
            where TEntity : class
        {
            var key = GetKey <TEntity>(tableName, fields, batchSize, hints);

            // Get from cache
            var context = InsertAllExecutionContextCache.Get <TEntity>(key);

            if (context != null)
            {
                return(context);
            }

            // Create
            var dbFields    = DbFieldCache.Get(connection, tableName, transaction);
            var commandText = (string)null;

            // Create a different kind of requests
            if (batchSize > 1)
            {
                var request = new InsertAllRequest(tableName,
                                                   connection,
                                                   transaction,
                                                   fields,
                                                   batchSize,
                                                   hints,
                                                   statementBuilder);
                commandText = CommandTextCache.GetInsertAllText(request);
            }
            else
            {
                var request = new InsertRequest(tableName,
                                                connection,
                                                transaction,
                                                fields,
                                                hints,
                                                statementBuilder);
                commandText = CommandTextCache.GetInsertText(request);
            }

            // Call
            context = CreateInternal <TEntity>(connection,
                                               tableName,
                                               dbFields,
                                               batchSize,
                                               fields,
                                               commandText);

            // Add to cache
            InsertAllExecutionContextCache.Add <TEntity>(key, context);

            // Return
            return(context);
        }
コード例 #2
0
ファイル: CommandTextCache.cs プロジェクト: orthoxerox/RepoDB
 /// <summary>
 ///
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 internal static string GetInsertAllText(InsertAllRequest request)
 {
     if (cache.TryGetValue(request, out var commandText) == false)
     {
         var fields = GetActualFields(request.Connection,
                                      request.Name,
                                      request.Fields,
                                      request.Transaction);
         commandText = GetInsertAllTextInternal(request, fields);
         cache.TryAdd(request, commandText);
     }
     return(commandText);
 }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        private static string GetInsertAllTextInternal(InsertAllRequest request,
                                                       IEnumerable <Field> fields)
        {
            var statementBuilder = EnsureStatementBuilder(request.Connection, request.StatementBuilder);
            var primaryField     = GetPrimaryField(request);
            var identityField    = GetIdentityField(request);

            return(statementBuilder.CreateInsertAll(new QueryBuilder(),
                                                    request.Name,
                                                    fields,
                                                    request.BatchSize,
                                                    primaryField,
                                                    identityField,
                                                    request.Hints));
        }
コード例 #4
0
ファイル: CommandTextCache.cs プロジェクト: orthoxerox/RepoDB
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        internal static async Task <string> GetInsertAllTextAsync(InsertAllRequest request,
                                                                  CancellationToken cancellationToken = default)
        {
            if (cache.TryGetValue(request, out var commandText) == false)
            {
                var fields = await GetActualFieldsAsync(request.Connection,
                                                        request.Name,
                                                        request.Fields,
                                                        request.Transaction,
                                                        cancellationToken);

                commandText = GetInsertAllTextInternal(request, fields);
                cache.TryAdd(request, commandText);
            }
            return(commandText);
        }
コード例 #5
0
        /// <summary>
        /// Gets a command text from the cache for the insert-all operation.
        /// </summary>
        /// <param name="request">The request object.</param>
        /// <returns>The cached command text.</returns>
        internal static string GetInsertAllText(InsertAllRequest request)
        {
            var commandText = (string)null;

            if (m_cache.TryGetValue(request, out commandText) == false)
            {
                var statementBuilder = EnsureStatementBuilder(request.Connection, request.StatementBuilder);
                var fields           = GetActualFields(request.Connection, request.Name, request.Fields, request.Transaction);
                var primaryField     = GetPrimaryField(request);
                var identityField    = GetIdentityField(request);
                commandText = statementBuilder.CreateInsertAll(new QueryBuilder(),
                                                               request.Name,
                                                               fields,
                                                               request.BatchSize,
                                                               primaryField,
                                                               identityField);
                m_cache.TryAdd(request, commandText);
            }
            return(commandText);
        }
コード例 #6
0
ファイル: InsertAll.cs プロジェクト: crazyants/RepoDb
        /// <summary>
        /// Inserts multiple data in the database in an asynchronous way.
        /// </summary>
        /// <typeparam name="TEntity">The type of the object (whether a data entity or a dynamic).</typeparam>
        /// <param name="connection">The connection object to be used.</param>
        /// <param name="tableName">The name of the target table to be used.</param>
        /// <param name="entities">The list of data entity or dynamic objects to be inserted.</param>
        /// <param name="batchSize">The batch size of the insertion.</param>
        /// <param name="fields">The mapping list of <see cref="Field"/> objects to be used.</param>
        /// <param name="commandTimeout">The command timeout in seconds to be used.</param>
        /// <param name="transaction">The transaction to be used.</param>
        /// <param name="trace">The trace object to be used.</param>
        /// <param name="statementBuilder">The statement builder object to be used.</param>
        /// <param name="skipIdentityCheck">True to skip the identity check.</param>
        /// <returns>The number of inserted rows.</returns>
        internal static async Task <int> InsertAllAsyncInternalBase <TEntity>(this IDbConnection connection,
                                                                              string tableName,
                                                                              IEnumerable <TEntity> entities,
                                                                              int batchSize = Constant.DefaultBatchOperationSize,
                                                                              IEnumerable <Field> fields = null,
                                                                              int?commandTimeout         = null,
                                                                              IDbTransaction transaction = null,
                                                                              ITrace trace = null,
                                                                              IStatementBuilder statementBuilder = null,
                                                                              bool skipIdentityCheck             = false)
            where TEntity : class
        {
            // Guard the parameters
            var count = GuardInsertAll(entities);

            // Validate the batch size
            batchSize = Math.Min(batchSize, count);

            // Get the function
            var callback = new Func <int, InsertAllExecutionContext <TEntity> >((int batchSizeValue) =>
            {
                // Variables needed
                var identity        = (Field)null;
                var dbFields        = DbFieldCache.Get(connection, tableName);
                var inputFields     = (IEnumerable <DbField>)null;
                var outputFields    = (IEnumerable <DbField>)null;
                var identityDbField = dbFields?.FirstOrDefault(f => f.IsIdentity);

                // Set the identity value
                if (skipIdentityCheck == false)
                {
                    identity = IdentityCache.Get <TEntity>()?.AsField();
                    if (identity == null && identityDbField != null)
                    {
                        identity = FieldCache.Get <TEntity>().FirstOrDefault(field =>
                                                                             field.UnquotedName.ToLower() == identityDbField.UnquotedName.ToLower());
                    }
                }

                // Filter the actual properties for input fields
                inputFields = dbFields?
                              .Where(dbField => dbField.IsIdentity == false)
                              .Where(dbField =>
                                     fields.FirstOrDefault(field => field.UnquotedName.ToLower() == dbField.UnquotedName.ToLower()) != null)
                              .AsList();

                // Set the output fields
                if (batchSizeValue > 1)
                {
                    outputFields = identityDbField?.AsEnumerable();
                }

                // Variables for the context
                var multipleEntitiesFunc = (Action <DbCommand, IList <TEntity> >)null;
                var identitySettersFunc  = (List <Action <TEntity, DbCommand> >)null;
                var singleEntityFunc     = (Action <DbCommand, TEntity>)null;
                var identitySetterFunc   = (Action <TEntity, object>)null;

                // Get if we have not skipped it
                if (skipIdentityCheck == false && identity != null)
                {
                    if (batchSizeValue <= 1)
                    {
                        identitySetterFunc = FunctionCache.GetDataEntityPropertyValueSetterFunction <TEntity>(identity);
                    }
                    else
                    {
                        identitySettersFunc = new List <Action <TEntity, DbCommand> >();
                        for (var index = 0; index < batchSizeValue; index++)
                        {
                            identitySettersFunc.Add(FunctionCache.GetDataEntityPropertySetterFromDbCommandParameterFunction <TEntity>(identity, identity.UnquotedName, index));
                        }
                    }
                }

                // Identity which objects to set
                if (batchSizeValue <= 1)
                {
                    singleEntityFunc = FunctionCache.GetDataEntityDbCommandParameterSetterFunction <TEntity>(
                        string.Concat(typeof(TEntity).FullName, ".", tableName, ".InsertAll"),
                        inputFields?.AsList(),
                        null);
                }
                else
                {
                    multipleEntitiesFunc = FunctionCache.GetDataEntitiesDbCommandParameterSetterFunction <TEntity>(
                        string.Concat(typeof(TEntity).FullName, ".", tableName, ".InsertAll"),
                        inputFields?.AsList(),
                        outputFields,
                        batchSizeValue);
                }

                // Identify the requests
                var insertAllRequest = (InsertAllRequest)null;
                var insertRequest    = (InsertRequest)null;

                // Create a different kind of requests
                if (typeof(TEntity) == typeof(object))
                {
                    if (batchSizeValue > 1)
                    {
                        insertAllRequest = new InsertAllRequest(tableName,
                                                                connection,
                                                                fields,
                                                                batchSizeValue,
                                                                statementBuilder);
                    }
                    else
                    {
                        insertRequest = new InsertRequest(tableName,
                                                          connection,
                                                          fields,
                                                          statementBuilder);
                    }
                }
                else
                {
                    if (batchSizeValue > 1)
                    {
                        insertAllRequest = new InsertAllRequest(typeof(TEntity),
                                                                connection,
                                                                fields,
                                                                batchSizeValue,
                                                                statementBuilder);
                    }
                    else
                    {
                        insertRequest = new InsertRequest(typeof(TEntity),
                                                          connection,
                                                          fields,
                                                          statementBuilder);
                    }
                }

                // Return the value
                return(new InsertAllExecutionContext <TEntity>
                {
                    CommandText = batchSizeValue > 1 ? CommandTextCache.GetInsertAllText(insertAllRequest) : CommandTextCache.GetInsertText(insertRequest),
                    InputFields = inputFields,
                    OutputFields = outputFields,
                    BatchSize = batchSizeValue,
                    SingleDataEntityParametersSetterFunc = singleEntityFunc,
                    MultipleDataEntitiesParametersSetterFunc = multipleEntitiesFunc,
                    IdentityPropertySetterFunc = identitySetterFunc,
                    IdentityPropertySettersFunc = identitySettersFunc
                });
            });

            // Get the context
            var context = (InsertAllExecutionContext <TEntity>)null;

            // Identify the number of entities (performance), get an execution context from cache
            context = batchSize == 1 ? InsertAllExecutionContextCache <TEntity> .Get(tableName, fields, 1, callback) :
                      InsertAllExecutionContextCache <TEntity> .Get(tableName, fields, batchSize, callback);

            // Before Execution
            if (trace != null)
            {
                var cancellableTraceLog = new CancellableTraceLog(context.CommandText, entities, null);
                trace.BeforeInsertAll(cancellableTraceLog);
                if (cancellableTraceLog.IsCancelled)
                {
                    if (cancellableTraceLog.IsThrowException)
                    {
                        throw new CancelledExecutionException(context.CommandText);
                    }
                    return(0);
                }
                context.CommandText = (cancellableTraceLog.Statement ?? context.CommandText);
                entities            = (IEnumerable <TEntity>)(cancellableTraceLog.Parameter ?? entities);
            }

            // Before Execution Time
            var beforeExecutionTime = DateTime.UtcNow;

            // Execution variables
            var result = 0;

            // Make sure to create transaction if there is no passed one
            var hasTransaction = (transaction != null);

            try
            {
                // Ensure the connection is open
                await connection.EnsureOpenAsync();

                if (hasTransaction == false)
                {
                    // Create a transaction
                    transaction = connection.BeginTransaction();
                }

                // Create the command
                using (var command = (DbCommand)connection.CreateCommand(context.CommandText,
                                                                         CommandType.Text, commandTimeout, transaction))
                {
                    // Directly execute if the entities is only 1 (performance)
                    if (context.BatchSize == 1)
                    {
                        foreach (var entity in entities)
                        {
                            // Set the values
                            context.SingleDataEntityParametersSetterFunc(command, entity);

                            // Actual Execution
                            var returnValue = ObjectConverter.DbNullToNull(await command.ExecuteScalarAsync());

                            // Set the return value
                            if (returnValue != null)
                            {
                                context.IdentityPropertySetterFunc?.Invoke(entity, returnValue);
                            }

                            // Iterate the result
                            result++;
                        }
                    }
                    else
                    {
                        foreach (var batchEntities in entities.Split(batchSize))
                        {
                            var batchItems = batchEntities.AsList();

                            // Break if there is no more records
                            if (batchItems.Count <= 0)
                            {
                                break;
                            }

                            // Check if the batch size has changed (probably the last batch on the enumerables)
                            if (batchItems.Count != batchSize)
                            {
                                // Get a new execution context from cache
                                context = InsertAllExecutionContextCache <TEntity> .Get(tableName, fields, batchItems.Count, callback);

                                // Set the command properties
                                command.CommandText = context.CommandText;

                                // Prepare the command
                                command.Prepare();
                            }

                            // Set the values
                            context.MultipleDataEntitiesParametersSetterFunc(command, batchItems);

                            // Actual Execution
                            result += await command.ExecuteNonQueryAsync();

                            // Set the identities
                            if (context.IdentityPropertySettersFunc != null && command.Parameters.Count > 0)
                            {
                                for (var index = 0; index < batchItems.Count; index++)
                                {
                                    var func = context.IdentityPropertySettersFunc.ElementAt(index);
                                    func(batchItems[index], command);
                                }
                            }
                        }
                    }
                }

                if (hasTransaction == false)
                {
                    // Commit the transaction
                    transaction.Commit();
                }
            }
            catch
            {
                if (hasTransaction == false)
                {
                    // Rollback for any exception
                    transaction.Rollback();
                }
                throw;
            }
            finally
            {
                if (hasTransaction == false)
                {
                    // Rollback and dispose the transaction
                    transaction.Dispose();
                }
            }

            // After Execution
            if (trace != null)
            {
                trace.AfterInsertAll(new TraceLog(context.CommandText, entities, result,
                                                  DateTime.UtcNow.Subtract(beforeExecutionTime)));
            }

            // Return the result
            return(result);
        }
コード例 #7
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="dbFields"></param>
        /// <param name="batchSize"></param>
        /// <param name="fields"></param>
        /// <param name="hints"></param>
        /// <param name="transaction"></param>
        /// <param name="statementBuilder"></param>
        /// <returns></returns>
        private static InsertAllExecutionContext <TEntity> CreateInternal <TEntity>(IDbConnection connection,
                                                                                    string tableName,
                                                                                    IEnumerable <DbField> dbFields,
                                                                                    int batchSize,
                                                                                    IEnumerable <Field> fields,
                                                                                    string hints = null,
                                                                                    IDbTransaction transaction         = null,
                                                                                    IStatementBuilder statementBuilder = null)
            where TEntity : class
        {
            var typeOfEntity    = typeof(TEntity);
            var dbSetting       = connection.GetDbSetting();
            var identity        = (Field)null;
            var inputFields     = (IEnumerable <DbField>)null;
            var identityDbField = dbFields?.FirstOrDefault(f => f.IsIdentity);

            // Set the identity value
            if (typeOfEntity.IsClassType())
            {
                identity = IdentityCache.Get <TEntity>()?.AsField() ??
                           FieldCache
                           .Get <TEntity>()?
                           .FirstOrDefault(field =>
                                           string.Equals(field.Name.AsUnquoted(true, dbSetting), identityDbField?.Name.AsUnquoted(true, dbSetting), StringComparison.OrdinalIgnoreCase)) ??
                           identityDbField?.AsField();
            }

            // Filter the actual properties for input fields
            inputFields = dbFields?
                          .Where(dbField => dbField.IsIdentity == false)
                          .Where(dbField =>
                                 fields.FirstOrDefault(field => string.Equals(field.Name.AsUnquoted(true, dbSetting), dbField.Name.AsUnquoted(true, dbSetting), StringComparison.OrdinalIgnoreCase)) != null)
                          .AsList();

            // Variables for the context
            var multipleEntitiesFunc = (Action <DbCommand, IList <TEntity> >)null;
            var identitySettersFunc  = (List <Action <TEntity, DbCommand> >)null;
            var singleEntityFunc     = (Action <DbCommand, TEntity>)null;
            var identitySetterFunc   = (Action <TEntity, object>)null;

            // Get if we have not skipped it
            if (typeOfEntity.IsClassType() && identity != null)
            {
                identitySetterFunc = FunctionCache.GetDataEntityPropertySetterCompiledFunction <TEntity>(identity);
            }

            // Identity which objects to set
            if (batchSize <= 1)
            {
                singleEntityFunc = FunctionCache.GetDataEntityDbParameterSetterCompiledFunction <TEntity>(
                    string.Concat(typeof(TEntity).FullName, StringConstant.Period, tableName, ".InsertAll"),
                    inputFields?.AsList(),
                    null,
                    dbSetting);
            }
            else
            {
                multipleEntitiesFunc = FunctionCache.GetDataEntityListDbParameterSetterCompiledFunction <TEntity>(
                    string.Concat(typeof(TEntity).FullName, StringConstant.Period, tableName, ".InsertAll"),
                    inputFields?.AsList(),
                    null,
                    batchSize,
                    dbSetting);
            }

            // Identify the requests
            var insertAllRequest = (InsertAllRequest)null;
            var insertRequest    = (InsertRequest)null;

            // Create a different kind of requests
            if (batchSize > 1)
            {
                insertAllRequest = new InsertAllRequest(tableName,
                                                        connection,
                                                        transaction,
                                                        fields,
                                                        batchSize,
                                                        hints,
                                                        statementBuilder);
            }
            else
            {
                insertRequest = new InsertRequest(tableName,
                                                  connection,
                                                  transaction,
                                                  fields,
                                                  hints,
                                                  statementBuilder);
            }

            // Return the value
            return(new InsertAllExecutionContext <TEntity>
            {
                CommandText = batchSize > 1 ? CommandTextCache.GetInsertAllText(insertAllRequest) : CommandTextCache.GetInsertText(insertRequest),
                InputFields = inputFields,
                BatchSize = batchSize,
                SingleDataEntityParametersSetterFunc = singleEntityFunc,
                MultipleDataEntitiesParametersSetterFunc = multipleEntitiesFunc,
                IdentityPropertySetterFunc = identitySetterFunc,
                IdentityPropertySettersFunc = identitySettersFunc
            });
        }
コード例 #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="batchSize"></param>
        /// <param name="fields"></param>
        /// <param name="hints"></param>
        /// <param name="transaction"></param>
        /// <param name="statementBuilder"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task <InsertAllExecutionContext> CreateAsync(Type entityType,
                                                                         IDbConnection connection,
                                                                         string tableName,
                                                                         int batchSize,
                                                                         IEnumerable <Field> fields,
                                                                         string hints = null,
                                                                         IDbTransaction transaction          = null,
                                                                         IStatementBuilder statementBuilder  = null,
                                                                         CancellationToken cancellationToken = default)
        {
            var key = GetKey(entityType, tableName, fields, batchSize, hints);

            // Get from cache
            var context = InsertAllExecutionContextCache.Get(key);

            if (context != null)
            {
                return(context);
            }

            // Create
            var dbFields = await DbFieldCache.GetAsync(connection, tableName, transaction, cancellationToken);

            var commandText = (string)null;

            // Create a different kind of requests
            if (batchSize > 1)
            {
                var request = new InsertAllRequest(tableName,
                                                   connection,
                                                   transaction,
                                                   fields,
                                                   batchSize,
                                                   hints,
                                                   statementBuilder);
                commandText = await CommandTextCache.GetInsertAllTextAsync(request, cancellationToken);
            }
            else
            {
                var request = new InsertRequest(tableName,
                                                connection,
                                                transaction,
                                                fields,
                                                hints,
                                                statementBuilder);
                commandText = await CommandTextCache.GetInsertTextAsync(request, cancellationToken);
            }

            // Call
            context = CreateInternal(entityType,
                                     connection,
                                     tableName,
                                     dbFields,
                                     batchSize,
                                     fields,
                                     commandText);

            // Add to cache
            InsertAllExecutionContextCache.Add(key, context);

            // Return
            return(context);
        }