コード例 #1
0
        public void CloneAddColumnNode()
        {
            IBaselineNode baselineNode = new BaselineNode(null);
            IAddColumnNode originalNode = new AddColumnNode(baselineNode, "Add Column");
            originalNode.Identity = true;
            originalNode.Length = 321;
            originalNode.Nullable = true;
            originalNode.Precision = 32;
            originalNode.PrimaryKey = true;
            originalNode.Scale = 55;
            originalNode.Table = "Qux";
            originalNode.Type = DbType.Boolean;
            
            IAddColumnNode clonedNode = AstUtil.Clone(originalNode);

            Assert.AreSame(originalNode.Parent, clonedNode.Parent);
            Assert.AreEqual(originalNode.Name, clonedNode.Name);
            Assert.AreEqual(originalNode.Identity, clonedNode.Identity);
            Assert.AreEqual(originalNode.Length, clonedNode.Length);
            Assert.AreEqual(originalNode.Nullable, clonedNode.Nullable);
            Assert.AreEqual(originalNode.Precision, clonedNode.Precision);
            Assert.AreEqual(originalNode.PrimaryKey, clonedNode.PrimaryKey);
            Assert.AreEqual(originalNode.Scale, clonedNode.Scale);
            Assert.AreEqual(originalNode.Type, clonedNode.Type);
            Assert.AreEqual(originalNode.Table, clonedNode.Table);
        }
コード例 #2
0
        private void BuildAddColumnNode(IColumnDefinition column, IAddTableNode parent)
        {
            IAddColumnNode addColumnNode = new AddColumnNode(parent, column.Name);
            parent.ChildNodes.Add(addColumnNode);

            SemanticModelUtil.Copy(column, addColumnNode);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        /// <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);
        }
コード例 #5
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;
        }
        /// <summary>
        /// Visits the given <paramref name="addTableNode"/>.
        /// </summary>
        /// <param name="addTableNode"></param>
        public override void Visit(IAddTableNode addTableNode)
        {
            //
            // If table has "primary-key" property set to "false", no not
            // resolve PK for it
            if(addTableNode.Properties[MdlSyntax.PrimaryKey] != null &&
                AstNodePropertyUtil.AsString(addTableNode.Properties, MdlSyntax.PrimaryKey) == "false")
                return;

            //
            // Iterate through IAddColumnNode children to see if there's a PK specified. If there is one
            // no default PK should be added.
            foreach(IAddColumnNode addColumnNode in Filter<IAddColumnNode>(addTableNode.ChildNodes))
            {
                //
                // We can't use Environment.Resolve here since no bindings were 
                // built yet. Rely on raw properties instead
                if(addColumnNode.Properties[MdlSyntax.PrimaryKey] != null &&
                    AstNodePropertyUtil.AsString(addColumnNode.Properties, MdlSyntax.PrimaryKey) == "true")
                    return;
            } // foreach

            if(defaultPrimaryKeyNode == null)
                return;

            //
            // Create an IAddColumnNode and copy all properties from the default PK...
            IAddColumnNode addPrimaryKeyColumnNode = new AddColumnNode(addTableNode, defaultPrimaryKeyNode.Name);
            foreach(IAstNodeProperty astNodeProperty in MergeableProperties(defaultPrimaryKeyNode.Properties))
                addPrimaryKeyColumnNode.Properties.AddProperty(astNodeProperty);

            //
            // Add it to the front...
            addTableNode.ChildNodes.Insert(0, addPrimaryKeyColumnNode);
        }
コード例 #7
0
        public void CopyColumnProperties()
        {
            IColumnDefinition columnDefinition = new ColumnDefinition("Foo", null, DbType.Binary, true, 234, 56, 78, true, true);
            
            IColumnNode columnNode = new AddColumnNode(null, "Foo");
            AstUtil.CopyProperties(columnDefinition, columnNode);

            Assert.AreEqual("true", AstNodePropertyUtil.AsString(columnNode.Properties[MdlSyntax.Identity].Value));
            Assert.AreEqual(234, AstNodePropertyUtil.AsInteger(columnNode.Properties[MdlSyntax.Length].Value));
            Assert.AreEqual("true", AstNodePropertyUtil.AsString(columnNode.Properties[MdlSyntax.Nullable].Value));
            Assert.AreEqual(78, AstNodePropertyUtil.AsInteger(columnNode.Properties[MdlSyntax.Precision].Value));
            Assert.AreEqual("true", AstNodePropertyUtil.AsString(columnNode.Properties[MdlSyntax.PrimaryKey].Value));
            Assert.AreEqual(56, AstNodePropertyUtil.AsInteger(columnNode.Properties[MdlSyntax.Scale].Value));
            Assert.AreEqual("Binary", AstNodePropertyUtil.AsString(columnNode.Properties[MdlSyntax.Type].Value));
        }
コード例 #8
0
        /// <summary>
        /// Clones the given <paramref name="addColumnNode"/>.
        /// </summary>
        /// <param name="addColumnNode"></param>
        /// <returns></returns>
        public static IAddColumnNode Clone(IAddColumnNode addColumnNode)
        {
            IAddColumnNode node = new AddColumnNode(addColumnNode.Parent, addColumnNode.Name);
            SemanticModelUtil.Copy(addColumnNode, node);

            foreach(IAstNodeProperty property in addColumnNode.Properties)
                node.Properties.AddProperty(new AstNodeProperty(property.Name, property.Value));

            return node;
        }