add foreign keys and constraints (#1316)

* add foreign keys and constraints

* add property name
This commit is contained in:
Alan Ren
2021-11-18 15:01:10 -08:00
committed by GitHub
parent c03557aae7
commit b131d1738d
13 changed files with 353 additions and 94 deletions

View File

@@ -11,6 +11,7 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
public const string Schema = "schema";
public const string Description = "description";
public const string Columns = "columns";
public const string ForeignKeys = "foreignKeys";
}
public static class TableColumnPropertyNames
@@ -27,4 +28,28 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
public const string IdentityIncrement = "identityIncrement";
public const string IdentitySeed = "identitySeed";
}
public static class ForeignKeyPropertyNames
{
public const string Name = "name";
public const string Enabled = "enabled";
public const string OnDeleteAction = "onDeleteAction";
public const string OnUpdateAction = "onUpdateAction";
public const string ColumnMapping = "columnMapping";
public const string PrimaryKeyTable = "primaryKeyTable";
public const string IsNotForReplication = "isNotForReplication";
}
public static class CheckConstraintPropertyNames
{
public const string Name = "name";
public const string Enabled = "enabled";
public const string Expression = "expression";
}
public static class ForeignKeyColumnMappingPropertyNames
{
public const string PrimaryKeyColumn = "primaryKeyColumn";
public const string ForeignKeyColumn = "foreignKeyColumn";
}
}

View File

@@ -12,7 +12,7 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner.Contracts
/// <summary>
/// Table component properties
/// </summary>
public abstract class TableComponentProperties<T> : ComponentPropertiesBase where T : ObjectViewModelBase
public class TableComponentProperties<T> : ComponentPropertiesBase
{
/// <summary>
/// The column names to be displayed
@@ -33,32 +33,5 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner.Contracts
/// The object list.
/// </summary>
public List<T> Data { get; set; } = new List<T>();
/// <summary>
/// Add a new object into the Data property
/// </summary>
public void AddNew()
{
this.Data.Add(this.CreateNew(this.GetDefaultNewObjectName()));
}
protected abstract string NewObjectNamePrefix { get; }
protected abstract T CreateNew(string name);
/// <summary>
/// Get the next available name for a new item
/// </summary>
protected string GetDefaultNewObjectName()
{
int i = 1;
string newName;
do
{
newName = string.Format("{0}{1}", this.NewObjectNamePrefix, i);
i++;
} while (this.Data?.AsEnumerable().FirstOrDefault(obj => string.Equals(obj.Name?.Value, newName, StringComparison.InvariantCultureIgnoreCase)) != null);
return newName;
}
}
}

View File

@@ -37,7 +37,7 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner.Contracts
/// <summary>
/// The name of the group the property will be placed in whe displayed in
/// </summary>
public bool ShowInPropertiesView { get; set; }
public bool ShowInPropertiesView { get; set; } = true;
/// <summary>

View File

@@ -14,13 +14,17 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner.Contracts
public class TableDesignerView
{
public List<DesignerDataPropertyInfo> AdditionalTableProperties { get; set; } = new List<DesignerDataPropertyInfo>();
public BuiltinTableOptions ColumnTableOptions { get; set; } = new BuiltinTableOptions();
public BuiltinTableOptions ForeignKeyTableOptions { get; set; } = new BuiltinTableOptions();
public BuiltinTableOptions CheckConstraintTableOptions { get; set; } = new BuiltinTableOptions();
}
public List<DesignerDataPropertyInfo> AdditionalTableColumnProperties { get; set; } = new List<DesignerDataPropertyInfo>();
public List<string> ColumnsTableProperties { get; set; } = new List<string>();
public bool CanAddColumns { get; set; }
public bool CanRemoveColumns { get; set; }
public class BuiltinTableOptions
{
public bool ShowTable { get; set; } = true;
public List<string> PropertiesToDisplay { get; set; } = new List<string>();
public bool canAddRows { get; set; } = true;
public bool canRemoveRows { get; set; } = true;
public List<DesignerDataPropertyInfo> AdditionalProperties { get; set; } = new List<DesignerDataPropertyInfo>();
}
}

