Skip to content

Commit

Permalink
Allow sending SQL as well as FetchXML from other tools
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Sep 25, 2021
1 parent 3698cad commit fb435d9
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions MarkMpn.Sql4Cds/PluginControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,14 @@ public void OnIncomingMessage(MessageBusEventArgs message)

if (param == null)
{
var xml = message.TargetArgument as string;
var str = message.TargetArgument as string;
param = new Dictionary<string, object>();
param["FetchXml"] = xml;

if (str.StartsWith("<"))
param["FetchXml"] = str;
else
param["SQL"] = str;

param["ConvertOnly"] = false;
}

Expand All @@ -255,24 +260,31 @@ public void OnIncomingMessage(MessageBusEventArgs message)
var con = _objectExplorer.SelectedConnection;
var metadata = _metadata[con];

var fetch = DeserializeFetchXml((string)param["FetchXml"]);
var options = new FetchXml2SqlOptions();
if (param.TryGetValue("FetchXml", out var xml) && xml is string xmlStr && !String.IsNullOrEmpty(xmlStr))
{
var fetch = DeserializeFetchXml(xmlStr);
var options = new FetchXml2SqlOptions();

if ((bool)param["ConvertOnly"])
options.ConvertFetchXmlOperatorsTo = FetchXmlOperatorConversion.SqlCalculations;
if ((bool)param["ConvertOnly"])
options.ConvertFetchXmlOperatorsTo = FetchXmlOperatorConversion.SqlCalculations;

_ai.TrackEvent("Convert", new Dictionary<string, string> { ["QueryType"] = "FetchXML", ["Source"] = "XrmToolBox" });
_ai.TrackEvent("Convert", new Dictionary<string, string> { ["QueryType"] = "FetchXML", ["Source"] = "XrmToolBox" });

var sql = FetchXml2Sql.Convert(con.ServiceClient, metadata, fetch, options, out _);
var sql = FetchXml2Sql.Convert(con.ServiceClient, metadata, fetch, options, out _);

if ((bool)param["ConvertOnly"])
{
param["Sql"] = sql;
OnOutgoingMessage(this, new MessageBusEventArgs(message.SourcePlugin) { TargetArgument = null });
if ((bool)param["ConvertOnly"])
{
param["Sql"] = sql;
OnOutgoingMessage(this, new MessageBusEventArgs(message.SourcePlugin) { TargetArgument = null });
}
else
{
CreateQuery(con, "-- Imported from " + message.SourcePlugin + "\r\n\r\n" + sql);
}
}
else
else if (param.TryGetValue("SQL", out var sql) && sql is string sqlStr && !String.IsNullOrEmpty(sqlStr))
{
CreateQuery(con, "-- Imported from " + message.SourcePlugin + "\r\n\r\n" + sql);
CreateQuery(con, "-- Imported from " + message.SourcePlugin + "\r\n\r\n" + sqlStr);
}
}

Expand Down

0 comments on commit fb435d9

Please sign in to comment.