Have default values in add database dialog input boxes (#12155)

* show default values in text boxes

* add sqlcmd formatting

* add tests

* Add some sqlcmd variable name validation

* Addressing comments

* fixes after merge

* fix test

* don't localize OtherServer

* fix for windows

* one more fix

* fix test
This commit is contained in:
Kim Santiago
2020-09-11 14:05:39 -07:00
committed by GitHub
parent 61ceb72cea
commit a567ff6de4
8 changed files with 196 additions and 29 deletions

View File

@@ -119,6 +119,8 @@ export const exampleUsage = localize('exampleUsage', "Example Usage");
export const enterSystemDbName = localize('enterSystemDbName', "Enter a database name for this system database");
export const databaseNameRequiredVariableOptional = localize('databaseNameRequiredVariableOptional', "A database name is required. The database variable is optional.");
export const databaseNameServerNameVariableRequired = localize('databaseNameServerNameVariableRequired', "A database name, server name, and server variable are required. The database variable is optional");
export const otherServer = 'OtherServer';
export const otherSeverVariable = '$(OtherServer)';
export const databaseProject = localize('databaseProject', "Database project");
// Error messages
@@ -166,6 +168,7 @@ export function unexpectedProjectContext(uri: string) { return localize('unexpec
export function unableToPerformAction(action: string, uri: string) { return localize('unableToPerformAction', "Unable to locate '{0}' target: '{1}'", action, uri); }
export function unableToFindObject(path: string, objType: string) { return localize('unableToFindFile', "Unable to find {1} with path '{0}'", path, objType); }
export function deployScriptExists(scriptType: string) { return localize('deployScriptExists', "A {0} script already exists. The new script will not be included in build.", scriptType); }
export function notValidVariableName(name: string) { return localize('notValidVariableName', "The variable name '{0}' is not valid.", name); }
export function cantAddCircularProjectReference(project: string) { return localize('cantAddCircularProjectReference', "A reference to project '{0} cannot be added. Adding this project as a reference would cause a circular dependency", project); }
// Action types
@@ -280,5 +283,5 @@ export const systemDbs = ['master', 'msdb', 'tempdb', 'model'];
// SQL queries
export const sameDatabaseExampleUsage = 'SELECT * FROM [Schema1].[Table1]';
export function differentDbSameServerExampleUsage(db: string) { return `SELECT * FROM [${db}].[Schema1].[Table1]"`; }
export function differentDbSameServerExampleUsage(db: string) { return `SELECT * FROM [${db}].[Schema1].[Table1]`; }
export function differentDbDifferentServerExampleUsage(server: string, db: string) { return `SELECT * FROM [${server}].[${db}].[Schema1].[Table1]`; }

View File

@@ -146,6 +146,77 @@ export function readSqlCmdVariables(xmlDoc: any): Record<string, string> {
}
/**
* Removes $() around a sqlcmd variable
* @param name
*/
export function removeSqlCmdVariableFormatting(name: string | undefined): string {
if (!name || name === '') {
return '';
}
if (name.length > 3) {
// Trim in case we get " $(x)"
name = name.trim();
let indexStart = name.startsWith('$(') ? 2 : 0;
let indexEnd = name.endsWith(')') ? 1 : 0;
if (indexStart > 0 || indexEnd > 0) {
name = name.substr(indexStart, name.length - indexEnd - indexStart);
}
}
// Trim in case the customer types " $(x )"
return name.trim();
}
/**
* Format as sqlcmd variable by adding $() if necessary
* if the variable already starts with $(, then add )
* @param name
*/
export function formatSqlCmdVariable(name: string): string {
if (!name || name === '') {
return name;
}
// Trim in case we get " $(x)"
name = name.trim();
if (!name.startsWith('$(') && !name.endsWith(')')) {
name = `$(${name})`;
} else if (name.startsWith('$(') && !name.endsWith(')')) {
// add missing end parenthesis, same behavior as SSDT
name = `${name})`;
}
return name;
}
/**
* 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 {
// remove $() around named if it's there
name = removeSqlCmdVariableFormatting(name);
// can't contain whitespace
if (!name || name.trim() === '' || name.includes(' ')) {
return false;
}
// can't contain these characters
if (name.includes('$') || name.includes('@') || name.includes('#') || name.includes('"') || name.includes('\'') || name.includes('-')) {
return false;
}
// 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;
}
/*
* Recursively gets all the sqlproj files at any depth in a folder
* @param folderPath
*/