-
Notifications
You must be signed in to change notification settings - Fork 23
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
Conversation
…od, ensuring the implementation is backwards compatible. Abstracted from the fork source PR jdorn/sql-formatter#86
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 😅
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
I'm closing the PR because it's outdated and as far as I understand #93 has replaced it anyway. |
This adds a new optional third parameter to the
format
method,$forceUppercase
which defaults tofalse
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