Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Microsoft-Produced SQL Parser for SQL Server #455

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AdrianJSClark
Copy link
Member

@AdrianJSClark AdrianJSClark commented Oct 22, 2019

(TO DO - write this summary properly)

  • Uses the Microsoft.SqlServer.DacFx package to parse script contents to split to commands
  • Updates the SQL Server package frameworks through necessity (required by the DacFx package)
  • Duplicates unit tests because existing SqlCommandSplitter tests aren't valid SQL

Resolves DbUp/dbup-sqlserver#4

The "Microsoft.SqlServer.DACFx" package doesn't support netstandard1.3
or net35 so we will upgrade.
Use the Microsoft "TSql150Parser" class to parse and split the script
content.

This commit also duplicates the splitter tests. The tests are duplicated
so they can be modified because some of the existing
SqlCommandSplitterTests content isn't valid SQL so isn't supported by
the parser.
The API approval test was giving me different results than the approved
build so extra ordering was added to improve the situation around
generating a good file for comparison.
Comment on lines -31 to 37
<PropertyGroup Condition=" '$(TargetFramework)' == 'net35' ">
<PropertyGroup Condition=" '$(TargetFramework)' == 'net46' ">
<DefineConstants>$(DefineConstants);SUPPORTS_SQL_CONTEXT</DefineConstants>
</PropertyGroup>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if net46 is actually valid for SUPPORTS_SQL_CONTEXT to be set, but I was just changing the net35 straight to net46.


public TSqlCommandSplitter()
{
parser = new TSql150Parser(true, SqlEngineType.All);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move these options to WithNewSqlParser?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion. Will definitely expose this somewhere.


if (errors.Count > 0)
{
throw new TSqlParseException($"Failure parsing SQL script \"{errors[0].Message}\" at [{errors[0].Line}, {errors[0].Column}]");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only first error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was lazy & wanted to get the first iteration done. I definitely plan to go back and build out that exception to properly handle a list of errors.

@@ -125,7 +125,7 @@ void Append(object argumentValue, string argumentName = null)

void AppendTypes(StringBuilder sb, int indent, IEnumerable<Type> types)
{
foreach (var type in types)
foreach (var type in types.OrderBy(t => t.Name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s worth adding ordinal sorting inside OrderBy so that the results do not differ on different OS.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. Although in the harsh light of the morning I'll probably rebase these changes out and consider submitting them separately. They have nothing to do with parsing SQL.

@sungam3r
Copy link
Member

@AdrianJSClark Please rebase onto master if you are still interested.

@sungam3r sungam3r added dbup-sqlserver Issue or pull request regarding dbup-sqlserver project needs confirmation An issue or pull request await confirmation from their author labels Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dbup-sqlserver Issue or pull request regarding dbup-sqlserver project needs confirmation An issue or pull request await confirmation from their author
Projects
Status: Pull Requests to check
Development

Successfully merging this pull request may close these issues.

Restructure SQL Parsing for SQL Server to use Microsoft.SqlServer.DacFx
2 participants