mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-01 17:24:42 -05:00
edit/revertCell (#268)
// edit/dispose -------------------------------------------------------------------------------
export interface EditDisposeParams extends IEditSessionOperationParams { }
export interface EditDisposeResult { }
* Initial plumbing for edit/revertCell
* Implementation of revert cell in the parents of the row edit base
* Adding unit tests
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.EditData.Contracts
|
||||
{
|
||||
public class EditRevertCellParams : RowOperationParams
|
||||
{
|
||||
public int ColumnId { get; set; }
|
||||
}
|
||||
|
||||
public class EditRevertCellResult
|
||||
{
|
||||
public string NewValue { get; set; }
|
||||
}
|
||||
|
||||
public class EditRevertCellRequest
|
||||
{
|
||||
public static readonly
|
||||
RequestType<EditRevertCellParams, EditRevertCellResult> Type =
|
||||
RequestType<EditRevertCellParams, EditRevertCellResult>.Create("edit/revertCell");
|
||||
}
|
||||
}
|
||||
@@ -89,6 +89,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData
|
||||
serviceHost.SetRequestHandler(EditDeleteRowRequest.Type, HandleDeleteRowRequest);
|
||||
serviceHost.SetRequestHandler(EditDisposeRequest.Type, HandleDisposeRequest);
|
||||
serviceHost.SetRequestHandler(EditInitializeRequest.Type, HandleInitializeRequest);
|
||||
serviceHost.SetRequestHandler(EditRevertCellRequest.Type, HandleRevertCellRequest);
|
||||
serviceHost.SetRequestHandler(EditRevertRowRequest.Type, HandleRevertRowRequest);
|
||||
serviceHost.SetRequestHandler(EditUpdateCellRequest.Type, HandleUpdateCellRequest);
|
||||
}
|
||||
@@ -217,6 +218,19 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData
|
||||
}
|
||||
}
|
||||
|
||||
internal Task HandleRevertCellRequest(EditRevertCellParams revertParams,
|
||||
RequestContext<EditRevertCellResult> requestContext)
|
||||
{
|
||||
return HandleSessionRequest(revertParams, requestContext, session =>
|
||||
{
|
||||
string newValue = session.RevertCell(revertParams.RowId, revertParams.ColumnId);
|
||||
return new EditRevertCellResult
|
||||
{
|
||||
NewValue = newValue
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
internal Task HandleRevertRowRequest(EditRevertRowParams revertParams,
|
||||
RequestContext<EditRevertRowResult> requestContext)
|
||||
{
|
||||
|
||||
@@ -186,6 +186,25 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverts a cell in a pending edit
|
||||
/// </summary>
|
||||
/// <param name="rowId">Internal ID of the row to have its edits reverted</param>
|
||||
/// <param name="columnId">Ordinal ID of the column to revert</param>
|
||||
/// <returns>String version of the old value for the cell</returns>
|
||||
public string RevertCell(long rowId, int columnId)
|
||||
{
|
||||
// Attempt to get the row edit with the given ID
|
||||
RowEditBase pendingEdit;
|
||||
if (!EditCache.TryGetValue(rowId, out pendingEdit))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(rowId), SR.EditDataUpdateNotPending);
|
||||
}
|
||||
|
||||
// Have the edit base revert the cell
|
||||
return pendingEdit.RevertCell(columnId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a pending row update from the update cache.
|
||||
/// </summary>
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
private const string InsertCompleteScript = "{0} VALUES ({1})";
|
||||
private const string InsertCompleteOutput = "{0} OUTPUT {1} VALUES ({2})";
|
||||
|
||||
private readonly CellUpdate[] newCells;
|
||||
internal readonly CellUpdate[] newCells;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new Row Creation edit to the result set
|
||||
@@ -159,6 +159,21 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
return string.Format(InsertCompleteScript, start, joinedValues);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverts a cell to an unset value.
|
||||
/// </summary>
|
||||
/// <param name="columnId">The ordinal ID of the cell to reset</param>
|
||||
/// <returns>The default value for the column, or null if no default is defined</returns>
|
||||
public override string RevertCell(int columnId)
|
||||
{
|
||||
// Validate that the column can be reverted
|
||||
Validate.IsWithinRange(nameof(columnId), columnId, 0, newCells.Length - 1);
|
||||
|
||||
// Remove the cell update from list of set cells
|
||||
newCells[columnId] = null;
|
||||
return null; // @TODO: Return default value when we have support checked in
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of a cell in the row to be added
|
||||
/// </summary>
|
||||
|
||||
@@ -81,6 +81,16 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
return GetCommandText(GetWhereClause(false).CommandText);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method should not be called. A cell cannot be reverted on a row that is pending
|
||||
/// deletion.
|
||||
/// </summary>
|
||||
/// <param name="columnId">Ordinal of the column to update</param>
|
||||
public override string RevertCell(int columnId)
|
||||
{
|
||||
throw new InvalidOperationException(SR.EditDataDeleteSetCell);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method should not be called. A cell cannot be updated on a row that is pending
|
||||
/// deletion.
|
||||
|
||||
@@ -91,6 +91,13 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
/// <returns>A SQL statement</returns>
|
||||
public abstract string GetScript();
|
||||
|
||||
/// <summary>
|
||||
/// Reverts a specific cell in row with pending edits
|
||||
/// </summary>
|
||||
/// <param name="columnId">Ordinal ID of the column to revert</param>
|
||||
/// <returns>String value of the original value of the cell</returns>
|
||||
public abstract string RevertCell(int columnId);
|
||||
|
||||
/// <summary>
|
||||
/// Changes the value a cell in the row.
|
||||
/// </summary>
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
@@ -29,7 +30,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
private const string UpdateScript = @"{0} SET {1} {2}";
|
||||
private const string UpdateScriptOutput = @"{0} SET {1} OUTPUT {2} {3}";
|
||||
|
||||
private readonly Dictionary<int, CellUpdate> cellUpdates;
|
||||
internal readonly ConcurrentDictionary<int, CellUpdate> cellUpdates;
|
||||
private readonly IList<DbCellValue> associatedRow;
|
||||
|
||||
/// <summary>
|
||||
@@ -41,7 +42,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
public RowUpdate(long rowId, ResultSet associatedResultSet, IEditTableMetadata associatedMetadata)
|
||||
: base(rowId, associatedResultSet, associatedMetadata)
|
||||
{
|
||||
cellUpdates = new Dictionary<int, CellUpdate>();
|
||||
cellUpdates = new ConcurrentDictionary<int, CellUpdate>();
|
||||
associatedRow = associatedResultSet.GetRow(rowId);
|
||||
}
|
||||
|
||||
@@ -137,6 +138,22 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
return string.Format(UpdateScript, statementStart, setClause, whereClause);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverts the value of a cell to its original value
|
||||
/// </summary>
|
||||
/// <param name="columnId">Ordinal of the column to revert</param>
|
||||
/// <returns>The value that was </returns>
|
||||
public override string RevertCell(int columnId)
|
||||
{
|
||||
Validate.IsWithinRange(nameof(columnId), columnId, 0, associatedRow.Count - 1);
|
||||
|
||||
// Remove the cell update
|
||||
CellUpdate cellUpdate;
|
||||
cellUpdates.TryRemove(columnId, out cellUpdate);
|
||||
|
||||
return associatedRow[columnId].DisplayValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of the cell in the associated row. If <paramref name="newValue"/> is
|
||||
/// identical to the original value, this will remove the cell update from the row update.
|
||||
@@ -157,11 +174,9 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
// NOTE: We must use .Equals in order to ignore object to object comparisons
|
||||
if (update.Value.Equals(associatedRow[columnId].RawObject))
|
||||
{
|
||||
// Remove any pending change and stop processing this
|
||||
if (cellUpdates.ContainsKey(columnId))
|
||||
{
|
||||
cellUpdates.Remove(columnId);
|
||||
}
|
||||
// Remove any pending change and stop processing this (we don't care if we fail to remove something)
|
||||
CellUpdate cu;
|
||||
cellUpdates.TryRemove(columnId, out cu);
|
||||
return new EditUpdateCellResult
|
||||
{
|
||||
HasCorrections = false,
|
||||
@@ -172,7 +187,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
|
||||
}
|
||||
|
||||
// The change is real, so set it
|
||||
cellUpdates[columnId] = update;
|
||||
cellUpdates.AddOrUpdate(columnId, update, (i, cu) => update);
|
||||
return new EditUpdateCellResult
|
||||
{
|
||||
HasCorrections = update.ValueAsString != newValue,
|
||||
|
||||
@@ -421,6 +421,14 @@ namespace Microsoft.SqlTools.ServiceLayer
|
||||
}
|
||||
}
|
||||
|
||||
public static string EditDataColumnUpdateNotPending
|
||||
{
|
||||
get
|
||||
{
|
||||
return Keys.GetString(Keys.EditDataColumnUpdateNotPending);
|
||||
}
|
||||
}
|
||||
|
||||
public static string EditDataObjectMetadataNotFound
|
||||
{
|
||||
get
|
||||
@@ -1032,6 +1040,9 @@ namespace Microsoft.SqlTools.ServiceLayer
|
||||
public const string EditDataUpdateNotPending = "EditDataUpdateNotPending";
|
||||
|
||||
|
||||
public const string EditDataColumnUpdateNotPending = "EditDataColumnUpdateNotPending";
|
||||
|
||||
|
||||
public const string EditDataObjectMetadataNotFound = "EditDataObjectMetadataNotFound";
|
||||
|
||||
|
||||
|
||||
@@ -380,7 +380,11 @@
|
||||
<comment></comment>
|
||||
</data>
|
||||
<data name="EditDataUpdateNotPending" xml:space="preserve">
|
||||
<value>Given row ID does not have pending updated</value>
|
||||
<value>Given row ID does not have pending update</value>
|
||||
<comment></comment>
|
||||
</data>
|
||||
<data name="EditDataColumnUpdateNotPending" xml:space="preserve">
|
||||
<value>Give column ID does not have pending update</value>
|
||||
<comment></comment>
|
||||
</data>
|
||||
<data name="EditDataObjectMetadataNotFound" xml:space="preserve">
|
||||
|
||||
@@ -178,7 +178,9 @@ EditDataRowOutOfRange = Given row ID is outside the range of rows in the edit ca
|
||||
|
||||
EditDataUpdatePending = An update is already pending for this row and must be reverted first
|
||||
|
||||
EditDataUpdateNotPending = Given row ID does not have pending updated
|
||||
EditDataUpdateNotPending = Given row ID does not have pending update
|
||||
|
||||
EditDataColumnUpdateNotPending = Give column ID does not have pending update
|
||||
|
||||
EditDataObjectMetadataNotFound = Table or view metadata could not be found
|
||||
|
||||
|
||||
@@ -460,7 +460,7 @@
|
||||
<note></note>
|
||||
</trans-unit>
|
||||
<trans-unit id="EditDataUpdateNotPending">
|
||||
<source>Given row ID does not have pending updated</source>
|
||||
<source>Given row ID does not have pending update</source>
|
||||
<target state="new">Given row ID does not have pending updated</target>
|
||||
<note></note>
|
||||
</trans-unit>
|
||||
@@ -536,6 +536,11 @@
|
||||
<target state="new"><TBD></target>
|
||||
<note></note>
|
||||
</trans-unit>
|
||||
<trans-unit id="EditDataColumnUpdateNotPending">
|
||||
<source>Give column ID does not have pending update</source>
|
||||
<target state="new">Give column ID does not have pending update</target>
|
||||
<note></note>
|
||||
</trans-unit>
|
||||
<trans-unit id="EditDataInitializeInProgress">
|
||||
<source>Another edit data initialize is in progress for this owner URI. Please wait for completion.</source>
|
||||
<target state="new">Another edit data initialize is in progress for this owner URI. Please wait for completion.</target>
|
||||
|
||||
Reference in New Issue
Block a user