Ejemplo n.º 1
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="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>
        /// <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 DbRepository <NpgsqlConnection> repository,
                                                             string tableName,
                                                             DbDataReader reader,
                                                             IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                                             int?bulkCopyTimeout = null,
                                                             BulkImportIdentityBehavior identityBehavior = default,
                                                             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.BinaryBulkInsertAsync(tableName : tableName,
                                                              reader : reader,
                                                              mappings : mappings,
                                                              bulkCopyTimeout : bulkCopyTimeout,
                                                              identityBehavior : identityBehavior,
                                                              pseudoTableType : pseudoTableType,
                                                              transaction : transaction,
                                                              cancellationToken : cancellationToken));
            }
            finally
            {
                // Dispose the connection
                if (repository.ConnectionPersistency == ConnectionPersistency.PerCall)
                {
                    if (transaction == null)
                    {
                        connection.Dispose();
                    }
                }
            }
        }
Ejemplo n.º 2
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="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 DbRepository <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
        {
            // Create a connection
            var connection = (transaction?.Connection ?? repository.CreateConnection());

            try
            {
                // Call the method
                return(connection.BinaryBulkInsert <TEntity>(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();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Update 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="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-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>
        /// <returns>The number of rows that has been updated into the target table.</returns>
        public static int BinaryBulkUpdate(this DbRepository <NpgsqlConnection> repository,
                                           string tableName,
                                           DbDataReader reader,
                                           IEnumerable <Field> qualifiers = null,
                                           IEnumerable <NpgsqlBulkInsertMapItem> mappings = null,
                                           int?bulkCopyTimeout = null,
                                           bool keepIdentity   = false,
                                           BulkImportPseudoTableType pseudoTableType = default,
                                           NpgsqlTransaction transaction             = null)
        {
            // Create a connection
            var connection = (transaction?.Connection ?? repository.CreateConnection());

            try
            {
                // Call the method
                return(connection.BinaryBulkUpdate(tableName: tableName,
                                                   reader: reader,
                                                   qualifiers: qualifiers,
                                                   mappings: mappings,
                                                   bulkCopyTimeout: bulkCopyTimeout,
                                                   keepIdentity: keepIdentity,
                                                   pseudoTableType: pseudoTableType,
                                                   transaction: transaction));
            }
            finally
            {
                // Dispose the connection
                if (repository.ConnectionPersistency == ConnectionPersistency.PerCall)
                {
                    if (transaction == null)
                    {
                        connection.Dispose();
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Bulk insert an instance of <see cref="DbDataReader"/> object into the database in an asynchronous way.
        /// </summary>
        /// <param name="repository">The instance of <see cref="DbRepository{TDbConnection}"/> object.</param>
        /// <param name="tableName">The target table for bulk-insert operation.</param>
        /// <param name="reader">The <see cref="DbDataReader"/> object to be used in the bulk-insert operation.</param>
        /// <param name="mappings">The list of the columns to be used for mappings. If this parameter is not set, then all columns will be used for mapping.</param>
        /// <param name="options">The bulk-copy options to be used.</param>
        /// <param name="batchSize">The size per batch to be used.</param>
        /// <param name="transaction">The transaction to be used.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
        /// <returns>The number of rows affected by the execution.</returns>
        public static async Task <int> BulkInsertAsync(this DbRepository <SqlConnection> repository,
                                                       string tableName,
                                                       DbDataReader reader,
                                                       IEnumerable <BulkInsertMapItem> mappings = null,
                                                       SqlBulkCopyOptions?options = null,
                                                       int?batchSize = null,
                                                       SqlTransaction transaction          = null,
                                                       CancellationToken cancellationToken = default)
        {
            // Create a connection
            var connection = (transaction?.Connection ?? repository.CreateConnection());

            try
            {
                // Call the method
                return(await connection.BulkInsertAsync(tableName : tableName,
                                                        reader : reader,
                                                        mappings : mappings,
                                                        options : options,
                                                        bulkCopyTimeout : repository.CommandTimeout,
                                                        batchSize : batchSize,
                                                        transaction : transaction,
                                                        cancellationToken : cancellationToken));
            }
            finally
            {
                // Dispose the connection
                if (repository.ConnectionPersistency == ConnectionPersistency.PerCall)
                {
                    if (transaction == null)
                    {
                        connection.Dispose();
                    }
                }
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Inserts a new data in the database in an asynchronous way.
 /// </summary>
 /// <typeparam name="TResult">The target type of the result.</typeparam>
 /// <param name="entity">The data entity object to be inserted.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The value of the identity field if present, otherwise, the value of primary field.</returns>
 public Task <TResult> InsertAsync <TResult>(TEntity entity,
                                             IDbTransaction transaction = null)
 {
     return(DbRepository.InsertAsync <TEntity, TResult>(entity: entity,
                                                        transaction: transaction));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Merges a data entity object into an existing data in the database in an asynchronous way.
 /// </summary>
 /// <param name="entity">The object to be merged.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The value of the identity field if present, otherwise, the value of primary field.</returns>
 public Task <object> MergeAsync(TEntity entity,
                                 IDbTransaction transaction = null)
 {
     return(DbRepository.MergeAsync <TEntity>(entity: entity,
                                              transaction: transaction));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Counts all the data from the database in an asynchronous way.
 /// </summary>
 /// <param name="hints">The table hints to be used. See <see cref="SqlServerTableHints"/> class.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected by the execution.</returns>
 public Task <long> CountAllAsync(string hints = null,
                                  IDbTransaction transaction = null)
 {
     return(DbRepository.CountAllAsync <TEntity>(hints: hints,
                                                 transaction: transaction));
 }
Ejemplo n.º 8
0
        public static void Main(string[] args)
        {
            var repository = new DbRepository <SqlConnection>("ConnectionString");

            repository.Query <Customer>(new { Id = 10045 }, recursive: true);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Updates an existing data in the database.
 /// </summary>
 /// <param name="entity">The data entity object to be updated.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected by the execution.</returns>
 public int Update(TEntity entity,
                   IDbTransaction transaction = null)
 {
     return(DbRepository.Update <TEntity>(entity: entity,
                                          transaction: transaction));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Truncates a table from the database in an asynchronous way.
 /// </summary>
 /// <param name="transaction">The transaction to be used.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
 /// <returns>The number of rows affected.</returns>
 public Task <int> TruncateAsync(IDbTransaction transaction          = null,
                                 CancellationToken cancellationToken = default)
 {
     return(DbRepository.TruncateAsync <TEntity>(transaction: transaction,
                                                 cancellationToken: cancellationToken));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Deletes an existing data from the database in an asynchronous way.
 /// </summary>
 /// <param name="where">The query expression to be used.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected by the execution.</returns>
 public Task <int> DeleteAsync(QueryGroup where,
                               IDbTransaction transaction = null)
 {
     return(DbRepository.DeleteAsync <TEntity>(where : where,
                                               transaction: transaction));
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Deletes an existing data from the database in an asynchronous way.
 /// </summary>
 /// <param name="where">The query expression to be used.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected by the execution.</returns>
 public Task <int> DeleteAsync(IEnumerable <QueryField> where,
                               IDbTransaction transaction = null)
 {
     return(DbRepository.DeleteAsync <TEntity>(where : where,
                                               transaction: transaction));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Deletes an existing data from the database in an asynchronous way.
 /// </summary>
 /// <param name="whereOrPrimaryKey">The dynamic expression or the primary key value to be used.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected by the execution.</returns>
 public Task <int> DeleteAsync(object whereOrPrimaryKey,
                               IDbTransaction transaction = null)
 {
     return(DbRepository.DeleteAsync <TEntity>(whereOrPrimaryKey: whereOrPrimaryKey,
                                               transaction: transaction));
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Truncates a table from the database in an asynchronous way.
 /// </summary>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected.</returns>
 public Task <int> TruncateAsync(IDbTransaction transaction = null)
 {
     return(DbRepository.TruncateAsync <TEntity>(transaction: transaction));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Truncates a table from the database in an asynchronous way.
 /// </summary>
 /// <returns>The number of rows affected.</returns>
 public Task <int> TruncateAsync()
 {
     return(DbRepository.TruncateAsync <TEntity>());
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Truncates a table from the database.
 /// </summary>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected.</returns>
 public int Truncate(IDbTransaction transaction = null)
 {
     return(DbRepository.Truncate <TEntity>(transaction: transaction));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Truncates a table from the database in an asynchronous way.
 /// </summary>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
 /// <returns>The number of rows affected.</returns>
 public Task <int> TruncateAsync(CancellationToken cancellationToken = default)
 {
     return(DbRepository.TruncateAsync <TEntity>(cancellationToken: cancellationToken));
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Deletes an existing data from the database.
 /// </summary>
 /// <param name="whereOrPrimaryKey">The dynamic expression or the primary key value to be used.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected by the execution.</returns>
 public int Delete(object whereOrPrimaryKey,
                   IDbTransaction transaction = null)
 {
     return(DbRepository.Delete <TEntity>(whereOrPrimaryKey: whereOrPrimaryKey,
                                          transaction: transaction));
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Updates an existing data in the database in an asynchronous way.
 /// </summary>
 /// <param name="entity">The data entity object to be updated.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected by the execution.</returns>
 public Task <int> UpdateAsync(TEntity entity,
                               IDbTransaction transaction = null)
 {
     return(DbRepository.UpdateAsync <TEntity>(entity: entity,
                                               transaction: transaction));
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Deletes an existing data from the database.
 /// </summary>
 /// <param name="where">The query expression to be used.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected by the execution.</returns>
 public int Delete(Expression <Func <TEntity, bool> > where,
                   IDbTransaction transaction = null)
 {
     return(DbRepository.Delete <TEntity>(where : where,
                                          transaction: transaction));
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Delete all the rows from the table in an asynchronous way.
 /// </summary>
 /// <param name="hints">The table hints to be used.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows that has been deleted from the table.</returns>
 public Task <int> DeleteAllAsync(string hints = null,
                                  IDbTransaction transaction = null)
 {
     return(DbRepository.DeleteAllAsync <TEntity>());
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Deletes an existing data from the database.
 /// </summary>
 /// <param name="where">The query expression to be used.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected by the execution.</returns>
 public int Delete(IEnumerable <QueryField> where,
                   IDbTransaction transaction = null)
 {
     return(DbRepository.Delete <TEntity>(where : where,
                                          transaction: transaction));
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Counts all the table data from the database.
 /// </summary>
 /// <param name="hints">The table hints to be used. See <see cref="SqlServerTableHints"/> class.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>An integer value that holds the number of data from the database.</returns>
 public long CountAll(string hints = null,
                      IDbTransaction transaction = null)
 {
     return(DbRepository.CountAll <TEntity>(hints: hints,
                                            transaction: transaction));
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Deletes an existing data from the database.
 /// </summary>
 /// <param name="where">The query expression to be used.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected by the execution.</returns>
 public int Delete(QueryGroup where,
                   IDbTransaction transaction = null)
 {
     return(DbRepository.Delete <TEntity>(where : where,
                                          transaction: transaction));
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Truncates a table from the database in an asynchronous way.
 /// </summary>
 /// <returns>The number of rows affected.</returns>
 public Task <int> TruncateAsync()
 {
     return(DbRepository.TruncateAsync <TEntity>(transaction: null,
                                                 cancellationToken: CancellationToken.None));
 }
Ejemplo n.º 26
0
 /// <summary>
 /// Deletes all the data from the database.
 /// </summary>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows affected by the execution.</returns>
 public int DeleteAll(IDbTransaction transaction = null)
 {
     return(DbRepository.DeleteAll <TEntity>(transaction: transaction));
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Merges a data entity object into an existing data in the database.
 /// </summary>
 /// <param name="entity">The object to be merged.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The value of the identity field if present, otherwise, the value of primary field.</returns>
 public object Merge(TEntity entity,
                     IDbTransaction transaction = null)
 {
     return(DbRepository.Merge <TEntity>(entity: entity,
                                         transaction: transaction));
 }
Ejemplo n.º 28
0
 /// <summary>
 /// Truncates a table from the database.
 /// </summary>
 /// <returns>The number of rows affected.</returns>
 public int Truncate()
 {
     return(DbRepository.Truncate <TEntity>());
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Delete all the rows from the table.
 /// </summary>
 /// <param name="hints">The table hints to be used.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The number of rows that has been deleted from the table.</returns>
 public int DeleteAll(string hints = null,
                      IDbTransaction transaction = null)
 {
     return(DbRepository.DeleteAll <TEntity>(hints: hints,
                                             transaction: transaction));
 }
Ejemplo n.º 30
0
 /// <summary>
 /// Inserts a new data in the database.
 /// </summary>
 /// <param name="entity">The data entity object to be inserted.</param>
 /// <param name="transaction">The transaction to be used.</param>
 /// <returns>The value of the identity field if present, otherwise, the value of primary field.</returns>
 public object Insert(TEntity entity,
                      IDbTransaction transaction = null)
 {
     return(DbRepository.Insert <TEntity>(entity: entity,
                                          transaction: transaction));
 }