Skip to content

Commit

Permalink
Github Issue 85 (Pull Request) followup:
Browse files Browse the repository at this point in the history
 - Removed "df" file, appears to have been a commit comment artifact from some Git client.
 - Added "Contributors" section to all modified files
 - Set option defaults in command-line tool - as an external interface, it needs defaults to be explicitly maintained.
 - Minor null-reference safety issues
 - Commented serialization code a little to avoid future confusion
 - Switched to using Object Initializers instead of many separate statements to set up the Options object; no good reason, just feels cleaner.
  • Loading branch information
TaoK committed Jan 3, 2013
1 parent 8403209 commit 5ff3dab
Show file tree
Hide file tree
Showing 14 changed files with 382 additions and 1,272 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

## Poor Man's T-SQL Formatter change log

### Version 1.5.1

* Github Issue #85: [Enhancement] Merged code cleanup changes by Timothy Klenke

### Version 1.4.4 (SSMS / Visual Studio Add-In only)

* Github Issue #79: [BugFix] Fix SSMS Add-In to work in unexpected languages [thanks Paulo Stradioti for the bug report]
Expand Down
32 changes: 26 additions & 6 deletions PoorMansTSqlFormatterCmdLine/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/*
Poor Man's T-SQL Formatter - a small free Transact-SQL formatting
library for .Net 2.0, written in C#.
Copyright (C) 2011 Tao Klerks
Copyright (C) 2011-2013 Tao Klerks
Additional Contributors:
* Timothy Klenke, 2012
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -37,18 +40,35 @@ class Program

static int Main(string[] args)
{
var options = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatterOptions();
options.KeywordStandardization = true;

//formatter engine option defaults
var options = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatterOptions
{
KeywordStandardization = true,
IndentString = "\t",
SpacesPerTab = 4,
MaxLineWidth = 999,
TrailingCommas = false,
SpaceAfterExpandedComma = false,
ExpandBetweenConditions = true,
ExpandBooleanExpressions = true,
ExpandCaseStatements = true,
ExpandCommaLists = true,
BreakJoinOnSections = false,
UppercaseKeywords = true
};

//bulk formatter options
bool allowParsingErrors = false;
bool showUsageFriendly = false;
bool showUsageError = false;
List<string> extensions = new List<string>();
bool backups = true;
bool recursiveSearch = false;
string outputFileOrFolder = null;
string uiLangCode = null;

//flow/tracking switches
bool showUsageFriendly = false;
bool showUsageError = false;

OptionSet p = new OptionSet()
.Add("is|indentString=", delegate(string v) { options.IndentString = v; })
.Add("st|spacesPerTab=", delegate(string v) { options.SpacesPerTab = int.Parse(v); })
Expand Down
43 changes: 23 additions & 20 deletions PoorMansTSqlFormatterDemo/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/*
Poor Man's T-SQL Formatter - a small free Transact-SQL formatting
library for .Net 2.0, written in C#.
Copyright (C) 2011 Tao Klerks
Copyright (C) 2011-2013 Tao Klerks
Additional Contributors:
* Timothy Klenke, 2012
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -160,30 +163,30 @@ private void SetFormatter()
PoorMansTSqlFormatterLib.Interfaces.ISqlTreeFormatter innerFormatter;
if (radio_Formatting_Standard.Checked)
{
var options = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatterOptions();
options.IndentString = txt_Indent.Text;
options.SpacesPerTab = int.Parse(txt_IndentWidth.Text);
options.MaxLineWidth = int.Parse(txt_MaxWidth.Text);
options.ExpandCommaLists = chk_ExpandCommaLists.Checked;
options.TrailingCommas = chk_TrailingCommas.Checked;
options.SpaceAfterExpandedComma = chk_SpaceAfterComma.Checked;
options.ExpandBooleanExpressions = chk_ExpandBooleanExpressions.Checked;
options.ExpandCaseStatements = chk_ExpandCaseStatements.Checked;
options.ExpandBetweenConditions = chk_ExpandBetweenConditions.Checked;
options.BreakJoinOnSections = chk_BreakJoinOnSections.Checked;
options.UppercaseKeywords = chk_UppercaseKeywords.Checked;
options.HTMLColoring = chk_Coloring.Checked;
options.KeywordStandardization = chk_EnableKeywordStandardization.Checked;

innerFormatter = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatter(options);
innerFormatter = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatter(new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatterOptions
{
IndentString = txt_Indent.Text,
SpacesPerTab = int.Parse(txt_IndentWidth.Text),
MaxLineWidth = int.Parse(txt_MaxWidth.Text),
ExpandCommaLists = chk_ExpandCommaLists.Checked,
TrailingCommas = chk_TrailingCommas.Checked,
SpaceAfterExpandedComma = chk_SpaceAfterComma.Checked,
ExpandBooleanExpressions = chk_ExpandBooleanExpressions.Checked,
ExpandCaseStatements = chk_ExpandCaseStatements.Checked,
ExpandBetweenConditions = chk_ExpandBetweenConditions.Checked,
BreakJoinOnSections = chk_BreakJoinOnSections.Checked,
UppercaseKeywords = chk_UppercaseKeywords.Checked,
HTMLColoring = chk_Coloring.Checked,
KeywordStandardization = chk_EnableKeywordStandardization.Checked
});
}
else if (radio_Formatting_Identity.Checked)
innerFormatter = new PoorMansTSqlFormatterLib.Formatters.TSqlIdentityFormatter(chk_IdentityColoring.Checked);
else
innerFormatter = new PoorMansTSqlFormatterLib.Formatters.TSqlObfuscatingFormatter(
chk_RandomizeKeywordCase.Checked,
chk_RandomizeColor.Checked,
chk_RandomizeLineLength.Checked,
chk_RandomizeKeywordCase.Checked,
chk_RandomizeColor.Checked,
chk_RandomizeLineLength.Checked,
chk_PreserveComments.Checked,
chk_KeywordSubstitution.Checked
);
Expand Down
52 changes: 29 additions & 23 deletions PoorMansTSqlFormatterLib/Formatters/TSqlStandardFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/*
Poor Man's T-SQL Formatter - a small free Transact-SQL formatting
library for .Net 2.0, written in C#.
Copyright (C) 2011 Tao Klerks
Copyright (C) 2011-2013 Tao Klerks
Additional Contributors:
* Timothy Klenke, 2012
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
Expand All @@ -16,8 +19,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

*/

