Пример #1
0
        /// <summary>
        /// Delete the existing rows by bulk via a list of primary keys in an asynchronous way. Underneath this operation is a call directly to the existing
        /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
        /// </summary>
        /// <typeparam name="TPrimaryKey">The type of the primary key.</typeparam>
        /// <param name="repository">The instance of <see cref="DbRepository{TDbConnection}"/> object.</param>
        /// <param name="tableName">The name of the target table from the database.</param>
        /// <param name="primaryKeys">The list of primary keys that targets the rows to be bulk-deleted from the target table.</param>
        /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
        /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the primary keys will be sent together in one-go.</param>
        /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
        /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
        /// <returns>The number of rows that has been deleted from the target table.</returns>
        public static async Task <int> BinaryBulkDeleteByKeyAsync <TPrimaryKey>(this DbRepository <NpgsqlConnection> repository,
                                                                                string tableName,
                                                                                IEnumerable <TPrimaryKey> primaryKeys,
                                                                                int?bulkCopyTimeout = null,
                                                                                int?batchSize       = null,
                                                                                BulkImportPseudoTableType pseudoTableType = default,
                                                                                NpgsqlTransaction transaction             = null,
                                                                                CancellationToken cancellationToken       = default)
        {
            // Create a connection
            var connection = (transaction?.Connection ?? repository.CreateConnection());

            try
            {
                // Call the method
                return(await connection.BinaryBulkDeleteByKeyAsync <TPrimaryKey>(tableName : tableName,
                                                                                 primaryKeys : primaryKeys,
                                                                                 bulkCopyTimeout : bulkCopyTimeout,
                                                                                 batchSize : batchSize,
                                                                                 pseudoTableType : pseudoTableType,
                                                                                 transaction : transaction,
                                                                                 cancellationToken : cancellationToken));
            }
            finally
            {
                // Dispose the connection
                if (repository.ConnectionPersistency == ConnectionPersistency.PerCall)
                {
                    if (transaction == null)
                    {
                        connection.Dispose();
                    }
                }
            }
        }
Пример #2
0
 /// <summary>
 /// Merges the rows of the <see cref="DataTable"/> into the target table by bulk in an asynchronous way. This operation is inserting a row (if not present), and updating an existing
 /// row (if present), based on the given qualifiers. It uses either of the 'INSERT/UPDATE' and 'ON CONFLICT DO UPDATE' commands of the
 /// PostgreSQL based on the value passed to the '<paramref name="mergeCommandType"/>' argument. Underneath this operation is a call directly to
 /// the existing <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the customized 'BinaryBulkInsertAsync' extended method.
 /// </summary>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="tableName">The name of the target table from the database. If not specified, the <see cref="DataTable.TableName"/> property will be used.</param>
 /// <param name="table">The source <see cref="DataTable"/> object that contains the rows to be bulk-merged to the target table.</param>
 /// <param name="rowState">The state of the rows to be bulk-merged. If not specified, all the rows of the table will be used.</param>
 /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the rows of the table will be sent together in one-go.</param>
 /// <param name="identityBehavior">The behavior of how the identity column would work during the operation.</param>
 /// <param name="mergeCommandType">The value that defines the type of command to be used during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
 /// <returns>The number of rows that has been merged into the target table.</returns>
 public static async Task <int> BinaryBulkMergeAsync(this NpgsqlConnection connection,
                                                     string tableName,
                                                     DataTable table,
                                                     DataRowState?rowState          = null,
                                                     IEnumerable <Field> qualifiers = null,
                                                     IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                     int?bulkCopyTimeout = null,
                                                     int?batchSize       = null,
                                                     BulkImportIdentityBehavior identityBehavior = default,
                                                     BulkImportMergeCommandType mergeCommandType = default,
                                                     BulkImportPseudoTableType pseudoTableType   = default,
                                                     NpgsqlTransaction transaction       = null,
                                                     CancellationToken cancellationToken = default) =>
 await BinaryBulkMergeBaseAsync(connection : connection,
                                tableName : (tableName ?? table?.TableName),
                                table : table,
                                rowState : rowState,
                                qualifiers : qualifiers,
                                mappings : mappings,
                                bulkCopyTimeout : bulkCopyTimeout,
                                batchSize : batchSize,
                                identityBehavior : identityBehavior,
                                mergeCommandType : mergeCommandType,
                                pseudoTableType : pseudoTableType,
                                transaction : transaction,
                                cancellationToken : cancellationToken);
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TPrimaryKey"></typeparam>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="primaryKeys"></param>
        /// <param name="bulkCopyTimeout"></param>
        /// <param name="batchSize"></param>
        /// <param name="pseudoTableType"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        private static int BinaryBulkDeleteByKeyBase <TPrimaryKey>(this NpgsqlConnection connection,
                                                                   string tableName,
                                                                   IEnumerable <TPrimaryKey> primaryKeys,
                                                                   int?bulkCopyTimeout = null,
                                                                   int?batchSize       = null,
                                                                   BulkImportPseudoTableType pseudoTableType = default,
                                                                   NpgsqlTransaction transaction             = null)
        {
            var identityBehavior = BulkImportIdentityBehavior.Unspecified;
            var dbSetting        = connection.GetDbSetting();
            var dbFields         = DbFieldCache.Get(connection, tableName, transaction);
            var primaryKey       = dbFields.FirstOrDefault(dbField => dbField.IsPrimary);
            var pseudoTableName  = tableName;
            IEnumerable <NpgsqlBulkInsertMapItem> mappings = null;

            return(PseudoBasedBinaryImport(connection,
                                           tableName,
                                           bulkCopyTimeout,
                                           dbFields,

                                           // getPseudoTableName
                                           () =>
                                           pseudoTableName = GetBinaryBulkDeleteByKeyPseudoTableName(tableName ?? ClassMappedNameCache.Get <TPrimaryKey>(), dbSetting),

                                           // getMappings
                                           () =>
                                           mappings = new[]
            {
                new NpgsqlBulkInsertMapItem(primaryKey.Name, primaryKey.Name)
            },

                                           // binaryImport
                                           (tableName) =>
                                           connection.BinaryImport(tableName,
                                                                   GetExpandoObjectData(primaryKeys, primaryKey.AsField()),
                                                                   mappings,
                                                                   dbFields,
                                                                   bulkCopyTimeout,
                                                                   batchSize,
                                                                   identityBehavior,
                                                                   dbSetting,
                                                                   transaction),

                                           // getDeleteToPseudoCommandText
                                           () =>
                                           GetDeleteByKeyCommandText(pseudoTableName,
                                                                     tableName,
                                                                     dbFields.FirstOrDefault(dbField => dbField.IsPrimary)?.AsField(),
                                                                     dbSetting),

                                           // setIdentities
                                           null,

                                           null,
                                           false,
                                           identityBehavior,
                                           pseudoTableType,
                                           dbSetting,
                                           transaction));
        }
