/// <summary>
        ///
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="entities"></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 <UpdateAllExecutionContext> CreateAsync(Type entityType,
                                                                         IDbConnection connection,
                                                                         string tableName,
                                                                         IEnumerable <object> entities,
                                                                         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 = UpdateAllExecutionContextCache.Get(key);

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

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

            var request = new UpdateAllRequest(tableName,
                                               connection,
                                               transaction,
                                               fields,
                                               qualifiers,
                                               batchSize,
                                               hints,
                                               statementBuilder);
            var commandText = await CommandTextCache.GetUpdateAllTextAsync(request, cancellationToken);

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

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

            // Return
            return(context);
        }
Beispiel #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 internal static string GetUpdateAllText(UpdateAllRequest request)
 {
     if (cache.TryGetValue(request, out var commandText) == false)
     {
         var fields = GetActualFields(request.Connection,
                                      request.Name,
                                      request.Fields,
                                      request.Transaction);
         commandText = GetUpdateAllTextInternal(request, fields);
         cache.TryAdd(request, commandText);
     }
     return(commandText);
 }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="entities"></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 UpdateAllExecutionContext <TEntity> Create <TEntity>(IDbConnection connection,
                                                                           string tableName,
                                                                           IEnumerable <TEntity> entities,
                                                                           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 = UpdateAllExecutionContextCache.Get <TEntity>(key);

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

            // Create
            var dbFields = DbFieldCache.Get(connection, tableName, transaction);
            var request  = new UpdateAllRequest(tableName,
                                                connection,
                                                transaction,
                                                fields,
                                                qualifiers,
                                                batchSize,
                                                hints,
                                                statementBuilder);
            var commandText = CommandTextCache.GetUpdateAllText(request);

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

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

            // Return
            return(context);
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        private static string GetUpdateAllTextInternal(UpdateAllRequest request,
                                                       IEnumerable <Field> fields)
        {
            var statementBuilder = EnsureStatementBuilder(request.Connection, request.StatementBuilder);
            var primaryField     = GetPrimaryField(request);
            var identityField    = GetIdentityField(request);

            return(statementBuilder.CreateUpdateAll(new QueryBuilder(),
                                                    request.Name,
                                                    fields,
                                                    request.Qualifiers,
                                                    request.BatchSize,
                                                    primaryField,
                                                    identityField,
                                                    request.Hints));
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        internal static async Task <string> GetUpdateAllTextAsync(UpdateAllRequest 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 = GetUpdateAllTextInternal(request, fields);
                cache.TryAdd(request, commandText);
            }
            return(commandText);
        }
Beispiel #6
0
        /// <summary>
        /// Gets a command text from the cache for the update-all operation.
        /// </summary>
        /// <param name="request">The request object.</param>
        /// <returns>The cached command text.</returns>
        internal static string GetUpdateAllText(UpdateAllRequest 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 qualifiers       = request.Qualifiers;
                var primaryField     = GetPrimaryField(request);
                var identityField    = GetIdentityField(request);
                commandText = statementBuilder.CreateUpdateAll(new QueryBuilder(),
                                                               request.Name,
                                                               fields,
                                                               request.Qualifiers,
                                                               request.BatchSize,
                                                               primaryField,
                                                               identityField);
                m_cache.TryAdd(request, commandText);
            }
            return(commandText);
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="entities"></param>
        /// <param name="dbFields"></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>
        private static UpdateAllExecutionContext <TEntity> CreateInternal <TEntity>(IDbConnection connection,
                                                                                    string tableName,
                                                                                    IEnumerable <TEntity> entities,
                                                                                    IEnumerable <DbField> dbFields,
                                                                                    IEnumerable <Field> qualifiers,
                                                                                    int batchSize,
                                                                                    IEnumerable <Field> fields,
                                                                                    string hints = null,
                                                                                    IDbTransaction transaction         = null,
                                                                                    IStatementBuilder statementBuilder = null)
            where TEntity : class
        {
            // Variables needed
            var dbSetting   = connection.GetDbSetting();
            var inputFields = new List <DbField>();

            // Check the qualifiers
            if (qualifiers?.Any() != true)
            {
                var key = dbFields?.FirstOrDefault(dbField => dbField.IsPrimary == true) ??
                          dbFields?.FirstOrDefault(dbField => dbField.IsIdentity == true);
                qualifiers = key?.AsField().AsEnumerable();
            }

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

            // Exclude the fields not on the actual entity
            if (typeof(TEntity).IsClassType() == false)
            {
                var entityFields = Field.Parse(entities?.FirstOrDefault());
                inputFields = inputFields?
                              .Where(field =>
                                     entityFields.FirstOrDefault(f => string.Equals(f.Name.AsUnquoted(true, dbSetting), field.Name.AsUnquoted(true, dbSetting), StringComparison.OrdinalIgnoreCase)) != null)
                              .AsList();
            }

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

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

            // Identity the requests
            var updateAllRequest = new UpdateAllRequest(tableName,
                                                        connection,
                                                        transaction,
                                                        fields,
                                                        qualifiers,
                                                        batchSize,
                                                        hints,
                                                        statementBuilder);

            // Return the value
            return(new UpdateAllExecutionContext <TEntity>
            {
                CommandText = CommandTextCache.GetUpdateAllText(updateAllRequest),
                InputFields = inputFields,
                SingleDataEntityParametersSetterFunc = singleEntityFunc,
                MultipleDataEntitiesParametersSetterFunc = multipleEntitiesFunc
            });
        }