예제 #1
0
        internal static IEnumerable <IPropertyMapping> GetPropertiesByOptions(this IEntityMapping mapping, BulkOptions options)
        {
            if (options.HasFlag(BulkOptions.OutputIdentity) && options.HasFlag(BulkOptions.OutputComputed))
            {
                return(mapping.Properties.Where(property => property.IsDbGenerated));
            }
            if (options.HasFlag(BulkOptions.OutputIdentity))
            {
                return(mapping.Properties.Where(property => property.IsPk && property.IsDbGenerated));
            }
            if (options.HasFlag(BulkOptions.OutputComputed))
            {
                return(mapping.Properties.Where(property => !property.IsPk && property.IsDbGenerated));
            }

            return(mapping.Properties);
        }
예제 #2
0
 internal static IEnumerable <IPropertyMapping> FilterPropertiesByOptions(this IEnumerable <IPropertyMapping> propertyMappings, BulkOptions options)
 {
     if (!options.HasFlag(BulkOptions.KeepForeingKeys))
     {
         return(propertyMappings.Where(property => !property.IsFk));
     }
     return(propertyMappings);
 }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="context"></param>
        /// <param name="collection"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        int IBulkOperation.CommitTransaction <TEntity>(IDbContextWrapper context, IEnumerable <TEntity> collection,
                                                       BulkOptions options)
        {
            var entityList = collection.ToList();

            if (!entityList.Any())
            {
                return(entityList.Count);
            }

            try
            {
                //Return generated IDs for bulk inserted elements.
                if (options.HasFlag(BulkOptions.OutputIdentity))
                {
                    var tmpTableName = context.EntityMapping.RandomTableName();
                    //Create temporary table.
                    context.ExecuteSqlCommand(context.EntityMapping.CreateTempTable(tmpTableName, OperationType.Insert, options));

                    //Bulk inset data to temporary temporary table.
                    context.BulkInsertToTable(entityList, tmpTableName, OperationType.Insert, options);

                    var tmpOutputTableName = context.EntityMapping.RandomTableName();
                    //Copy data from temporary table to destination table with ID output to another temporary table.
                    var commandText = context.EntityMapping.GetInsertIntoStagingTableCmd(tmpOutputTableName,
                                                                                         tmpTableName, context.EntityMapping.Pks.First().ColumnName, OperationType.Insert, options);
                    context.ExecuteSqlCommand(commandText);

                    //Load generated IDs from temporary output table into the entities.
                    context.LoadFromTmpOutputTable(tmpOutputTableName, context.EntityMapping.Pks.First(), entityList);
                }
                else
                {
                    //Bulk inset data to temporary destination table.
                    context.BulkInsertToTable(entityList, context.EntityMapping.FullTableName, OperationType.Insert, options);
                }

                //Commit if internal transaction exists.
                context.Commit();
                return(entityList.Count);
            }
            catch (Exception)
            {
                //Rollback if internal transaction exists.
                context.Rollback();
                throw;
            }
        }
예제 #4
0
 internal static bool WillOutputGeneratedValues(this IEntityMapping mapping, BulkOptions options)
 {
     return(options.HasFlag(BulkOptions.OutputIdentity) && mapping.HasGeneratedKeys ||
            options.HasFlag(BulkOptions.OutputComputed) && mapping.HasComputedColumns);
 }