Пример #4
0
 /// <summary>
 /// Merges a list of entities into the target table by bulk in an asynchronous way. This operation is inserting a row (if not present), and updating an existing
 /// row (if present), based on the given qualifiers. It uses either of the 'INSERT/UPDATE' and 'ON CONFLICT DO UPDATE' commands of the
 /// PostgreSQL based on the value passed to the '<paramref name="mergeCommandType"/>' argument. Underneath this operation is a call directly to
 /// the existing <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the customized 'BinaryBulkInsertAsync' extended method.
 /// </summary>
 /// <typeparam name="TEntity">The type of the entity.</typeparam>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="tableName">The name of the target table from the database.</param>
 /// <param name="entities">The list of entities to be bulk-merged to the target table.
 /// This can be an <see cref="IEnumerable{T}"/> of the following objects (<typeparamref name="TEntity"/> (as class/model), <see cref="ExpandoObject"/>,
 /// <see cref="IDictionary{TKey, TValue}"/> (of <see cref="string"/>/<see cref="object"/>) and Anonymous Types).</param>
 /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the rows of the table will be sent together in one-go.</param>
 /// <param name="identityBehavior">The behavior of how the identity column would work during the operation.</param>
 /// <param name="mergeCommandType">The value that defines the type of command to be used during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
 /// <returns>The number of rows that has been merged into the target table.</returns>
 public static async Task <int> BinaryBulkMergeAsync <TEntity>(this NpgsqlConnection connection,
                                                               string tableName,
                                                               IEnumerable <TEntity> entities,
                                                               IEnumerable <Field> qualifiers = null,
                                                               IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                               int?bulkCopyTimeout = null,
                                                               int?batchSize       = null,
                                                               BulkImportIdentityBehavior identityBehavior = default,
                                                               BulkImportMergeCommandType mergeCommandType = default,
                                                               BulkImportPseudoTableType pseudoTableType   = default,
                                                               NpgsqlTransaction transaction       = null,
                                                               CancellationToken cancellationToken = default)
     where TEntity : class =>
 await BinaryBulkMergeBaseAsync <TEntity>(connection : connection,
                                          tableName : (tableName ?? ClassMappedNameCache.Get <TEntity>()),
                                          entities : entities,
                                          qualifiers : qualifiers,
                                          mappings : mappings,
                                          bulkCopyTimeout : bulkCopyTimeout,
                                          batchSize : batchSize,
                                          identityBehavior : identityBehavior,
                                          mergeCommandType : mergeCommandType,
                                          pseudoTableType : pseudoTableType,
                                          transaction : transaction,
                                          cancellationToken : cancellationToken);
Пример #5
0
        /// <summary>
        /// Inserts the rows of the <see cref="DbDataReader"/> into the target table by bulk. Underneath this operation is a call directly to the existing
        /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method.
        /// </summary>
        /// <param name="repository">The instance of <see cref="DbRepository{TDbConnection}"/> object.</param>
        /// <param name="tableName">The name of the target table from the database.</param>
        /// <param name="reader">The instance of <see cref="DbDataReader"/> object that contains the rows to be bulk-inserted to the target table.</param>
        /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not an entity mapping)</param>
        /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
        /// <param name="identityBehavior">The behavior of how the identity column would work during the operation.</param>
        /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
        /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
        /// <returns>The number of rows that has been inserted into the target table.</returns>
        public static int BinaryBulkInsert(this DbRepository <NpgsqlConnection> repository,
                                           string tableName,
                                           DbDataReader reader,
                                           IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                           int?bulkCopyTimeout = null,
                                           BulkImportIdentityBehavior identityBehavior = default,
                                           BulkImportPseudoTableType pseudoTableType   = default,
                                           NpgsqlTransaction transaction = null)
        {
            // Create a connection
            var connection = (transaction?.Connection ?? repository.CreateConnection());

            try
            {
                // Call the method
                return(connection.BinaryBulkInsert(tableName: tableName,
                                                   reader: reader,
                                                   mappings: mappings,
                                                   bulkCopyTimeout: bulkCopyTimeout,
                                                   identityBehavior: identityBehavior,
                                                   pseudoTableType: pseudoTableType,
                                                   transaction: transaction));
            }
            finally
            {
                // Dispose the connection
                if (repository.ConnectionPersistency == ConnectionPersistency.PerCall)
                {
                    if (transaction == null)
                    {
                        connection.Dispose();
                    }
                }
            }
        }
Пример #6
0
 /// <summary>
 /// Delete the existing rows by bulk via a list of primary keys. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <typeparam name="TPrimaryKey">The type of the primary key.</typeparam>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="tableName">The name of the target table from the database.</param>
 /// <param name="primaryKeys">The list of primary keys that targets the rows to be bulk-deleted from the target table.</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the primary keys will be sent together in one-go.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <returns>The number of rows that has been deleted from the target table.</returns>
 public static int BinaryBulkDeleteByKey <TPrimaryKey>(this NpgsqlConnection connection,
                                                       string tableName,
                                                       IEnumerable <TPrimaryKey> primaryKeys,
                                                       int?bulkCopyTimeout = null,
                                                       int?batchSize       = null,
                                                       BulkImportPseudoTableType pseudoTableType = default,
                                                       NpgsqlTransaction transaction             = null) =>
 BinaryBulkDeleteByKeyBase <TPrimaryKey>(connection: connection,
                                         tableName: tableName,
                                         primaryKeys: primaryKeys,
                                         bulkCopyTimeout: bulkCopyTimeout,
                                         batchSize: batchSize,
                                         pseudoTableType: pseudoTableType,
                                         transaction: transaction);
Пример #7
0
 /// <summary>
 /// Delete the existing rows by bulk via a list of primary keys. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <typeparam name="TEntity">The type of the entity.</typeparam>
 /// <typeparam name="TPrimaryKey">The type of the primary key.</typeparam>
 /// <param name="repository">The instance of <see cref="BaseRepository{TEntity, TDbConnection}"/> object.</param>
 /// <param name="tableName">The name of the target table from the database.</param>
 /// <param name="primaryKeys">The list of primary keys that targets the rows to be bulk-deleted from the target table.</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the primary keys will be sent together in one-go.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <returns>The number of rows that has been deleted from the target table.</returns>
 public static int BinaryBulkDeleteByKey <TEntity, TPrimaryKey>(this BaseRepository <TEntity, NpgsqlConnection> repository,
                                                                string tableName,
                                                                IEnumerable <TPrimaryKey> primaryKeys,
                                                                int?bulkCopyTimeout = null,
                                                                int?batchSize       = null,
                                                                BulkImportPseudoTableType pseudoTableType = default,
                                                                NpgsqlTransaction transaction             = null)
     where TEntity : class =>
 repository.DbRepository.BinaryBulkDeleteByKey <TPrimaryKey>(tableName : tableName,
                                                             primaryKeys : primaryKeys,
                                                             bulkCopyTimeout : bulkCopyTimeout,
                                                             batchSize : batchSize,
                                                             pseudoTableType : pseudoTableType,
                                                             transaction : transaction);
Пример #8
0
 /// <summary>
 /// Delete the existing rows by bulk via a list of primary keys in an asynchronous way. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <typeparam name="TPrimaryKey">The type of the primary key.</typeparam>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="tableName">The name of the target table from the database.</param>
 /// <param name="primaryKeys">The list of primary keys that targets the rows to be bulk-deleted from the target table.</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the primary keys will be sent together in one-go.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
 /// <returns>The number of rows that has been deleted from the target table.</returns>
 public static async Task <int> BinaryBulkDeleteByKeyAsync <TPrimaryKey>(this NpgsqlConnection connection,
                                                                         string tableName,
                                                                         IEnumerable <TPrimaryKey> primaryKeys,
                                                                         int?bulkCopyTimeout = null,
                                                                         int?batchSize       = null,
                                                                         BulkImportPseudoTableType pseudoTableType = default,
                                                                         NpgsqlTransaction transaction             = null,
                                                                         CancellationToken cancellationToken       = default) =>
 await BinaryBulkDeleteByKeyBaseAsync <TPrimaryKey>(connection : connection,
                                                    tableName : tableName,
                                                    primaryKeys : primaryKeys,
                                                    bulkCopyTimeout : bulkCopyTimeout,
                                                    batchSize : batchSize,
                                                    pseudoTableType : pseudoTableType,
                                                    transaction : transaction,
                                                    cancellationToken : cancellationToken);
