microsoft / SqlScriptDOM

ScriptDOM/SqlDOM is a .NET library for parsing T-SQL statements and interacting with its abstract syntax tree
MIT License
135 stars 18 forks source link

Unable to inherit 'SqlScriptGenerator' class #42

Open terryfkjc opened 1 year ago

terryfkjc commented 1 year ago

Is your feature request related to a problem? Please describe.

Consider the following code to inherit SqlScriptGenerator

internal class CustomSqlScriptGenerator : SqlScriptGenerator
{
    public CustomSqlScriptGenerator(SqlScriptGeneratorOptions options) : base(options)
    {
    }

    internal override SqlScriptGeneratorVisitor CreateSqlScriptGeneratorVisitor(SqlScriptGeneratorOptions options, ScriptWriter scriptWriter)
    {
        throw new NotImplementedException();
    }
}

However, this will get compilation error because SqlScriptGeneratorVisitor and ScriptWriter are marked as internal, which is impossible to inherit this class.

Describe the solution you'd like

Here is the suggested changes:

protected abstract SqlScriptGeneratorVisitor CreateSqlScriptGeneratorVisitor(SqlScriptGeneratorOptions options);

And update Sql###ScriptGenerator become

Example:

public sealed class Sql100ScriptGenerator : SqlScriptGenerator
{
    public Sql100ScriptGenerator()
        : this(new SqlScriptGeneratorOptions())
    {
    }

    public Sql100ScriptGenerator(SqlScriptGeneratorOptions options)
        : base(options)
    {
    }

    protected override SqlScriptGeneratorVisitor CreateSqlScriptGeneratorVisitor(SqlScriptGeneratorOptions options)
    {
        // Create instance of ScriptWriter. 
        //Currently this is created by private function in SqlScriptGenerator
        ScriptWriter scriptWriter = new ScriptWriter(options);

        ScriptGeneratorSupporter.CheckForNullReference((object) options, nameof (options));
        ScriptGeneratorSupporter.CheckForNullReference((object) scriptWriter, nameof (scriptWriter));
        return (SqlScriptGeneratorVisitor) new Sql100ScriptGeneratorVisitor(options, scriptWriter);
    }
}
zijchen commented 1 year ago

I believe this is the line of code in question: https://github.com/microsoft/SqlScriptDOM/blob/4b7e0af48237191f14612855acda32c97470fe02/SqlScriptDom/ScriptDom/SqlServer/SqlScriptGenerator.cs#L116