Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<configuration>
<appSettings>
<add key="excludeDataTypes" value="timestamp;" />
<add key="sortColumnsByOrdinalPosition" value="false" />
<add key="sortRowsByPrimaryKey" value="false" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
Expand Down
31 changes: 29 additions & 2 deletions ScriptWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class ScriptWriter
public bool OptionProcWrapUp = false;
private string _BatchSeparator = "GO";
private List<string> _excludeDataTypes = new List<string>();
private bool _sortColumnsByOrdinalPosition = false;
private bool _sortRowsByPrimaryKey = false;


public ScriptWriter(CAMOsoft.DbUtils.MsSqlSession db, string outputFolder)
Expand All @@ -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<string>(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();
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
{
Expand Down