diff --git a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs index 33b5f92f..94a84adf 100644 --- a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs +++ b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs @@ -412,6 +412,31 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection } } } + /// + /// Creates a IDbCommand and calls ExecuteReader using the provided connection string. + /// + /// The connection string. + /// The command text to execute + /// A delegate used to read from the reader + /// Optional delegate to initialize the IDbCommand object + /// Optional exception handling. Pass back 'true' to handle the + /// exception, 'false' to throw. If Null is passed in then all exceptions are thrown. + public static void ExecuteReader( + string connectionString, + string commandText, + Action readResult, + Action initializeCommand = null, + Predicate catchException = null) + { + Validate.IsNotNull(nameof(connectionString), connectionString); + Validate.IsNotNullOrEmptyString(nameof(commandText), commandText); + Validate.IsNotNull(nameof(readResult), readResult); + using (var sqlConnection = new SqlConnection(connectionString)) + { + sqlConnection.Open(); + ExecuteReader(sqlConnection, commandText, readResult, initializeCommand, catchException); + } + } /// /// optional 'initializeCommand' routine. This initializes the IDbCommand diff --git a/src/Microsoft.SqlTools.ServiceLayer/TableDesigner/TableDesignerService.cs b/src/Microsoft.SqlTools.ServiceLayer/TableDesigner/TableDesignerService.cs index 60e511a8..69f81086 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/TableDesigner/TableDesignerService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/TableDesigner/TableDesignerService.cs @@ -6,7 +6,9 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.Data.SqlClient; using Microsoft.SqlTools.Hosting.Protocol; +using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection; using Microsoft.SqlTools.ServiceLayer.Hosting; using Microsoft.SqlTools.ServiceLayer.TableDesigner.Contracts; @@ -17,6 +19,9 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner /// public sealed class TableDesignerService : IDisposable { + // The query is copied from SSMS table designer, sys and INFORMATION_SCHEMA can not be selected. + const string GetSchemasQuery = "select name from sys.schemas where principal_id <> 3 and principal_id <> 4 order by name"; + private bool disposed = false; private static readonly Lazy instance = new Lazy(() => new TableDesignerService()); @@ -59,15 +64,15 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner { try { - // TODO: populate the data and view information - TableViewModel tableModel = new TableViewModel(); - TableDesignerView view = new TableDesignerView(); + var schemas = this.GetSchemas(tableInfo); + var viewModel = this.GetTableViewModel(tableInfo, schemas); + var view = this.GetDesignerViewInfo(tableInfo); await requestContext.SendResult(new TableDesignerInfo() { - ViewModel = tableModel, + ViewModel = viewModel, View = view, ColumnTypes = this.GetSupportedColumnTypes(tableInfo), - Schemas = this.GetSchemas(tableInfo) + Schemas = schemas }); } catch (Exception e) @@ -170,10 +175,46 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner return columnTypes; } + private TableViewModel GetTableViewModel(TableInfo tableInfo, List schemas) + { + var tableViewModel = new TableViewModel(); + // Schema + if (tableInfo.IsNewTable) + { + tableViewModel.Schema.Value = schemas.Contains("dbo") ? "dbo" : schemas[0]; + } + else + { + tableViewModel.Schema.Value = tableInfo.Schema; + } + + // Table Name + if (!tableInfo.IsNewTable) + { + tableViewModel.Name.Value = tableInfo.Name; + } + + // TODO: set other properties of the table + return tableViewModel; + } + + private TableDesignerView GetDesignerViewInfo(TableInfo tableInfo) + { + // TODO: set the view information + var view = new TableDesignerView(); + return view; + } + private List GetSchemas(TableInfo tableInfo) { - //TODO: get the schemas. var schemas = new List(); + ReliableConnectionHelper.ExecuteReader(tableInfo.ConnectionString, GetSchemasQuery, (reader) => + { + while (reader.Read()) + { + schemas.Add(reader[0].ToString()); + } + }); return schemas; }