diff --git a/src/Core/Repositories/PostgreSql/BasePostgreSqlRepository.cs b/src/Core/Repositories/PostgreSql/BasePostgreSqlRepository.cs index 24c351364..cb698f250 100644 --- a/src/Core/Repositories/PostgreSql/BasePostgreSqlRepository.cs +++ b/src/Core/Repositories/PostgreSql/BasePostgreSqlRepository.cs @@ -24,5 +24,16 @@ namespace Bit.Core.Repositories.PostgreSql var startUnderscores = Regex.Match(input, @"^_+"); return startUnderscores + Regex.Replace(input, @"([a-z0-9])([A-Z])", "$1_$2").ToLowerInvariant(); } + + protected static DynamicParameters ToParam(T obj) + { + var dp = new DynamicParameters(); + var properties = typeof(T).GetProperties(); + foreach(var property in properties) + { + dp.Add($"_{SnakeCase(property.Name)}", property.GetValue(obj)); + } + return dp; + } } } diff --git a/src/Core/Repositories/PostgreSql/Repository.cs b/src/Core/Repositories/PostgreSql/Repository.cs index 9515fc201..051e2bf8b 100644 --- a/src/Core/Repositories/PostgreSql/Repository.cs +++ b/src/Core/Repositories/PostgreSql/Repository.cs @@ -33,7 +33,7 @@ namespace Bit.Core.Repositories.PostgreSql { var results = await connection.QueryAsync( $"{Table}_read_by_id", - new { id = id }, + ToParam(new { Id = id }), commandType: CommandType.StoredProcedure); return results.SingleOrDefault(); @@ -47,7 +47,7 @@ namespace Bit.Core.Repositories.PostgreSql { var results = await connection.ExecuteAsync( $"{Table}_create", - obj, + ToParam(obj), commandType: CommandType.StoredProcedure); } } @@ -58,7 +58,7 @@ namespace Bit.Core.Repositories.PostgreSql { var results = await connection.ExecuteAsync( $"{Table}_update", - obj, + ToParam(obj), commandType: CommandType.StoredProcedure); } } @@ -81,7 +81,7 @@ namespace Bit.Core.Repositories.PostgreSql { await connection.ExecuteAsync( $"{Table}_delete_by_id", - new { id = obj.Id }, + ToParam(new { Id = obj.Id }), commandType: CommandType.StoredProcedure); } } diff --git a/src/Core/Repositories/PostgreSql/UserRepository.cs b/src/Core/Repositories/PostgreSql/UserRepository.cs index cc9cfc14e..fca3a8625 100644 --- a/src/Core/Repositories/PostgreSql/UserRepository.cs +++ b/src/Core/Repositories/PostgreSql/UserRepository.cs @@ -31,7 +31,7 @@ namespace Bit.Core.Repositories.PostgreSql { var results = await connection.QueryAsync( "user_read_by_email", - new { email = email }, + ToParam(new { Email = email }), commandType: CommandType.StoredProcedure); return results.SingleOrDefault(); @@ -44,7 +44,7 @@ namespace Bit.Core.Repositories.PostgreSql { var results = await connection.QueryAsync( "user_read_kdf_by_email", - new { email = email }, + ToParam(new { Email = email }), commandType: CommandType.StoredProcedure); return results.SingleOrDefault(); @@ -57,7 +57,7 @@ namespace Bit.Core.Repositories.PostgreSql { var results = await connection.QueryAsync( "user_search", - new { email = email, skip = skip, take = take }, + ToParam(new { Email = email, Skip = skip, Take = take }), commandType: CommandType.StoredProcedure, commandTimeout: 120); @@ -71,7 +71,7 @@ namespace Bit.Core.Repositories.PostgreSql { var results = await connection.QueryAsync( "user_read_by_premium", - new { premium = premium }, + ToParam(new { Premium = premium }), commandType: CommandType.StoredProcedure); return results.ToList(); @@ -96,7 +96,7 @@ namespace Bit.Core.Repositories.PostgreSql { var results = await connection.QueryAsync( "user_read_public_key_by_id", - new { id = id }, + ToParam(new { Id = id }), commandType: CommandType.StoredProcedure); return results.SingleOrDefault(); @@ -109,7 +109,7 @@ namespace Bit.Core.Repositories.PostgreSql { var results = await connection.QueryAsync( "user_read_account_revision_date_by_id", - new { id = id }, + ToParam(new { Id = id }), commandType: CommandType.StoredProcedure); return results.SingleOrDefault(); @@ -127,7 +127,7 @@ namespace Bit.Core.Repositories.PostgreSql { await connection.ExecuteAsync( $"user_delete_by_id", - new { id = user.Id }, + ToParam(new { Id = user.Id }), commandType: CommandType.StoredProcedure, commandTimeout: 180); } @@ -139,7 +139,7 @@ namespace Bit.Core.Repositories.PostgreSql { await connection.ExecuteAsync( "user_update_storage", - new { id = id }, + ToParam(new { Id = id }), commandType: CommandType.StoredProcedure, commandTimeout: 180); } @@ -151,7 +151,7 @@ namespace Bit.Core.Repositories.PostgreSql { await connection.ExecuteAsync( "user_update_renewal_reminder_date", - new { id = id, renewal_reminder_date = renewalReminderDate }, + ToParam(new { Id = id, RenewalReminderDate = renewalReminderDate }), commandType: CommandType.StoredProcedure); } }