Skip to content

Commit c7e7348

Browse files
authored
Merge pull request #22 from microsoft/dev/marktayl/add-tool-code
add tsqlParser code
2 parents f8ada60 + 1d8d400 commit c7e7348

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Microsoft.SqlServer.TransactSql.ScriptDom;
5+
using Newtonsoft.Json;
6+
7+
namespace SqlParserApp
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
string sqlQuery = string.Empty;
14+
if (args.Length == 0)
15+
{
16+
Console.WriteLine("Please provide a SQL query string (--string) or file (--file) as an argument.");
17+
return;
18+
}
19+
if (args[0] == "--file")
20+
{
21+
if (args.Length < 2)
22+
{
23+
Console.WriteLine("Please provide a file path after --file.");
24+
return;
25+
}
26+
string filePath = args[1];
27+
if (!File.Exists(filePath))
28+
{
29+
Console.WriteLine($"File not found: {filePath}");
30+
return;
31+
}
32+
sqlQuery = File.ReadAllText(filePath);
33+
}
34+
else if (args[0] == "--string")
35+
{
36+
sqlQuery = string.Join(" ", args); // Join the array elements into a single string
37+
}
38+
else
39+
{
40+
Console.WriteLine("Invalid argument. Use --file or --string.");
41+
}
42+
43+
IList<ParseError> errors = ParseSqlQuery(sqlQuery);
44+
45+
var errorList = new List<Dictionary<string, object>>();
46+
47+
foreach (var error in errors)
48+
{
49+
var errorDict = new Dictionary<string, object>
50+
{
51+
{ "Line", error.Line },
52+
{ "Column", error.Column },
53+
{ "Error", error.Message }
54+
};
55+
errorList.Add(errorDict);
56+
}
57+
58+
string jsonOutput = JsonConvert.SerializeObject(errorList, Formatting.Indented);
59+
Console.WriteLine(jsonOutput);
60+
}
61+
62+
static IList<ParseError> ParseSqlQuery(string sqlQuery)
63+
{
64+
TSql150Parser parser = new TSql150Parser(false);
65+
IList<ParseError> errors;
66+
using (TextReader reader = new StringReader(sqlQuery))
67+
{
68+
parser.Parse(reader, out errors);
69+
}
70+
return errors;
71+
}
72+
}
73+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <!-- Change this to your target platform -->
9+
<!-- for more identifiers see: https://learn.microsoft.com/en-us/dotnet/core/rid-catalog -->
10+
<PublishSingleFile>true</PublishSingleFile>
11+
<SelfContained>true</SelfContained>
12+
<PublishTrimmed>true</PublishTrimmed> <!-- Optional: Reduces the size of the output file -->
13+
</PropertyGroup>
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.SqlServer.TransactSql.ScriptDom" Version="170.23.0" />
16+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.5.2.0
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tsqlParser", "tsqlParser.csproj", "{64697CE7-07C6-C079-07F8-ED5DBAE7F0E2}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{64697CE7-07C6-C079-07F8-ED5DBAE7F0E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{64697CE7-07C6-C079-07F8-ED5DBAE7F0E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{64697CE7-07C6-C079-07F8-ED5DBAE7F0E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{64697CE7-07C6-C079-07F8-ED5DBAE7F0E2}.Release|Any CPU.Build.0 = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(SolutionProperties) = preSolution
19+
HideSolutionNode = FALSE
20+
EndGlobalSection
21+
GlobalSection(ExtensibilityGlobals) = postSolution
22+
SolutionGuid = {FFA6A40F-F60A-4ED9-B8BA-EE8F243FEC3E}
23+
EndGlobalSection
24+
EndGlobal

0 commit comments

Comments
 (0)