From 8f04a38858ef15c0c3fbfdb51bc7a270d441cdca Mon Sep 17 00:00:00 2001 From: Anthony Dresser Date: Wed, 9 May 2018 11:00:06 -0700 Subject: [PATCH] add auto detect json based on first row of each column (#571) --- .../QueryExecution/ResultSet.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/ResultSet.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/ResultSet.cs index cddb4f6e..d9c5ae88 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/ResultSet.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/ResultSet.cs @@ -8,6 +8,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Data.Common; using System.Linq; +using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts; @@ -362,6 +363,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution } // Check if resultset is 'for xml/json'. If it is, set isJson/isXml value in column metadata SingleColumnXmlJsonResultSet(); + CheckForIsJson(); } finally { @@ -589,6 +591,28 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution } } + /// + /// Check columns for json type and set isJson if needed + /// + private void CheckForIsJson() + { + if (Columns?.Length > 0 && RowCount != 0) + { + Regex regex = new Regex(@"({.*?})"); + var row = GetRow(0); + for (int i = 0; i < Columns.Length; i++) + { + if (Columns[i].DataTypeName.Equals("nvarchar")) + { + if (regex.IsMatch(row[i].DisplayValue)) + { + Columns[i].IsJson = true; + } + } + } + } + } + /// /// Determine the special action, if any, for this result set ///