load the schema information (#1274)

* load the schema information

* pr comments
This commit is contained in:
Alan Ren
2021-10-20 12:54:54 -07:00
committed by GitHub
parent 1a52a9ee84
commit e305d30004
2 changed files with 72 additions and 6 deletions

View File

@@ -412,6 +412,31 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection
}
}
}
/// <summary>
/// Creates a IDbCommand and calls ExecuteReader using the provided connection string.
/// </summary>
/// <param name="connectionString">The connection string.</param>
/// <param name="commandText">The command text to execute</param>
/// <param name="readResult">A delegate used to read from the reader</param>
/// <param name="initializeCommand">Optional delegate to initialize the IDbCommand object</param>
/// <param name="catchException">Optional exception handling. Pass back 'true' to handle the
/// exception, 'false' to throw. If Null is passed in then all exceptions are thrown.</param>
public static void ExecuteReader(
string connectionString,
string commandText,
Action<IDataReader> readResult,
Action<IDbCommand> initializeCommand = null,
Predicate<Exception> 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);
}
}
/// <summary>
/// optional 'initializeCommand' routine. This initializes the IDbCommand

View File

@@ -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
/// </summary>
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<TableDesignerService> instance = new Lazy<TableDesignerService>(() => 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<string> 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<string> GetSchemas(TableInfo tableInfo)
{
//TODO: get the schemas.
var schemas = new List<string>();
ReliableConnectionHelper.ExecuteReader(tableInfo.ConnectionString, GetSchemasQuery, (reader) =>
{
while (reader.Read())
{
schemas.Add(reader[0].ToString());
}
});
return schemas;
}