Пример #9
0
 /// <summary>
 /// Inserts the rows of the <see cref="DbDataReader"/> into the target table by bulk. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method.
 /// </summary>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="tableName">The name of the target table from the database.</param>
 /// <param name="reader">The instance of <see cref="DbDataReader"/> object that contains the rows to be bulk-inserted to the target table.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not an entity mapping)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="identityBehavior">The behavior of how the identity column would work during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <returns>The number of rows that has been inserted into the target table.</returns>
 public static int BinaryBulkInsert(this NpgsqlConnection connection,
                                    string tableName,
                                    DbDataReader reader,
                                    IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                    int?bulkCopyTimeout = null,
                                    BulkImportIdentityBehavior identityBehavior = default,
                                    BulkImportPseudoTableType pseudoTableType   = default,
                                    NpgsqlTransaction transaction = null) =>
 BinaryBulkInsertBase(connection: connection,
                      tableName: tableName,
                      reader: reader,
                      mappings: mappings,
                      bulkCopyTimeout: bulkCopyTimeout,
                      identityBehavior: identityBehavior,
                      pseudoTableType: pseudoTableType,
                      transaction: transaction);
Пример #10
0
 /// <summary>
 /// Inserts a list of entities into the target table by bulk. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <typeparam name="TEntity">The type of the entity.</typeparam>
 /// <param name="repository">The instance of <see cref="BaseRepository{TEntity, TDbConnection}"/> object.</param>
 /// <param name="entities">The list of entities to be bulk-inserted to the target table.
 /// This can be an <see cref="IEnumerable{T}"/> of the following objects (<typeparamref name="TEntity"/> (as class/model), <see cref="ExpandoObject"/>,
 /// <see cref="IDictionary{TKey, TValue}"/> (of <see cref="string"/>/<see cref="object"/>) and Anonymous Types).</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not an entity mapping)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the entities will be sent together in one-go.</param>
 /// <param name="identityBehavior">The behavior of how the identity column would work during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <returns>The number of rows that has been inserted into the target table.</returns>
 public static int BinaryBulkInsert <TEntity>(this BaseRepository <TEntity, NpgsqlConnection> repository,
                                              IEnumerable <TEntity> entities,
                                              IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                              int?bulkCopyTimeout = null,
                                              int?batchSize       = null,
                                              BulkImportIdentityBehavior identityBehavior = default,
                                              BulkImportPseudoTableType pseudoTableType   = default,
                                              NpgsqlTransaction transaction = null)
     where TEntity : class =>
 repository.DbRepository.BinaryBulkInsert <TEntity>(tableName : ClassMappedNameCache.Get <TEntity>(),
                                                    entities : entities,
                                                    mappings : mappings,
                                                    bulkCopyTimeout : bulkCopyTimeout,
                                                    batchSize : batchSize,
                                                    identityBehavior : identityBehavior,
                                                    pseudoTableType : pseudoTableType,
                                                    transaction : transaction);
Пример #11
0
 /// <summary>
 /// Inserts the rows of the <see cref="DbDataReader"/> into the target table by bulk in an asynchronous way. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method.
 /// </summary>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="tableName">The name of the target table from the database.</param>
 /// <param name="reader">The instance of <see cref="DbDataReader"/> object that contains the rows to be bulk-inserted to the target table.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not an entity mapping)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="identityBehavior">The behavior of how the identity column would work during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
 /// <returns>The number of rows that has been inserted into the target table.</returns>
 public static async Task <int> BinaryBulkInsertAsync(this NpgsqlConnection connection,
                                                      string tableName,
                                                      DbDataReader reader,
                                                      IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                      int?bulkCopyTimeout = null,
                                                      BulkImportIdentityBehavior identityBehavior = default,
                                                      BulkImportPseudoTableType pseudoTableType   = default,
                                                      NpgsqlTransaction transaction       = null,
                                                      CancellationToken cancellationToken = default) =>
 await BinaryBulkInsertBaseAsync(connection : connection,
                                 tableName : tableName,
                                 reader : reader,
                                 mappings : mappings,
                                 bulkCopyTimeout : bulkCopyTimeout,
                                 identityBehavior : identityBehavior,
                                 pseudoTableType : pseudoTableType,
                                 transaction : transaction,
                                 cancellationToken : cancellationToken);
Пример #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="pseudoTableName"></param>
        /// <param name="mappings"></param>
        /// <param name="bulkCopyTimeout"></param>
        /// <param name="identityBehavior"></param>
        /// <param name="pseudoTableType"></param>
        /// <param name="dbSetting"></param>
        /// <param name="transaction"></param>
        private static void CreatePseudoTable(NpgsqlConnection connection,
                                              string tableName,
                                              string pseudoTableName,
                                              IEnumerable <NpgsqlBulkInsertMapItem> mappings,
                                              int?bulkCopyTimeout = null,
                                              BulkImportIdentityBehavior identityBehavior = default,
                                              BulkImportPseudoTableType pseudoTableType   = default,
                                              IDbSetting dbSetting          = null,
                                              NpgsqlTransaction transaction = null)
        {
            var commandText = pseudoTableType == BulkImportPseudoTableType.Physical ?
                              GetCreatePseudoTableCommandText(tableName, pseudoTableName, mappings, identityBehavior, dbSetting) :
                              GetCreatePseudoTemporaryTableCommandText(tableName, pseudoTableName, mappings, identityBehavior, dbSetting);

            connection.ExecuteNonQuery(commandText,
                                       bulkCopyTimeout,
                                       transaction: transaction);
        }
Пример #13
0
 /// <summary>
 /// Delete the existing rows via <see cref="DbDataReader"/> by bulk. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="tableName">The name of the target table from the database.</param>
 /// <param name="reader">The instance of <see cref="DbDataReader"/> object that contains the rows to be bulk-deleted to the target table.</param>
 /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="keepIdentity">A value that indicates whether the existing identity property values from the <see cref="DbDataReader"/> will be kept during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <returns>The number of rows that has been deleted from the target table.</returns>
 public static int BinaryBulkDelete(this NpgsqlConnection connection,
                                    string tableName,
                                    DbDataReader reader,
                                    IEnumerable <Field> qualifiers = null,
                                    IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                    int?bulkCopyTimeout = null,
                                    bool keepIdentity   = true,
                                    BulkImportPseudoTableType pseudoTableType = default,
                                    NpgsqlTransaction transaction             = null) =>
 BinaryBulkDeleteBase(connection: connection,
                      tableName: tableName,
                      reader: reader,
                      qualifiers: qualifiers,
                      mappings: mappings,
                      bulkCopyTimeout: bulkCopyTimeout,
                      keepIdentity: keepIdentity,
                      pseudoTableType: pseudoTableType,
                      transaction: transaction);
