diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c4efe2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,261 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/SQL Format.sln b/SQL Format.sln new file mode 100644 index 0000000..e4b5e06 --- /dev/null +++ b/SQL Format.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.168 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQL Format", "SQL Format\SQL Format.csproj", "{832405D6-AD9E-46B6-B346-CBD774FCC49D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {832405D6-AD9E-46B6-B346-CBD774FCC49D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {832405D6-AD9E-46B6-B346-CBD774FCC49D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {832405D6-AD9E-46B6-B346-CBD774FCC49D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {832405D6-AD9E-46B6-B346-CBD774FCC49D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {10DD0264-B0D0-4A29-94B6-A29DBCDFB86D} + EndGlobalSection +EndGlobal diff --git a/SQL Format/App.config b/SQL Format/App.config new file mode 100644 index 0000000..731f6de --- /dev/null +++ b/SQL Format/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SQL Format/FormSQLFormat.Designer.cs b/SQL Format/FormSQLFormat.Designer.cs new file mode 100644 index 0000000..dcb2d90 --- /dev/null +++ b/SQL Format/FormSQLFormat.Designer.cs @@ -0,0 +1,136 @@ +namespace SQL_Format +{ + partial class FormSQLFormat + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormSQLFormat)); + this.TabCtrl = new System.Windows.Forms.TabControl(); + this.TPSource = new System.Windows.Forms.TabPage(); + this.splitContainer2 = new System.Windows.Forms.SplitContainer(); + this.MSource = new System.Windows.Forms.TextBox(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.TabCtrl.SuspendLayout(); + this.TPSource.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit(); + this.splitContainer2.Panel1.SuspendLayout(); + this.splitContainer2.Panel2.SuspendLayout(); + this.splitContainer2.SuspendLayout(); + this.SuspendLayout(); + // + // TabCtrl + // + this.TabCtrl.Controls.Add(this.TPSource); + this.TabCtrl.Dock = System.Windows.Forms.DockStyle.Fill; + this.TabCtrl.Location = new System.Drawing.Point(0, 0); + this.TabCtrl.Name = "TabCtrl"; + this.TabCtrl.SelectedIndex = 0; + this.TabCtrl.Size = new System.Drawing.Size(1410, 571); + this.TabCtrl.TabIndex = 0; + // + // TPSource + // + this.TPSource.Controls.Add(this.splitContainer2); + this.TPSource.Location = new System.Drawing.Point(4, 22); + this.TPSource.Name = "TPSource"; + this.TPSource.Padding = new System.Windows.Forms.Padding(3); + this.TPSource.Size = new System.Drawing.Size(1402, 545); + this.TPSource.TabIndex = 0; + this.TPSource.Text = "Source"; + this.TPSource.UseVisualStyleBackColor = true; + this.TPSource.Leave += new System.EventHandler(this.TPSource_Leave); + // + // splitContainer2 + // + this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill; + this.splitContainer2.Location = new System.Drawing.Point(3, 3); + this.splitContainer2.Name = "splitContainer2"; + this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal; + // + // splitContainer2.Panel1 + // + this.splitContainer2.Panel1.Controls.Add(this.MSource); + // + // splitContainer2.Panel2 + // + this.splitContainer2.Panel2.Controls.Add(this.groupBox2); + this.splitContainer2.Panel2Collapsed = true; + this.splitContainer2.Size = new System.Drawing.Size(1396, 539); + this.splitContainer2.SplitterDistance = 465; + this.splitContainer2.TabIndex = 0; + // + // MSource + // + this.MSource.Dock = System.Windows.Forms.DockStyle.Fill; + this.MSource.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.MSource.Location = new System.Drawing.Point(0, 0); + this.MSource.Multiline = true; + this.MSource.Name = "MSource"; + this.MSource.ScrollBars = System.Windows.Forms.ScrollBars.Both; + this.MSource.Size = new System.Drawing.Size(1396, 539); + this.MSource.TabIndex = 1; + this.MSource.Text = resources.GetString("MSource.Text"); + this.MSource.WordWrap = false; + // + // groupBox2 + // + this.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill; + this.groupBox2.Location = new System.Drawing.Point(0, 0); + this.groupBox2.Name = "groupBox2"; + this.groupBox2.Size = new System.Drawing.Size(150, 46); + this.groupBox2.TabIndex = 0; + this.groupBox2.TabStop = false; + this.groupBox2.Text = "Options"; + // + // FormSQLFormat + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1410, 571); + this.Controls.Add(this.TabCtrl); + this.Name = "FormSQLFormat"; + this.Text = "SQL Format"; + this.TabCtrl.ResumeLayout(false); + this.TPSource.ResumeLayout(false); + this.splitContainer2.Panel1.ResumeLayout(false); + this.splitContainer2.Panel1.PerformLayout(); + this.splitContainer2.Panel2.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit(); + this.splitContainer2.ResumeLayout(false); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.TabControl TabCtrl; + private System.Windows.Forms.TabPage TPSource; + private System.Windows.Forms.SplitContainer splitContainer2; + private System.Windows.Forms.TextBox MSource; + private System.Windows.Forms.GroupBox groupBox2; + } +} \ No newline at end of file diff --git a/SQL Format/FormSQLFormat.cs b/SQL Format/FormSQLFormat.cs new file mode 100644 index 0000000..dec665d --- /dev/null +++ b/SQL Format/FormSQLFormat.cs @@ -0,0 +1,111 @@ +using Microsoft.SqlServer.TransactSql.ScriptDom; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace SQL_Format +{ + public partial class FormSQLFormat : Form + { + List> items = new List>(); + + public FormSQLFormat() + { + InitializeComponent(); + TabCtrl.SelectedIndex = 0; + + AddItemByClass(new SQLTranslatorSelect()); + AddItemByClass(new SQLTranslatorUpdate()); + AddItemByClass(new SQLTranslatorMerge()); + AddItemByClass(new SQLTranslatorInsert()); + AddItemByClass(new SQLTranslatorSame()); + AddItemByClass(new SQLTranslatorXmlSelect()); + } + + void AddItemByClass(SQLTranslator t) + { + int idx = TabCtrl.TabPages.Count; + TabCtrl.TabPages.Add(t.GetCaption()); + TabPage tp = TabCtrl.TabPages[idx]; + + SplitContainer splitContainer = new SplitContainer(); + tp.Controls.Add(splitContainer); + TextBox textBox = new TextBox(); + GroupBox groupBox = new GroupBox(); + + splitContainer.Dock = System.Windows.Forms.DockStyle.Fill; + splitContainer.FixedPanel = System.Windows.Forms.FixedPanel.Panel2; + splitContainer.Location = new System.Drawing.Point(3, 3); + splitContainer.Orientation = System.Windows.Forms.Orientation.Horizontal; + splitContainer.Panel1.Controls.Add(textBox); + splitContainer.Panel2.Controls.Add(groupBox); + splitContainer.Panel2MinSize = 50; + splitContainer.Size = new System.Drawing.Size(1396, 539); + splitContainer.SplitterDistance = 510; + splitContainer.TabIndex = 2; + + textBox.Dock = DockStyle.Fill; + textBox.Multiline = true; + textBox.WordWrap = false; + textBox.Font = MSource.Font; + textBox.Name = "textbox"; + textBox.ScrollBars = MSource.ScrollBars; + + groupBox.Text = "Options"; + groupBox.Dock = DockStyle.Fill; + + FlowLayoutPanel flowLayoutPanel = new FlowLayoutPanel(); + flowLayoutPanel.Dock = DockStyle.Fill; + groupBox.Controls.Add(flowLayoutPanel); + + t.SetupOptionsContent(flowLayoutPanel, OptionChanged); + + items.Add(new Tuple(splitContainer, t)); + } + + private void OptionChanged(object sender, EventArgs e) + { + ParseSource(); + } + + void ParseSource() + { + using (StringReader sr = new StringReader(MSource.Text)) + { + + IList errors; + TSql100Parser parser = new TSql100Parser(true); + TSqlScript script = parser.Parse(sr, out errors) as TSqlScript; + + foreach(TSqlBatch b in script.Batches) + { + foreach(TSqlStatement statement in b.Statements) + { + if (!(statement is CreateTableStatement)) continue; + + CreateTableStatement cts = (CreateTableStatement)statement; + + foreach(var i in items) + { + string s = i.Item2.Translate(cts.Definition, i.Item1); + (i.Item1.Panel1.Controls.Find("textbox", true)[0] as TextBox).Text = s; + } + break; + } + } + } + } + + private void TPSource_Leave(object sender, EventArgs e) + { + ParseSource(); + } + } +} diff --git a/SQL Format/FormSQLFormat.resx b/SQL Format/FormSQLFormat.resx new file mode 100644 index 0000000..eb86fa5 --- /dev/null +++ b/SQL Format/FormSQLFormat.resx @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + CREATE TABLE dbo.SomeTable( + ID INT IDENTITY (1, 1) NOT NULL, + Name VARCHAR (1000) NOT NULL, + fileSize BIGINT NULL, + fileSize2 BIGINT NULL, + fileSize3 BIGINT NULL, + fileSize4 BIGINT NULL, + fileSize5 BIGINT NULL, + CONSTRAINT PK PRIMARY KEY CLUSTERED (ID ASC) +); + + \ No newline at end of file diff --git a/SQL Format/Program.cs b/SQL Format/Program.cs new file mode 100644 index 0000000..60115a8 --- /dev/null +++ b/SQL Format/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace SQL_Format +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new FormSQLFormat()); + } + } +} diff --git a/SQL Format/Properties/AssemblyInfo.cs b/SQL Format/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..6f092b5 --- /dev/null +++ b/SQL Format/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("SQL Format")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SQL Format")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("832405d6-ad9e-46b6-b346-cbd774fcc49d")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/SQL Format/Properties/Resources.Designer.cs b/SQL Format/Properties/Resources.Designer.cs new file mode 100644 index 0000000..dcb5688 --- /dev/null +++ b/SQL Format/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace SQL_Format.Properties +{ + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SQL_Format.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/SQL Format/Properties/Resources.resx b/SQL Format/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/SQL Format/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/SQL Format/Properties/Settings.Designer.cs b/SQL Format/Properties/Settings.Designer.cs new file mode 100644 index 0000000..e476017 --- /dev/null +++ b/SQL Format/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace SQL_Format.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/SQL Format/Properties/Settings.settings b/SQL Format/Properties/Settings.settings new file mode 100644 index 0000000..3964565 --- /dev/null +++ b/SQL Format/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/SQL Format/SQL Format.csproj b/SQL Format/SQL Format.csproj new file mode 100644 index 0000000..123ecbd --- /dev/null +++ b/SQL Format/SQL Format.csproj @@ -0,0 +1,95 @@ + + + + + Debug + AnyCPU + {832405D6-AD9E-46B6-B346-CBD774FCC49D} + WinExe + SQL_Format + SQL Format + v4.6.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Microsoft.SqlServer.TransactSql.ScriptDom.15.0.4200.1\lib\net40\Microsoft.SqlServer.TransactSql.ScriptDom.dll + + + + + + + + + + + + + + + + Form + + + FormSQLFormat.cs + + + + + + + + + + + + + FormSQLFormat.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + \ No newline at end of file diff --git a/SQL Format/SQLTranslator.cs b/SQL Format/SQLTranslator.cs new file mode 100644 index 0000000..33cfba2 --- /dev/null +++ b/SQL Format/SQLTranslator.cs @@ -0,0 +1,17 @@ +using Microsoft.SqlServer.TransactSql.ScriptDom; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace SQL_Format +{ + public abstract class SQLTranslator + { + public abstract string Translate(TableDefinition tableDefinition, object options); + public abstract string GetCaption(); + public abstract void SetupOptionsContent(Control Parent, EventHandler changedHandler); + } +} diff --git a/SQL Format/SQLTranslatorInsert.cs b/SQL Format/SQLTranslatorInsert.cs new file mode 100644 index 0000000..29e4a3a --- /dev/null +++ b/SQL Format/SQLTranslatorInsert.cs @@ -0,0 +1,159 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Microsoft.SqlServer.TransactSql.ScriptDom; + +namespace SQL_Format +{ + public class SQLTranslatorInsert : SQLTranslator + { + public override string GetCaption() + { + return "Insert"; + } + + public override void SetupOptionsContent(Control Parent, EventHandler changedHandler) + { + { + Label label = new Label(); + label.Margin = new Padding(10, 3, 0, 0); + label.Text = "Source Alias: "; + label.MinimumSize = new Size(0, 17); + label.AutoSize = false; + label.AutoSize = true; + Parent.Controls.Add(label); + + TextBox tbAlias = new TextBox(); + tbAlias.Text = "s"; + tbAlias.Name = "option_alias_src"; + tbAlias.TextChanged += changedHandler; + Parent.Controls.Add(tbAlias); + } + + { + Label label = new Label(); + label.Margin = new Padding(10, 3, 0, 0); + label.Text = "Target Alias: "; + label.MinimumSize = new Size(0, 17); + label.AutoSize = false; + label.AutoSize = true; + Parent.Controls.Add(label); + + TextBox tbAlias = new TextBox(); + tbAlias.Text = "t"; + tbAlias.Name = "option_alias_dest"; + tbAlias.TextChanged += changedHandler; + Parent.Controls.Add(tbAlias); + } + + { + CheckBox checkBox = new CheckBox(); + checkBox.Text = "Explicit projection names"; + checkBox.AutoSize = false; + checkBox.AutoSize = true; + checkBox.Name = "option_explicit_pname"; + checkBox.Checked = true; + checkBox.CheckedChanged += changedHandler; + Parent.Controls.Add(checkBox); + } + + { + CheckBox checkBox = new CheckBox(); + checkBox.Text = "Inline"; + checkBox.Name = "option_inline"; + checkBox.CheckedChanged += changedHandler; + Parent.Controls.Add(checkBox); + } + + } + + public override string Translate(TableDefinition tableDefinition, object options) + { + string optionAllias0 = null; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_alias_src", true); + if (r.Length > 0) + { + optionAllias0 = (r[0] as TextBox).Text; + } + } + string optionAlliasDest0 = null; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_alias_dest", true); + if (r.Length > 0) + { + optionAlliasDest0 = (r[0] as TextBox).Text; + } + } + + bool bOptionInline = false; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_inline", true); + if (r.Length > 0) + { + bOptionInline = (r[0] as CheckBox).Checked; + } + } + string sColumnSeparator = Environment.NewLine; + if (bOptionInline) sColumnSeparator = " "; + + bool bOptionExplicitNames = false; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_explicit_pname", true); + if (r.Length > 0) + { + bOptionExplicitNames = (r[0] as CheckBox).Checked; + } + } + + string optionAllias = optionAllias0; + string optionAlliasDest = optionAlliasDest0; + if (!String.IsNullOrEmpty(optionAllias)) optionAllias = optionAllias + '.'; + if (!String.IsNullOrEmpty(optionAlliasDest)) optionAlliasDest = optionAlliasDest + '.'; + string columnIdent = "\t"; + if (bOptionInline) columnIdent = null; + + StringBuilder result = new StringBuilder(); + string sep = null; + // insert into + { + result.Append($"insert into {optionAlliasDest0}({sColumnSeparator}"); + sep = null; + foreach (ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + result.Append($"{columnIdent}{sep}{ident}{sColumnSeparator}"); + if (String.IsNullOrEmpty(sep)) sep = ", "; + } + result.Append($"){Environment.NewLine}"); + } + // select + { + result.Append($"select{sColumnSeparator}"); + sep = null; + foreach (ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + if (bOptionExplicitNames) + { + result.Append($"{columnIdent}{sep}{ident} = {optionAllias}{ident}{sColumnSeparator}"); + } else + { + result.Append($"{columnIdent}{sep}{optionAllias}{ident}{sColumnSeparator}"); + } + if (String.IsNullOrEmpty(sep)) sep = ", "; + } + result.Append($"from {optionAllias0}{Environment.NewLine}"); + } + return result.ToString(); + } + } +} diff --git a/SQL Format/SQLTranslatorMerge.cs b/SQL Format/SQLTranslatorMerge.cs new file mode 100644 index 0000000..a43a0da --- /dev/null +++ b/SQL Format/SQLTranslatorMerge.cs @@ -0,0 +1,164 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Microsoft.SqlServer.TransactSql.ScriptDom; + +namespace SQL_Format +{ + public class SQLTranslatorMerge : SQLTranslator + { + public override string GetCaption() + { + return "Merge"; + } + + public override void SetupOptionsContent(Control Parent, EventHandler changedHandler) + { + { + Label label = new Label(); + label.Margin = new Padding(10, 3, 0, 0); + label.Text = "Source Alias: "; + label.MinimumSize = new Size(0, 17); + label.AutoSize = false; + label.AutoSize = true; + Parent.Controls.Add(label); + + TextBox tbAlias = new TextBox(); + tbAlias.Text = "s"; + tbAlias.Name = "option_alias_src"; + tbAlias.TextChanged += changedHandler; + Parent.Controls.Add(tbAlias); + } + + { + Label label = new Label(); + label.Margin = new Padding(10, 3, 0, 0); + label.Text = "Target Alias: "; + label.MinimumSize = new Size(0, 17); + label.AutoSize = false; + label.AutoSize = true; + Parent.Controls.Add(label); + + TextBox tbAlias = new TextBox(); + tbAlias.Text = "t"; + tbAlias.Name = "option_alias_dest"; + tbAlias.TextChanged += changedHandler; + Parent.Controls.Add(tbAlias); + } + } + + public override string Translate(TableDefinition tableDefinition, object options) + { + string optionAllias0 = null; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_alias_src", true); + if (r.Length > 0) + { + optionAllias0 = (r[0] as TextBox).Text; + } + } + string optionAlliasDest0 = null; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_alias_dest", true); + if (r.Length > 0) + { + optionAlliasDest0 = (r[0] as TextBox).Text; + } + } + + string optionAllias = optionAllias0; + string optionAlliasDest = optionAlliasDest0; + if (!String.IsNullOrEmpty(optionAllias)) optionAllias = optionAllias + '.'; + if (!String.IsNullOrEmpty(optionAlliasDest)) optionAlliasDest = optionAlliasDest + '.'; + + string firstColumnIdent = null; + + StringBuilder result = new StringBuilder(); + string sep = null; + // with src as + { + result.Append($";with src as ({Environment.NewLine}"); + result.Append($"\tselect{Environment.NewLine}"); + sep = null; + foreach (ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + if (String.IsNullOrEmpty(firstColumnIdent)) firstColumnIdent = ident; + result.Append($"\t\t{sep}{ident} = {optionAllias}{ident}{Environment.NewLine}"); + if (String.IsNullOrEmpty(sep)) sep = ", "; + } + result.Append($"\tfrom {optionAllias0}{Environment.NewLine}"); + result.Append($"){Environment.NewLine}"); + } + // , targ as + { + result.Append($", targ as ({Environment.NewLine}"); + result.Append($"\tselect{Environment.NewLine}"); + sep = null; + foreach (ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + result.Append($"\t\t{sep}{optionAlliasDest}{ident}{Environment.NewLine}"); + if (String.IsNullOrEmpty(sep)) sep = ", "; + } + result.Append($"\tfrom {optionAlliasDest0}{Environment.NewLine}"); + result.Append($"){Environment.NewLine}"); + } + //merge + { + result.Append($"merge targ as {optionAlliasDest0}{Environment.NewLine}"); + result.Append($"using src as {optionAllias0}{Environment.NewLine}"); + result.Append($"on ({optionAlliasDest}{firstColumnIdent} = {optionAllias}{firstColumnIdent}){Environment.NewLine}"); + } + //insert + { + result.Append($"when not matched by target then{Environment.NewLine}"); + result.Append($"\tinsert({Environment.NewLine}"); + sep = null; + foreach (ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + result.Append($"\t\t{sep}{ident}{Environment.NewLine}"); + if (String.IsNullOrEmpty(sep)) sep = ", "; + } + result.Append($"\t){Environment.NewLine}"); + result.Append($"\tvalues({Environment.NewLine}"); + sep = null; + foreach (ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + result.Append($"\t\t{sep}{optionAllias}{ident}{Environment.NewLine}"); + if (string.IsNullOrEmpty(sep)) sep = ", "; + } + result.Append($"\t){Environment.NewLine}"); + } + //update + { + result.Append($"when matched then{Environment.NewLine}"); + result.Append($"\tupdate set{Environment.NewLine}"); + sep = null; + foreach (ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + result.Append($"\t\t{sep}{ident} = {optionAllias}{ident}{Environment.NewLine}"); + if (String.IsNullOrEmpty(sep)) sep = ", "; + } + } + + //delete + { + result.Append($"when not matched by source then{Environment.NewLine}"); + result.Append($"\tdelete{Environment.NewLine}"); + } + + result.Append($";{Environment.NewLine}"); + return result.ToString(); + } + } +} diff --git a/SQL Format/SQLTranslatorSame.cs b/SQL Format/SQLTranslatorSame.cs new file mode 100644 index 0000000..3340ff9 --- /dev/null +++ b/SQL Format/SQLTranslatorSame.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Microsoft.SqlServer.TransactSql.ScriptDom; + +namespace SQL_Format +{ + public class SQLTranslatorSame : SQLTranslator + { + public override string GetCaption() + { + return "Same"; + } + + public override void SetupOptionsContent(Control Parent, EventHandler changedHandler) + { + { + Label label = new Label(); + label.Margin = new Padding(10, 3, 0, 0); + label.Text = "Source Alias: "; + label.MinimumSize = new Size(0, 17); + label.AutoSize = false; + label.AutoSize = true; + Parent.Controls.Add(label); + + TextBox tbAlias = new TextBox(); + tbAlias.Text = "s"; + tbAlias.Name = "option_alias_src"; + tbAlias.TextChanged += changedHandler; + Parent.Controls.Add(tbAlias); + } + + { + Label label = new Label(); + label.Margin = new Padding(10, 3, 0, 0); + label.Text = "Target Alias: "; + label.MinimumSize = new Size(0, 17); + label.AutoSize = false; + label.AutoSize = true; + Parent.Controls.Add(label); + + TextBox tbAlias = new TextBox(); + tbAlias.Text = "t"; + tbAlias.Name = "option_alias_dest"; + tbAlias.TextChanged += changedHandler; + Parent.Controls.Add(tbAlias); + } + + { + CheckBox checkBox = new CheckBox(); + checkBox.Text = "Inline"; + checkBox.Name = "option_inline"; + checkBox.CheckedChanged += changedHandler; + Parent.Controls.Add(checkBox); + } + + } + + public override string Translate(TableDefinition tableDefinition, object options) + { + string optionAllias0 = null; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_alias_src", true); + if (r.Length > 0) + { + optionAllias0 = (r[0] as TextBox).Text; + } + } + string optionAlliasDest0 = null; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_alias_dest", true); + if (r.Length > 0) + { + optionAlliasDest0 = (r[0] as TextBox).Text; + } + } + + bool bOptionInline = false; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_inline", true); + if (r.Length > 0) + { + bOptionInline = (r[0] as CheckBox).Checked; + } + } + string sColumnSeparator = Environment.NewLine; + if (bOptionInline) sColumnSeparator = " "; + + bool bOptionExplicitNames = false; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_explicit_pname", true); + if (r.Length > 0) + { + bOptionExplicitNames = (r[0] as CheckBox).Checked; + } + } + + string optionAllias = optionAllias0; + string optionAlliasDest = optionAlliasDest0; + if (!String.IsNullOrEmpty(optionAllias)) optionAllias = optionAllias + '.'; + if (!String.IsNullOrEmpty(optionAlliasDest)) optionAlliasDest = optionAlliasDest + '.'; + string columnIdent = "\t\t"; + if (bOptionInline) columnIdent = null; + + StringBuilder result = new StringBuilder(); + string sep = null; + // line1 + { + result.Append($"/*same*/ exists({Environment.NewLine}"); + result.Append($"\tselect{sColumnSeparator}"); + sep = null; + foreach (ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + result.Append($"{columnIdent}{sep}{optionAlliasDest}{ident}{sColumnSeparator}"); + if (String.IsNullOrEmpty(sep)) sep = ", "; + } + } + //line2 + { + if (bOptionInline) result.Append($"{Environment.NewLine}"); + result.Append($"\tintersect{Environment.NewLine}"); + result.Append($"\tselect{sColumnSeparator}"); + sep = null; + foreach (ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + result.Append($"{columnIdent}{sep}{optionAllias}{ident}{sColumnSeparator}"); + if (String.IsNullOrEmpty(sep)) sep = ", "; + } + result.Append($"{Environment.NewLine}"); + } + result.Append($"){Environment.NewLine}"); + return result.ToString(); + } + } +} diff --git a/SQL Format/SQLTranslatorSelect.cs b/SQL Format/SQLTranslatorSelect.cs new file mode 100644 index 0000000..ef0b871 --- /dev/null +++ b/SQL Format/SQLTranslatorSelect.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Microsoft.SqlServer.TransactSql.ScriptDom; + +namespace SQL_Format +{ + public class SQLTranslatorSelect : SQLTranslator + { + public override string GetCaption() + { + return "Select"; + } + + public override void SetupOptionsContent(Control Parent, EventHandler changedHandler) + { + { + Label label = new Label(); + label.Margin = new Padding(10, 3, 0, 0); + label.Text = "Alias: "; + label.MinimumSize = new Size(0, 17); + label.AutoSize = false; + label.AutoSize = true; + Parent.Controls.Add(label); + + TextBox tbAlias = new TextBox(); + tbAlias.Text = "t"; + tbAlias.Name = "option_alias"; + tbAlias.TextChanged += changedHandler; + Parent.Controls.Add(tbAlias); + } + { + CheckBox checkBox = new CheckBox(); + checkBox.Text = "Inline"; + checkBox.Name = "option_inline"; + checkBox.CheckedChanged += changedHandler; + Parent.Controls.Add(checkBox); + } + } + + public override string Translate(TableDefinition tableDefinition, object options) + { + string optionAllias = null; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_alias", true); + if (r.Length > 0) + { + optionAllias = (r[0] as TextBox).Text; + } + } + if (!String.IsNullOrEmpty(optionAllias)) optionAllias = optionAllias + '.'; + + bool bOptionInline = false; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_inline", true); + if (r.Length > 0) + { + bOptionInline = (r[0] as CheckBox).Checked; + } + } + string sColumnSeparator = Environment.NewLine; + if (bOptionInline) sColumnSeparator = " "; + + StringBuilder result = new StringBuilder(); + foreach(ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + result.Append($"{optionAllias}{ident},{sColumnSeparator}"); + } + + return result.ToString(); + } + } +} diff --git a/SQL Format/SQLTranslatorUpdate.cs b/SQL Format/SQLTranslatorUpdate.cs new file mode 100644 index 0000000..86283de --- /dev/null +++ b/SQL Format/SQLTranslatorUpdate.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Microsoft.SqlServer.TransactSql.ScriptDom; + +namespace SQL_Format +{ + public class SQLTranslatorUpdate : SQLTranslator + { + public override string GetCaption() + { + return "Update"; + } + + public override void SetupOptionsContent(Control Parent, EventHandler changedHandler) + { + { + Label label = new Label(); + label.Margin = new Padding(10, 3, 0, 0); + label.Text = "Source Alias: "; + label.MinimumSize = new Size(0, 17); + label.AutoSize = false; + label.AutoSize = true; + Parent.Controls.Add(label); + + TextBox tbAlias = new TextBox(); + tbAlias.Text = "s"; + tbAlias.Name = "option_alias_src"; + tbAlias.TextChanged += changedHandler; + Parent.Controls.Add(tbAlias); + } + + { + Label label = new Label(); + label.Margin = new Padding(10, 3, 0, 0); + label.Text = "Dest Alias: "; + label.MinimumSize = new Size(0, 17); + label.AutoSize = false; + label.AutoSize = true; + Parent.Controls.Add(label); + + TextBox tbAlias = new TextBox(); + tbAlias.Text = "t"; + tbAlias.Name = "option_alias_dest"; + tbAlias.TextChanged += changedHandler; + Parent.Controls.Add(tbAlias); + } + } + + public override string Translate(TableDefinition tableDefinition, object options) + { + string optionAllias = null; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_alias_src", true); + if (r.Length > 0) + { + optionAllias = (r[0] as TextBox).Text; + } + } + string optionAlliasDest = null; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_alias_dest", true); + if (r.Length > 0) + { + optionAlliasDest = (r[0] as TextBox).Text; + } + } + + if (!String.IsNullOrEmpty(optionAllias)) optionAllias = optionAllias + '.'; + if (!String.IsNullOrEmpty(optionAlliasDest)) optionAlliasDest = optionAlliasDest + '.'; + + StringBuilder result = new StringBuilder(); + foreach (ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + result.Append($"{optionAlliasDest}{ident} = {optionAllias}{ident},{Environment.NewLine}"); + } + + return result.ToString(); + } + } +} diff --git a/SQL Format/SQLTranslatorXmlSelect.cs b/SQL Format/SQLTranslatorXmlSelect.cs new file mode 100644 index 0000000..3b69599 --- /dev/null +++ b/SQL Format/SQLTranslatorXmlSelect.cs @@ -0,0 +1,94 @@ +using Microsoft.SqlServer.TransactSql.ScriptDom; +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace SQL_Format +{ + public class SQLTranslatorXmlSelect : SQLTranslator + { + public override string GetCaption() + { + return "Xml Select"; + } + + public override void SetupOptionsContent(Control Parent, EventHandler changedHandler) + { + { + Label label = new Label(); + label.Margin = new Padding(10, 3, 0, 0); + label.Text = "Source: "; + label.MinimumSize = new Size(0, 17); + label.AutoSize = false; + label.AutoSize = true; + Parent.Controls.Add(label); + + TextBox tbAlias = new TextBox(); + tbAlias.Text = "x"; + tbAlias.Name = "option_source"; + tbAlias.TextChanged += changedHandler; + Parent.Controls.Add(tbAlias); + } + + { + Label label = new Label(); + label.Margin = new Padding(10, 3, 0, 0); + label.Text = "Row Alias: "; + label.MinimumSize = new Size(0, 17); + label.AutoSize = false; + label.AutoSize = true; + Parent.Controls.Add(label); + + TextBox tbAlias = new TextBox(); + tbAlias.Text = "n"; + tbAlias.Name = "option_alias_row"; + tbAlias.TextChanged += changedHandler; + Parent.Controls.Add(tbAlias); + } + + } + + public override string Translate(TableDefinition tableDefinition, object options) + { + string optionSource0 = null; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_source", true); + if (r.Length > 0) + { + optionSource0 = (r[0] as TextBox).Text; + } + } + string optionRowAlias0 = null; + if (options is Control) + { + var r = ((Control)options).Controls.Find("option_alias_row", true); + if (r.Length > 0) + { + optionRowAlias0 = (r[0] as TextBox).Text; + } + } + + StringBuilder result = new StringBuilder(); + string sep = null; + result.Append($"select{Environment.NewLine}"); + // rows + { + sep = null; + foreach (ColumnDefinition columnDefinition in tableDefinition.ColumnDefinitions) + { + string ident = TSQLHelper.Identifier2Value(columnDefinition.ColumnIdentifier); + string typ = TSQLHelper.Column2TypeStr(columnDefinition); + result.Append($"\t{sep}{ident} = {optionRowAlias0}.value('@{ident}', '{typ}'){Environment.NewLine}"); + if (String.IsNullOrEmpty(sep)) sep = ", "; + } + } + result.Append($"from @{optionSource0}.nodes('/root[1]/data[1]/row') as t({optionRowAlias0}){Environment.NewLine}"); + return result.ToString(); + } + } +} diff --git a/SQL Format/TSQLHelper.cs b/SQL Format/TSQLHelper.cs new file mode 100644 index 0000000..f16165c --- /dev/null +++ b/SQL Format/TSQLHelper.cs @@ -0,0 +1,36 @@ +using Microsoft.SqlServer.TransactSql.ScriptDom; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SQL_Format +{ + public static class TSQLHelper + { + public static string Identifier2Value(Identifier identifier) + { + if (identifier.QuoteType == QuoteType.SquareBracket) + { + return $"[{identifier.Value}]"; + } + return identifier.Value; + } + + public static string Column2TypeStr(ColumnDefinition columnDefinition) + { + string result = columnDefinition.DataType.Name.Identifiers[0].Value.ToLowerInvariant(); + //+ Name {Microsoft.SqlServer.TransactSql.ScriptDom.SchemaObjectName} Microsoft.SqlServer.TransactSql.ScriptDom.SchemaObjectName + if (columnDefinition.DataType is ParameterizedDataTypeReference) + { + if (((ParameterizedDataTypeReference)columnDefinition.DataType).Parameters.Count > 0) + { + result = $"{result}({((ParameterizedDataTypeReference)columnDefinition.DataType).Parameters[0].Value})"; + } + } + + return result; + } + } +} diff --git a/SQL Format/packages.config b/SQL Format/packages.config new file mode 100644 index 0000000..8cb7912 --- /dev/null +++ b/SQL Format/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file