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

Option to force uppercase SQL keywords #73

Open
wants to merge 4 commits into
base: 1.1.x
Choose a base branch
from
Open

Conversation

othyn
Copy link

@othyn othyn commented Apr 8, 2021

This adds a new optional third parameter to the format method, $forceUppercase which defaults to false to maintain backwards compatibility.

When passed as true, all keywords will be uppercased.

There is a new test written in for this functionality that passes.

Fixes: #32.
Implementation abstracted from the original PR against the forked source: jdorn/sql-formatter#86

@greg0ire greg0ire requested a review from goetas August 19, 2021 14:24
@@ -47,7 +49,7 @@ public function __construct(?Highlighter $highlighter = null)
*
* @return string The SQL string with HTML styles and formatting wrapped in a <pre> tag
*/
public function format(string $string, string $indentString = ' '): string
public function format(string $string, string $indentString = ' ', bool $forceUppercase = false): string
Copy link
Member

Choose a reason for hiding this comment

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

How likely is it that somebody will want to sometimes uppercase SQL words, sometimes not in the same application? I wonder if this wouldn't be better as a constructor parameter. What do you think @goetas @rodnaph @othyn ?

Copy link

Choose a reason for hiding this comment

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

For me, unlikely - the purpose of the tool is standardisation.

(If someone did, they could create multiple instances perhaps).

Copy link
Member

@greg0ire greg0ire Aug 19, 2021

Choose a reason for hiding this comment

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

Too bad I did not use a more generic name when introducing Highlighter in #21 , this looks like it could be implemented more cleanly with a custom highlighter.

Copy link
Author

@othyn othyn Aug 19, 2021

Choose a reason for hiding this comment

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

Man it's so long ago I can't even remember my own use case! Haha 😅 I think... it was regarding an export tool I was writing to generate formatted SQL statements from Laravel migrations, in which having uppercased keywords suited that functionality better.

As for implementation, if you think it works better as a constructor option then I'm happy to oblige.

Copy link
Member

Choose a reason for hiding this comment

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

That or maybe there should be an abstraction next to Highlighter. Highlighting is about adding things before and after, there could be another interface in charge of changing the case, so that you would have

            $highlighted = $this->highlighter->highlightToken(
                $token->type(),
                $this->changeCase($token->value())
            );

and echo (new SqlFormatter(new NullHighlighter(), new UppercaseSomethingSomething()))->format($query)

Naming ideas welcome, and let's wait for @goetas to look at this, he's good at design.

Copy link
Member

Choose a reason for hiding this comment

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

@SenseException can you please rephrase? I don't understand 😅

Copy link
Member

Choose a reason for hiding this comment

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

$forceUppercase and also $indentString are used for the representation of the SQL string output and I see both as options that could be injected as a config for the second constructor argument. While $indentString has to stay in the format function for version 1, I can imagine that a future 2.0 can contain this info for "use uppercase" and "use spaces as indent" inside e.g. a Config object that SqlFormatter (or somewhere else) gets injected.

Copy link
Member

@greg0ire greg0ire Aug 20, 2021

Choose a reason for hiding this comment

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

Maybe we should have a fromConfig named constructor that should call a somewhat complicated constructor in v2 yeah. PHP 8 usage could be SqlFormatter::fromConfig(indentString: '>', case_strategy: UPPERCASE_SQL_KEYWORDS, highlighter: HTML) and then it would result in the constructor being called with the right implementations, that way we don't have to maintain a config object with properties, getters and setters, it all gets validated and thrown away in the named constructor, and people can still swap in / swap out their own implementations by using the normal constructor.

Copy link
Member

Choose a reason for hiding this comment

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

I like the idea of a named constructor. Not having a Config object to maintain has its advantages, but getter and setter methods aren't needed considering typed properties that are already part in 7.4.

Choose a reason for hiding this comment

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

I sustain it, but adding a $forceUppercase option would add only ONE option.

Please read my last suggestion instead : #85

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.

Feature request: uppercase SQL keywords
5 participants