Пример #14
0
 /// <summary>
 /// Inserts the rows of the <see cref="DataTable"/> into the target table by bulk. It uses the <see cref="DataTable.TableName"/> property
 /// as the target table. Underneath this operation is a call directly to the existing <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method.
 /// </summary>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="table">The source <see cref="DataTable"/> object that contains the rows to be bulk-inserted to the target table.</param>
 /// <param name="rowState">The state of the rows to be bulk-inserted. If not specified, all the rows of the table will be used.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not an entity mapping)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the rows of the table will be sent together in one-go.</param>
 /// <param name="identityBehavior">The behavior of how the identity column would work during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <returns>The number of rows that has been inserted into the target table.</returns>
 public static int BinaryBulkInsert(this NpgsqlConnection connection,
                                    DataTable table,
                                    DataRowState?rowState = null,
                                    IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                    int?bulkCopyTimeout = null,
                                    int?batchSize       = null,
                                    BulkImportIdentityBehavior identityBehavior = default,
                                    BulkImportPseudoTableType pseudoTableType   = default,
                                    NpgsqlTransaction transaction = null) =>
 BinaryBulkInsert(connection: connection,
                  tableName: table?.TableName,
                  table: table,
                  rowState: rowState,
                  mappings: mappings,
                  bulkCopyTimeout: bulkCopyTimeout,
                  batchSize: batchSize,
                  identityBehavior: identityBehavior,
                  pseudoTableType: pseudoTableType,
                  transaction: transaction);
Пример #15
0
 /// <summary>
 /// Delete the existing rows via entities by bulk. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <typeparam name="TEntity">The type of the entity.</typeparam>
 /// <param name="repository">The instance of <see cref="BaseRepository{TEntity, TDbConnection}"/> object.</param>
 /// <param name="entities">The list of entities to be bulk-deleted to the target table.
 /// This can be an <see cref="IEnumerable{T}"/> of the following objects (<typeparamref name="TEntity"/> (as class/model), <see cref="ExpandoObject"/>,
 /// <see cref="IDictionary{TKey, TValue}"/> (of <see cref="string"/>/<see cref="object"/>) and Anonymous Types).</param>
 /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the entities will be sent together in one-go.</param>
 /// <param name="keepIdentity">A value that indicates whether the existing identity property values from the entities will be kept during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <returns>The number of rows that has been deleted from the target table.</returns>
 public static int BinaryBulkDelete <TEntity>(this BaseRepository <TEntity, NpgsqlConnection> repository,
                                              IEnumerable <TEntity> entities,
                                              IEnumerable <Field> qualifiers = null,
                                              IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                              int?bulkCopyTimeout = null,
                                              int?batchSize       = null,
                                              bool keepIdentity   = true,
                                              BulkImportPseudoTableType pseudoTableType = default,
                                              NpgsqlTransaction transaction             = null)
     where TEntity : class =>
 repository.DbRepository.BinaryBulkDelete <TEntity>(tableName : ClassMappedNameCache.Get <TEntity>(),
                                                    entities : entities,
                                                    qualifiers : qualifiers,
                                                    mappings : mappings,
                                                    bulkCopyTimeout : bulkCopyTimeout,
                                                    batchSize : batchSize,
                                                    keepIdentity : keepIdentity,
                                                    pseudoTableType : pseudoTableType,
                                                    transaction : transaction);
