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

Modified library and console application: Added ability to add or re… #239

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions PoorMansTSqlFormatterCmdLine/GeneralLanguageContent.es.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
<data name="UnrecognizedArgumentsErrorMessage" xml:space="preserve">
<value>Hemos encontrado argumentos no reconocidos!</value>
</data>
<data name="InvalidBracketsAroundNamesCombination" xml:space="preserve">
<value>InvalidBracketsAroundNamesCombination!</value>
</data>
Copy link

Choose a reason for hiding this comment

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

Need to also add after Line 186:

ab  addBracketsAroundNames (predeterminado: falso)
rb  removeBracketsAroundNames (predeterminado: falso)

Copy link
Author

Choose a reason for hiding this comment

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

Made requested pull request changes.

<data name="BackupFailureWarningMessage" xml:space="preserve">
<value>Copia de seguridad fallada: {0}{1} Saltando este archivo.</value>
<comment>{0} is the filename, {1} is a linebreak.</comment>
Expand Down
5 changes: 5 additions & 0 deletions PoorMansTSqlFormatterCmdLine/GeneralLanguageContent.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
<data name="UnrecognizedArgumentsErrorMessage" xml:space="preserve">
<value>Paramètres inconnus trouvés!</value>
</data>
<data name="InvalidBracketsAroundNamesCombination" xml:space="preserve">
<value>InvalidBracketsAroundNamesCombination!</value>
</data>
<data name="BackupFailureWarningMessage" xml:space="preserve">
<value>Sauvegarde échouée: {0}{1} On saute la mise en forme pour ce fichier.</value>
<comment>{0} is the filename, {1} is a linebreak.</comment>
Expand Down Expand Up @@ -181,6 +184,8 @@ ecl expandCommaLists (par défaut: vrai)
eil expandInLists (par défaut: vrai)
uk uppercaseKeywords (par défaut: vrai)
sk standardizeKeywords (par défaut: faux)
ab addBracketsAroundNames (default: false)
ChrisWiitamaki marked this conversation as resolved.
Show resolved Hide resolved
rb removeBracketsAroundNames (default: false)
ChrisWiitamaki marked this conversation as resolved.
Show resolved Hide resolved
ae allowParsingErrors (par défaut: faux)
e extensions (par défaut: sql)
r recursive (par défaut: faux)
Expand Down
5 changes: 5 additions & 0 deletions PoorMansTSqlFormatterCmdLine/GeneralLanguageContent.resx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ ecl expandCommaLists (default: true)
eil expandInLists (default: true)
uk uppercaseKeywords (default: true)
sk standardizeKeywords (default: false)
ab addBracketsAroundNames (default: false)
rb removeBracketsAroundNames (default: false)
ae allowParsingErrors (default: false)
e extensions (default: sql)
r recursive (default: false)
Expand All @@ -204,6 +206,9 @@ echo select somecolumn from sometable | SqlFormatter
<data name="UnrecognizedArgumentsErrorMessage" xml:space="preserve">
<value>Unrecognized arguments found!</value>
</data>
<data name="InvalidBracketsAroundNamesCombination" xml:space="preserve">
<value>Cannot specify both 'ab' and 'rb'!</value>
</data>
<data name="UnrecognizedLanguageErrorMessage" xml:space="preserve">
<value>The provided Language Code is not supported.</value>
</data>
Expand Down
13 changes: 12 additions & 1 deletion PoorMansTSqlFormatterCmdLine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ static int Main(string[] args)
ExpandCommaLists = true,
BreakJoinOnSections = false,
UppercaseKeywords = true,
ExpandInLists = true
ExpandInLists = true,
AddBracketsAroundNames = false,
RemoveBracketsAroundNames = false
};

//bulk formatter options
Expand Down Expand Up @@ -88,6 +90,8 @@ static int Main(string[] args)
.Add("bjo|breakJoinOnSections", delegate(string v) { options.BreakJoinOnSections = v != null; })
.Add("uk|uppercaseKeywords", delegate(string v) { options.UppercaseKeywords = v != null; })
.Add("sk|standardizeKeywords", delegate(string v) { options.KeywordStandardization = v != null; })
.Add("ab|addBracketsAroundNames", delegate (string v) { options.AddBracketsAroundNames = v != null; })
.Add("rb|removeBracketsAroundNames", delegate (string v) { options.RemoveBracketsAroundNames = v != null; })

.Add("ae|allowParsingErrors", delegate(string v) { allowParsingErrors = v != null; })
.Add("e|extensions=", delegate(string v) { extensions.Add((v.StartsWith(".") ? "" : ".") + v); })
Expand Down Expand Up @@ -152,6 +156,13 @@ static int Main(string[] args)
Console.Error.WriteLine(_generalResourceManager.GetString("UnrecognizedArgumentsErrorMessage"));
}

// then check for incompatible settings
if (options.AddBracketsAroundNames && options.RemoveBracketsAroundNames)
{
showUsageError = true;
Console.Error.WriteLine(_generalResourceManager.GetString("InvalidBracketsAroundNamesCombination"));
}

if (extensions.Count == 0)
extensions.Add(".sql");

Expand Down
28 changes: 24 additions & 4 deletions PoorMansTSqlFormatterLibShared/Formatters/TSqlStandardFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public TSqlStandardFormatter(TSqlStandardFormatterOptions options)
}

