mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 17:23:27 -05:00
This change adds a couple things _Multipart Identifier Decoding_ The ability to decode a multipart identifier (with or without escaping) has been added to the SqlScriptFormatter utility class. This code is utilized to split a table name provided to the edit/initialize request into schema and table name. _Default Schema Workaround_ The code that retrieves the SMO metadata objects originally used the `[]` operator to access the objects. Due to a bug(?) in SMO, this results in problems when loading tables without a default schema (in our case if you're logged in as SA). Using the metadata object constructors gets around this issue, we are explicitly using them. * Adding decoding of multipart identifiers Adding code fix for default schema issue * Adding some more localizable strings for errors when loading metadata * Adding localization files... again? * Changes as per pull request comments
147 lines
5.5 KiB
C#
147 lines
5.5 KiB
C#
//
|
|
// Copyright (c) Microsoft. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
//
|
|
|
|
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.SqlTools.ServiceLayer.EditData;
|
|
using Microsoft.SqlTools.ServiceLayer.EditData.Contracts;
|
|
using Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement;
|
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
|
|
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
|
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
|
|
using Moq;
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
|
{
|
|
public class Common
|
|
{
|
|
public const string OwnerUri = "testFile";
|
|
|
|
public static EditInitializeParams BasicInitializeParameters
|
|
{
|
|
get
|
|
{
|
|
return new EditInitializeParams
|
|
{
|
|
Filters = new EditInitializeFiltering(),
|
|
ObjectName = "tbl",
|
|
ObjectType = "tbl"
|
|
};
|
|
}
|
|
}
|
|
|
|
public static async Task<EditSession> GetCustomSession(Query q, EditTableMetadata etm)
|
|
{
|
|
// Step 1) Create the Session object
|
|
// Mock metadata factory
|
|
Mock<IEditMetadataFactory> metaFactory = new Mock<IEditMetadataFactory>();
|
|
metaFactory
|
|
.Setup(f => f.GetObjectMetadata(It.IsAny<DbConnection>(), It.IsAny<string[]>(), It.IsAny<string>()))
|
|
.Returns(etm);
|
|
|
|
EditSession session = new EditSession(metaFactory.Object);
|
|
|
|
|
|
// Step 2) Initialize the Session
|
|
// Mock connector that does nothing
|
|
EditSession.Connector connector = () => Task.FromResult<DbConnection>(null);
|
|
|
|
// Mock query runner that returns the query we were provided
|
|
EditSession.QueryRunner queryRunner = (s) => Task.FromResult(new EditSession.EditSessionQueryExecutionState(q));
|
|
|
|
// Initialize
|
|
session.Initialize(BasicInitializeParameters, connector, queryRunner, () => Task.FromResult(0), (e) => Task.FromResult(0));
|
|
await session.InitializeTask;
|
|
|
|
return session;
|
|
}
|
|
|
|
public static EditTableMetadata GetStandardMetadata(DbColumn[] columns, bool isMemoryOptimized = false)
|
|
{
|
|
// Create column metadata providers
|
|
var columnMetas = columns.Select((c, i) =>
|
|
{
|
|
var ecm = new EditColumnMetadata
|
|
{
|
|
EscapedName = c.ColumnName,
|
|
Ordinal = i
|
|
};
|
|
return ecm;
|
|
}).ToArray();
|
|
|
|
// Create column wrappers
|
|
var columnWrappers = columns.Select(c => new DbColumnWrapper(c)).ToArray();
|
|
|
|
// Create the table metadata
|
|
EditTableMetadata editTableMetadata = new EditTableMetadata
|
|
{
|
|
Columns = columnMetas,
|
|
EscapedMultipartName = "tbl",
|
|
IsMemoryOptimized = isMemoryOptimized
|
|
};
|
|
editTableMetadata.Extend(columnWrappers);
|
|
return editTableMetadata;
|
|
}
|
|
|
|
public static DbColumn[] GetColumns(bool includeIdentity)
|
|
{
|
|
List<DbColumn> columns = new List<DbColumn>();
|
|
|
|
if (includeIdentity)
|
|
{
|
|
columns.Add(new TestDbColumn("id") {IsKey = true, IsIdentity = true, IsAutoIncrement = true});
|
|
}
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
columns.Add(new TestDbColumn($"col{i}"));
|
|
}
|
|
return columns.ToArray();
|
|
}
|
|
|
|
public static async Task<Query> GetQuery(DbColumn[] columns, bool includIdentity, int rowCount = 1)
|
|
{
|
|
Query q = QueryExecution.Common.GetBasicExecutedQuery();
|
|
q.Batches[0].ResultSets[0] = await GetResultSet(columns, includIdentity, rowCount);
|
|
return q;
|
|
}
|
|
|
|
public static async Task<ResultSet> GetResultSet(DbColumn[] columns, bool includeIdentity, int rowCount = 1)
|
|
{
|
|
IEnumerable<object[]> rows = includeIdentity
|
|
? Enumerable.Repeat(new object[] { "id", "1", "2", "3" }, rowCount)
|
|
: Enumerable.Repeat(new object[] { "1", "2", "3" }, rowCount);
|
|
var testResultSet = new TestResultSet(columns, rows);
|
|
var reader = new TestDbDataReader(new[] { testResultSet });
|
|
var resultSet = new ResultSet(0, 0, MemoryFileSystem.GetFileStreamFactory());
|
|
await resultSet.ReadResultToEnd(reader, CancellationToken.None);
|
|
return resultSet;
|
|
}
|
|
|
|
public static DbDataReader GetNewRowDataReader(DbColumn[] columns, bool includeIdentity)
|
|
{
|
|
object[][] rows = includeIdentity
|
|
? new[] {new object[] {"id", "q", "q", "q"}}
|
|
: new[] {new object[] {"q", "q", "q"}};
|
|
var testResultSet = new TestResultSet(columns, rows);
|
|
return new TestDbDataReader(new [] {testResultSet});
|
|
}
|
|
|
|
public static void AddCells(RowEditBase rc, bool includeIdentity)
|
|
{
|
|
// Skip the first column since if identity, since identity columns can't be updated
|
|
int start = includeIdentity ? 1 : 0;
|
|
for (int i = start; i < rc.AssociatedResultSet.Columns.Length; i++)
|
|
{
|
|
rc.SetCell(i, "123");
|
|
}
|
|
}
|
|
}
|
|
}
|