/// <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(); } } } }
/// <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(); } } } }
/// <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(); } } } }
/// <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(); } } } }
/// <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)); }
/// <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)); }
/// <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)); }
public static void Main(string[] args) { var repository = new DbRepository <SqlConnection>("ConnectionString"); repository.Query <Customer>(new { Id = 10045 }, recursive: true); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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>()); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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>()); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <summary> /// Truncates a table from the database. /// </summary> /// <returns>The number of rows affected.</returns> public int Truncate() { return(DbRepository.Truncate <TEntity>()); }
/// <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)); }
/// <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)); }