foreign keys and constraints (#1318)

* foreign keys and constraints

* revert sdk change
This commit is contained in:
Alan Ren
2021-11-19 16:44:52 -08:00
committed by GitHub
parent c942de3b25
commit 0599de2de7
3 changed files with 92 additions and 1 deletions

View File

@@ -20,7 +20,7 @@
<PackageReference Update="Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider" Version="1.1.1" />
<PackageReference Update="Microsoft.Data.SqlClient" Version="3.0.0"/>
<PackageReference Update="Microsoft.SqlServer.SqlManagementObjects" Version="161.46367.54" />
<PackageReference Update="Microsoft.SqlServer.DACFx" Version="160.5332.1-preview" GeneratePathProperty="true" />
<PackageReference Update="Microsoft.SqlServer.DACFx" Version="160.5339.7-preview" GeneratePathProperty="true" />
<PackageReference Update="Microsoft.Azure.Kusto.Data" Version="9.0.4" />
<PackageReference Update="Microsoft.Azure.Kusto.Language" Version="9.0.4"/>
<PackageReference Update="Microsoft.SqlServer.Assessment" Version="[1.0.305]" />

View File

@@ -0,0 +1,56 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Data.Tools.Sql.DesignServices.TableDesigner;
namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
{
public static class SqlForeignKeyActionUtil
{
private static Dictionary<string, SqlForeignKeyAction> mapping = new Dictionary<string, SqlForeignKeyAction>();
static SqlForeignKeyActionUtil()
{
mapping.Add(SR.SqlForeignKeyAction_NoAction, SqlForeignKeyAction.NoAction);
mapping.Add(SR.SqlForeignKeyAction_Cascade, SqlForeignKeyAction.Cascade);
mapping.Add(SR.SqlForeignKeyAction_SetNull, SqlForeignKeyAction.SetNull);
mapping.Add(SR.SqlForeignKeyAction_SetDefault, SqlForeignKeyAction.SetDefault);
}
public static List<string> ActionNames
{
get
{
return mapping.Keys.ToList();
}
}
public static string GetName(SqlForeignKeyAction action)
{
foreach (var key in mapping.Keys)
{
if (mapping[key] == action)
{
return key;
}
}
throw new NotSupportedException(SR.UnKnownSqlForeignKeyAction(action.ToString()));
}
public static SqlForeignKeyAction GetValue(string displayName)
{
if (mapping.ContainsKey(displayName))
{
return mapping[displayName];
}
else
{
throw new KeyNotFoundException(SR.UnKnownSqlForeignKeyAction(displayName));
}
}
}
}

View File

@@ -221,6 +221,41 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
tableViewModel.Columns.Data.Add(columnViewModel);
}
foreach (var foreignKey in table.ForeignKeys.Items)
{
var foreignKeyViewModel = new ForeignKeyViewModel();
foreignKeyViewModel.Name.Value = foreignKey.Name;
foreignKeyViewModel.Enabled.Checked = foreignKey.Enabled;
foreignKeyViewModel.OnDeleteAction.Value = SqlForeignKeyActionUtil.GetName(foreignKey.OnDeleteAction);
foreignKeyViewModel.OnDeleteAction.Values = SqlForeignKeyActionUtil.ActionNames;
foreignKeyViewModel.OnUpdateAction.Value = SqlForeignKeyActionUtil.GetName(foreignKey.OnUpdateAction);
foreignKeyViewModel.OnUpdateAction.Values = SqlForeignKeyActionUtil.ActionNames;
foreignKeyViewModel.PrimaryKeyTable.Value = foreignKey.PrimaryKeyTable;
foreignKeyViewModel.PrimaryKeyTable.Values = table.AllTables.ToList();
foreignKeyViewModel.IsNotForReplication.Checked = foreignKey.IsNotForReplication;
for (int i = 0; i < foreignKey.ForeignKeyColumns.Count; i++)
{
var foreignKeyColumn = foreignKey.ForeignKeyColumns[i];
var primaryKeyColumn = foreignKey.PrimaryKeyColumns[i];
var mapping = new ForeignKeyColumnMapping();
mapping.ForeignKeyColumn.Value = foreignKeyColumn;
mapping.ForeignKeyColumn.Values = table.Columns.Items.Select(c => c.Name).ToList();
mapping.PrimaryKeyColumn.Value = primaryKeyColumn;
mapping.PrimaryKeyColumn.Values = table.GetColumnsForTable(foreignKey.PrimaryKeyTable).ToList();
foreignKeyViewModel.Columns.Data.Add(mapping);
}
tableViewModel.ForeignKeys.Data.Add(foreignKeyViewModel);
}
foreach (var checkConstraint in table.CheckConstraints.Items)
{
var constraint = new CheckConstraintViewModel();
constraint.Name.Value = checkConstraint.Name;
constraint.Expression.Value = checkConstraint.Expression;
constraint.Enabled.Checked = checkConstraint.Enabled;
tableViewModel.CheckConstraints.Data.Add(constraint);
}
tableViewModel.Script.Enabled = false;
tableViewModel.Script.Value = table.Script;
// TODO: set other properties of the table