diff --git a/RepoDb.PostgreSql/RepoDb.PostgreSql/DbHelpers/PostgreSqlDbHelper.cs b/RepoDb.PostgreSql/RepoDb.PostgreSql/DbHelpers/PostgreSqlDbHelper.cs index 123ba425f..f9fe46e90 100644 --- a/RepoDb.PostgreSql/RepoDb.PostgreSql/DbHelpers/PostgreSqlDbHelper.cs +++ b/RepoDb.PostgreSql/RepoDb.PostgreSql/DbHelpers/PostgreSqlDbHelper.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Data; +using System.Data.Common; using System.Threading; using System.Threading.Tasks; @@ -45,9 +46,9 @@ public PostgreSqlDbHelper(IResolver dbTypeResolver) #region Helpers /// - /// Returns the command text that is being used to extract schema definitions. + /// /// - /// The command text. + /// private string GetCommandText() { return @" @@ -76,11 +77,11 @@ JOIN information_schema.constraint_column_usage AS CCU USING (constraint_schema, } /// - /// Converts the object into object. + /// /// - /// The instance of object. - /// The instance of converted object. - private DbField ReaderToDbField(IDataReader reader) + /// + /// + private DbField ReaderToDbField(DbDataReader reader) { return new DbField(reader.GetString(0), reader.IsDBNull(1) ? false : reader.GetBoolean(1), @@ -93,6 +94,26 @@ private DbField ReaderToDbField(IDataReader reader) reader.IsDBNull(4) ? "text" : reader.GetString(4)); } + /// + /// + /// + /// + /// + /// + private async Task ReaderToDbFieldAsync(DbDataReader reader, + CancellationToken cancellationToken = default) + { + return new DbField(await reader.GetFieldValueAsync(0, cancellationToken), + await reader.IsDBNullAsync(1, cancellationToken) ? false : await reader.GetFieldValueAsync(1, cancellationToken), + await reader.IsDBNullAsync(2, cancellationToken) ? false : await reader.GetFieldValueAsync(2, cancellationToken), + await reader.IsDBNullAsync(3, cancellationToken) ? false : await reader.GetFieldValueAsync(3, cancellationToken), + await reader.IsDBNullAsync(4, cancellationToken) ? DbTypeResolver.Resolve("text") : DbTypeResolver.Resolve(await reader.GetFieldValueAsync(4, cancellationToken)), + null, + null, + null, + await reader.IsDBNullAsync(4) ? "text" : reader.GetString(4)); + } + #endregion #region Methods @@ -119,7 +140,7 @@ public IEnumerable GetFields(IDbConnection connection, }; // Iterate and extract - using (var reader = connection.ExecuteReader(commandText, param, transaction: transaction)) + using (var reader = (DbDataReader)connection.ExecuteReader(commandText, param, transaction: transaction)) { var dbFields = new List(); @@ -140,15 +161,13 @@ public IEnumerable GetFields(IDbConnection connection, /// The instance of the connection object. /// The name of the target table. /// The transaction object that is currently in used. - /// A to observe while waiting for the task to complete. + /// The object to be used during the asynchronous operation. /// A list of of the target table. public async Task> GetFieldsAsync(IDbConnection connection, string tableName, IDbTransaction transaction = null, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - // Variables var commandText = GetCommandText(); var param = new @@ -158,14 +177,15 @@ public async Task> GetFieldsAsync(IDbConnection connection, }; // Iterate and extract - using (var reader = await connection.ExecuteReaderAsync(commandText, param, transaction: transaction)) + using (var reader = (DbDataReader)await connection.ExecuteReaderAsync(commandText, param, transaction: transaction, + cancellationToken: cancellationToken)) { var dbFields = new List(); // Iterate the list of the fields - while (reader.Read()) + while (await reader.ReadAsync(cancellationToken)) { - dbFields.Add(ReaderToDbField(reader)); + dbFields.Add(await ReaderToDbFieldAsync(reader)); } // Return the list of fields @@ -187,7 +207,7 @@ public object GetScopeIdentity(IDbConnection connection, IDbTransaction transaction = null) { // TODO: May fail with trigger? - return connection.ExecuteScalar("SELECT lastval();"); + return connection.ExecuteScalar("SELECT lastval();", transaction: transaction); } /// @@ -195,12 +215,15 @@ public object GetScopeIdentity(IDbConnection connection, /// /// The instance of the connection object. /// The transaction object that is currently in used. + /// The object to be used during the asynchronous operation. /// The newly generated identity from the database. public async Task GetScopeIdentityAsync(IDbConnection connection, - IDbTransaction transaction = null) + IDbTransaction transaction = null, + CancellationToken cancellationToken = default) { // TODO: May fail with trigger? - return await connection.ExecuteScalarAsync("SELECT lastval();"); + return await connection.ExecuteScalarAsync("SELECT lastval();", transaction: transaction, + cancellationToken: cancellationToken); } #endregion