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

Add new options for formatting to ScriptGeneratorOptions #103

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

Stefan-Puskarica
Copy link
Contributor

@Stefan-Puskarica Stefan-Puskarica commented Nov 30, 2024

Brief

When writing SQL people have certain stylistic and formatting conventions they like to follow. This PR adds four new options to the SqlScriptGeneratorOptions to enable people to customise the formatting of the generated SQL further to their liking. These options have been added with defaults such that the existing behaviour is preserved. New behaviour can only be enabled by changing the option to something other than the default. Some of these options have been added due to stylistic preferences but others greatly enhance readability.

Added Options

NewLineFormattedIndexDefinition

Description

This option affects the formatting of index definitions. By default the option is false and they are formatted as current. With the option enabled there will be newlines inserted between UNIQUE, INCLUDE and WHERE along with an indent to clearly show it belongs to a certain index.

INDEX name
    UNIQUE (col1, col2, col3)
    INCLUDE (col4, col5, col6)
    WHERE ...

Motivation

With the current way indexes are generated, any index with some complexity can end up quite unreadable with it trailing far off the line.

CREATE TABLE DummyTable (
    INDEX ComplicatedIndex UNIQUE (Col1, Col2, Col3) INCLUDE (Col4, Col5, Col6, Col7, Col8) WHERE Col4 = 'AR'
                                                                                                  AND Col3 IN ('ABC', 'XYZ')
                                                                                                      AND Col5 = 0
                                                                                                          AND Col6 = 1
                                                                                                              AND Col7 = 0
                                                                                                                  AND Col8 IS NOT NULL
);

This option combats this, while it doesn't change the way predicate logic is formatted, by separating the components of the index into their own lines we can more easily see the distinction and the line will not trail as far.

CREATE TABLE DummyTable (
    INDEX ComplicatedIndex
        UNIQUE (Col1, Col2, Col3)
        INCLUDE (Col4, Col5, Col6, Col7, Col8)
        WHERE Col4 = 'AR'
              AND Col3 IN ('ABC', 'XYZ')
                  AND Col5 = 0
                      AND Col6 = 1
                          AND Col7 = 0
                              AND Col8 IS NOT NULL
);

NewlineFormattedCheckConstraint

Description

This option affects the formatting of check constraints. By default the option is false and they are formatted as current. When the option is true the CHECK part of a check constraint definition will be on its own line.

CONSTRAINT name
    CHECK ...

Motivation

With the way predicate expressions are generated, check constraints can end up having very long lines.

CREATE TABLE DummyTable (
    CONSTRAINT ComplicatedConstraint CHECK ((Col1 IS NULL
                                             AND (Col2 <> ''
                                                  OR Col3 = 0))
                                            OR (Col1 IS NOT NULL
                                                AND ((Col2 = ''
                                                      AND Col3 <> 0)
                                                     OR (Col4 IN ('', 'ABC', 'JKL', 'XYZ')
                                                         AND Col3 < 0
                                                         AND (Col5 <> ''
                                                              OR Col6 <> '')))))

);

By having the CHECK part of a check constraint on its own line we can combat this.

CREATE TABLE DummyTable (
    CONSTRAINT ComplicatedConstraint
        CHECK ((Col1 IS NULL
                AND (Col2 <> ''
                     OR Col3 = 0))
               OR (Col1 IS NOT NULL
                   AND ((Col2 = ''
                         AND Col3 <> 0)
                        OR (Col4 IN ('', 'ABC', 'JKL', 'XYZ')
                            AND Col3 < 0
                            AND (Col5 <> ''
                                 OR Col6 <> '')))))
);

SpaceBetweenDataTypeAndParameters

Description

This option affects the formatting of datatypes, currently the script generator generates a space between the datatype and the brackets containing the parameters (if any). The default value of this option preserves this behavior but when the option is false a space will not be generated.

VARCHAR(50)

Motivation

This option has simply been added for stylistic preference reasons, some people prefer having a space there and others don't. This option gives them the flexibility to choose.

SpaceBetweenParametersInDataType

Description

This option affects the formatting of parameters in data types. By default the script generator generates a comma separated lists with spaces. This is preserved in the default value of this new option. However if the option is set to false we get a comma separated list without spaces.

DECIMAL (5,2)

Motivation

This option has simply been added for stylistic preference reasons, some people prefer the parameters separated with a space and others don't. This option gives them the flexibility to choose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant