add foreign keys and constraints (#17697)

* foreign keys and constraints

* refactoring

* fix issues
This commit is contained in:
Alan Ren
2021-11-17 19:15:24 -08:00
committed by GitHub
parent 6f03cbac97
commit 7c26e14605
8 changed files with 310 additions and 75 deletions

View File

@@ -10,7 +10,6 @@ import { localize } from 'vs/nls';
import { designers } from 'sql/workbench/api/common/sqlExtHostTypes';
import { Emitter, Event } from 'vs/base/common/event';
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
import { ColumnPropertyDescriptions, TablePropertyDescriptions } from 'sql/workbench/services/tableDesigner/browser/tableDesignerStrings';
import { deepClone, equals } from 'vs/base/common/objects';
export class TableDesignerComponentInput implements DesignerComponentInput {
@@ -146,11 +145,46 @@ export class TableDesignerComponentInput implements DesignerComponentInput {
this._originalViewModel = deepClone(this._viewModel);
this.setDefaultData();
const tabs = [];
if (designerInfo.view.showColumnsTab) {
tabs.push(this.getColumnsTab(designerInfo));
}
if (designerInfo.view.showForeignKeysTab) {
tabs.push(this.getForeignKeysTab(designerInfo));
}
if (designerInfo.view.showCheckConstraintsTab) {
tabs.push(this.getCheckConstraintsTab(designerInfo));
}
if (designerInfo.view.additionalTabs) {
tabs.push(...designerInfo.view.additionalTabs);
}
tabs.push(this.getGeneralTab(designerInfo));
this._view = {
components: [{
componentType: 'input',
propertyName: designers.TableColumnProperty.Name,
description: localize('designer.table.description.name', "The name of the table object."),
componentProperties: {
title: localize('tableDesigner.nameTitle', "Table name"),
width: 200
}
}],
tabs: tabs
};
}
private getGeneralTab(designerInfo: azdata.designers.TableDesignerInfo): DesignerTab {
const generalTabComponents: DesignerDataPropertyInfo[] = [
{
componentType: 'dropdown',
propertyName: designers.TableProperty.Schema,
description: TablePropertyDescriptions.SCHEMA,
description: localize('designer.table.description.schema', "The schema that contains the table."),
componentProperties: <DropDownProperties>{
title: localize('tableDesigner.schemaTitle', "Schema"),
values: designerInfo.schemas
@@ -158,7 +192,7 @@ export class TableDesignerComponentInput implements DesignerComponentInput {
}, {
componentType: 'input',
propertyName: designers.TableProperty.Description,
description: TablePropertyDescriptions.DESCRIPTION,
description: localize('designer.table.description.description', "Description for the table."),
componentProperties: {
title: localize('tableDesigner.descriptionTitle', "Description")
}
@@ -169,16 +203,19 @@ export class TableDesignerComponentInput implements DesignerComponentInput {
generalTabComponents.push(...designerInfo.view.additionalTableProperties);
}
const generalTab = <DesignerTab>{
return <DesignerTab>{
title: localize('tableDesigner.generalTab', "General"),
components: generalTabComponents
};
}
private getColumnsTab(designerInfo: azdata.designers.TableDesignerInfo): DesignerTab {
const columnProperties: DesignerDataPropertyInfo[] = [
{
componentType: 'input',
propertyName: designers.TableColumnProperty.Name,
description: ColumnPropertyDescriptions.NAME,
description: localize('designer.column.description.name', "The name of the column object."),
componentProperties: {
title: localize('tableDesigner.columnNameTitle', "Name"),
width: 150
@@ -186,7 +223,7 @@ export class TableDesignerComponentInput implements DesignerComponentInput {
}, {
componentType: 'dropdown',
propertyName: designers.TableColumnProperty.Type,
description: ColumnPropertyDescriptions.DATA_TYPE,
description: localize('designer.column.description.dataType', "Displays the data type name for the column"),
componentProperties: {
title: localize('tableDesigner.columnTypeTitle', "Type"),
width: 100,
@@ -195,7 +232,7 @@ export class TableDesignerComponentInput implements DesignerComponentInput {
}, {
componentType: 'input',
propertyName: designers.TableColumnProperty.Length,
description: ColumnPropertyDescriptions.LENGTH,
description: localize('designer.column.description.length', "The maximum length (in characters) that can be stored in this database object."),
componentProperties: {
title: localize('tableDesigner.columnLengthTitle', "Length"),
width: 60
@@ -203,7 +240,7 @@ export class TableDesignerComponentInput implements DesignerComponentInput {
}, {
componentType: 'input',
propertyName: designers.TableColumnProperty.DefaultValue,
description: ColumnPropertyDescriptions.DEFAULT_VALUE_OR_BINDING,
description: localize('designer.column.description.defaultValueBinding', "A predefined global default value for the column or binding."),
componentProperties: {
title: localize('tableDesigner.columnDefaultValueTitle', "Default Value"),
width: 150
@@ -211,21 +248,21 @@ export class TableDesignerComponentInput implements DesignerComponentInput {
}, {
componentType: 'checkbox',
propertyName: designers.TableColumnProperty.AllowNulls,
description: ColumnPropertyDescriptions.ALLOW_NULLS,
description: localize('designer.column.description.allowNulls', "Specifies whether the column may have a NULL value."),
componentProperties: {
title: localize('tableDesigner.columnAllowNullTitle', "Allow Nulls"),
}
}, {
componentType: 'checkbox',
propertyName: designers.TableColumnProperty.IsPrimaryKey,
description: ColumnPropertyDescriptions.PRIMARY_KEY,
description: localize('designer.column.description.primaryKey', "Specifies whether the column is included in the primary key for the table."),
componentProperties: {
title: localize('tableDesigner.columnIsPrimaryKeyTitle', "Primary Key"),
}
}, {
componentType: 'input',
propertyName: designers.TableColumnProperty.Precision,
description: ColumnPropertyDescriptions.PRECISION,
description: localize('designer.column.description.precision', "For numeric data, the maximum number of decimal digits that can be stored in this database object."),
componentProperties: {
title: localize('tableDesigner.columnPrecisionTitle', "Precision"),
width: 60
@@ -233,7 +270,7 @@ export class TableDesignerComponentInput implements DesignerComponentInput {
}, {
componentType: 'input',
propertyName: designers.TableColumnProperty.Scale,
description: ColumnPropertyDescriptions.NAME,
description: localize('designer.column.description.scale', "For numeric data, the maximum number of decimal digits that can be stored in this database object to the right of decimal point."),
componentProperties: {
title: localize('tableDesigner.columnScaleTitle', "Scale"),
width: 60
@@ -256,7 +293,7 @@ export class TableDesignerComponentInput implements DesignerComponentInput {
designers.TableColumnProperty.DefaultValue,
];
const columnsTab = <DesignerTab>{
return <DesignerTab>{
title: localize('tableDesigner.columnsTabTitle', "Columns"),
components: [
{
@@ -274,26 +311,154 @@ export class TableDesignerComponentInput implements DesignerComponentInput {
}
]
};
}
const tabs = [columnsTab, generalTab];
if (designerInfo.view.additionalTabs) {
tabs.push(...tabs);
}
private getForeignKeysTab(designerInfo: azdata.designers.TableDesignerInfo): DesignerTab {
this._view = {
components: [{
componentType: 'input',
propertyName: designers.TableColumnProperty.Name,
description: TablePropertyDescriptions.NAME,
const foreignKeyColumnMappingProperties: DesignerDataPropertyInfo[] = [
{
componentType: 'dropdown',
propertyName: designers.ForeignKeyColumnMappingProperty.ForeignKeyColumn,
componentProperties: {
title: localize('tableDesigner.nameTitle', "Table name"),
title: localize('tableDesigner.foreignKeyColumn', "Foreign Key Column"),
width: 150
}
},
{
componentType: 'dropdown',
propertyName: designers.ForeignKeyColumnMappingProperty.PrimaryKeyColumn,
componentProperties: {
title: localize('tableDesigner.primaryKeyColumn', "Primary Key Column"),
width: 150
}
},
];
const foreignKeyProperties: DesignerDataPropertyInfo[] = [
{
componentType: 'input',
propertyName: designers.TableForeignKeyProperty.Name,
description: localize('designer.foreignkey.description.name', "The name of the foreign key."),
componentProperties: {
title: localize('tableDesigner.foreignKeyNameTitle', "Name"),
width: 300
}
},
{
componentType: 'dropdown',
propertyName: designers.TableForeignKeyProperty.PrimaryKeyTable,
description: localize('designer.foreignkey.description.primaryKeyTable', "The table which contains the primary or unique key column."),
componentProperties: {
title: localize('tableDesigner.PrimaryKeyTableName', "Primary Key Table"),
width: 200
}
}],
tabs: tabs
},
{
componentType: 'dropdown',
propertyName: designers.TableForeignKeyProperty.OnUpdateAction,
description: localize('designer.foreignkey.description.onUpdateAction', "The behavior when a user tries to update a row with data that is involved in a foreign key relationship."),
componentProperties: {
title: localize('tableDesigner.foreignKeyOnUpdateAction', "On Update Action"),
width: 100
}
},
{
componentType: 'dropdown',
propertyName: designers.TableForeignKeyProperty.OnDeleteAction,
description: localize('designer.foreignkey.description.onDeleteAction', "The behavior when a user tries to delete a row with data that is involved in a foreign key relationship."),
componentProperties: {
title: localize('tableDesigner.foreignKeyOnDeleteAction', "On Delete Action"),
width: 100
}
},
{
componentType: 'table',
propertyName: designers.TableForeignKeyProperty.Columns,
group: localize('tableDesigner.foreignKeyColumns', "Column Mapping"),
componentProperties: <DesignerTableProperties>{
ariaLabel: localize('tableDesigner.foreignKeyColumns', "Column Mapping"),
columns: [designers.ForeignKeyColumnMappingProperty.ForeignKeyColumn, designers.ForeignKeyColumnMappingProperty.PrimaryKeyColumn],
itemProperties: foreignKeyColumnMappingProperties,
objectTypeDisplayName: '',
canAddRows: designerInfo.view.canAddForeignKeys,
canRemoveRows: designerInfo.view.canRemoveColumns,
}
},
];
if (designerInfo.view.additionalForeignKeyProperties) {
foreignKeyProperties.push(...designerInfo.view.additionalForeignKeyProperties);
}
const foreignKeysTableProperties = designerInfo.view.foreignKeysTableProperties?.length > 0 ? designerInfo.view.foreignKeysTableProperties : [
designers.TableForeignKeyProperty.Name,
designers.TableForeignKeyProperty.PrimaryKeyTable,
];
return <DesignerTab>{
title: localize('tableDesigner.foreignKeysTabTitle', "Foreign Keys"),
components: [
{
componentType: 'table',
propertyName: designers.TableProperty.ForeignKeys,
showInPropertiesView: false,
componentProperties: <DesignerTableProperties>{
ariaLabel: localize('tableDesigner.foreignKeysTabTitle', "Foreign Keys"),
columns: foreignKeysTableProperties,
itemProperties: foreignKeyProperties,
objectTypeDisplayName: localize('tableDesigner.ForeignKeyTypeName', "Foreign Key"),
canAddRows: designerInfo.view.canAddForeignKeys,
canRemoveRows: designerInfo.view.canRemoveForeignKeys
}
}
]
};
}
private getCheckConstraintsTab(designerInfo: azdata.designers.TableDesignerInfo): DesignerTab {
const checkConstraintProperties: DesignerDataPropertyInfo[] = [
{
componentType: 'input',
propertyName: designers.TableCheckConstraintProperty.Name,
description: localize('designer.checkConstraint.description.name', "The name of the check constraint."),
componentProperties: {
title: localize('tableDesigner.checkConstraintNameTitle', "Name"),
width: 200
}
}, {
componentType: 'input',
propertyName: designers.TableCheckConstraintProperty.Expression,
description: localize('designer.checkConstraint.description.expression', "The expression defining the check constraint."),
componentProperties: {
title: localize('tableDesigner.checkConstraintExpressionTitle', "Expression"),
width: 300
}
}
];
if (designerInfo.view.additionalCheckConstraintProperties) {
checkConstraintProperties.push(...designerInfo.view.additionalCheckConstraintProperties);
}
return <DesignerTab>{
title: localize('tableDesigner.checkConstraintsTabTitle', "Check Constraints"),
components: [
{
componentType: 'table',
propertyName: designers.TableProperty.CheckConstraints,
showInPropertiesView: false,
componentProperties: <DesignerTableProperties>{
ariaLabel: localize('tableDesigner.checkConstraintsTabTitle', "Check Constraints"),
columns: [designers.TableCheckConstraintProperty.Name, designers.TableCheckConstraintProperty.Expression],
itemProperties: checkConstraintProperties,
objectTypeDisplayName: localize('tableDesigner.checkConstraintTypeName', "Check Constraint"),
canAddRows: designerInfo.view.canAddCheckConstraints,
canRemoveRows: designerInfo.view.canRemoveCheckConstraints
}
}
]
};
}
private setDefaultData(): void {
const properties = Object.keys(this._viewModel);
this.setDefaultInputData(properties, designers.TableProperty.Name);