Skip to content

Commit

Permalink
Improved TDS Endpoint support
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Jan 28, 2022
1 parent 9d1d8c9 commit 51b1066
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
3 changes: 3 additions & 0 deletions MarkMpn.Sql4Cds.Engine/Ado/Sql4CdsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public override void Prepare()
if (_plan != null)
return;

if (_connection.Options.UseTDSEndpoint)
_planBuilder.TDSEndpointAvailable = _connection.TDSEndpointEnabled;

_plan = _planBuilder.Build(CommandText, ((Sql4CdsParameterCollection)Parameters).GetParameterTypes());
}

Expand Down
16 changes: 16 additions & 0 deletions MarkMpn.Sql4Cds.Engine/Ado/Sql4CdsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,21 @@ protected override DbCommand CreateDbCommand()
{
return new Sql4CdsCommand(this);
}

public bool TDSEndpointEnabled
{
get
{
#if NETCOREAPP
if (!(DataSources[Database].Connection is ServiceClient svc))
return false;
#else
if (!(DataSources[Database].Connection is CrmServiceClient svc))
return false;
#endif

return TSqlEndpoint.IsEnabled(svc);
}
}
}
}
17 changes: 17 additions & 0 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/SqlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ public DataTable Execute(IDictionary<string, DataSource> dataSources, IQueryExec
{
cmd.CommandTimeout = (int)TimeSpan.FromMinutes(2).TotalSeconds;
cmd.CommandText = Sql;

foreach (var paramValue in parameterValues)
{
if (paramValue.Key.StartsWith("@@"))
continue;

var param = cmd.CreateParameter();
param.ParameterName = paramValue.Key;

if (paramValue.Value is SqlEntityReference er)
param.Value = (SqlGuid)er;
else
param.Value = paramValue.Value;

cmd.Parameters.Add(param);
}

var result = new DataTable();

using (var adapter = new SqlDataAdapter(cmd))
Expand Down
37 changes: 34 additions & 3 deletions MarkMpn.Sql4Cds.Engine/TSqlEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Xml;
using Microsoft.Xrm.Sdk.Query;
using System.Net.Sockets;
#if NETCOREAPP
using Microsoft.PowerPlatform.Dataverse.Client;
#else
Expand All @@ -13,7 +14,7 @@ namespace MarkMpn.Sql4Cds.Engine
{
public static class TSqlEndpoint
{
private static IDictionary<string, bool> _cache = new Dictionary<string, bool>();
private static readonly IDictionary<string, bool> _cache = new Dictionary<string, bool>();

#if NETCOREAPP
public static bool IsEnabled(ServiceClient svc)
Expand All @@ -36,7 +37,7 @@ public static bool IsEnabled(CrmServiceClient svc)

if (String.IsNullOrEmpty(orgSettings))
{
enabled = false;
enabled = TestConnection(svc);
}
else
{
Expand All @@ -45,14 +46,44 @@ public static bool IsEnabled(CrmServiceClient svc)

var enabledNode = xml.SelectSingleNode("//EnableTDSEndpoint/text()");

enabled = enabledNode?.Value == "true";
if (enabledNode != null)
enabled = enabledNode.Value == "true";
else
enabled = TestConnection(svc);
}

_cache[host] = enabled;

return enabled;
}

#if NETCOREAPP
private static bool TestConnection(ServiceClient svc)
#else
private static bool TestConnection(CrmServiceClient svc)
#endif
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

#if NETCOREAPP
var result = socket.BeginConnect(svc.ConnectedOrgUriActual.Host, 1433, null, null);
#else
var result = socket.BeginConnect(svc.CrmConnectOrgUriActual.Host, 1433, null, null);
#endif
var success = result.AsyncWaitHandle.WaitOne(1000, true);

if (socket.Connected)
{
socket.EndConnect(result);
return true;
}
else
{
socket.Close();
return false;
}
}

#if NETCOREAPP
public static void Enable(ServiceClient svc)
#else
Expand Down
2 changes: 1 addition & 1 deletion MarkMpn.Sql4Cds.SSMS/DmlExecute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void OnExecuteQuery(string Guid, int ID, object CustomIn, object CustomO
try
{
_ai.TrackEvent("Execute", new Dictionary<string, string> { ["QueryType"] = query.GetType().Name, ["Source"] = "SSMS" });
var msg = query.Execute(new Dictionary<string, DataSource>(StringComparer.OrdinalIgnoreCase) { [dataSource.Name] = dataSource }, options, null, null);
var msg = query.Execute(new Dictionary<string, DataSource>(StringComparer.OrdinalIgnoreCase) { [dataSource.Name] = dataSource }, options, null, null, out _);

sqlScriptEditorControl.Results.AddStringToMessages(msg + "\r\n\r\n");

Expand Down

0 comments on commit 51b1066

Please sign in to comment.