Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="connection"></param>
        /// <param name="entities"></param>
        /// <param name="tableName"></param>
        /// <param name="qualifiers"></param>
        /// <param name="batchSize"></param>
        /// <param name="fields"></param>
        /// <param name="hints"></param>
        /// <param name="transaction"></param>
        /// <param name="statementBuilder"></param>
        /// <param name="skipIdentityCheck"></param>
        /// <returns></returns>
        public static MergeAllExecutionContext <TEntity> Create <TEntity>(IDbConnection connection,
                                                                          IEnumerable <TEntity> entities,
                                                                          string tableName,
                                                                          IEnumerable <Field> qualifiers,
                                                                          int batchSize,
                                                                          IEnumerable <Field> fields,
                                                                          string hints = null,
                                                                          IDbTransaction transaction         = null,
                                                                          IStatementBuilder statementBuilder = null,
                                                                          bool skipIdentityCheck             = false)
            where TEntity : class
        {
            var key = GetKey <TEntity>(tableName, qualifiers, fields, batchSize, hints);

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

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

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

            context = CreateInternal <TEntity>(connection,
                                               entities,
                                               dbFields,
                                               tableName,
                                               qualifiers,
                                               batchSize,
                                               fields,
                                               hints,
                                               transaction,
                                               statementBuilder,
                                               skipIdentityCheck);

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

            // Return
            return(context);
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="connection"></param>
        /// <param name="entities"></param>
        /// <param name="tableName"></param>
        /// <param name="qualifiers"></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 MergeAllExecutionContext <TEntity> Create <TEntity>(IDbConnection connection,
                                                                          IEnumerable <TEntity> entities,
                                                                          string tableName,
                                                                          IEnumerable <Field> qualifiers,
                                                                          int batchSize,
                                                                          IEnumerable <Field> fields,
                                                                          string hints = null,
                                                                          IDbTransaction transaction         = null,
                                                                          IStatementBuilder statementBuilder = null)
            where TEntity : class
        {
            var key = GetKey <TEntity>(tableName, qualifiers, fields, batchSize, hints);

            // Get from cache
            var context = MergeAllExecutionContextCache.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 MergeAllRequest(tableName,
                                                  connection,
                                                  transaction,
                                                  fields,
                                                  qualifiers,
                                                  batchSize,
                                                  hints,
                                                  statementBuilder);
                commandText = CommandTextCache.GetMergeAllText(request);
            }
            else
            {
                var request = new MergeRequest(tableName,
                                               connection,
                                               transaction,
                                               fields,
                                               qualifiers,
                                               hints,
                                               statementBuilder);
                commandText = CommandTextCache.GetMergeText(request);
            }

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

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

            // Return
            return(context);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="connection"></param>
        /// <param name="entities"></param>
        /// <param name="tableName"></param>
        /// <param name="qualifiers"></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 <MergeAllExecutionContext> CreateAsync(Type entityType,
                                                                        IDbConnection connection,
                                                                        IEnumerable <object> entities,
                                                                        string tableName,
                                                                        IEnumerable <Field> qualifiers,
                                                                        int batchSize,
                                                                        IEnumerable <Field> fields,
                                                                        string hints = null,
                                                                        IDbTransaction transaction          = null,
                                                                        IStatementBuilder statementBuilder  = null,
                                                                        CancellationToken cancellationToken = default)
        {
            var key = GetKey(entityType, tableName, qualifiers, fields, batchSize, hints);

            // Get from cache
            var context = MergeAllExecutionContextCache.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 MergeAllRequest(tableName,
                                                  connection,
                                                  transaction,
                                                  fields,
                                                  qualifiers,
                                                  batchSize,
                                                  hints,
                                                  statementBuilder);
                commandText = await CommandTextCache.GetMergeAllTextAsync(request, cancellationToken);
            }
            else
            {
                var request = new MergeRequest(tableName,
                                               connection,
                                               transaction,
                                               fields,
                                               qualifiers,
                                               hints,
                                               statementBuilder);
                commandText = await CommandTextCache.GetMergeTextAsync(request, cancellationToken);
            }

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

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

            // Return
            return(context);
        }