Edit Data: Better Formatting Errors (#562)

* Refactoring sql script formatting helpers into To and From helpers

* Updates to make error messages for formatting errors more useful

* Fixing dumb breaks in unit tests

* Addressing comments from PR

* Updates to the SR files...
This commit is contained in:
Benjamin Russell
2017-12-05 17:00:13 -08:00
committed by GitHub
parent e7b76a6dec
commit 64133d929e
37 changed files with 7869 additions and 17711 deletions

View File

@@ -15,7 +15,7 @@ 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.Utility;
using Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.EditData
@@ -440,12 +440,9 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData
public static string[] GetEditTargetName(EditInitializeParams initParams)
{
// Step 1) Look up the SMO metadata
if (initParams.SchemaName != null)
{
return new [] { initParams.SchemaName, initParams.ObjectName };
}
return SqlScriptFormatter.DecodeMultipartIdenfitier(initParams.ObjectName);
return initParams.SchemaName != null
? new [] { initParams.SchemaName, initParams.ObjectName }
: FromSqlScript.DecodeMultipartIdentifier(initParams.ObjectName);
}
private async Task CommitEditsInternal(DbConnection connection, Func<Task> successHandler, Func<Exception, Task> errorHandler)

View File

@@ -10,7 +10,7 @@ using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.EditData
@@ -92,12 +92,12 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData
// The default value may be escaped
string defaultValue = smoColumn.DefaultConstraint == null
? null
: SqlScriptFormatter.UnwrapLiteral(smoColumn.DefaultConstraint.Text);
: FromSqlScript.UnwrapLiteral(smoColumn.DefaultConstraint.Text);
EditColumnMetadata column = new EditColumnMetadata
{
DefaultValue = defaultValue,
EscapedName = SqlScriptFormatter.FormatIdentifier(smoColumn.Name),
EscapedName = ToSqlScript.FormatIdentifier(smoColumn.Name),
Ordinal = i,
};
editColumns.Add(column);
@@ -114,14 +114,14 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData
// Escape the parts of the name
string[] objectNameParts = {smoResult.Schema, smoResult.Name};
string escapedMultipartName = SqlScriptFormatter.FormatMultipartIdentifier(objectNameParts);
string escapedMultipartName = ToSqlScript.FormatMultipartIdentifier(objectNameParts);
return new EditTableMetadata
{
Columns = editColumns.ToArray(),
EscapedMultipartName = escapedMultipartName,
IsMemoryOptimized = isMemoryOptimized,
IsMemoryOptimized = isMemoryOptimized
};
}
}
}
}

View File

