Skip to content

Commit

Permalink
Changing defaults in new database dashboard dialog (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
rishky-msft authored Feb 9, 2023
1 parent d9bcf53 commit 667e65f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 40 deletions.
78 changes: 49 additions & 29 deletions src/dialogs/newDatabaseDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as azdata from 'azdata';
import { SqlOpsDataClient } from 'dataprotocol-client';
import * as vscode from 'vscode';
import { CharsetInfo } from '../models/newDatabaseModels';
import { CancelButtonLabel, CreateButtonLabel, DatabaseCharsetDropDownLabel, DatabaseCharsetLabel, DatabaseCollationDropDownLabel, DatabaseCollationLabel, DatabaseNameLabel, DatabaseNameTextBoxLabel, DatabaseNameTextBoxPlaceHolder, NewDatabaseDetailsTitle, NewDatabaseDialogName, NewDatabaseTitle } from '../uiConstants';
import { Deferred } from '../utils/PromiseUtils';
import { ToolsServiceUtils } from '../utils/toolsserviceUtils';
Expand All @@ -20,8 +21,11 @@ export class NewDatabaseDialog {
private formBuilder: azdata.FormBuilder | undefined;
private toDispose: vscode.Disposable[] = [];
private initDialogComplete: Deferred = new Deferred();
private DEFAULT_CHARSET: string = "Default Charset";
private DEFAULT_COLLATION: string = "Default Collation"
private DEFAULT_CHARSET_VALUE = "utf8";
private DEFAULT_COLLATION_VALUE = "utf8_general_ci"
private charsetsCache: string[] = [];
private defaultCollationCache: Map<string, string> = new Map<string, string>();
private collationsCache: Map<string, string[]> = new Map<string, string[]>();

constructor(client: SqlOpsDataClient) {
this.client = client;
Expand Down Expand Up @@ -49,12 +53,15 @@ export class NewDatabaseDialog {
private async loadDialogData(): Promise<void> {
await this.loadConnectionOwnerUri();
await this.loadAndUpdateCharsetValues();
await this.tryUpdateCollationDropDown(this.DEFAULT_CHARSET_VALUE);
}

private onLoadingComplete(): void {
this.databaseCharsetDropDown.loading = false;
this.databaseNameTextBox.enabled = true;
this.databaseCharsetDropDown.loading = false;
this.databaseCharsetDropDown.enabled = true;
this.databaseCollationDropDown.loading = false;
this.databaseCollationDropDown.enabled = true;
}

private async loadConnectionOwnerUri(): Promise<void> {
Expand All @@ -64,8 +71,12 @@ export class NewDatabaseDialog {

private async loadAndUpdateCharsetValues(): Promise<void> {
try {
var charsetValues = [this.DEFAULT_CHARSET];
this.databaseCharsetDropDown.values = charsetValues.concat(await ToolsServiceUtils.getCharsets(this.connectionOwnerUri, this.client));
var charsets: CharsetInfo[] = await ToolsServiceUtils.getCharsets(this.connectionOwnerUri, this.client);
charsets.forEach(charset => {
this.charsetsCache.push(charset.name);
this.defaultCollationCache.set(charset.name, charset.defaultCollation);
});
this.databaseCharsetDropDown.values = this.charsetsCache;
}
catch (e) {
// Log the error message and keep the values of charset as default.
Expand Down Expand Up @@ -140,8 +151,8 @@ export class NewDatabaseDialog {

private createDatabaseCharsetRow(view: azdata.ModelView): azdata.FlexContainer {
this.databaseCharsetDropDown = view.modelBuilder.dropDown().withProps({
values: [this.DEFAULT_CHARSET],
value: this.DEFAULT_CHARSET,
values: [this.DEFAULT_CHARSET_VALUE],
value: this.DEFAULT_CHARSET_VALUE,
ariaLabel: DatabaseCharsetDropDownLabel,
required: false,
width: '310px',
Expand All @@ -150,7 +161,7 @@ export class NewDatabaseDialog {
}).component();

this.databaseCharsetDropDown.onValueChanged(() => {
this.tryUpdateCollationDropDown(this.databaseCharsetDropDown.value);
this.tryUpdateCollationDropDown(this.getCurrentCharset());
});

const databaseCharsetLabel = view.modelBuilder.text().withProps({
Expand All @@ -164,10 +175,14 @@ export class NewDatabaseDialog {
return databaseCharsetRow;
}

private async getCollationValues(charset_name: string | azdata.CategoryValue): Promise<string[]> {
let collationValues = [this.DEFAULT_COLLATION];
private async getCollationValues(charset: string): Promise<string[]> {
let collationValues = [this.defaultCollationCache.get(charset)];
try {
collationValues = collationValues.concat(await ToolsServiceUtils.getCollations(this.connectionOwnerUri, charset_name, this.client));
if (!this.collationsCache.has(charset)) {
let collations = await ToolsServiceUtils.getCollations(this.connectionOwnerUri, charset, this.client);
this.collationsCache.set(charset, collations);
}
collationValues = this.collationsCache.get(charset);
}
catch (e) {
// Log the error message and keep the values of collation value as default.
Expand All @@ -178,12 +193,13 @@ export class NewDatabaseDialog {

private createDatabaseCollationRow(view: azdata.ModelView): azdata.FlexContainer {
this.databaseCollationDropDown = view.modelBuilder.dropDown().withProps({
values: [this.DEFAULT_COLLATION],
value: this.DEFAULT_COLLATION,
values: [this.DEFAULT_COLLATION_VALUE],
value: this.DEFAULT_COLLATION_VALUE,
ariaLabel: DatabaseCollationDropDownLabel,
required: false,
width: '310px',
enabled: false
enabled: false,
loading: true
}).component();

const databaseCollationLabel = view.modelBuilder.text().withProps({
Expand All @@ -197,19 +213,11 @@ export class NewDatabaseDialog {
return databaseCharsetRow;
}

private async tryUpdateCollationDropDown(charset_name: string | azdata.CategoryValue): Promise<void> {
if (this.databaseCharsetDropDown.value != this.DEFAULT_CHARSET) {
this.databaseCollationDropDown.value = this.DEFAULT_COLLATION;
this.databaseCollationDropDown.loading = true;
this.databaseCollationDropDown.values = (await this.getCollationValues(charset_name));
this.databaseCollationDropDown.loading = false;
this.databaseCollationDropDown.enabled = true;
}
else {
this.databaseCollationDropDown.enabled = false;
this.databaseCollationDropDown.value = this.DEFAULT_COLLATION;
this.databaseCollationDropDown.values = [this.DEFAULT_COLLATION];
}
private async tryUpdateCollationDropDown(charset_name: string): Promise<void> {
this.databaseCollationDropDown.loading = true;
this.databaseCollationDropDown.value = this.defaultCollationCache.get(charset_name);
this.databaseCollationDropDown.values = (await this.getCollationValues(charset_name));
this.databaseCollationDropDown.loading = false;
}

private tryEnableCreateButton(): void {
Expand All @@ -228,8 +236,8 @@ export class NewDatabaseDialog {
try {
await ToolsServiceUtils.createDatabase(
this.databaseNameTextBox.value,
this.databaseCharsetDropDown.value == this.DEFAULT_CHARSET ? '' : this.databaseCharsetDropDown.value,
this.databaseCollationDropDown.value == this.DEFAULT_COLLATION ? '' : this.databaseCollationDropDown.value,
this.getCurrentCharset(),
this.getCurrentCollation(),
this.connectionOwnerUri,
this.client);
return true;
Expand All @@ -240,6 +248,18 @@ export class NewDatabaseDialog {
return false;
}

private getCurrentCharset(): string {
let charset = this.databaseCharsetDropDown.value;
let charsetValue = (typeof charset === 'string') ? charset : charset.name;
return charsetValue;
}

private getCurrentCollation(): string {
let collation = this.databaseCollationDropDown.value;
let collationValue = (typeof collation === 'string') ? collation : collation.name;
return collationValue;
}

protected showErrorMessage(message: string): void {
this.dialog.message = {
text: message,
Expand Down
7 changes: 6 additions & 1 deletion src/models/newDatabaseModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ export interface GetCharsetsRequestParams {
ownerUri: string
}

export interface CharsetInfo {
name: string,
defaultCollation: string
}

export interface GetCharsetsResponse {
charsets: string[]
charsets: CharsetInfo[]
}

export interface GetCollationsRequestParams {
Expand Down
15 changes: 6 additions & 9 deletions src/utils/toolsserviceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@ import * as azdata from "azdata";
import { SqlOpsDataClient } from "dataprotocol-client";
import { SimpleExecuteRequest } from "dataprotocol-client/lib/protocol";
import { CreateDatabaseRequest, GetCharsetsRequest, GetCollationsRequest } from "../contracts/contracts";
import { CreateDatabaseRequestParams, GetCharsetsRequestParams, GetCharsetsResponse, GetCollationsRequestParams, GetCollationsResponse } from "../models/newDatabaseModels";
import { CharsetInfo, CreateDatabaseRequestParams, GetCharsetsRequestParams, GetCharsetsResponse, GetCollationsRequestParams, GetCollationsResponse } from "../models/newDatabaseModels";

export class ToolsServiceUtils {

public static async getCharsets(ownerUri: string, client: SqlOpsDataClient): Promise<string[]> {
public static async getCharsets(ownerUri: string, client: SqlOpsDataClient): Promise<CharsetInfo[]> {
let params: GetCharsetsRequestParams = {ownerUri: ownerUri};
let result: GetCharsetsResponse = (await client.sendRequest(GetCharsetsRequest.type, params));
return result.charsets;
}

public static async getCollations(ownerUri:string, charset: string | azdata.CategoryValue, client: SqlOpsDataClient): Promise<string[]> {
let charsetvalue = (typeof charset === 'string') ? charset : charset.name;
let params: GetCollationsRequestParams = {ownerUri: ownerUri, charset: charsetvalue};
public static async getCollations(ownerUri:string, charset: string, client: SqlOpsDataClient): Promise<string[]> {
let params: GetCollationsRequestParams = {ownerUri: ownerUri, charset: charset};
let result: GetCollationsResponse = (await client.sendRequest(GetCollationsRequest.type, params));
return result.collations;
}

public static async createDatabase(dbname: string, charset: string | azdata.CategoryValue, collation: string | azdata.CategoryValue, ownerUri: string, client: SqlOpsDataClient): Promise<void> {
let charsetvalue = (typeof charset === 'string') ? charset : charset.name;
let collationvalue = (typeof collation === 'string') ? collation : collation.name;
let params: CreateDatabaseRequestParams = {ownerUri: ownerUri, dbName: dbname, charset: charsetvalue, collation: collationvalue};
public static async createDatabase(dbname: string, charset: string, collation: string, ownerUri: string, client: SqlOpsDataClient): Promise<void> {
let params: CreateDatabaseRequestParams = {ownerUri: ownerUri, dbName: dbname, charset: charset, collation: collation};
await client.sendRequest(CreateDatabaseRequest.type, params);
}

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-2.0.0.tgz#f544a148d3ab35801c1f633a7441fd87c2e484bf"
integrity sha512-XCuKFP5PS55gnMVu3dty8KPatLqUoy/ZYzDzAGCQ8JNFCkLXzmI7vNHCR+XpbZaMWQK/vQubr7PkYq8g470J/A==

"@types/azdata@1.41.0":
"@types/azdata@^1.41.0":
version "1.41.0"
resolved "https://msazure.pkgs.visualstudio.com/_packaging/AzurePortal/npm/registry/@types/azdata/-/azdata-1.41.0.tgz#503d7d6a9c4aa719d05aa39a33aa76eaed8334fc"
integrity sha1-UD19apxKpxnQWqOaM6p26u2DNPw=
Expand Down

0 comments on commit 667e65f

Please sign in to comment.