-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
472d2d1
commit a955e5e
Showing
8 changed files
with
360 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
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; | ||
using SQL_Format.Helpers; | ||
|
||
namespace SQL_Format | ||
{ | ||
public class SQLTranslatorCopySingle : SQLTranslator | ||
{ | ||
public override string GetCaption() | ||
{ | ||
return "Copy Table Sinlge"; | ||
} | ||
|
||
public override void SetupOptionsContent(Control Parent, EventHandler changedHandler) | ||
{ | ||
this.AddOptionTextBox("Var Suffix", "varSuffix", "11245U1", Parent, changedHandler); | ||
this.AddOptionCheckBox("Use Transaction", "useTransaction", true, Parent, changedHandler); | ||
this.AddOptionCheckBox("Rollback", "useRollback", true, Parent, changedHandler); | ||
} | ||
|
||
public override string TranslateExt2(CreateTableStatement createTableStatement, object options, string content, TSqlScript sqlScript) | ||
{ | ||
string varSuffix = this.GetOptionString("varSuffix", options) ?? ""; | ||
bool useTransaction = this.GetOptionBoolDef("useTransaction", options, false); | ||
bool useRollback = this.GetOptionBoolDef("useRollback", options, false); | ||
|
||
SqlBuilder result = new SqlBuilder(); | ||
SqlDomBuilder sqlDomBuilder = new SqlDomBuilder(sqlScript, result); | ||
|
||
|
||
string tableSuffix = "_new"; | ||
result.TableNameFull = TSQLHelper.Identifiers2Value(createTableStatement.SchemaObjectName.Identifiers); | ||
result.TableName = TSQLHelper.Identifiers2ValueLast(createTableStatement.SchemaObjectName.Identifiers); | ||
string tableNameNew = result.TableName + tableSuffix; | ||
string tableNameFullNew = result.TableNameFull + tableSuffix; | ||
{ | ||
result.AppendBegin(SqlBuilder.BEGIN_TRY); | ||
{ | ||
if (useTransaction) result.AppendLine($"begin tran;").NL(); | ||
|
||
string messageVarName = $"@msg{varSuffix}"; | ||
result.AppendLine($"declare {messageVarName} nvarchar(2024);").NL(); | ||
sqlDomBuilder.VarMsgName = messageVarName; | ||
sqlDomBuilder.Print($"Running Script{varSuffix}.{result.TableNameFull}...").NL(); | ||
|
||
result.AppendIf($"object_id('{tableNameFullNew}') is null").AppendBegin(); | ||
{ | ||
result.AppendLine("-- create new table"); | ||
sqlDomBuilder.ProduceFullTableRenameContent(sqlScript, tableSuffix, content); | ||
|
||
result.AppendEnd(); | ||
} | ||
|
||
sqlDomBuilder.ProduceCopyTableSingle(createTableStatement, varSuffix, targetTableNameFull: tableNameFullNew); | ||
|
||
result.AppendBegin(); | ||
{ | ||
result.AppendLine("-- rename source table to _old"); | ||
sqlDomBuilder.ProduceFullTableRenameScript(sqlScript, "_old"); | ||
result.AppendEnd(); | ||
} | ||
|
||
result.AppendBegin(); | ||
{ | ||
result.AppendLine("-- rename new table to current"); | ||
sqlDomBuilder.ProduceFullTableRenameScript(sqlScript, tableSuffix, reverse: true); | ||
|
||
result.AppendEnd(); | ||
} | ||
|
||
sqlDomBuilder.ProduceCopytableVerify(createTableStatement, varSuffix, targetTableNameFull: result.TableNameFull + "_old"); | ||
|
||
result.NL(); | ||
sqlDomBuilder.Print("Dropping table {result.TableNameFull}_old..."); | ||
result.AppendLine($"drop table if exists {result.TableNameFull}_old;").NL(); | ||
|
||
if (useTransaction) | ||
{ | ||
if (useRollback) | ||
result.AppendLine($"rollback;").NL(); | ||
else | ||
{ | ||
result.AppendLine($"commit;").NL(); | ||
sqlDomBuilder.Print("Committed."); | ||
} | ||
} | ||
|
||
result.AppendEnd(SqlBuilder.END_TRY, false); | ||
} | ||
result.AppendCatchTypicalRollback(); | ||
} | ||
return result.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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; | ||
using SQL_Format.Helpers; | ||
|
||
namespace SQL_Format | ||
{ | ||
public class SQLTranslatorMyScript: SQLTranslator | ||
{ | ||
public override string GetCaption() | ||
{ | ||
return "My Script"; | ||
} | ||
|
||
public override void SetupOptionsContent(Control Parent, EventHandler changedHandler) | ||
{ | ||
} | ||
|
||
public override string TranslateExt2(CreateTableStatement createTableStatement, object options, string content, TSqlScript sqlScript) | ||
{ | ||
SqlBuilder result = new SqlBuilder(); | ||
SqlDomBuilder sqlDomBuilder = new SqlDomBuilder(sqlScript, result); | ||
result.TableNameFull = TSQLHelper.Identifiers2Value(createTableStatement.SchemaObjectName.Identifiers); | ||
result.TableName = TSQLHelper.Identifiers2ValueLast(createTableStatement.SchemaObjectName.Identifiers); | ||
|
||
int[] precs = new int[] { 18, 26, 20}; | ||
|
||
result.AppendLine($"----------------- {result.TableNameFull} -----------------"); | ||
result.AppendLine($"raiserror('Altering {result.TableNameFull}...', 10, 1) with nowait;"); | ||
|
||
int addedCount = 0; | ||
foreach (var c in createTableStatement.Definition.ColumnDefinitions) | ||
{ | ||
string colType = TSQLHelper.Column2TypeStr(c).ToUpperInvariant(); | ||
if (colType.StartsWith("DECIMAL")) | ||
{ | ||
int prec = TSQLHelper.ColumnDecimalPrecision(c); | ||
if (precs.Contains(prec)) | ||
{ | ||
addedCount++; | ||
bool isNullable = TSQLHelper.ColumnIsNullable(c); | ||
string null_ = isNullable ? " NULL" : " NOT NULL"; | ||
string colName = TSQLHelper.Identifier2Value(c.ColumnIdentifier); | ||
string subalterColumn = $"column {colName} {colType}{null_}"; | ||
result.AppendIf($"not exists(select * from INFORMATION_SCHEMA.COLUMNS c where c.TABLE_NAME = '{result.TableName}' and c.COLUMN_NAME = '{colName}' and c.NUMERIC_PRECISION = {prec})").AppendBegin(); | ||
{ | ||
string alterColumn = $"alter {subalterColumn}"; | ||
result.AppendLine($"raiserror('{alterColumn}...', 10, 1) with nowait;"); | ||
if (isNullable) | ||
result.AppendLine($"update {result.TableNameFull} set {colName} = try_cast({colName} as {colType});"); | ||
result.AppendLine($"alter table {result.TableNameFull}").Indent(); | ||
result.AppendLine($"{alterColumn};").Unindent(); | ||
result.AppendEnd(addSpaceafterwards: false); | ||
} | ||
result.AppendLine("else").Indent(); | ||
{ | ||
result.AppendLine($"raiserror('already {subalterColumn}.', 10, 1) with nowait;"); | ||
result.Unindent(); | ||
} | ||
|
||
result.AppendLine("GO"); | ||
} | ||
} | ||
} | ||
result.AppendLine($"----------------------------------"); | ||
return result.ToString(); | ||
} | ||
} | ||
} |
Oops, something went wrong.