View File

@@ -0,0 +1,18 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Newtonsoft.Json;
namespace Microsoft.SqlTools.ServiceLayer.TableDesigner.Contracts
{
/// <summary>
/// The view model of check constraint
/// </summary>
public class CheckConstraintViewModel : ObjectViewModelBase
{
public InputBoxProperties Expression { get; set; } = new InputBoxProperties();
public CheckBoxProperties Enabled { get; set; } = new CheckBoxProperties();
}
}

View File

@@ -0,0 +1,34 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Newtonsoft.Json;
namespace Microsoft.SqlTools.ServiceLayer.TableDesigner.Contracts
{
/// <summary>
/// The view model of foreign key.
/// </summary>
public class ForeignKeyViewModel : ObjectViewModelBase
{
public CheckBoxProperties Enabled { get; set; } = new CheckBoxProperties();
public DropdownProperties OnDeleteAction { get; set; } = new DropdownProperties();
public DropdownProperties OnUpdateAction { get; set; } = new DropdownProperties();
public DropdownProperties PrimaryKeyTable { get; set; } = new DropdownProperties();
public CheckBoxProperties IsNotForReplication { get; set; } = new CheckBoxProperties();
public TableComponentProperties<ForeignKeyColumnMapping> Columns { get; set; } = new TableComponentProperties<ForeignKeyColumnMapping>();
}
public class ForeignKeyColumnMapping
{
public DropdownProperties PrimaryKeyColumn { get; set; } = new DropdownProperties();
public DropdownProperties ForeignKeyColumn { get; set; } = new DropdownProperties();
}
}

View File

@@ -16,22 +16,12 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner.Contracts
public InputBoxProperties Description { get; set; } = new InputBoxProperties();
public TableColumnCollection Columns { get; set; } = new TableColumnCollection();
public TableComponentProperties<TableColumnViewModel> Columns { get; set; } = new TableComponentProperties<TableColumnViewModel>();
public TableComponentProperties<ForeignKeyViewModel> ForeignKeys { get; set; } = new TableComponentProperties<ForeignKeyViewModel>();
public TableComponentProperties<CheckConstraintViewModel> CheckConstraints { get; set; } = new TableComponentProperties<CheckConstraintViewModel>();
public InputBoxProperties Script { get; set; } = new InputBoxProperties();
}
public class TableColumnCollection : TableComponentProperties<TableColumnViewModel>
{
[JsonIgnore]
protected override string NewObjectNamePrefix { get { return "column"; } }
protected override TableColumnViewModel CreateNew(string name)
{
//TODO: Add the default values
var column = new TableColumnViewModel();
column.Name.Value = this.GetDefaultNewObjectName();
return column;
}
}
}

View File

