예제 #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();
                    }
                }
            }
        }
예제 #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();
                    }
                }
            }
        }
예제 #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();
                    }
                }
            }
        }
예제 #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();
                    }
                }
            }
        }
예제 #5
0
파일: Insert.cs 프로젝트: softempire/RepoDb
 /// <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));
 }
예제 #6
0
파일: Merge.cs 프로젝트: softempire/RepoDb
 /// <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));
 }
예제 #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));
 }
예제 #8
0
        public static void Main(string[] args)
        {
            var repository = new DbRepository <SqlConnection>("ConnectionString");

            repository.Query <Customer>(new { Id = 10045 }, recursive: true);
        }
예제 #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));
 }
예제 #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));
 }
예제 #11
0
파일: Delete.cs 프로젝트: softempire/RepoDb
 /// <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));
 }
예제 #12
0
파일: Delete.cs 프로젝트: softempire/RepoDb
 /// <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));
 }
예제 #13
0
파일: Delete.cs 프로젝트: softempire/RepoDb
 /// <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));
 }
예제 #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));
 }
예제 #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>());
 }
예제 #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));
 }
예제 #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));
 }
예제 #18
0
파일: Delete.cs 프로젝트: softempire/RepoDb
 /// <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));
 }
예제 #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));
 }
예제 #20
0
파일: Delete.cs 프로젝트: softempire/RepoDb
 /// <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));
 }
예제 #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>());
 }
예제 #22
0
파일: Delete.cs 프로젝트: softempire/RepoDb
 /// <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));
 }
예제 #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));
 }
예제 #24
0
파일: Delete.cs 프로젝트: softempire/RepoDb
 /// <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));
 }
예제 #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));
 }
예제 #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));
 }
예제 #27
0
파일: Merge.cs 프로젝트: softempire/RepoDb
 /// <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));
 }
예제 #28
0
 /// <summary>
 /// Truncates a table from the database.
 /// </summary>
 /// <returns>The number of rows affected.</returns>
 public int Truncate()
 {
     return(DbRepository.Truncate <TEntity>());
 }
예제 #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));
 }
예제 #30
0
파일: Insert.cs 프로젝트: softempire/RepoDb
 /// <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));
 }