Пример #16
0
        /// <summary>
        /// Update the existing rows via <see cref="DataTable"/> by bulk in an asynchronous way. Underneath this operation is a call directly to the existing
        /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
        /// </summary>
        /// <param name="repository">The instance of <see cref="DbRepository{TDbConnection}"/> object.</param>
        /// <param name="tableName">The name of the target table from the database. If not specified, the <see cref="DataTable.TableName"/> property will be used.</param>
        /// <param name="table">The source <see cref="DataTable"/> object that contains the rows to be bulk-updated to the target table.</param>
        /// <param name="rowState">The state of the rows to be bulk-updated. If not specified, all the rows of the table will be used.</param>
        /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
        /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
        /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
        /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the rows of the table will be sent together in one-go.</param>
        /// <param name="keepIdentity">A value that indicates whether the existing identity property values from the entities will be kept during the operation.</param>
        /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
        /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
        /// <returns>The number of rows that has been updated into the target table.</returns>
        public static async Task <int> BinaryBulkUpdateAsync(this DbRepository <NpgsqlConnection> repository,
                                                             string tableName,
                                                             DataTable table,
                                                             DataRowState?rowState          = null,
                                                             IEnumerable <Field> qualifiers = null,
                                                             IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                             int?bulkCopyTimeout = null,
                                                             int?batchSize       = null,
                                                             bool keepIdentity   = false,
                                                             BulkImportPseudoTableType pseudoTableType = default,
                                                             NpgsqlTransaction transaction             = null,
                                                             CancellationToken cancellationToken       = default)
        {
            // Create a connection
            var connection = (transaction?.Connection ?? repository.CreateConnection());

            try
            {
                // Call the method
                return(await connection.BinaryBulkUpdateAsync(tableName : (tableName ?? table?.TableName),
                                                              table : table,
                                                              rowState : rowState,
                                                              qualifiers : qualifiers,
                                                              mappings : mappings,
                                                              bulkCopyTimeout : bulkCopyTimeout,
                                                              batchSize : batchSize,
                                                              keepIdentity : keepIdentity,
                                                              pseudoTableType : pseudoTableType,
                                                              transaction : transaction,
                                                              cancellationToken : cancellationToken));
            }
            finally
            {
                // Dispose the connection
                if (repository.ConnectionPersistency == ConnectionPersistency.PerCall)
                {
                    if (transaction == null)
                    {
                        connection.Dispose();
                    }
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Delete the existing rows via entities by bulk in an asynchronous way. Underneath this operation is a call directly to the existing
        /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="repository">The instance of <see cref="DbRepository{TDbConnection}"/> object.</param>
        /// <param name="tableName">The name of the target table from the database.</param>
        /// <param name="entities">The list of entities to be bulk-deleted to the target table.
        /// This can be an <see cref="IEnumerable{T}"/> of the following objects (<typeparamref name="TEntity"/> (as class/model), <see cref="ExpandoObject"/>,
        /// <see cref="IDictionary{TKey, TValue}"/> (of <see cref="string"/>/<see cref="object"/>) and Anonymous Types).</param>
        /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
        /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
        /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
        /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the rows of the table will be sent together in one-go.</param>
        /// <param name="keepIdentity">A value that indicates whether the existing identity property values from the entities will be kept during the operation.</param>
        /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
        /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
        /// <returns>The number of rows that has been deleted from the target table.</returns>
        public static async Task <int> BinaryBulkDeleteAsync <TEntity>(this DbRepository <NpgsqlConnection> repository,
                                                                       string tableName,
                                                                       IEnumerable <TEntity> entities,
                                                                       IEnumerable <Field> qualifiers = null,
                                                                       IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                                       int?bulkCopyTimeout = null,
                                                                       int?batchSize       = null,
                                                                       bool keepIdentity   = true,
                                                                       BulkImportPseudoTableType pseudoTableType = default,
                                                                       NpgsqlTransaction transaction             = null,
                                                                       CancellationToken cancellationToken       = default)
            where TEntity : class
        {
            // Create a connection
            var connection = (transaction?.Connection ?? repository.CreateConnection());

            try
            {
                // Call the method
                return(await connection.BinaryBulkDeleteAsync <TEntity>(tableName : (tableName ?? ClassMappedNameCache.Get <TEntity>()),
                                                                        entities : entities,
                                                                        qualifiers : qualifiers,
                                                                        mappings : mappings,
                                                                        bulkCopyTimeout : bulkCopyTimeout,
                                                                        batchSize : batchSize,
                                                                        keepIdentity : keepIdentity,
                                                                        pseudoTableType : pseudoTableType,
                                                                        transaction : transaction,
                                                                        cancellationToken : cancellationToken));
            }
            finally
            {
                // Dispose the connection
                if (repository.ConnectionPersistency == ConnectionPersistency.PerCall)
                {
                    if (transaction == null)
                    {
                        connection.Dispose();
                    }
                }
            }
        }
Пример #18
0
 /// <summary>
 /// Inserts a list of entities into the target table by bulk in an asynchronous way. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <typeparam name="TEntity">The type of the entity.</typeparam>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="entities">The list of entities to be bulk-inserted to the target table.
 /// This can be an <see cref="IEnumerable{T}"/> of the following objects (<typeparamref name="TEntity"/> (as class/model), <see cref="ExpandoObject"/>,
 /// <see cref="IDictionary{TKey, TValue}"/> (of <see cref="string"/>/<see cref="object"/>) and Anonymous Types).</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not an entity mapping)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the entities will be sent together in one-go.</param>
 /// <param name="identityBehavior">The behavior of how the identity column would work during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
 /// <returns>The number of rows that has been inserted into the target table.</returns>
 public static Task <int> BinaryBulkInsertAsync <TEntity>(this NpgsqlConnection connection,
                                                          IEnumerable <TEntity> entities,
                                                          IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                          int?bulkCopyTimeout = null,
                                                          int?batchSize       = null,
                                                          BulkImportIdentityBehavior identityBehavior = default,
                                                          BulkImportPseudoTableType pseudoTableType   = default,
                                                          NpgsqlTransaction transaction       = null,
                                                          CancellationToken cancellationToken = default)
     where TEntity : class =>
 BinaryBulkInsertAsync <TEntity>(connection : connection,
                                 tableName : ClassMappedNameCache.Get <TEntity>(),
                                 entities : entities,
                                 mappings : mappings,
                                 bulkCopyTimeout : bulkCopyTimeout,
                                 batchSize : batchSize,
                                 identityBehavior : identityBehavior,
                                 pseudoTableType : pseudoTableType,
                                 transaction : transaction,
                                 cancellationToken : cancellationToken);
Пример #19
0
 /// <summary>
 /// Update the existing rows via <see cref="DbDataReader"/> by bulk in an asynchronous way. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="tableName">The name of the target table from the database.</param>
 /// <param name="reader">The instance of <see cref="DbDataReader"/> object that contains the rows to be bulk-updated to the target table.</param>
 /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="keepIdentity">A value that indicates whether the existing identity property values from the entities will be kept during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
 /// <returns>The number of rows that has been updated into the target table.</returns>
 public static async Task <int> BinaryBulkUpdateAsync(this NpgsqlConnection connection,
                                                      string tableName,
                                                      DbDataReader reader,
                                                      IEnumerable <Field> qualifiers = null,
                                                      IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                      int?bulkCopyTimeout = null,
                                                      bool keepIdentity   = false,
                                                      BulkImportPseudoTableType pseudoTableType = default,
                                                      NpgsqlTransaction transaction             = null,
                                                      CancellationToken cancellationToken       = default) =>
 await BinaryBulkUpdateBaseAsync(connection : connection,
                                 tableName : tableName,
                                 reader : reader,
                                 qualifiers : qualifiers,
                                 mappings : mappings,
                                 bulkCopyTimeout : bulkCopyTimeout,
                                 keepIdentity : keepIdentity,
                                 pseudoTableType : pseudoTableType,
                                 transaction : transaction,
                                 cancellationToken : cancellationToken);
Пример #20
0
 /// <summary>
 /// Update the existing rows via entities by bulk in an asynchronous way. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <typeparam name="TEntity">The type of the entity.</typeparam>
 /// <param name="repository">The instance of <see cref="BaseRepository{TEntity, TDbConnection}"/> object.</param>
 /// <param name="entities">The list of entities to be bulk-updated to the target table.
 /// This can be an <see cref="IEnumerable{T}"/> of the following objects (<typeparamref name="TEntity"/> (as class/model), <see cref="ExpandoObject"/>,
 /// <see cref="IDictionary{TKey, TValue}"/> (of <see cref="string"/>/<see cref="object"/>) and Anonymous Types).</param>
 /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the entities will be sent together in one-go.</param>
 /// <param name="keepIdentity">A value that indicates whether the existing identity property values from the entities will be kept during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
 /// <returns>The number of rows that has been updated into the target table.</returns>
 public static async Task <int> BinaryBulkUpdateAsync <TEntity>(this BaseRepository <TEntity, NpgsqlConnection> repository,
                                                                IEnumerable <TEntity> entities,
                                                                IEnumerable <Field> qualifiers = null,
                                                                IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                                int?bulkCopyTimeout = null,
                                                                int?batchSize       = null,
                                                                bool keepIdentity   = false,
                                                                BulkImportPseudoTableType pseudoTableType = default,
                                                                NpgsqlTransaction transaction             = null,
                                                                CancellationToken cancellationToken       = default)
     where TEntity : class =>
 await repository.DbRepository.BinaryBulkUpdateAsync <TEntity>(tableName : ClassMappedNameCache.Get <TEntity>(),
                                                               entities : entities,
                                                               qualifiers : qualifiers,
                                                               mappings : mappings,
                                                               bulkCopyTimeout : bulkCopyTimeout,
                                                               batchSize : batchSize,
                                                               keepIdentity : keepIdentity,
                                                               pseudoTableType : pseudoTableType,
                                                               transaction : transaction,
                                                               cancellationToken : cancellationToken);
Пример #21
0
 /// <summary>
 /// Update the existing rows via entities by bulk. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <typeparam name="TEntity">The type of the entity.</typeparam>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="tableName">The name of the target table from the database.</param>
 /// <param name="entities">The list of entities to be bulk-updated to the target table.
 /// This can be an <see cref="IEnumerable{T}"/> of the following objects (<typeparamref name="TEntity"/> (as class/model), <see cref="ExpandoObject"/>,
 /// <see cref="IDictionary{TKey, TValue}"/> (of <see cref="string"/>/<see cref="object"/>) and Anonymous Types).</param>
 /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the rows of the table will be sent together in one-go.</param>
 /// <param name="keepIdentity">A value that indicates whether the existing identity property values from the entities will be kept during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <returns>The number of rows that has been updated into the target table.</returns>
 public static int BinaryBulkUpdate <TEntity>(this NpgsqlConnection connection,
                                              string tableName,
                                              IEnumerable <TEntity> entities,
                                              IEnumerable <Field> qualifiers = null,
                                              IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                              int?bulkCopyTimeout = null,
                                              int?batchSize       = null,
                                              bool keepIdentity   = false,
                                              BulkImportPseudoTableType pseudoTableType = default,
                                              NpgsqlTransaction transaction             = null)
     where TEntity : class =>
 BinaryBulkUpdateBase <TEntity>(connection : connection,
                                tableName : (tableName ?? ClassMappedNameCache.Get <TEntity>()),
                                entities : entities,
                                qualifiers : qualifiers,
                                mappings : mappings,
                                bulkCopyTimeout : bulkCopyTimeout,
                                batchSize : batchSize,
                                keepIdentity : keepIdentity,
                                pseudoTableType : pseudoTableType,
                                transaction : transaction);
Пример #22
0
 /// <summary>
 /// Delete the existing rows via <see cref="DataTable"/> by bulk. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="table">The source <see cref="DataTable"/> object that contains the rows to be bulk-deleted to the target table.</param>
 /// <param name="rowState">The state of the rows to be bulk-deleted. If not specified, all the rows of the table will be used.</param>
 /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the rows of the table will be sent together in one-go.</param>
 /// <param name="keepIdentity">A value that indicates whether the existing identity property values from the <see cref="DataTable"/> will be kept during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <returns>The number of rows that has been deleted from the target table.</returns>
 public static int BinaryBulkDelete(this NpgsqlConnection connection,
                                    DataTable table,
                                    DataRowState?rowState          = null,
                                    IEnumerable <Field> qualifiers = null,
                                    IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                    int?bulkCopyTimeout = null,
                                    int?batchSize       = null,
                                    bool keepIdentity   = true,
                                    BulkImportPseudoTableType pseudoTableType = default,
                                    NpgsqlTransaction transaction             = null) =>
 BinaryBulkDelete(connection: connection,
                  tableName: table?.TableName,
                  table: table,
                  rowState: rowState,
                  qualifiers: qualifiers,
                  mappings: mappings,
                  bulkCopyTimeout: bulkCopyTimeout,
                  batchSize: batchSize,
                  keepIdentity: keepIdentity,
                  pseudoTableType: pseudoTableType,
                  transaction: transaction);
Пример #23
0
 /// <summary>
 /// Merges a list of entities into the target table by bulk. This operation is inserting a row (if not present), and updating an existing
 /// row (if present), based on the given qualifiers. It uses either of the 'INSERT/UPDATE' and 'ON CONFLICT DO UPDATE' commands of the
 /// PostgreSQL based on the value passed to the '<paramref name="mergeCommandType"/>' argument. Underneath this operation is a call directly to
 /// the existing <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the customized 'BinaryBulkInsert' extended method.
 /// </summary>
 /// <typeparam name="TEntity">The type of the entity.</typeparam>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="entities">The list of entities to be bulk-merged to the target table.
 /// This can be an <see cref="IEnumerable{T}"/> of the following objects (<typeparamref name="TEntity"/> (as class/model), <see cref="ExpandoObject"/>,
 /// <see cref="IDictionary{TKey, TValue}"/> (of <see cref="string"/>/<see cref="object"/>) and Anonymous Types).</param>
 /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the entities will be sent together in one-go.</param>
 /// <param name="identityBehavior">The behavior of how the identity column would work during the operation.</param>
 /// <param name="mergeCommandType">The value that defines the type of command to be used during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <returns>The number of rows that has been merged into the target table.</returns>
 public static int BinaryBulkMerge <TEntity>(this NpgsqlConnection connection,
                                             IEnumerable <TEntity> entities,
                                             IEnumerable <Field> qualifiers = null,
                                             IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                             int?bulkCopyTimeout = null,
                                             int?batchSize       = null,
                                             BulkImportIdentityBehavior identityBehavior = default,
                                             BulkImportMergeCommandType mergeCommandType = default,
                                             BulkImportPseudoTableType pseudoTableType   = default,
                                             NpgsqlTransaction transaction = null)
     where TEntity : class =>
 BinaryBulkMerge <TEntity>(connection : connection,
                           tableName : ClassMappedNameCache.Get <TEntity>(),
                           entities : entities,
                           qualifiers : qualifiers,
                           mappings : mappings,
                           bulkCopyTimeout : bulkCopyTimeout,
                           batchSize : batchSize,
                           identityBehavior : identityBehavior,
                           mergeCommandType : mergeCommandType,
                           pseudoTableType : pseudoTableType,
                           transaction : transaction);
Пример #24
0
        /// <summary>
        /// Inserts a list of entities into the target table by bulk. Underneath this operation is a call directly to the existing
        /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="repository">The instance of <see cref="DbRepository{TDbConnection}"/> object.</param>
        /// <param name="tableName">The name of the target table from the database.</param>
        /// <param name="entities">The list of entities to be bulk-inserted to the target table.
        /// This can be an <see cref="IEnumerable{T}"/> of the following objects (<typeparamref name="TEntity"/> (as class/model), <see cref="ExpandoObject"/>,
        /// <see cref="IDictionary{TKey, TValue}"/> (of <see cref="string"/>/<see cref="object"/>) and Anonymous Types).</param>
        /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not an entity mapping)</param>
        /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
        /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the rows of the table will be sent together in one-go.</param>
        /// <param name="identityBehavior">The behavior of how the identity column would work during the operation.</param>
        /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
        /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
        /// <returns>The number of rows that has been inserted into the target table.</returns>
        public static int BinaryBulkInsert <TEntity>(this DbRepository <NpgsqlConnection> repository,
                                                     string tableName,
                                                     IEnumerable <TEntity> entities,
                                                     IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                     int?bulkCopyTimeout = null,
                                                     int?batchSize       = null,
                                                     BulkImportIdentityBehavior identityBehavior = default,
                                                     BulkImportPseudoTableType pseudoTableType   = default,
                                                     NpgsqlTransaction transaction = null)
            where TEntity : class
        {
            // Create a connection
            var connection = (transaction?.Connection ?? repository.CreateConnection());

            try
            {
                // Call the method
                return(connection.BinaryBulkInsert <TEntity>(tableName: (tableName ?? ClassMappedNameCache.Get <TEntity>()),
                                                             entities: entities,
                                                             mappings: mappings,
                                                             bulkCopyTimeout: bulkCopyTimeout,
                                                             batchSize: batchSize,
                                                             identityBehavior: identityBehavior,
                                                             pseudoTableType: pseudoTableType,
                                                             transaction: transaction));
            }
            finally
            {
                // Dispose the connection
                if (repository.ConnectionPersistency == ConnectionPersistency.PerCall)
                {
                    if (transaction == null)
                    {
                        connection.Dispose();
                    }
                }
            }
        }
Пример #25
0
 /// <summary>
 /// Update the existing rows via <see cref="DataTable"/> by bulk in an asynchronous way. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <param name="connection">The current connection object in used.</param>
 /// <param name="table">The source <see cref="DataTable"/> object that contains the rows to be bulk-updated to the target table.</param>
 /// <param name="rowState">The state of the rows to be bulk-updated. If not specified, all the rows of the table will be used.</param>
 /// <param name="qualifiers">The list of qualifier fields to be used during the operation. Ensure to target the indexed columns to make the execution more performant. If not specified, the primary key will be used.</param>
 /// <param name="mappings">The list of mappings to be used. If not specified, only the matching properties/columns from the target table will be used. (This is not the entity mappings, but is working on top of it)</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the rows of the table will be sent together in one-go.</param>
 /// <param name="keepIdentity">A value that indicates whether the existing identity property values from the entities will be kept during the operation.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
 /// <returns>The number of rows that has been updated into the target table.</returns>
 public static Task <int> BinaryBulkUpdateAsync(this NpgsqlConnection connection,
                                                DataTable table,
                                                DataRowState?rowState          = null,
                                                IEnumerable <Field> qualifiers = null,
                                                IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                int?bulkCopyTimeout = null,
                                                int?batchSize       = null,
                                                bool keepIdentity   = false,
                                                BulkImportPseudoTableType pseudoTableType = default,
                                                NpgsqlTransaction transaction             = null,
                                                CancellationToken cancellationToken       = default) =>
 BinaryBulkUpdateAsync(connection: connection,
                       tableName: table?.TableName,
                       table: table,
                       rowState: rowState,
                       qualifiers: qualifiers,
                       mappings: mappings,
                       bulkCopyTimeout: bulkCopyTimeout,
                       batchSize: batchSize,
                       keepIdentity: keepIdentity,
                       pseudoTableType: pseudoTableType,
                       transaction: transaction,
                       cancellationToken: cancellationToken);
Пример #26
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="bulkCopyTimeout"></param>
        /// <param name="dbFields"></param>
        /// <param name="getPseudoTableName"></param>
        /// <param name="getMappings"></param>
        /// <param name="binaryImport"></param>
        /// <param name="getMergeToPseudoCommandText"></param>
        /// <param name="setIdentities"></param>
        /// <param name="qualifiers"></param>
        /// <param name="isBinaryBulkInsert"></param>
        /// <param name="identityBehavior"></param>
        /// <param name="pseudoTableType"></param>
        /// <param name="dbSetting"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        private static int PseudoBasedBinaryImport(this NpgsqlConnection connection,
                                                   string tableName,
                                                   int?bulkCopyTimeout,
                                                   IEnumerable <DbField> dbFields,
                                                   Func <string> getPseudoTableName,
                                                   Func <IEnumerable <NpgsqlBulkInsertMapItem> > getMappings,
                                                   Func <string, int> binaryImport,
                                                   Func <string> getMergeToPseudoCommandText,
                                                   Action <IEnumerable <IdentityResult> > setIdentities,
                                                   IEnumerable <Field> qualifiers,
                                                   bool isBinaryBulkInsert,
                                                   BulkImportIdentityBehavior identityBehavior,
                                                   BulkImportPseudoTableType pseudoTableType,
                                                   IDbSetting dbSetting,
                                                   NpgsqlTransaction transaction)
        {
            string pseudoTableName = null;
            var    withPseudoTable = identityBehavior == BulkImportIdentityBehavior.ReturnIdentity ||
                                     isBinaryBulkInsert == false;

            try
            {
                // Mappings
                var mappings = getMappings?.Invoke();

                // Create (TEMP)
                if (withPseudoTable)
                {
                    pseudoTableName = getPseudoTableName?.Invoke();

                    DropPseudoTable(connection,
                                    pseudoTableName,
                                    bulkCopyTimeout,
                                    transaction);

                    CreatePseudoTable(connection,
                                      tableName,
                                      pseudoTableName,
                                      mappings,
                                      bulkCopyTimeout,
                                      identityBehavior,
                                      pseudoTableType,
                                      dbSetting,
                                      transaction);
                }

                // Import
                var result = binaryImport?.Invoke(pseudoTableName ?? tableName);

                // Create Index
                if (isBinaryBulkInsert == false && withPseudoTable)
                {
                    qualifiers = qualifiers?.Any() == true ? qualifiers :
                                 dbFields?.FirstOrDefault(dbField => dbField.IsPrimary).AsField().AsEnumerable();

                    CreatePseudoTableIndex(connection,
                                           pseudoTableName,
                                           qualifiers,
                                           bulkCopyTimeout,
                                           dbSetting,
                                           transaction);
                }

                // Merge/Update/Delete
                if (withPseudoTable)
                {
                    var identityResults = MergeToPseudoTableWithIdentityResults(connection,
                                                                                getMergeToPseudoCommandText,
                                                                                bulkCopyTimeout,
                                                                                transaction)?.AsList();

                    if (identityBehavior == BulkImportIdentityBehavior.ReturnIdentity)
                    {
                        setIdentities?.Invoke(identityResults);
                    }

                    result = identityResults.Count();
                }

                // Return
                return(result.GetValueOrDefault());
            }
            finally
            {
                if (withPseudoTable)
                {
                    DropPseudoTable(connection,
                                    pseudoTableName,
                                    bulkCopyTimeout,
                                    transaction);
                }
            }
        }
Пример #27
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="reader"></param>
        /// <param name="qualifiers"></param>
        /// <param name="mappings"></param>
        /// <param name="bulkCopyTimeout"></param>
        /// <param name="keepIdentity"></param>
        /// <param name="pseudoTableType"></param>
        /// <param name="transaction"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private static async Task <int> BinaryBulkDeleteBaseAsync(this NpgsqlConnection connection,
                                                                  string tableName,
                                                                  DbDataReader reader,
                                                                  IEnumerable <Field> qualifiers = null,
                                                                  IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                                  int?bulkCopyTimeout = null,
                                                                  bool keepIdentity   = true,
                                                                  BulkImportPseudoTableType pseudoTableType = default,
                                                                  NpgsqlTransaction transaction             = null,
                                                                  CancellationToken cancellationToken       = default)
        {
            var dbSetting = connection.GetDbSetting();
            var dbFields  = await DbFieldCache.GetAsync(connection, tableName, transaction, cancellationToken);

            var pseudoTableName  = tableName;
            var identityBehavior = keepIdentity ? BulkImportIdentityBehavior.KeepIdentity : BulkImportIdentityBehavior.Unspecified;

            return(await PseudoBasedBinaryImportAsync(connection,
                                                      tableName,
                                                      bulkCopyTimeout,
                                                      dbFields,

                                                      // getPseudoTableName
                                                      () =>
                                                      pseudoTableName = GetBinaryBulkDeletePseudoTableName(tableName, dbSetting),

                                                      // getMappings
                                                      () =>
            {
                var includeIdentity = identityBehavior == BulkImportIdentityBehavior.KeepIdentity;
                var includePrimary = true;

                return mappings = mappings?.Any() == true ? mappings :
                                  GetMappings(reader,
                                              dbFields,
                                              includePrimary,
                                              includeIdentity,
                                              dbSetting);
            },

                                                      // binaryImport
                                                      async (tableName) =>
                                                      await connection.BinaryImportAsync(tableName,
                                                                                         reader,
                                                                                         mappings,
                                                                                         dbFields,
                                                                                         bulkCopyTimeout,
                                                                                         identityBehavior,
                                                                                         dbSetting,
                                                                                         transaction,
                                                                                         cancellationToken),

                                                      // getDeleteToPseudoCommandText
                                                      () =>
                                                      GetDeleteCommandText(pseudoTableName,
                                                                           tableName,
                                                                           mappings.Select(mapping => new Field(mapping.DestinationColumn)),
                                                                           qualifiers,
                                                                           dbFields.FirstOrDefault(dbField => dbField.IsPrimary)?.AsField(),
                                                                           dbFields.FirstOrDefault(dbField => dbField.IsIdentity)?.AsField(),
                                                                           identityBehavior,
                                                                           dbSetting),

                                                      // setIdentities
                                                      null,

                                                      qualifiers,
                                                      false,
                                                      identityBehavior : identityBehavior,
                                                      pseudoTableType : pseudoTableType,
                                                      dbSetting,
                                                      transaction : transaction,
                                                      cancellationToken));
        }