@@ -8,6 +8,7 @@ using System.Globalization;
using System.Text.RegularExpressions;
using Microsoft.SqlTools.ServiceLayer.EditData.Contracts;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
@@ -34,50 +35,60 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
Validate.IsNotNull(nameof(valueAsString), valueAsString);
// Store the state that won't be changed
Column = column;
Type columnType = column.DataType;
try
{
Column = column;
Type columnType = column.DataType;
// Check for null
if (valueAsString == NullString)
{
ProcessNullValue();
// Check for null
if (valueAsString == NullString)
{
ProcessNullValue();
}
else if (columnType == typeof(byte[]))
{
// Binary columns need special attention
ProcessBinaryCell(valueAsString);
}
else if (columnType == typeof(string))
{
ProcessTextCell(valueAsString);
}
else if (columnType == typeof(Guid))
{
Value = Guid.Parse(valueAsString);
ValueAsString = Value.ToString();
}
else if (columnType == typeof(TimeSpan))
{
ProcessTimespanColumn(valueAsString);
}
else if (columnType == typeof(DateTimeOffset))
{
Value = DateTimeOffset.Parse(valueAsString, CultureInfo.CurrentCulture);
ValueAsString = Value.ToString();
}
else if (columnType == typeof(bool))
{
ProcessBooleanCell(valueAsString);
}
// @TODO: Microsoft.SqlServer.Types.SqlHierarchyId
else
{
// Attempt to go straight to the destination type, if we know what it is, otherwise
// leave it as a string
Value = columnType != null
? Convert.ChangeType(valueAsString, columnType, CultureInfo.CurrentCulture)
: valueAsString;
ValueAsString = Value.ToString();
}
}
else if (columnType == typeof(byte[]))
catch (FormatException fe)
{
// Binary columns need special attention
ProcessBinaryCell(valueAsString);
}
else if (columnType == typeof(string))
{
ProcessTextCell(valueAsString);
}
else if (columnType == typeof(Guid))
{
Value = Guid.Parse(valueAsString);
ValueAsString = Value.ToString();
}
else if (columnType == typeof(TimeSpan))
{
ProcessTimespanColumn(valueAsString);
}
else if (columnType == typeof(DateTimeOffset))
{
Value = DateTimeOffset.Parse(valueAsString, CultureInfo.CurrentCulture);
ValueAsString = Value.ToString();
}
else if (columnType == typeof(bool))
{
ProcessBooleanCell(valueAsString);
}
// @TODO: Microsoft.SqlServer.Types.SqlHierarchyId
else
{
// Attempt to go straight to the destination type, if we know what it is, otherwise
// leave it as a string
Value = columnType != null
? Convert.ChangeType(valueAsString, columnType, CultureInfo.CurrentCulture)
: valueAsString;
ValueAsString = Value.ToString();
// Pretty up the exception so the user can learn a bit from it
// NOTE: Other formatting errors raised by helpers are InvalidOperationException to
// avoid being prettied here
throw new FormatException(SR.EditDataInvalidFormat(column.ColumnName, ToSqlScript.FormatColumnType(column)), fe);
}
}
@@ -181,8 +192,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
}
else
{
// Invalid format
throw new FormatException(SR.EditDataInvalidFormatBinary);
throw new InvalidOperationException(SR.EditDataInvalidFormatBinary);
}
// Generate the hex string as the return value
@@ -205,8 +215,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
Value = false;
break;
default:
throw new ArgumentOutOfRangeException(nameof(valueAsString),
SR.EditDataInvalidFormatBoolean);
throw new InvalidOperationException(SR.EditDataInvalidFormatBoolean);
}
}
else
@@ -253,7 +262,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
{
string columnSizeString = $"({Column.ColumnSize.Value})";
string columnTypeString = Column.DataTypeName.ToUpperInvariant() + columnSizeString;
throw new FormatException(SR.EditDataValueTooLarge(valueAsString, columnTypeString));
throw new InvalidOperationException(SR.EditDataValueTooLarge(valueAsString, columnTypeString));
}
ValueAsString = valueAsString;

View File

@@ -14,7 +14,7 @@ using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.EditData.Contracts;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
@@ -202,7 +202,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
// Add an out column if we're doing this for a command
if (forCommand)
{
outColumns.Add($"inserted.{SqlScriptFormatter.FormatIdentifier(column.ColumnName)}");
outColumns.Add($"inserted.{ToSqlScript.FormatIdentifier(column.ColumnName)}");
}
// Skip columns that cannot be updated
@@ -237,11 +237,11 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
else
{
// This script isn't for command use, add the value, formatted for insertion
inValues.Add(SqlScriptFormatter.FormatValue(cell.Value, column));
inValues.Add(ToSqlScript.FormatValue(cell.Value, column));
}
// Add the column to the in columns
inColumns.Add(SqlScriptFormatter.FormatIdentifier(column.ColumnName));
inColumns.Add(ToSqlScript.FormatIdentifier(column.ColumnName));
}
// Begin the script (ie, INSERT INTO blah)

View File

@@ -12,7 +12,7 @@ using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.EditData.Contracts;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters;
namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
{
@@ -27,6 +27,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
/// <summary>
/// Internal parameterless constructor, required for mocking
/// </summary>
// ReSharper disable once UnusedMember.Global
protected internal RowEditBase() { }
/// <summary>
@@ -204,7 +205,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
else
{
// Add the clause component with the formatted value
cellDataClause = $"= {SqlScriptFormatter.FormatValue(cellData, col.DbColumn)}";
cellDataClause = $"= {ToSqlScript.FormatValue(cellData, col.DbColumn)}";
}
}
}

