Skip to content

Commit

Permalink
Added dedicated toolbar button to convert SQL to FetchXML
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Feb 24, 2022
1 parent dcf9464 commit d65dbe0
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 46 deletions.
2 changes: 1 addition & 1 deletion MarkMpn.Sql4Cds.SSMS/Sql2FetchXmlCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private void Execute(object sender, EventArgs e)
{
editPoint.Insert("\r\n‼ WARNING ‼\r\n");
editPoint.Insert("This query requires additional processing. This FetchXML gives the required data, but needs additional processing to format it in the same way as returned by the TDS Endpoint or SQL 4 CDS.\r\n\r\n");
editPoint.Insert("Learn more at https://markcarrington.dev/sql-4-cds/additional-processing/\r\n\r\n");
editPoint.Insert("See the estimated execution plan to see what extra processing is performed by SQL 4 CDS");
}

editPoint.Insert("\r\n\r\n-->");
Expand Down
77 changes: 74 additions & 3 deletions MarkMpn.Sql4Cds/PluginControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using MarkMpn.Sql4Cds.Engine;
Expand Down Expand Up @@ -464,15 +465,15 @@ private void SyncExecuteButton(object sender, EventArgs e)
tscbConnection.Enabled = false;
tsbExecute.Enabled = false;
tsbPreviewFetchXml.Enabled = false;
tsbPowerBi.Enabled = false;
tsbConvertToFetchXMLSplitButton.Enabled = false;
return;
}

tscbConnection.Enabled = !query.Busy;
tscbConnection.Text = query.Connection.ConnectionName;
tsbExecute.Enabled = query.Connection != null && !query.Busy;
tsbPreviewFetchXml.Enabled = query.Connection != null && !query.Busy;
tsbPowerBi.Enabled = query.Connection != null && !query.Busy;
tsbConvertToFetchXMLSplitButton.Enabled = query.Connection != null && !query.Busy;
}

private void tsbStop_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -546,7 +547,7 @@ private void tsbFetchXMLBuilder_Click(object sender, EventArgs e)
OnOutgoingMessage(this, args);
}

private void tsbPowerBi_Click(object sender, EventArgs e)
private void powerBIMToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!(dockPanel.ActiveDocument is SqlQueryControl sql))
return;
Expand All @@ -570,6 +571,76 @@ 4. Copy & paste in this query
CreateM(m);
}

private void fetchXMLToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!(dockPanel.ActiveDocument is SqlQueryControl sql))
return;