using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -34,40 +37,43 @@ public class TSqlStandardFormatter : Interfaces.ISqlTreeFormatter
public TSqlStandardFormatter() : this(new TSqlStandardFormatterOptions()) { }

public TSqlStandardFormatter(TSqlStandardFormatterOptions options)
{
{
if (options == null)
throw new ArgumentNullException("options");

Options = options;

if (options.KeywordStandardization)
KeywordMapping = StandardKeywordRemapping.Instance;
ErrorOutputPrefix = Interfaces.MessagingConstants.FormatErrorDefaultMessage + Environment.NewLine;
}

[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)
{
var options = new TSqlStandardFormatterOptions();

options.IndentString = indentString;
options.SpacesPerTab = spacesPerTab;
options.MaxLineWidth = maxLineWidth;
options.ExpandCommaLists = expandCommaLists;
options.TrailingCommas = trailingCommas;
options.SpaceAfterExpandedComma = spaceAfterExpandedComma;
options.ExpandBooleanExpressions = expandBooleanExpressions;
options.ExpandBetweenConditions = expandBetweenConditions;
options.ExpandCaseStatements = expandCaseStatements;
options.UppercaseKeywords = uppercaseKeywords;
options.BreakJoinOnSections = breakJoinOnSections;
options.HTMLColoring = htmlColoring;
options.KeywordStandardization = keywordStandardization;

Options = options;
{
Options = new TSqlStandardFormatterOptions
{
IndentString = indentString,
SpacesPerTab = spacesPerTab,
MaxLineWidth = maxLineWidth,
ExpandCommaLists = expandCommaLists,
TrailingCommas = trailingCommas,
SpaceAfterExpandedComma = spaceAfterExpandedComma,
ExpandBooleanExpressions = expandBooleanExpressions,
ExpandBetweenConditions = expandBetweenConditions,
ExpandCaseStatements = expandCaseStatements,
UppercaseKeywords = uppercaseKeywords,
BreakJoinOnSections = breakJoinOnSections,
HTMLColoring = htmlColoring,
KeywordStandardization = keywordStandardization
};

if (keywordStandardization)
KeywordMapping = StandardKeywordRemapping.Instance;
ErrorOutputPrefix = Interfaces.MessagingConstants.FormatErrorDefaultMessage + Environment.NewLine;
}

public TSqlStandardFormatterOptions Options { get; set; }
public TSqlStandardFormatterOptions Options { get; private set; }

public IDictionary<string, string> KeywordMapping = new Dictionary<string, string>();

Expand Down
64 changes: 35 additions & 29 deletions PoorMansTSqlFormatterLib/Formatters/TSqlStandardFormatterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Poor Man's T-SQL Formatter - a small free Transact-SQL formatting
library for .Net 2.0, written in C#.
Copyright (C) 2011 Tao Klerks
Additional Contributors:
* Timothy Klenke, 2012
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
Expand All @@ -16,14 +19,10 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using PoorMansTSqlFormatterLib.Interfaces;
using System.Linq;

namespace PoorMansTSqlFormatterLib.Formatters
Expand All @@ -45,12 +44,18 @@ public TSqlStandardFormatterOptions()
BreakJoinOnSections = false;
HTMLColoring = false;
KeywordStandardization = false;
}
}