Пример #28
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="entities"></param>
        /// <param name="qualifiers"></param>
        /// <param name="mappings"></param>
        /// <param name="bulkCopyTimeout"></param>
        /// <param name="batchSize"></param>
        /// <param name="keepIdentity"></param>
        /// <param name="pseudoTableType"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        private static int BinaryBulkDeleteBase <TEntity>(this NpgsqlConnection connection,
                                                          string tableName,
                                                          IEnumerable <TEntity> entities,
                                                          IEnumerable <Field> qualifiers = null,
                                                          IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                          int?bulkCopyTimeout = null,
                                                          int?batchSize       = null,
                                                          bool keepIdentity   = true,
                                                          BulkImportPseudoTableType pseudoTableType = default,
                                                          NpgsqlTransaction transaction             = null)
            where TEntity : class
        {
            var entityType       = entities?.First()?.GetType() ?? typeof(TEntity); // Solving the anonymous types
            var isDictionary     = entityType.IsDictionaryStringObject();
            var dbSetting        = connection.GetDbSetting();
            var dbFields         = DbFieldCache.Get(connection, tableName, transaction);
            var pseudoTableName  = tableName;
            var identityBehavior = keepIdentity ? BulkImportIdentityBehavior.KeepIdentity : BulkImportIdentityBehavior.Unspecified;

            return(PseudoBasedBinaryImport(connection,
                                           tableName,
                                           bulkCopyTimeout,
                                           dbFields,

                                           // getPseudoTableName
                                           () =>
                                           pseudoTableName = GetBinaryBulkDeletePseudoTableName(tableName ?? ClassMappedNameCache.Get <TEntity>(), dbSetting),

                                           // getMappings
                                           () =>
            {
                var includeIdentity = identityBehavior == BulkImportIdentityBehavior.KeepIdentity;
                var includePrimary = true;

                return mappings = mappings?.Any() == true ? mappings :
                                  isDictionary ?
                                  GetMappings(entities?.First() as IDictionary <string, object>,
                                              dbFields,
                                              includePrimary,
                                              includeIdentity,
                                              dbSetting) :
                                  GetMappings(dbFields,
                                              PropertyCache.Get(entityType),
                                              includePrimary,
                                              includeIdentity,
                                              dbSetting);
            },

                                           // binaryImport
                                           (tableName) =>
                                           connection.BinaryImport <TEntity>(tableName,
                                                                             entities,
                                                                             mappings,
                                                                             dbFields,
                                                                             bulkCopyTimeout,
                                                                             batchSize,
                                                                             identityBehavior,
                                                                             dbSetting,
                                                                             transaction),

                                           // getDeleteToPseudoCommandText
                                           () =>
                                           GetDeleteCommandText(pseudoTableName,
                                                                tableName,
                                                                mappings.Select(mapping => new Field(mapping.DestinationColumn)),
                                                                qualifiers,
                                                                dbFields.FirstOrDefault(dbField => dbField.IsPrimary)?.AsField(),
                                                                dbFields.FirstOrDefault(dbField => dbField.IsIdentity)?.AsField(),
                                                                identityBehavior,
                                                                dbSetting),

                                           // setIdentities
                                           (identityResults) =>
                                           SetIdentities(entityType, entities, dbFields, identityResults, dbSetting),

                                           qualifiers,
                                           false,
                                           identityBehavior,
                                           pseudoTableType,
                                           dbSetting,
                                           transaction));
        }
