public override void Visit(IRefactorNode refactorNode) { IRefactoring refactoring = null; if(refactorNode.Name == "add-audit-table") refactoring = new AddAuditTableRefactoring(); if(refactoring != null) { refactoring.Execute(dbPlatform, refactorNode, Environment); } // if }
public void Execute(IDbPlatform platform, IRefactorNode refactorNode, Environment environment) { string auditTableName = AstNodePropertyUtil.AsString(refactorNode.Properties["audit-table"].Value); string tableName = AstNodePropertyUtil.AsString(refactorNode.Properties["table"].Value); // // We only need columns here ITableDefinition tableDefinition = environment.Schema.GetTable(tableName); ITableDefinition auditTable = new TableDefinition(auditTableName); foreach(ColumnDefinition column in tableDefinition.Columns) { // // Keep name and type only. IColumnDefinition auditColumn = new ColumnDefinition(column.Name); // // Special handling for rowversion if(column.Type == DbType.Time) { auditColumn.Length = 8; auditColumn.Type = DbType.Binary; } // if else { auditColumn.Length = column.Length; auditColumn.Type = column.Type; } // else auditColumn.Nullable = true; auditTable.AddColumn(auditColumn); } // foreach environment.Schema.AddTable(auditTable); // // Now build an AST IAddTableNode addAuditTableNode = new AddTableNode(refactorNode.Parent, auditTableName); foreach(IColumnDefinition auditColumn in auditTable.Columns) { IAddColumnNode addAuditColumnNode = new AddColumnNode(addAuditTableNode, auditColumn.Name); addAuditColumnNode.Type = auditColumn.Type; addAuditColumnNode.Length = auditColumn.Length; addAuditColumnNode.Nullable = auditColumn.Nullable; addAuditTableNode.ChildNodes.Add(addAuditColumnNode); } // foreach addAuditTableNode.Parent.ChildNodes.Add(addAuditTableNode); }