@@ -11,7 +11,7 @@ using Microsoft.Data.SqlClient;
using Microsoft.SqlTools.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.Hosting;
using Microsoft.SqlTools.ServiceLayer.TableDesigner.Contracts;
using Table = Microsoft.Data.Tools.Sql.DesignServices.TableDesigner.TableDesignerViewModel;
using Dac = Microsoft.Data.Tools.Sql.DesignServices.TableDesigner;
namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
{
@@ -20,7 +20,7 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
/// </summary>
public sealed class TableDesignerService : IDisposable
{
private Dictionary<string, Table> idTableMap = new Dictionary<string, Table>();
private Dictionary<string, Dac.TableDesignerViewModel> idTableMap = new Dictionary<string, Dac.TableDesignerViewModel>();
private bool disposed = false;
private static readonly Lazy<TableDesignerService> instance = new Lazy<TableDesignerService>(() => new TableDesignerService());
@@ -81,7 +81,7 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
var connectinStringbuilder = new SqlConnectionStringBuilder(tableInfo.ConnectionString);
connectinStringbuilder.InitialCatalog = tableInfo.Database;
var connectionString = connectinStringbuilder.ToString();
var table = new Table(connectionString, tableInfo.Schema, tableInfo.Name, tableInfo.IsNewTable);
var table = new Dac.TableDesignerViewModel(connectionString, tableInfo.Schema, tableInfo.Name, tableInfo.IsNewTable);
this.idTableMap.Add(tableInfo.Id, table);
var viewModel = this.GetTableViewModel(tableInfo);
var view = this.GetDesignerViewInfo(tableInfo);
@@ -230,7 +230,8 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
private TableDesignerView GetDesignerViewInfo(TableInfo tableInfo)
{
var view = new TableDesignerView();
view.AdditionalTableColumnProperties.Add(new DesignerDataPropertyInfo()
view.ColumnTableOptions.AdditionalProperties.AddRange(new DesignerDataPropertyInfo[] {
new DesignerDataPropertyInfo()
{
PropertyName = TableColumnPropertyNames.IsIdentity,
Description = SR.TableColumnIsIdentityPropertyDescription,
@@ -240,8 +241,7 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
{
Title = SR.TableColumnIsIdentityPropertyTitle
}
});
view.AdditionalTableColumnProperties.Add(new DesignerDataPropertyInfo()
}, new DesignerDataPropertyInfo()
{
PropertyName = TableColumnPropertyNames.IdentitySeed,
Description = SR.TableColumnIdentitySeedPropertyDescription,
@@ -251,8 +251,7 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
{
Title = SR.TableColumnIdentitySeedPropertyTitle
}
});
view.AdditionalTableColumnProperties.Add(new DesignerDataPropertyInfo()
},new DesignerDataPropertyInfo()
{
PropertyName = TableColumnPropertyNames.IdentityIncrement,
Description = SR.TableColumnIdentityIncrementPropertyDescription,
@@ -262,15 +261,52 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
{
Title = SR.TableColumnIdentityIncrementPropertyTitle
}
});
view.CanAddColumns = true;
view.CanRemoveColumns = true;
}});
view.ColumnTableOptions.canAddRows = true;
view.ColumnTableOptions.canRemoveRows = true;
view.ForeignKeyTableOptions.AdditionalProperties.AddRange(new DesignerDataPropertyInfo[] {
new DesignerDataPropertyInfo()
{
PropertyName = ForeignKeyPropertyNames.Enabled,
Description = SR.ForeignKeyIsEnabledDescription,
ComponentType = DesignerComponentType.Checkbox,
ComponentProperties = new CheckBoxProperties()
{
Title = SR.TableDesignerIsEnabledPropertyTitle
}
},
new DesignerDataPropertyInfo()
{
PropertyName = ForeignKeyPropertyNames.IsNotForReplication,
Description = SR.ForeignKeyIsNotForReplicationDescription,
ComponentType = DesignerComponentType.Checkbox,
ComponentProperties = new CheckBoxProperties()
{
Title = SR.ForeignKeyIsNotForReplicationTitle
}
}});
view.ForeignKeyTableOptions.canAddRows = true;
view.ForeignKeyTableOptions.canRemoveRows = true;
view.CheckConstraintTableOptions.AdditionalProperties.Add(
new DesignerDataPropertyInfo()
{
PropertyName = CheckConstraintPropertyNames.Enabled,
Description = SR.CheckConstraintIsEnabledDescription,
ComponentType = DesignerComponentType.Checkbox,
ComponentProperties = new CheckBoxProperties()
{
Title = SR.TableDesignerIsEnabledPropertyTitle
}
});
view.CheckConstraintTableOptions.canAddRows = true;
view.CheckConstraintTableOptions.canRemoveRows = true;
return view;
}
private Table GetTable(TableInfo tableInfo)
private Dac.TableDesignerViewModel GetTable(TableInfo tableInfo)
{
Table table;
Dac.TableDesignerViewModel table;
if (this.idTableMap.TryGetValue(tableInfo.Id, out table))
{
return table;