mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-26 01:25:42 -05:00
edit/createRow Default Values (#266)
Returns strings for the default value of a column when a new row is created. The values that could come back: * `null` when there isn't a default for the column * a string when there is a default for the column * a placeholder (currently <TBD>) when the column cannot be updated * Renaming EditTableMetadata to reflect its SMO source * Implementation of returning default values * Unit test for default values * Reworking column defaults using default constraints * Adding unit test for new sql script unwrapper * Disabling flaky test * Fixing oddities in tests, removing personal tests * Fixing broken unit test
This commit is contained in:
@@ -9,6 +9,7 @@ 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;
|
||||
|
||||
@@ -64,6 +65,19 @@ 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
|
||||
|
||||
/// <summary>
|
||||
@@ -141,6 +155,27 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
|
||||
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;
|
||||
}
|
||||
|
||||
#region Private Helpers
|
||||
|
||||
private static string SimpleFormatter(object value)
|
||||
@@ -258,6 +293,14 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user