mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-10 10:12:39 -05:00
edit/commit Command (#262)
The main goal of this feature is to enable a command that will 1) Generate a parameterized command for each edit that is in the session 2) Execute that command against the server 3) Update the cached results of the table/view that's being edited with the committed changes (including computed/identity columns) There's some secret sauce in here where I cheated around worrying about gaps in the updated results. This was accomplished by implementing an IComparable for row edit objects that ensures deletes are the *last* actions to occur and that they occur from the bottom of the list up (highest row ID to lowest). Thus, all other actions that are dependent on the row ID are performed first, then the largest row ID is deleted, then next largest, etc. Nevertheless, by the end of a commit the associated ResultSet is still the source of truth. It is expected that the results grid will need updating once changes are committed. Also worth noting, although this pull request supports a "many edits, one commit" approach, it will work just fine for a "one edit, one commit" approach. * WIP * Adding basic commit support. Deletions work! * Nailing down the commit logic, insert commits work! * Updates work! * Fixing bug in DbColumnWrapper IsReadOnly setting * Comments * ResultSet unit tests, fixing issue with seeking in mock writers * Unit tests for RowCreate commands * Unit tests for RowDelete * RowUpdate unit tests * Session and edit base tests * Fixing broken unit tests * Moving constants to constants file * Addressing code review feedback * Fixes from merge issues, string consts * Removing ad-hoc code * fixing as per @abist requests * Fixing a couple more issues
This commit is contained in:
@@ -4,9 +4,12 @@
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Data.Common;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.EditData.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
{
|
||||
@@ -29,15 +32,53 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sort ID for a RowDelete object. Setting to 2 ensures that these are the LAST changes
|
||||
/// to be committed
|
||||
/// </summary>
|
||||
protected override int SortId => 2;
|
||||
|
||||
/// <summary>
|
||||
/// Applies the changes to the associated result set after successfully executing the
|
||||
/// change on the database
|
||||
/// </summary>
|
||||
/// <param name="dataReader">
|
||||
/// Reader returned from the execution of the command to insert a new row. Should NOT
|
||||
/// contain any rows.
|
||||
/// </param>
|
||||
public override Task ApplyChanges(DbDataReader dataReader)
|
||||
{
|
||||
// Take the result set and remove the row from it
|
||||
AssociatedResultSet.RemoveRow(RowId);
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a command for deleting the selected row
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override DbCommand GetCommand(DbConnection connection)
|
||||
{
|
||||
Validate.IsNotNull(nameof(connection), connection);
|
||||
|
||||
// Return a SqlCommand with formatted with the parameters from the where clause
|
||||
WhereClause where = GetWhereClause(true);
|
||||
string commandText = GetCommandText(where.CommandText);
|
||||
|
||||
DbCommand command = connection.CreateCommand();
|
||||
command.CommandText = commandText;
|
||||
command.Parameters.AddRange(where.Parameters.ToArray());
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a DELETE statement to delete this row
|
||||
/// </summary>
|
||||
/// <returns>String of the DELETE statement</returns>
|
||||
public override string GetScript()
|
||||
{
|
||||
string formatString = AssociatedObjectMetadata.IsMemoryOptimized ? DeleteMemoryOptimizedStatement : DeleteStatement;
|
||||
return string.Format(CultureInfo.InvariantCulture, formatString,
|
||||
AssociatedObjectMetadata.EscapedMultipartName, GetWhereClause(false).CommandText);
|
||||
return GetCommandText(GetWhereClause(false).CommandText);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -51,5 +92,23 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
{
|
||||
throw new InvalidOperationException(SR.EditDataDeleteSetCell);
|
||||
}
|
||||
|
||||
protected override int CompareToSameType(RowEditBase rowEdit)
|
||||
{
|
||||
// We want to sort by row ID *IN REVERSE* to make sure we delete from the bottom first.
|
||||
// If we delete from the top first, it will change IDs, making all subsequent deletes
|
||||
// off by one or more!
|
||||
return RowId.CompareTo(rowEdit.RowId) * -1;
|
||||
}
|
||||
|
||||
private string GetCommandText(string whereText)
|
||||
{
|
||||
string formatString = AssociatedObjectMetadata.IsMemoryOptimized
|
||||
? DeleteMemoryOptimizedStatement
|
||||
: DeleteStatement;
|
||||
|
||||
return string.Format(CultureInfo.InvariantCulture, formatString,
|
||||
AssociatedObjectMetadata.EscapedMultipartName, whereText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user