Fix for multiple parameter error. (#1082)

* Clearing first use of parameters

* restore space

* added finally block

* Added try catch block in correct position

* fixed spacing

* added space before try

* duplicate row check simplified by Alan

* Revert "duplicate row check simplified by Alan"

This reverts commit 445ac506bd96b353266778abd0cf9bad2be2a3c3.

* removed exception and changed message

* added exception back for test purposes

* added working executescalar

* Added "<=" for delete action that deletes nothing.

* spacing fix for check duplicate

* Added comments and changed function logic.

* added clarifying message

* Added new extended class

* small space fix
This commit is contained in:
Alex Ma
2020-09-25 10:52:59 -07:00
committed by GitHub
parent fd4d067e71
commit 47e7a2f492
3 changed files with 90 additions and 23 deletions

View File

@@ -89,9 +89,9 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
WhereClause where = GetWhereClause(true);
string commandText = GetCommandText(where.CommandText);
string verifyText = GetVerifyText(where.CommandText);
if (!CheckForDuplicateDeleteRows(where, verifyText, connection))
if (HasDuplicateRows(where, verifyText, connection))
{
throw new EditDataDeleteException("This action will delete more than one row!");
throw new EditDataDeleteException("Cannot delete: Action will delete more than one row");
}
DbCommand command = connection.CreateCommand();
@@ -103,34 +103,23 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
/// <summary>
/// Runs a query using the where clause to determine if duplicates are found (causes issues when deleting).
/// If no duplicates are found, the check passes, else it returns false;
/// If duplicates are found, the check returns true, else it returns false;
/// </summary>
private bool CheckForDuplicateDeleteRows(WhereClause where, string input, DbConnection connection)
private bool HasDuplicateRows(WhereClause where, string input, DbConnection connection)
{
using (DbCommand command = connection.CreateCommand())
{
command.CommandText = input;
command.Parameters.AddRange(where.Parameters.ToArray());
using (DbDataReader reader = command.ExecuteReader())
try
{
try
{
while (reader.Read())
{
//If the count of the row is
if (reader.GetInt32(0) != 1)
{
return false;
}
}
}
catch (Exception ex)
{
Logger.Write(TraceEventType.Error, ex.ToString());
}
return (Convert.ToInt32(command.ExecuteScalar())) > 1;
}
finally
{
command.Parameters.Clear();
}
}
return true;
}
/// <summary>