Surfacing better error messages about SQLCMD var names (#22509)

* Surfacing better error messages about SQLCMD var names

* correcting docstring

* adding space to join char
This commit is contained in:
Benjin Dubishar
2023-03-29 14:23:03 -07:00
committed by GitHub
parent 2a9705c495
commit edc2c5e200
5 changed files with 37 additions and 38 deletions

View File

@@ -242,25 +242,24 @@ export function formatSqlCmdVariable(name: string): string {
* Checks if it's a valid sqlcmd variable name
* https://docs.microsoft.com/en-us/sql/ssms/scripting/sqlcmd-use-with-scripting-variables?redirectedfrom=MSDN&view=sql-server-ver15#guidelines-for-scripting-variable-names-and-values
* @param name variable name to validate
*/
export function isValidSqlCmdVariableName(name: string | undefined): boolean {
* @returns null if valid, otherwise an error message describing why input is invalid
*/
export function validateSqlCmdVariableName(name: string | undefined): string | null {
// remove $() around named if it's there
name = removeSqlCmdVariableFormatting(name);
const cleanedName = removeSqlCmdVariableFormatting(name);
// can't contain whitespace
if (!name || name.trim() === '' || name.includes(' ')) {
return false;
if (!cleanedName || cleanedName.trim() === '' || cleanedName.includes(' ')) {
return constants.sqlcmdVariableNameCannotContainWhitespace(name ?? '');
}
// can't contain these characters
if (name.includes('$') || name.includes('@') || name.includes('#') || name.includes('"') || name.includes('\'') || name.includes('-')) {
return false;
if (constants.illegalSqlCmdChars.some(c => cleanedName?.includes(c))) {
return constants.sqlcmdVariableNameCannotContainIllegalChars(name ?? '');
}
// TODO: tsql parsing to check if it's a reserved keyword or invalid tsql https://github.com/microsoft/azuredatastudio/issues/12204
// TODO: give more detail why variable name was invalid https://github.com/microsoft/azuredatastudio/issues/12231
return true;
return null;
}
/**