mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-30 09:35:38 -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:
@@ -9,6 +9,7 @@ using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.EditData;
|
||||
using Microsoft.SqlTools.ServiceLayer.EditData.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement;
|
||||
@@ -99,13 +100,104 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
rt.ValidateWhereClauseNoKeys();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SortingByTypeTest()
|
||||
{
|
||||
// Setup: Create a result set and metadata we can reuse
|
||||
var cols = Common.GetColumns(false);
|
||||
var rs = Common.GetResultSet(cols, false);
|
||||
var etm = Common.GetMetadata(cols);
|
||||
|
||||
// If: I request to sort a list of the three different edit operations
|
||||
List<RowEditBase> rowEdits = new List<RowEditBase>
|
||||
{
|
||||
new RowDelete(0, rs, etm),
|
||||
new RowUpdate(0, rs, etm),
|
||||
new RowCreate(0, rs, etm)
|
||||
};
|
||||
rowEdits.Sort();
|
||||
|
||||
// Then: Delete should be the last operation to execute
|
||||
// (we don't care about the order of the other two)
|
||||
Assert.IsType<RowDelete>(rowEdits.Last());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SortingUpdatesByRowIdTest()
|
||||
{
|
||||
// Setup: Create a result set and metadata we can reuse
|
||||
var cols = Common.GetColumns(false);
|
||||
var rs = Common.GetResultSet(cols, false, 4);
|
||||
var etm = Common.GetMetadata(cols);
|
||||
|
||||
// If: I sort 3 edit operations of the same type
|
||||
List<RowEditBase> rowEdits = new List<RowEditBase>
|
||||
{
|
||||
new RowUpdate(3, rs, etm),
|
||||
new RowUpdate(1, rs, etm),
|
||||
new RowUpdate(2, rs, etm)
|
||||
};
|
||||
rowEdits.Sort();
|
||||
|
||||
// Then: They should be in order by row ID ASCENDING
|
||||
Assert.Equal(1, rowEdits[0].RowId);
|
||||
Assert.Equal(2, rowEdits[1].RowId);
|
||||
Assert.Equal(3, rowEdits[2].RowId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SortingCreatesByRowIdTest()
|
||||
{
|
||||
// Setup: Create a result set and metadata we can reuse
|
||||
var cols = Common.GetColumns(false);
|
||||
var rs = Common.GetResultSet(cols, false);
|
||||
var etm = Common.GetMetadata(cols);
|
||||
|
||||
// If: I sort 3 edit operations of the same type
|
||||
List<RowEditBase> rowEdits = new List<RowEditBase>
|
||||
{
|
||||
new RowCreate(3, rs, etm),
|
||||
new RowCreate(1, rs, etm),
|
||||
new RowCreate(2, rs, etm)
|
||||
};
|
||||
rowEdits.Sort();
|
||||
|
||||
// Then: They should be in order by row ID ASCENDING
|
||||
Assert.Equal(1, rowEdits[0].RowId);
|
||||
Assert.Equal(2, rowEdits[1].RowId);
|
||||
Assert.Equal(3, rowEdits[2].RowId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SortingDeletesByRowIdTest()
|
||||
{
|
||||
// Setup: Create a result set and metadata we can reuse
|
||||
var cols = Common.GetColumns(false);
|
||||
var rs = Common.GetResultSet(cols, false);
|
||||
var etm = Common.GetMetadata(cols);
|
||||
|
||||
// If: I sort 3 delete operations of the same type
|
||||
List<RowEditBase> rowEdits = new List<RowEditBase>
|
||||
{
|
||||
new RowDelete(1, rs, etm),
|
||||
new RowDelete(3, rs, etm),
|
||||
new RowDelete(2, rs, etm)
|
||||
};
|
||||
rowEdits.Sort();
|
||||
|
||||
// Then: They should be in order by row ID DESCENDING
|
||||
Assert.Equal(3, rowEdits[0].RowId);
|
||||
Assert.Equal(2, rowEdits[1].RowId);
|
||||
Assert.Equal(1, rowEdits[2].RowId);
|
||||
}
|
||||
|
||||
private static ResultSet GetResultSet(DbColumn[] columns, object[] row)
|
||||
{
|
||||
object[][] rows = {row};
|
||||
var testResultSet = new TestResultSet(columns, rows);
|
||||
var testReader = new TestDbDataReader(new [] {testResultSet});
|
||||
var resultSet = new ResultSet(testReader, 0,0, MemoryFileSystem.GetFileStreamFactory());
|
||||
resultSet.ReadResultToEnd(CancellationToken.None).Wait();
|
||||
var resultSet = new ResultSet(0,0, MemoryFileSystem.GetFileStreamFactory());
|
||||
resultSet.ReadResultToEnd(testReader, CancellationToken.None).Wait();
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
@@ -179,6 +271,18 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override Task ApplyChanges(DbDataReader reader)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override DbCommand GetCommand(DbConnection conn)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override int SortId => 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user