Пример #29
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="tableName"></param>
        /// <param name="table"></param>
        /// <param name="rowState"></param>
        /// <param name="qualifiers"></param>
        /// <param name="mappings"></param>
        /// <param name="bulkCopyTimeout"></param>
        /// <param name="batchSize"></param>
        /// <param name="keepIdentity"></param>
        /// <param name="pseudoTableType"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        private static int BinaryBulkDeleteBase(this NpgsqlConnection connection,
                                                string tableName,
                                                DataTable table,
                                                DataRowState?rowState          = null,
                                                IEnumerable <Field> qualifiers = null,
                                                IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                int?bulkCopyTimeout = null,
                                                int?batchSize       = null,
                                                bool keepIdentity   = true,
                                                BulkImportPseudoTableType pseudoTableType = default,
                                                NpgsqlTransaction transaction             = null)
        {
            var dbSetting        = connection.GetDbSetting();
            var dbFields         = DbFieldCache.Get(connection, tableName, transaction);
            var pseudoTableName  = tableName;
            var identityBehavior = keepIdentity ? BulkImportIdentityBehavior.KeepIdentity : BulkImportIdentityBehavior.Unspecified;

            return(PseudoBasedBinaryImport(connection,
                                           tableName,
                                           bulkCopyTimeout,
                                           dbFields,

                                           // getPseudoTableName
                                           () =>
                                           pseudoTableName = GetBinaryBulkDeletePseudoTableName(tableName, dbSetting),

                                           // getMappings
                                           () =>
            {
                var includeIdentity = identityBehavior == BulkImportIdentityBehavior.KeepIdentity;
                var includePrimary = true;

                return mappings = mappings?.Any() == true ? mappings :
                                  GetMappings(table,
                                              dbFields,
                                              includePrimary,
                                              includeIdentity,
                                              dbSetting);
            },

                                           // binaryImport
                                           (tableName) =>
                                           connection.BinaryImport(tableName,
                                                                   table,
                                                                   rowState,
                                                                   mappings,
                                                                   dbFields,
                                                                   bulkCopyTimeout,
                                                                   batchSize,
                                                                   identityBehavior,
                                                                   dbSetting,
                                                                   transaction),

                                           // getDeleteToPseudoCommandText
                                           () =>
                                           GetDeleteCommandText(pseudoTableName,
                                                                tableName,
                                                                mappings.Select(mapping => new Field(mapping.DestinationColumn)),
                                                                qualifiers,
                                                                dbFields.FirstOrDefault(dbField => dbField.IsPrimary)?.AsField(),
                                                                dbFields.FirstOrDefault(dbField => dbField.IsIdentity)?.AsField(),
                                                                identityBehavior,
                                                                dbSetting),

                                           // setIdentities
                                           (identityResults) =>
                                           SetDataTableIdentities(table, dbFields, identityResults, dbSetting),

                                           qualifiers,
                                           false,
                                           identityBehavior: identityBehavior,
                                           pseudoTableType: pseudoTableType,
                                           dbSetting,
                                           transaction: transaction));
        }
