private void BuildAddTableNode(ITableDefinition table, IAstNode parent)
        {
            IAddTableNode addTableNode = new AddTableNode(parent, table.Name);
            parent.ChildNodes.Add(addTableNode);

            foreach(IColumnDefinition column in table.Columns)
            {
                BuildAddColumnNode(column, addTableNode);
            } // foreach
        }
        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);
        }
        /// <summary>
        /// Visits the given <paramref name="versionNode"/>.
        /// </summary>
        /// <param name="versionNode"></param>
        public override void Visit(IVersionNode versionNode)
        {
            //
            // Add a IAddTableNode which will create "SchemaInfo" tabled 
            // to the first IVersionNode we encounter 
            if(builtSchemaInfo)
                return;

            IAddTableNode addSchemaInfoTableNode = new AddTableNode(versionNode, "SchemaInfo");
            
            IAddColumnNode addVersionColumnNode = new AddColumnNode(addSchemaInfoTableNode, "Version");
            addVersionColumnNode.Properties.AddProperty(AstNodeProperty.Symbol(MdlSyntax.Type, DbType.Int64.ToString()));
            addVersionColumnNode.Properties.AddProperty(AstNodeProperty.Symbol(MdlSyntax.Unique, "true"));
            addVersionColumnNode.Properties.AddProperty(AstNodeProperty.Symbol(MdlSyntax.Nullable, "false"));

            addSchemaInfoTableNode.ChildNodes.Add(addVersionColumnNode);

            builtSchemaInfo = true;

            versionNode.ChildNodes.Insert(0, addSchemaInfoTableNode);
        }
示例#4
0
        /// <summary>
        /// Builds up an AST with <paramref name="rootNode"/> as a root node 
        /// from the provided <paramref name="schema"/>.
        /// </summary>
        /// <param name="rootNode"></param>
        /// <param name="schema"></param>
        /// <returns></returns>
        public static IAstNode BuildAstNodeFromSchema(IAstNode rootNode, Schema schema)
        {
            foreach(ITableDefinition table in schema.Tables)
            {
                IAddTableNode addTableNode = new AddTableNode(rootNode, table.Name);
                rootNode.ChildNodes.Add(addTableNode);

                foreach(IColumnDefinition column in table.Columns)
                {
                    IAddColumnNode addColumnNode = new AddColumnNode(addTableNode, column.Name);
                    addTableNode.ChildNodes.Add(addColumnNode);

                    SemanticModelUtil.Copy(column, addColumnNode);
                    CopyProperties(column, addColumnNode);
                } // foreach

                foreach(IIndexDefinition index in table.Indexes)
                {
                    IAddIndexNode addIndexNode = new AddIndexNode(rootNode, index.Name);
                    rootNode.ChildNodes.Add(addIndexNode);

                    SemanticModelUtil.Copy(index, addIndexNode);
                    CopyProperties(index, addIndexNode);
                } // foreach

                foreach(IReferenceDefinition reference in table.References)
                {
                    IAddReferenceNode addReferenceNode = new AddReferenceNode(rootNode, reference.Name);
                    rootNode.ChildNodes.Add(addReferenceNode);

                    SemanticModelUtil.Copy(reference, addReferenceNode);
                    CopyProperties(reference, addReferenceNode);
                } // foreach
            } // foreach

            return rootNode;
        }