using (var con = new Sql4CdsConnection(sql.DataSources.Values.ToList()))
using (var cmd = con.CreateCommand())
{
new QueryExecutionOptions(this, null).ApplySettings(con, cmd, false);
con.UseTDSEndpoint = false;
cmd.CommandText = sql.Sql;

try
{
var queries = cmd.GeneratePlan(false);

foreach (var query in queries)
{
_ai.TrackEvent("Convert", new Dictionary<string, string> { ["QueryType"] = query.GetType().Name, ["Source"] = "XrmToolBox" });

var sb = new StringBuilder();
sb.Append("<!--\r\nCreated from query:\r\n\r\n");
sb.Append(query.Sql);

var nodes = GetAllNodes(query).ToList();
var fetchXmlNodes = nodes.OfType<IFetchXmlExecutionPlanNode>().ToList();

if (fetchXmlNodes.Count == 0)
continue;

if (nodes.Count < fetchXmlNodes.Count)
{
sb.Append("\r\n‼ WARNING ‼\r\n");
sb.Append("This query requires additional processing. This FetchXML gives the required data, but needs additional processing to format it in the same way as returned by the TDS Endpoint or SQL 4 CDS.\r\n\r\n");
sb.Append("See the estimated execution plan to see what extra processing is performed by SQL 4 CDS");
}

sb.Append("\r\n\r\n-->");

foreach (var fetchXml in nodes.OfType<IFetchXmlExecutionPlanNode>())
{
sb.Append("\r\n\r\n");
sb.Append(fetchXml.FetchXmlString);
}

CreateFetchXML(sb.ToString());
}
}
catch (NotSupportedQueryFragmentException ex)
{
MessageBox.Show(this, "The query could not be converted to FetchXML: " + ex.Message, "Query Not Supported", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (QueryParseException ex)
{
MessageBox.Show(this, "The query could not be parsed: " + ex.Message, "Query Parsing Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

private IEnumerable<IExecutionPlanNode> GetAllNodes(IExecutionPlanNode node)
{
foreach (var source in node.GetSources())
{
yield return source;

foreach (var subSource in GetAllNodes(source))
yield return subSource;
}
}

private void tscbConnection_SelectedIndexChanged(object sender, EventArgs e)
{
if (dockPanel.ActiveDocument is SqlQueryControl query && tscbConnection.SelectedItem != null)
Expand Down
83 changes: 53 additions & 30 deletions MarkMpn.Sql4Cds/PluginControl.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 32 additions & 12 deletions MarkMpn.Sql4Cds/PluginControl.resx
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,40 @@
81H/fABSBnEPJn8TgPBA92BymgCeYN8fdZgS7Bc7EcIbFPRwMzSLTHoAAAAASUVORK5CYII=
</value>
</data>
<data name="tsbPowerBi.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<data name="fetchXMLToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
wwAADsMBx2+oZAAAAQhJREFUOE+1kjGqwkAQhnMEj+IZ0gdbS2+Q0hvkAoIIFgF7QUktCoKYU3iBB8Ym
7TjfurMmsuQV4sLHzvwz8282SWKrbdsxiEiIfSlBg88VND9wB2/gYnTqgwY00ZymqVRVpZok7ORm8p+B
Gy6Kwg0gspObyc8NvrsCy0wA0WJ0NXKNgwYsmsGaDa8fFZ7IIHe9veYY2ni7bi6yniwD5OjUo0NdOHE3
30pzfwTI0am7hrqu3Utjt4JBXk7XclocAuQ9A4aNmEH38Y2egZlYrMw6cdSg+WteBnZKlmWS57m7BhCj
UTuvTr1hctXfL1GTkp+HAbsKMZrW9srwZ9RkpJS+2AVtRE8cSZ5FG6K5mWB0KgAAAABJRU5ErkJggg==
</value>
</data>
<data name="powerBIMToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
wwAADsMBx2+oZAAAAlxJREFUOE+NkmtIk2EYht+RpZXmjhVUPwoi6kdYkIGCRaRBB8koJbEDQhadi4xq
PxxmYERCISTSCbFCMwYylZY4WyKaTaduOtfm3Le5g7pc7fu+fevP3UspBRuzG55fD9fF+3C/ZKEEOlee
C3QoMNOuwJRWAX+bHL4WuXduvXCmPyhU/4DwNMswqZZhbr1w/K1ylU/zF3Q3ScE0Sv9f4FLLVMw7GZi3
dBqkcL6WwFEviS8QxovehO3HwVuPgbfkgRvJxbQ+HfY6CewvxbA9E8cXUFA3D7LD+xEazIG/Mx2jtWJY
a1NhebIivoAzH9LNgz/69+D7lyx4tGkwVaditDoF5kfJ8QUhY7buN9iXhWBvJoLdO+HWbIaxKhmmquUY
erA0WvDT+9Ab8dxHxF0BwaWCwCjpC/Iw27Ud3z5uBaPeAEPlMhgrkzBwLzFaEHHfpWAZBOcdCBM3ITiu
IzSQi4BuC/1EG+FoXIPP5YkwlC9GX1lCtEBw3qZgKQWvQhi/iLDtLL19L6a16zHVthb2ejm6lQnoVS5C
zy1RDMH4FYTtFyhYAv5rMa3wBII9GfBrVsPXLIf1eQr0pSJ03yDoukaiBWHbGQqeBj9WRLsvAD96BLOf
0uBVi+FpSoalZgl0lwn0lwg6z8cQ8GOFFMwHP5IH3nwQnGkfvX8TJhuS4H6VAPNjEbQlBB102otjCUYO
gzMfADecA3ZoN1hjJma06+CqE4F5QWh1BK2nCN6fJGgrjCHghrO97OAuCmaA7d8B1rANUy2rMPGUwFFD
MFBB0FxA0JJPoDlKPH8oQn4ByNis2cV0ThIAAAAASUVORK5CYII=
</value>
</data>
<data name="tsbConvertToFetchXMLSplitButton.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAJcSURBVDhPjZJbSNNhGIe/0UorzblDBdVFQURdhAUZKFhE
GnSQFqlkR4QsOhcZ1S4cZmBEQiEk0gmxQjMGY1Na4mzJ0Ew3dXOHnHMHd3Q5c/vvv6tfH+XowuF64eW7
eHkevpffS1JVSL36YqhbhOkuEQIqEfydQriVPO/8OHUFP4ukCdCnFMIjF2BKJsD8OHX5lQKpT/EPdLfz
4Wzj/7/AJRNInR8FcH6g3cqH41027C3ZiwtitpPvY7YTYKwlYMxiRMeKEdTkwtacDdsbHsZf8hYXRC0l
6gQYGT2IueEi+HtyYWriwdqUBfPzVSkExiPqBPhraB9mvxfAo8qBoSELpoZMGJ9mLC6Y1Req/4ADBQj3
5yOs3Q23Yiv09Rkw1K/EyOPlCwVx9xNv3PMIcXctWJcUrFNCfyDGTO9O/PyyHU7ZJgzWrYC+Lh26h2nJ
BA8oWA3WcR/s5B2w9luY0xUjpN5Gj2gz7G3r8K0mDYM1SzFQzV0oYB33KFhFwRtgJ64gNn6B7r4fQdVG
BDrXw9YihFbCRb9kCfrucpIIJq4jZrtMwUowPypohKcR7suDX7EWPrkQ1leZ0FRxoL1N0HuTLBTExs9T
8BwYyymafRkY0zHMfM2BV8aDpz0D5sZlUF8j0Fwl6LmURMBYyilYCmZMDMZ4GFHDAbr/Fky1psP9lgvj
Mw5UlQTdtLsqkgnGjiJqPIToaBEiI3sR0edjWrUBrmYOnK8JjY6g4yzBpzMEneVJBFFdoTcyvIeCeYgM
7UJkcAcCyjWYfEFgbyTQ1RLIywiUpfQ9Tjx/KUJ+A4OhrL0L+0yeAAAAAElFTkSuQmCC
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAD8SURBVDhPpZJPCoJAGMU9QkfpDO6lbctu4LIbeIEgghZC
+6BwHQlB5Cm6QDAG/llO/ga/ScNqwA8efu+b997MqJ5UURRT8Nk7FeKqqhSAS+8UImbf93WSJJoZT7hT
iJijKDIGmcElxAi/1eiA0VegJER2k97JLIV4yMCsLMtzE8iJDODO4Y3wfttd9Xa2toAzbyW/ix0Py73O
1dMCztwKsiwzL42nXWgLHs+3Ol2dLOC9AMyCoYDu8QW9AAmRvq7rhfRgKCB/5O+NEAVBoMMwNNcA9MxY
u2zSnhnee4nNZ4n5eTDIVeiZNQHHv59RKTUhpCtqhTFrrWygPO8FEuzKCENDC8kAAAAASUVORK5CYII=
</value>
</data>
<data name="tsbFormat.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
Expand Down

0 comments on commit d65dbe0

Please sign in to comment.