View File

@@ -13,7 +13,7 @@ using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.EditData.Contracts;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
@@ -81,7 +81,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
List<string> setComponents = new List<string>();
foreach (var updateElement in cellUpdates)
{
string formattedColumnName = SqlScriptFormatter.FormatIdentifier(updateElement.Value.Column.ColumnName);
string formattedColumnName = ToSqlScript.FormatIdentifier(updateElement.Value.Column.ColumnName);
string paramName = $"@Value{RowId}_{updateElement.Key}";
setComponents.Add($"{formattedColumnName} = {paramName}");
SqlParameter parameter = new SqlParameter(paramName, updateElement.Value.Column.SqlDbType)
@@ -94,7 +94,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
// Build the "OUTPUT" portion of the statement
var outColumns = from c in AssociatedResultSet.Columns
let formatted = SqlScriptFormatter.FormatIdentifier(c.ColumnName)
let formatted = ToSqlScript.FormatIdentifier(c.ColumnName)
select $"inserted.{formatted}";
string outColumnsJoined = string.Join(", ", outColumns);
@@ -148,8 +148,8 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
// Build the "SET" portion of the statement
var setComponents = cellUpdates.Values.Select(cellUpdate =>
{
string formattedColumnName = SqlScriptFormatter.FormatIdentifier(cellUpdate.Column.ColumnName);
string formattedValue = SqlScriptFormatter.FormatValue(cellUpdate.Value, cellUpdate.Column);
string formattedColumnName = ToSqlScript.FormatIdentifier(cellUpdate.Column.ColumnName);
string formattedValue = ToSqlScript.FormatValue(cellUpdate.Value, cellUpdate.Column);
return $"{formattedColumnName} = {formattedValue}";
});
string setClause = string.Join(", ", setComponents);

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -208,6 +208,8 @@ EditDataUpdateNotPending = Given row ID does not have pending update
EditDataObjectMetadataNotFound = Table or view metadata could not be found
EditDataInvalidFormat(string colName, string colType) = Invalid format for column '{0}', column is defined as {1}
EditDataInvalidFormatBinary = Invalid format for binary column
EditDataInvalidFormatBoolean = Allowed values for boolean columns are 0, 1, "true", or "false"
@@ -303,7 +305,11 @@ TestLocalizationConstant = test
############################################################################
# Utilities
SqlScriptFormatterDecimalMissingPrecision = Decimal column is missing numeric precision or numeric scale
SqlScriptFormatterDecimalMissingPrecision = Exact numeric column is missing numeric precision or numeric scale
SqlScriptFormatterLengthTypeMissingSize = Column with length is missing size
SqlScriptFormatterScalarTypeMissingScale = Scalar column missing scale
############################################################################
# Object Explorer Service

View File

@@ -523,7 +523,7 @@
<note></note>
</trans-unit>
<trans-unit id="SqlScriptFormatterDecimalMissingPrecision">
<source>Decimal column is missing numeric precision or numeric scale</source>
<source>Exact numeric column is missing numeric precision or numeric scale</source>
<target state="new">Decimal column is missing numeric precision or numeric scale</target>
<note></note>
</trans-unit>
@@ -2312,6 +2312,22 @@
<note>.
Parameters: 0 - value (string), 1 - columnType (string) </note>
</trans-unit>
<trans-unit id="EditDataInvalidFormat">
<source>Invalid format for column '{0}', column is defined as {1}</source>
<target state="new">Invalid format for column '{0}', column is defined as {1}</target>
<note>.
Parameters: 0 - colName (string), 1 - colType (string) </note>
</trans-unit>
<trans-unit id="SqlScriptFormatterLengthTypeMissingSize">
<source>Column with length is missing size</source>
<target state="new">Column with length is missing size</target>
<note></note>
</trans-unit>
<trans-unit id="SqlScriptFormatterScalarTypeMissingScale">
<source>Scalar column missing scale</source>
<target state="new">Scalar column missing scale</target>
<note></note>
</trans-unit>
<trans-unit id="StoredProcedureScriptParameterComment">
<source>-- TODO: Set parameter values here.</source>
<target state="new">-- TODO: Set parameter values here.</target>
@@ -2319,7 +2335,7 @@
</trans-unit>
<trans-unit id="ScriptingGeneralError">
<source>An error occurred while scripting the objects.</source>
<target state="new">An error occurred while scripting the objects</target>
<target state="new">An error occurred while scripting the objects.</target>
<note></note>
</trans-unit>
<trans-unit id="ScriptingExecuteNotSupportedError">

View File

@@ -10,7 +10,7 @@ using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters;
namespace Microsoft.SqlTools.ServiceLayer.Metadata
{
@@ -92,13 +92,13 @@ namespace Microsoft.SqlTools.ServiceLayer.Metadata
// The default value may be escaped
string defaultValue = smoColumn.DefaultConstraint == null
? null
: SqlScriptFormatter.UnwrapLiteral(smoColumn.DefaultConstraint.Text);
: FromSqlScript.UnwrapLiteral(smoColumn.DefaultConstraint.Text);
ColumnMetadata column = new ColumnMetadata
{
DefaultValue = defaultValue,
EscapedName = SqlScriptFormatter.FormatIdentifier(smoColumn.Name),
Ordinal = i,
EscapedName = ToSqlScript.FormatIdentifier(smoColumn.Name),
Ordinal = i
};
editColumns.Add(column);
}
@@ -109,7 +109,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Metadata
// Escape the parts of the name
string[] objectNameParts = {smoResult.Schema, smoResult.Name};
string escapedMultipartName = SqlScriptFormatter.FormatMultipartIdentifier(objectNameParts);
string escapedMultipartName = ToSqlScript.FormatMultipartIdentifier(objectNameParts);
return new TableMetadata
{

View File

@@ -18,8 +18,8 @@
<Reference Include="System.Data.SqlClient" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.8" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.8" />
</ItemGroup>
<ItemGroup>
<Compile Include="**\*.cs" />

View File

@@ -0,0 +1,149 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters
{
/// <summary>
/// Provides utilities for converting from SQL script syntax into POCOs.
/// </summary>
public static class FromSqlScript
{
// Regex: optionally starts with N, captures string wrapped in single quotes
private static readonly Regex StringRegex = new Regex("^N?'(.*)'$", RegexOptions.Compiled);
/// <summary>
/// Decodes a multipart identifier as used in a SQL script into an array of the multiple
/// parts of the identifier. Implemented as a state machine that iterates over the
/// characters of the multipart identifier.
/// </summary>
/// <param name="multipartIdentifier">Multipart identifier to decode (eg, "[dbo].[test]")</param>
/// <returns>The parts of the multipart identifier in an array (eg, "dbo", "test")</returns>
/// <exception cref="FormatException">
/// Thrown if an invalid state transition is made, indicating that the multipart identifer
/// is not valid.
/// </exception>
public static string[] DecodeMultipartIdentifier(string multipartIdentifier)
{
StringBuilder sb = new StringBuilder();
List<string> namedParts = new List<string>();
bool insideBrackets = false;
bool bracketsClosed = false;
for (int i = 0; i < multipartIdentifier.Length; i++)
{
char iChar = multipartIdentifier[i];
if (insideBrackets)
{
if (iChar == ']')
{
if (HasNextCharacter(multipartIdentifier, ']', i))
{
// This is an escaped ]
sb.Append(iChar);
i++;
}
else
{
// This bracket closes the bracket we were in
insideBrackets = false;
bracketsClosed = true;
}
}
else
{
// This is a standard character
sb.Append(iChar);
}
}
else
{
switch (iChar)
{
case '[':
if (bracketsClosed)
{
throw new FormatException();
}
// We're opening a set of brackets
insideBrackets = true;
bracketsClosed = false;
break;
case '.':
if (sb.Length == 0)
{
throw new FormatException();
}
// We're splitting the identifier into a new part
namedParts.Add(sb.ToString());
sb = new StringBuilder();
bracketsClosed = false;
break;
default:
if (bracketsClosed)
{
throw new FormatException();
}
// This is a standard character
sb.Append(iChar);
break;
}
}
}
if (sb.Length == 0)
{
throw new FormatException();
}
namedParts.Add(sb.ToString());
return namedParts.ToArray();
}
/// <summary>
/// Converts a value from a script into a plain version by unwrapping literal wrappers
/// and unescaping characters.
/// </summary>
/// <param name="literal">The value to unwrap (eg, "(N'foo''bar')")</param>
/// <returns>The unwrapped/unescaped literal (eg, "foo'bar")</returns>
public static string UnwrapLiteral(string literal)
{
// Always remove parens
literal = literal.Trim('(', ')');
// Attempt to unwrap inverted commas around a string
Match match = StringRegex.Match(literal);
if (match.Success)
{
// Like: N'stuff' or 'stuff'
return UnEscapeString(match.Groups[1].Value, '\'');
}
return literal;
}
#region Private Helpers
private static bool HasNextCharacter(string haystack, char needle, int position)
{
return position + 1 < haystack.Length
&& haystack[position + 1] == needle;
}
private static string UnEscapeString(string value, char escapeCharacter)
{
Validate.IsNotNull(nameof(value), value);
// Replace 2x of the escape character with 1x of the escape character
return value.Replace(new string(escapeCharacter, 2), escapeCharacter.ToString());
}
#endregion
}
}

View File

@@ -9,17 +9,16 @@ using System.Data.Common;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.Utility
namespace Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters
{
/// <summary>
/// Provides utility for converting arbitrary objects into strings that are ready to be
/// inserted into SQL strings
/// </summary>
public class SqlScriptFormatter
public static class ToSqlScript
{
#region Constants
@@ -27,16 +26,16 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
private static readonly Dictionary<string, Func<object, DbColumn, string>> FormatFunctions =
new Dictionary<string, Func<object, DbColumn, string>>
{ // CLR Type --------
{ // CLR Type --------
{"bigint", (val, col) => SimpleFormatter(val)}, // long
{"bit", (val, col) => FormatBool(val)}, // bool
{"int", (val, col) => SimpleFormatter(val)}, // int
{"smallint", (val, col) => SimpleFormatter(val)}, // short
{"tinyint", (val, col) => SimpleFormatter(val)}, // byte
{"money", (val, col) => FormatMoney(val, "MONEY")}, // Decimal
{"smallmoney", (val, col) => FormatMoney(val, "SMALLMONEY")}, // Decimal
{"decimal", (val, col) => FormatPreciseNumeric(val, col, "DECIMAL")}, // Decimal
{"numeric", (val, col) => FormatPreciseNumeric(val, col, "NUMERIC")}, // Decimal
{"money", FormatDecimalLike}, // Decimal
{"smallmoney", FormatDecimalLike}, // Decimal
{"decimal", FormatDecimalLike}, // Decimal
{"numeric", FormatDecimalLike}, // Decimal
{"real", (val, col) => FormatFloat(val)}, // float
{"float", (val, col) => FormatDouble(val)}, // double
{"smalldatetime", (val, col) => FormatDateTime(val, "yyyy-MM-dd HH:mm:ss")}, // DateTime
@@ -65,21 +64,105 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
// sysname - it doesn't appear possible to insert a sysname column
};
private static readonly Type[] NumericTypes =
{
typeof(byte),
typeof(short),
typeof(int),
typeof(long),
typeof(decimal),
typeof(float),
typeof(double)
};
private static Regex StringRegex = new Regex("^N?'(.*)'$", RegexOptions.Compiled);
#endregion
#region Public Methods
/// <summary>
/// Extracts a DbColumn's datatype and turns it into script ready
/// </summary>
/// <param name="column"></param>
/// <returns></returns>
/// <seealso cref="Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel.SmoColumnCustomNodeHelper.GetTypeSpecifierLabel"/>
/// <exception cref="InvalidOperationException"></exception>
public static string FormatColumnType(DbColumn column)
{
string typeName = column.DataTypeName.ToUpperInvariant();
// TODO: This doesn't support UDTs at all.
// TODO: It's unclear if this will work on a case-sensitive db collation
// If the type supports length parameters, the add those
switch (column.DataTypeName.ToLowerInvariant())
{
// Types with length
case "char":
case "nchar":
case "varchar":
case "nvarchar":
case "binary":
case "varbinary":
if (!column.ColumnSize.HasValue)
{
throw new InvalidOperationException(SR.SqlScriptFormatterLengthTypeMissingSize);
}
string length = column.ColumnSize.Value == int.MaxValue
? "MAX"
: column.ColumnSize.Value.ToString();
typeName += $"({length})";
break;
// Types with precision and scale
case "numeric":
case "decimal":
if (!column.NumericPrecision.HasValue || !column.NumericScale.HasValue)
{
throw new InvalidOperationException(SR.SqlScriptFormatterDecimalMissingPrecision);
}
typeName += $"({column.NumericPrecision}, {column.NumericScale})";
break;
// Types with scale only
case "datetime2":
case "datetimeoffset":
case "time":
if (!column.NumericScale.HasValue)
{
throw new InvalidOperationException(SR.SqlScriptFormatterScalarTypeMissingScale);
}
typeName += $"({column.NumericScale})";
break;
}
return typeName;
}
/// <summary>
/// Escapes an identifier such as a table name or column name by wrapping it in square brackets
/// </summary>
/// <param name="identifier">The identifier to format</param>
/// <returns>Identifier formatted for use in a SQL script</returns>
public static string FormatIdentifier(string identifier)
{
return $"[{EscapeString(identifier, ']')}]";
}
/// <summary>
/// Escapes a multi-part identifier such as a table name or column name with multiple
/// parts split by '.'
/// </summary>
/// <param name="identifier">The identifier to escape (eg, "dbo.test")</param>
/// <returns>The escaped identifier (eg, "[dbo].[test]")</returns>
public static string FormatMultipartIdentifier(string identifier)
{
// If the object is a multi-part identifier (eg, dbo.tablename) split it, and escape as necessary
return FormatMultipartIdentifier(identifier.Split('.'));
}
/// <summary>
/// Escapes a multipart identifier such as a table name, given an array of the parts of the
/// multipart identifier.
/// </summary>
/// <param name="identifiers">The parts of the identifier to escape (eg, "dbo", "test")</param>
/// <returns>An escaped version of the multipart identifier (eg, "[dbo].[test]")</returns>
public static string FormatMultipartIdentifier(string[] identifiers)
{
IEnumerable<string> escapedParts = identifiers.Select(FormatIdentifier);
return string.Join(".", escapedParts);
}
/// <summary>
/// Converts an object into a string for SQL script
/// </summary>
@@ -107,7 +190,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
}
return FormatFunctions[dataType](value, column);
}
/// <summary>
/// Converts a cell value into a string for SQL script
/// </summary>
@@ -120,229 +203,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
return FormatValue(value.RawObject, column);
}
/// <summary>
/// Escapes an identifier such as a table name or column name by wrapping it in square brackets
/// </summary>
/// <param name="identifier">The identifier to format</param>
/// <returns>Identifier formatted for use in a SQL script</returns>
public static string FormatIdentifier(string identifier)
{
return $"[{EscapeString(identifier, ']')}]";
}
/// <summary>
/// Escapes a multi-part identifier such as a table name or column name with multiple
/// parts split by '.'
/// </summary>
/// <param name="identifier">The identifier to escape</param>
/// <returns>The escaped identifier</returns>
public static string FormatMultipartIdentifier(string identifier)
{
// If the object is a multi-part identifier (eg, dbo.tablename) split it, and escape as necessary
return FormatMultipartIdentifier(identifier.Split('.'));
}
/// <summary>
/// Escapes a multipart identifier such as a table name, given an array of the parts of the
/// multipart identifier.
/// </summary>
/// <param name="identifiers">The parts of the identifier to escape</param>
/// <returns>An escaped version of the multipart identifier</returns>
public static string FormatMultipartIdentifier(string[] identifiers)
{
IEnumerable<string> escapedParts = identifiers.Select(FormatIdentifier);
return string.Join(".", escapedParts);
}
/// <summary>
/// Converts a value from a script into a plain version by unwrapping literal wrappers
/// and unescaping characters.
/// </summary>
/// <param name="literal">The value to unwrap</param>
/// <returns>The unwrapped/unescaped literal</returns>
public static string UnwrapLiteral(string literal)
{
// Always remove parens
literal = literal.Trim('(', ')');
// Attempt to unwrap inverted commas around a string
Match match = StringRegex.Match(literal);
if (match.Success)
{
// Like: N'stuff' or 'stuff'
return UnEscapeString(match.Groups[1].Value, '\'');
}
return literal;
}
public static string[] DecodeMultipartIdenfitier(string multipartIdentifier)
{
StringBuilder sb = new StringBuilder();
List<string> namedParts = new List<string>();
bool insideBrackets = false;
bool bracketsClosed = false;
for (int i = 0; i < multipartIdentifier.Length; i++)
{
char iChar = multipartIdentifier[i];
if (insideBrackets)
{
if (iChar == ']')
{
if (HasNextCharacter(multipartIdentifier, ']', i))
{
// This is an escaped ]
sb.Append(iChar);
i++;
}
else
{
// This bracket closes the bracket we were in
insideBrackets = false;
bracketsClosed = true;
}
}
else
{
// This is a standard character
sb.Append(iChar);
}
}
else
{
switch (iChar)
{
case '[':
if (bracketsClosed)
{
throw new FormatException();
}
// We're opening a set of brackets
insideBrackets = true;
bracketsClosed = false;
break;
case '.':
if (sb.Length == 0)
{
throw new FormatException();
}
// We're splitting the identifier into a new part
namedParts.Add(sb.ToString());
sb = new StringBuilder();
bracketsClosed = false;
break;
default:
if (bracketsClosed)
{
throw new FormatException();
}
// This is a standard character
sb.Append(iChar);
break;
}
}
}
if (sb.Length == 0)
{
throw new FormatException();
}
namedParts.Add(sb.ToString());
return namedParts.ToArray();
}
#endregion
#region Private Helpers
private static string SimpleFormatter(object value)
{
return value.ToString();
}
private static string SimpleStringFormatter(object value)
{
return EscapeQuotedSqlString(value.ToString());
}
private static string FormatMoney(object value, string type)
{
// we have to manually format the string by ToStringing the value first, and then converting
// the potential (European formatted) comma to a period.
string numericString = ((decimal)value).ToString(CultureInfo.InvariantCulture);
return $"CAST({numericString} AS {type})";
}
private static string FormatFloat(object value)
{
// The "R" formatting means "Round Trip", which preserves fidelity
return ((float)value).ToString("R");
}
private static string FormatDouble(object value)
{
// The "R" formatting means "Round Trip", which preserves fidelity
return ((double)value).ToString("R");
}
private static string FormatBool(object value)
{
// Attempt to cast to bool
bool boolValue = (bool)value;
return boolValue ? "1" : "0";
}
private static string FormatPreciseNumeric(object value, DbColumn column, string type)
{
// Make sure we have numeric precision and numeric scale
if (!column.NumericPrecision.HasValue || !column.NumericScale.HasValue)
{
throw new InvalidOperationException(SR.SqlScriptFormatterDecimalMissingPrecision);
}
// Convert the value to a decimal, then convert that to a string
string numericString = ((decimal)value).ToString(CultureInfo.InvariantCulture);
return string.Format(CultureInfo.InvariantCulture, "CAST({0} AS {1}({2}, {3}))",
numericString, type, column.NumericPrecision.Value, column.NumericScale.Value);
}
private static string FormatTimeSpan(object value)
{
// "c" provides "HH:mm:ss.FFFFFFF", and time column accepts up to 7 precision
string timeSpanString = ((TimeSpan)value).ToString("c", CultureInfo.InvariantCulture);
return EscapeQuotedSqlString(timeSpanString);
}
private static string FormatDateTime(object value, string format)
{
string dateTimeString = ((DateTime)value).ToString(format, CultureInfo.InvariantCulture);
return EscapeQuotedSqlString(dateTimeString);
}
private static string FormatDateTimeOffset(object value)
{
string dateTimeString = ((DateTimeOffset)value).ToString(CultureInfo.InvariantCulture);
return EscapeQuotedSqlString(dateTimeString);
}
private static string FormatBinary(object value)
{
byte[] bytes = value as byte[];
if (bytes == null)
{
// Bypass processing if we can't turn this into a byte[]
return "NULL";
}
return "0x" + BitConverter.ToString(bytes).Replace("-", string.Empty);
}
private static bool HasNextCharacter(string haystack, char needle, int position)
{
return position + 1 < haystack.Length
&& haystack[position + 1] == needle;
}
/// <summary>
/// Returns a valid SQL string packaged in single quotes with single quotes inside escaped
/// </summary>
@@ -352,7 +217,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
{
return $"N'{EscapeString(rawString, '\'')}'";
}
/// <summary>
/// Replaces all instances of <paramref name="escapeCharacter"/> with a duplicate of
/// <paramref name="escapeCharacter"/>. For example "can't" becomes "can''t"
@@ -375,15 +240,74 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
}
return sb.ToString();
}
private static string UnEscapeString(string value, char escapeCharacter)
private static string FormatBinary(object value)
{
Validate.IsNotNull(nameof(value), value);
byte[] bytes = value as byte[];
if (bytes == null)
{
// Bypass processing if we can't turn this into a byte[]
return "NULL";
}
// Replace 2x of the escape character with 1x of the escape character
return value.Replace(new string(escapeCharacter, 2), escapeCharacter.ToString());
return "0x" + BitConverter.ToString(bytes).Replace("-", string.Empty);
}
private static string FormatBool(object value)
{
// Attempt to cast to bool
bool boolValue = (bool)value;
return boolValue ? "1" : "0";
}
private static string FormatDateTime(object value, string format)
{
string dateTimeString = ((DateTime)value).ToString(format, CultureInfo.InvariantCulture);
return EscapeQuotedSqlString(dateTimeString);
}
private static string FormatDateTimeOffset(object value)
{
string dateTimeString = ((DateTimeOffset)value).ToString(CultureInfo.InvariantCulture);
return EscapeQuotedSqlString(dateTimeString);
}
private static string FormatDouble(object value)
{
// The "R" formatting means "Round Trip", which preserves fidelity
return ((double)value).ToString("R");
}
private static string FormatFloat(object value)
{
// The "R" formatting means "Round Trip", which preserves fidelity
return ((float)value).ToString("R");
}
private static string FormatDecimalLike(object value, DbColumn column)
{
string numericString = ((decimal)value).ToString(CultureInfo.InvariantCulture);
string typeString = FormatColumnType(column);
return $"CAST({numericString} AS {typeString})";
}
private static string FormatTimeSpan(object value)
{
// "c" provides "HH:mm:ss.FFFFFFF", and time column accepts up to 7 precision
string timeSpanString = ((TimeSpan)value).ToString("c", CultureInfo.InvariantCulture);
return EscapeQuotedSqlString(timeSpanString);
}
private static string SimpleFormatter(object value)
{
return value.ToString();
}
private static string SimpleStringFormatter(object value)
{
return EscapeQuotedSqlString(value.ToString());
}
#endregion
}
}
}