Пример #30
0
 /// <summary>
 /// Delete the existing rows by bulk via a list of primary keys in an asynchronous way. Underneath this operation is a call directly to the existing
 /// <see cref="NpgsqlConnection.BeginBinaryExport(string)"/> method via the 'BinaryImport' extended method.
 /// </summary>
 /// <typeparam name="TEntity">The type of the entity.</typeparam>
 /// <typeparam name="TPrimaryKey">The type of the primary key.</typeparam>
 /// <param name="repository">The instance of <see cref="BaseRepository{TEntity, TDbConnection}"/> object.</param>
 /// <param name="tableName">The name of the target table from the database.</param>
 /// <param name="primaryKeys">The list of primary keys that targets the rows to be bulk-deleted from the target table.</param>
 /// <param name="bulkCopyTimeout">The timeout expiration of the operation (see <see cref="NpgsqlBinaryImporter.Timeout"/>).</param>
 /// <param name="batchSize">The size per batch to be sent to the database. If not specified, all the primary keys will be sent together in one-go.</param>
 /// <param name="pseudoTableType">The value that defines whether an actual or temporary table will be created for the pseudo-table.</param>
 /// <param name="transaction">The current transaction object in used. If not specified, an implicit transaction will be created and used.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
 /// <returns>The number of rows that has been deleted from the target table.</returns>
 public static async Task <int> BinaryBulkDeleteByKeyAsync <TEntity, TPrimaryKey>(this BaseRepository <TEntity, NpgsqlConnection> repository,
                                                                                  string tableName,
                                                                                  IEnumerable <TPrimaryKey> primaryKeys,
                                                                                  int?bulkCopyTimeout = null,
                                                                                  int?batchSize       = null,
                                                                                  BulkImportPseudoTableType pseudoTableType = default,
                                                                                  NpgsqlTransaction transaction             = null,
                                                                                  CancellationToken cancellationToken       = default)
     where TEntity : class =>
 await repository.DbRepository.BinaryBulkDeleteByKeyAsync <TPrimaryKey>(tableName : tableName,
                                                                        primaryKeys : primaryKeys,
                                                                        bulkCopyTimeout : bulkCopyTimeout,
                                                                        batchSize : batchSize,
                                                                        pseudoTableType : pseudoTableType,
                                                                        transaction : transaction,
                                                                        cancellationToken : cancellationToken);