Merging mssql-scripter changes (#430)

* Manual port of latest 'feature/mssq-scripter' branch

* Bumpnuget package SqlScriptPublishModel.140.2.0 to Microsoft.SqlServer.Management.SqlScriptPublishModel.140.2.3

* In TestDriver, fix the path to Microsoft.SqlTools.ServiceLayer.exe after move to .NET Core 2.0
This commit is contained in:
Brian O'Neill
2017-08-09 08:25:13 -07:00
committed by GitHub
parent 1cbc78a266
commit a4a27f9559
11 changed files with 350 additions and 62 deletions

View File

@@ -52,16 +52,28 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
/// </summary>
/// <param name="includeCriteria">The include object criteria.</param>
/// <param name="excludeCriteria">The exclude object criteria.</param>
/// <param name="includeSchemas">The include schema filter.</param>
/// <param name="excludeSchemas">The exclude schema filter.</param>
/// <param name="includeTypes">The include type filter.</param>
/// <param name="excludeTypes">The exclude type filter.</param>
/// <param name="candidates">The candidate object to filter.</param>
/// <returns>The matching scripting objects.</returns>
public static IEnumerable<ScriptingObject> Match(
ScriptingObject includeCriteria,
ScriptingObject excludeCriteria,
string includeSchemas,
string excludeSchemas,
string includeTypes,
string excludeTypes,
IEnumerable<ScriptingObject> candidates)
{
return Match(
includeCriteria == null ? new ScriptingObject[0] : new[] { includeCriteria },
excludeCriteria == null ? new ScriptingObject[0] : new[] { excludeCriteria },
includeSchemas == null ? new List<string>(): new List<string> { includeSchemas },
excludeSchemas == null ? new List<string>(): new List<string> { excludeSchemas },
includeTypes == null ? new List<string>(): new List<string> { includeTypes },
excludeTypes == null ? new List<string>(): new List<string> { excludeTypes },
candidates);
}
@@ -71,15 +83,23 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
/// </summary>
/// <param name="includeCriteria">The collection of include object criteria items.</param>
/// <param name="excludeCriteria">The collection of exclude object criteria items.</param>
/// <param name="includeSchemas">The collection of include schema items.</param>
/// <param name="excludeSchemas">The collection of exclude schema items.</param>
/// <param name="includeTypes">The collection of include type items.</param>
/// <param name="excludeTypes">The collection of exclude type items.</param>
/// <param name="candidates">The candidate object to filter.</param>
/// <returns>The matching scripting objects.</returns>
public static IEnumerable<ScriptingObject> Match(
IEnumerable<ScriptingObject> includeCriteria,
IEnumerable<ScriptingObject> excludeCriteria,
IEnumerable<string> includeSchemas,
IEnumerable<string> excludeSchemas,
IEnumerable<string> includeTypes,
IEnumerable<string> excludeTypes,
IEnumerable<ScriptingObject> candidates)
{
Validate.IsNotNull("candidates", candidates);
IEnumerable<ScriptingObject> matchedObjects = new List<ScriptingObject>();
if (includeCriteria != null && includeCriteria.Any())
@@ -95,15 +115,84 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
matchedObjects = candidates;
}
if (excludeCriteria != null)
if (excludeCriteria != null && excludeCriteria.Any())
{
foreach (ScriptingObject scriptingObjectCriteria in excludeCriteria)
{
IEnumerable<ScriptingObject> matches = MatchCriteria(scriptingObjectCriteria, candidates);
IEnumerable<ScriptingObject> matches = MatchCriteria(scriptingObjectCriteria, matchedObjects);
matchedObjects = matchedObjects.Except(matches);
}
}
// Apply additional filters if included.
matchedObjects = ExcludeSchemaAndOrType(excludeSchemas, excludeTypes, matchedObjects);
matchedObjects = IncludeSchemaAndOrType(includeSchemas, includeTypes, matchedObjects);
return matchedObjects;
}
private static IEnumerable<ScriptingObject> ExcludeSchemaAndOrType(IEnumerable<string> excludeSchemas, IEnumerable<string> excludeTypes,
IEnumerable<ScriptingObject> candidates)
{
// Given a list of candidates, we remove any objects that match the excluded schema and/or type.
IEnumerable<ScriptingObject> remainingObjects = candidates;
IEnumerable<ScriptingObject> matches = null;
if (excludeSchemas != null && excludeSchemas.Any())
{
foreach (string exclude_schema in excludeSchemas)
{
matches = MatchCriteria(exclude_schema, (candidate) => { return candidate.Schema; }, candidates);
remainingObjects = remainingObjects.Except(matches);
}
}
if (excludeTypes != null && excludeTypes.Any())
{
foreach (string exclude_type in excludeTypes)
{
matches = remainingObjects.Where(o => string.Equals(exclude_type, o.Type, StringComparison.OrdinalIgnoreCase));
remainingObjects = remainingObjects.Except(matches);
}
}
return remainingObjects;
}
private static IEnumerable<ScriptingObject> IncludeSchemaAndOrType(IEnumerable<string> includeSchemas, IEnumerable<string> includeTypes,
IEnumerable<ScriptingObject> candidates)
{
// Given a list of candidates, we return a new list of scripting objects that match
// the schema and/or type filter.
IEnumerable<ScriptingObject> matchedSchema = new List<ScriptingObject>();
IEnumerable<ScriptingObject> matchedType = new List<ScriptingObject>();
IEnumerable<ScriptingObject> matchedObjects = new List<ScriptingObject>();
IEnumerable<ScriptingObject> matches = null;
if (includeSchemas != null && includeSchemas.Any())
{
foreach (string include_schema in includeSchemas)
{
matches = MatchCriteria(include_schema, (candidate) => { return candidate.Schema; }, candidates);
matchedSchema = matchedSchema.Union(matches);
}
matchedObjects = matchedSchema;
}
else
{
matchedObjects = candidates;
}
if (includeTypes != null && includeTypes.Any())
{
foreach (string include_type in includeTypes)
{
matches = matchedObjects.Where(o => string.Equals(include_type, o.Type, StringComparison.OrdinalIgnoreCase));
matchedType = matchedType.Union(matches);
}
matchedObjects = matchedType;
}
return matchedObjects;
}
@@ -137,9 +226,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
}
if (property.EndsWith(Wildcard, StringComparison.OrdinalIgnoreCase))
{
matchedObjects = candidates.Where(o => propertySelector(o).StartsWith(
propertySelector(o).Substring(0, propertySelector(o).Length - 1),
StringComparison.OrdinalIgnoreCase));
matchedObjects = candidates.Where(
o =>
propertySelector(o) != null &&
propertySelector(o).StartsWith(
propertySelector(o).Substring(0, propertySelector(o).Length - 1),
StringComparison.OrdinalIgnoreCase));
}
else
{