From 08a63cdb85921bf978d1658093d85b88415df4e5 Mon Sep 17 00:00:00 2001 From: David Mine Date: Tue, 22 Aug 2023 20:38:26 -0400 Subject: [PATCH] Allow for sorting by col and row Nothing changes by default. If the user configures the app to sort by column, the cols are sorted by their ordinal position in the database table If the user configures the app to sort by row, the primary keys are discovered and used to sort the main data table --- App.config | 2 ++ ScriptWriter.cs | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/App.config b/App.config index 1009345..8b6ef99 100644 --- a/App.config +++ b/App.config @@ -2,6 +2,8 @@ + + diff --git a/ScriptWriter.cs b/ScriptWriter.cs index 3c02a70..5e23548 100644 --- a/ScriptWriter.cs +++ b/ScriptWriter.cs @@ -19,6 +19,8 @@ public class ScriptWriter public bool OptionProcWrapUp = false; private string _BatchSeparator = "GO"; private List _excludeDataTypes = new List(); + private bool _sortColumnsByOrdinalPosition = false; + private bool _sortRowsByPrimaryKey = false; public ScriptWriter(CAMOsoft.DbUtils.MsSqlSession db, string outputFolder) @@ -27,10 +29,16 @@ public ScriptWriter(CAMOsoft.DbUtils.MsSqlSession db, string outputFolder) _OutputFolder = outputFolder; string sql = "SELECT @@SPID AS SPID, SUSER_NAME() AS UserName, DB_NAME() AS DbName, @@SERVERNAME AS ServerName, @@VERSION as ServerVersion;"; _ServerInfoRow = _db.SelectRow(sql); - + if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["excludeDataTypes"])) _excludeDataTypes = new List(ConfigurationManager.AppSettings["excludeDataTypes"].Split(new char[] { ';' })); + if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["sortColumnsByOrdinalPosition"])) + bool.TryParse(ConfigurationManager.AppSettings["sortColumnsByOrdinalPosition"], out _sortColumnsByOrdinalPosition); + + if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["sortRowsByPrimaryKey"])) + bool.TryParse(ConfigurationManager.AppSettings["sortRowsByPrimaryKey"], out _sortRowsByPrimaryKey); + InitTable(); } @@ -109,8 +117,13 @@ private DataTable LoadColumnsInfo(ScriptObject so) var filteredDr = dt.Select(filter); var filteredDt = new DataTable(); - if (filteredDr.Length != 0) + if (filteredDr.Length != 0) { filteredDt = filteredDr.CopyToDataTable(); + if (_sortColumnsByOrdinalPosition) { + filteredDt.DefaultView.Sort = "ORDINAL_POSITION"; + filteredDt = filteredDt.DefaultView.ToTable(); + } + } filteredDt.TableName = dt.TableName; return filteredDt; @@ -316,6 +329,10 @@ private void ScriptTableMerge(ScriptObject so, DataTable colInfoTable, DataTable string tmpTableName = so.TempCounterpart; bool useIdentity = hasIdentity(colInfoTable); + if (_sortRowsByPrimaryKey) { + mainTable = DataTableSortedByPrimaryKey(colInfoTable, mainTable); + } + w.WriteLine(String.Format("IF OBJECT_ID('tempdb.dbo.{0}') IS NOT NULL DROP TABLE {0};", tmpTableName)); w.WriteLine(String.Format("SELECT {2} INTO {1} FROM {0} WHERE 0=1;", so.FullQuoted, tmpTableName, colList)); w.WriteLine(_BatchSeparator); @@ -406,6 +423,9 @@ private void ScriptTableMerge(ScriptObject so, DataTable colInfoTable, DataTable private void ScriptTableInitialInsert(ScriptObject so, DataTable colInfoTable, DataTable mainTable, System.IO.StreamWriter w, string colList, string scolList, string PKcolListOn) { bool useIdentity = hasIdentity(colInfoTable); + if (_sortRowsByPrimaryKey) { + mainTable = DataTableSortedByPrimaryKey(colInfoTable, mainTable); + } //Begin IF w.WriteLine(String.Format("IF NOT EXISTS (SELECT TOP (1) * FROM {0})", so.FullQuoted)); @@ -450,6 +470,13 @@ private void ScriptTableInitialInsert(ScriptObject so, DataTable colInfoTable, D w.WriteLine(_BatchSeparator); } + private DataTable DataTableSortedByPrimaryKey(DataTable colInfoTable, DataTable mainTable) { + var primaryKeyColumns = colInfoTable.Select("constraint_type = 'PRIMARY KEY'").Select(dr => dr["COLUMN_NAME"].ToString()).ToList(); + + mainTable.DefaultView.Sort = string.Join(", ", primaryKeyColumns); + return mainTable.DefaultView.ToTable(); + } + public string OutputFolder {