[Obsolete("Use the constructor with the TSqlStandardFormatterOptions parameter")]
public TSqlStandardFormatter(string indentString, int spacesPerTab, int maxLineWidth, bool expandCommaLists, bool trailingCommas, bool spaceAfterExpandedComma, bool expandBooleanExpressions, bool expandCaseStatements, bool expandBetweenConditions, bool breakJoinOnSections, bool uppercaseKeywords, bool htmlColoring, bool keywordStandardization)
public TSqlStandardFormatter(string indentString, int spacesPerTab, int maxLineWidth, bool expandCommaLists, bool trailingCommas, bool spaceAfterExpandedComma, bool expandBooleanExpressions, bool expandCaseStatements, bool expandBetweenConditions, bool breakJoinOnSections, bool uppercaseKeywords, bool htmlColoring, bool keywordStandardization, bool addBracketsAroundNames, bool removeBracketsAroundNames)
{
Options = new TSqlStandardFormatterOptions
{
Expand All @@ -63,7 +63,9 @@ public TSqlStandardFormatter(string indentString, int spacesPerTab, int maxLineW
UppercaseKeywords = uppercaseKeywords,
BreakJoinOnSections = breakJoinOnSections,
HTMLColoring = htmlColoring,
KeywordStandardization = keywordStandardization
KeywordStandardization = keywordStandardization,
AddBracketsAroundNames = addBracketsAroundNames,
RemoveBracketsAroundNames = removeBracketsAroundNames
};

if (keywordStandardization)
Expand Down Expand Up @@ -534,7 +536,14 @@ private void ProcessSqlNode(Node contentElement, TSqlStandardFormattingState sta

case SqlStructureConstants.ENAME_BRACKET_QUOTED_NAME:
WhiteSpace_SeparateWords(state);
state.AddOutputContent("[" + contentElement.TextValue.Replace("]", "]]") + "]");
if (Options.RemoveBracketsAroundNames)
{
state.AddOutputContent(contentElement.TextValue.Replace("[", "").Replace("]", ""));
}
else
{
state.AddOutputContent("[" + contentElement.TextValue.Replace("]", "]]") + "]");
}
state.WordSeparatorExpected = true;
break;

Expand Down Expand Up @@ -646,7 +655,18 @@ private void ProcessSqlNode(Node contentElement, TSqlStandardFormattingState sta
case SqlStructureConstants.ENAME_MONETARY_VALUE:
case SqlStructureConstants.ENAME_LABEL:
WhiteSpace_SeparateWords(state);
state.AddOutputContent(contentElement.TextValue);
if (Options.AddBracketsAroundNames && !contentElement.TextValue.StartsWith("@"))
{
state.AddOutputContent("[" + contentElement.TextValue + "]");
}
else if (Options.RemoveBracketsAroundNames)
{
state.AddOutputContent(contentElement.TextValue.Replace("[", "").Replace("]", ""));
}
else
{
state.AddOutputContent(contentElement.TextValue);
}
state.WordSeparatorExpected = true;
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public TSqlStandardFormatterOptions()
ExpandInLists = true;
NewClauseLineBreaks = 1;
NewStatementLineBreaks = 2;
AddBracketsAroundNames = false;
RemoveBracketsAroundNames = false;
}

//Doesn't particularly need to be lazy-loaded, and doesn't need to be threadsafe.
Expand Down Expand Up @@ -82,7 +84,9 @@ public TSqlStandardFormatterOptions(string serializedString) : this() {
else if (key == "ExpandInLists") ExpandInLists = Convert.ToBoolean(value);
else if (key == "NewClauseLineBreaks") NewClauseLineBreaks = Convert.ToInt32(value);
else if (key == "NewStatementLineBreaks") NewStatementLineBreaks = Convert.ToInt32(value);
else throw new ArgumentException("Unknown option: " + key);
else if (key == "AddBracketsAroundNames") AddBracketsAroundNames = Convert.ToBoolean(value);
else if (key == "RemoveBracketsAroundNames") RemoveBracketsAroundNames = Convert.ToBoolean(value);
else throw new ArgumentException("Unknown option: " + key);
}

}
Expand Down Expand Up @@ -111,7 +115,9 @@ public string ToSerializedString()
if (NewClauseLineBreaks != _defaultOptions.NewClauseLineBreaks) overrides.Add("NewClauseLineBreaks", NewClauseLineBreaks.ToString());
if (NewStatementLineBreaks != _defaultOptions.NewStatementLineBreaks) overrides.Add("NewStatementLineBreaks", NewStatementLineBreaks.ToString());
NewStatementLineBreaks = 2;

if (AddBracketsAroundNames != _defaultOptions.AddBracketsAroundNames) overrides.Add("AddBracketsAroundNames", AddBracketsAroundNames.ToString());
if (RemoveBracketsAroundNames != _defaultOptions.RemoveBracketsAroundNames) overrides.Add("RemoveBracketsAroundNames", RemoveBracketsAroundNames.ToString());

if (overrides.Count == 0) return string.Empty;
return string.Join(",", overrides.Select((kvp) => kvp.Key + "=" + kvp.Value).ToArray());

Expand Down Expand Up @@ -145,6 +151,8 @@ public string IndentString
public bool ExpandInLists { get; set; }
public int NewClauseLineBreaks { get; set; }
public int NewStatementLineBreaks { get; set; }
public bool AddBracketsAroundNames { get; set; }
public bool RemoveBracketsAroundNames { get; set; }

}
}