//Doesn't particularly need to be lazy-loaded, and doesn't need to be threadsafe.
private static readonly TSqlStandardFormatterOptions _defaultOptions = new TSqlStandardFormatterOptions();

public TSqlStandardFormatterOptions(string serializedString) : this() {

if (string.IsNullOrEmpty(serializedString)) return;

if (string.IsNullOrEmpty(serializedString))
return;

//PLEASE NOTE: This is not reusable/general-purpose key-value serialization: it does not handle commas in data.
// For now, this is used in the Test library only.
foreach (string kvp in serializedString.Split(','))
{
string[] splitPair = kvp.Split('=');
Expand All @@ -73,26 +78,27 @@ public TSqlStandardFormatterOptions(string serializedString) : this() {
else throw new ArgumentException("Unknown option: " + key);
}

}

public string ToSerializedString() {
var overrides = new Dictionary<string, string>();

var defaultOptions = new TSqlStandardFormatterOptions();

if (IndentString != defaultOptions.IndentString) overrides.Add("IndentString", IndentString);
if (SpacesPerTab != defaultOptions.SpacesPerTab) overrides.Add("SpacesPerTab", SpacesPerTab.ToString());
if (MaxLineWidth != defaultOptions.MaxLineWidth) overrides.Add("MaxLineWidth", MaxLineWidth.ToString());
if (ExpandCommaLists != defaultOptions.ExpandCommaLists) overrides.Add("ExpandCommaLists", ExpandCommaLists.ToString());
if (TrailingCommas != defaultOptions.TrailingCommas) overrides.Add("TrailingCommas", TrailingCommas.ToString());
if (SpaceAfterExpandedComma != defaultOptions.SpaceAfterExpandedComma) overrides.Add("SpaceAfterExpandedComma", SpaceAfterExpandedComma.ToString());
if (ExpandBooleanExpressions != defaultOptions.ExpandBooleanExpressions) overrides.Add("ExpandBooleanExpressions", ExpandBooleanExpressions.ToString());
if (ExpandBetweenConditions != defaultOptions.ExpandBetweenConditions) overrides.Add("ExpandBetweenConditions", ExpandBetweenConditions.ToString());
if (ExpandCaseStatements != defaultOptions.ExpandCaseStatements) overrides.Add("ExpandCaseStatements", ExpandCaseStatements.ToString());
if (UppercaseKeywords != defaultOptions.UppercaseKeywords) overrides.Add("UppercaseKeywords", UppercaseKeywords.ToString());
if (BreakJoinOnSections != defaultOptions.BreakJoinOnSections) overrides.Add("BreakJoinOnSections", BreakJoinOnSections.ToString());
if (HTMLColoring != defaultOptions.HTMLColoring) overrides.Add("HTMLColoring", HTMLColoring.ToString());
if (KeywordStandardization != defaultOptions.KeywordStandardization) overrides.Add("KeywordStandardization", KeywordStandardization.ToString());
}

//PLEASE NOTE: This is not reusable/general-purpose key-value serialization: it does not handle commas in data.
// For now, this is used in the Test library only.
public string ToSerializedString()
{
var overrides = new Dictionary<string, string>();

if (IndentString != _defaultOptions.IndentString) overrides.Add("IndentString", IndentString);
if (SpacesPerTab != _defaultOptions.SpacesPerTab) overrides.Add("SpacesPerTab", SpacesPerTab.ToString());
if (MaxLineWidth != _defaultOptions.MaxLineWidth) overrides.Add("MaxLineWidth", MaxLineWidth.ToString());
if (ExpandCommaLists != _defaultOptions.ExpandCommaLists) overrides.Add("ExpandCommaLists", ExpandCommaLists.ToString());
if (TrailingCommas != _defaultOptions.TrailingCommas) overrides.Add("TrailingCommas", TrailingCommas.ToString());
if (SpaceAfterExpandedComma != _defaultOptions.SpaceAfterExpandedComma) overrides.Add("SpaceAfterExpandedComma", SpaceAfterExpandedComma.ToString());
if (ExpandBooleanExpressions != _defaultOptions.ExpandBooleanExpressions) overrides.Add("ExpandBooleanExpressions", ExpandBooleanExpressions.ToString());
if (ExpandBetweenConditions != _defaultOptions.ExpandBetweenConditions) overrides.Add("ExpandBetweenConditions", ExpandBetweenConditions.ToString());
if (ExpandCaseStatements != _defaultOptions.ExpandCaseStatements) overrides.Add("ExpandCaseStatements", ExpandCaseStatements.ToString());
if (UppercaseKeywords != _defaultOptions.UppercaseKeywords) overrides.Add("UppercaseKeywords", UppercaseKeywords.ToString());
if (BreakJoinOnSections != _defaultOptions.BreakJoinOnSections) overrides.Add("BreakJoinOnSections", BreakJoinOnSections.ToString());
if (HTMLColoring != _defaultOptions.HTMLColoring) overrides.Add("HTMLColoring", HTMLColoring.ToString());
if (KeywordStandardization != _defaultOptions.KeywordStandardization) overrides.Add("KeywordStandardization", KeywordStandardization.ToString());

if (overrides.Count == 0) return string.Empty;
return string.Join(",", overrides.Select((kvp) => kvp.Key + "=" + kvp.Value).ToArray());
Expand Down
35 changes: 19 additions & 16 deletions PoorMansTSqlFormatterPluginShared/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/*
Poor Man's T-SQL Formatter - a small free Transact-SQL formatting
library for .Net 2.0, written in C#.
Copyright (C) 2011 Tao Klerks
Copyright (C) 2011-2013 Tao Klerks
Additional Contributors:
* Timothy Klenke, 2012
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
Expand All @@ -28,22 +31,22 @@ public static class Utils
{
public static PoorMansTSqlFormatterLib.SqlFormattingManager GetFormattingManager(ISqlSettings settings)
{
var options = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatterOptions();
options.IndentString = settings.IndentString;
options.SpacesPerTab = settings.SpacesPerTab;
options.MaxLineWidth = settings.MaxLineWidth;
options.ExpandCommaLists = settings.ExpandCommaLists;
options.TrailingCommas = settings.TrailingCommas;
options.SpaceAfterExpandedComma = settings.SpaceAfterExpandedComma;
options.ExpandBooleanExpressions = settings.ExpandBooleanExpressions;
options.ExpandCaseStatements = settings.ExpandCaseStatements;
options.ExpandBetweenConditions = settings.ExpandBetweenConditions;
options.BreakJoinOnSections = settings.BreakJoinOnSections;
options.UppercaseKeywords = settings.UppercaseKeywords;
options.KeywordStandardization = settings.KeywordStandardization;
var formatter = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatter(new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatterOptions
{
IndentString = settings.IndentString,
SpacesPerTab = settings.SpacesPerTab,
MaxLineWidth = settings.MaxLineWidth,
ExpandCommaLists = settings.ExpandCommaLists,
TrailingCommas = settings.TrailingCommas,
SpaceAfterExpandedComma = settings.SpaceAfterExpandedComma,
ExpandBooleanExpressions = settings.ExpandBooleanExpressions,
ExpandCaseStatements = settings.ExpandCaseStatements,
ExpandBetweenConditions = settings.ExpandBetweenConditions,
BreakJoinOnSections = settings.BreakJoinOnSections,
UppercaseKeywords = settings.UppercaseKeywords,
KeywordStandardization = settings.KeywordStandardization
});

var formatter = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatter(options);

ResourceManager _generalResourceManager = new ResourceManager("PoorMansTSqlFormatterPluginShared.GeneralLanguageContent", Assembly.GetExecutingAssembly());
formatter.ErrorOutputPrefix = _generalResourceManager.GetString("ParseErrorWarningPrefix") + System.Environment.NewLine;
var formattingManager = new PoorMansTSqlFormatterLib.SqlFormattingManager(formatter);
Expand Down
Loading

0 comments on commit 5ff3dab

Please sign in to comment.