internal SqlChildToParentLinkNode(SqlChildToParentLink link) : base(link)
 {
     // set...
     if (!(link.ParentTable.Generate))
     {
         this.Text += " (parent not generated)";
     }
 }
Exemple #2
0
        /// <summary>
        /// Gets the statement that can create a foreign key.
        /// </summary>
        /// <returns></returns>
        public override string[] GetCreateForeignKeyStatement(SqlChildToParentLink foreignKey)
        {
            if (foreignKey == null)
            {
                throw new ArgumentNullException("foreignKey");
            }

            StringBuilder builder = new StringBuilder();

            // alter table authors add constraint pk_foobar primary key (au_id)
            builder.Append(this.AlterTableKeyword);
            builder.Append(" ");
            builder.Append(this.FormatTableName(foreignKey.Table.NativeName));
            builder.Append(" ");
            builder.Append(this.AddConstraintKeyword);
            builder.Append(" ");
            builder.Append(FormatForeignKeyName(foreignKey.NativeName));
            builder.Append(" FOREIGN KEY (");

            bool first = true;

            foreach (SqlColumn column in foreignKey.LinkFields)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    builder.Append(", ");
                }
                builder.Append(this.FormatColumnName(column.NativeName));
            }
            builder.Append(") REFERENCES ");
            builder.Append(this.FormatTableName(foreignKey.ParentTable.NativeName));
            builder.Append(" (");

            first = true;
            foreach (SqlColumn column in foreignKey.ParentTable.GetKeyColumns())
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    builder.Append(", ");
                }
                builder.Append(this.FormatColumnName(column.NativeName));
            }
            builder.Append(")");

            builder.Append(this.StatementSeparator);

            // return...
            return(new string[] { builder.ToString() });
        }
        public override void AddSchemaTables(EntityType et, Type type, BootFX.Common.Data.Schema.SqlTable coreTable,
                                             BootFX.Common.Data.Schema.SqlSchema entitySchema)
        {
            // base...
            base.AddSchemaTables(et, type, coreTable, entitySchema);

            // have we done anything?
            if (!(et.HasExtendedProperties))
            {
                return;
            }

            // create a table...
            SqlTable table = new SqlTable(et, et.NativeNameExtended.Name);

            // key...
            EntityField[] keyFields = et.GetKeyFields();
            if (keyFields == null)
            {
                throw new InvalidOperationException("keyFields is null.");
            }
            ArrayList extendedKeyColumns = new ArrayList();

            foreach (EntityField keyField in keyFields)
            {
                SqlColumn column = new SqlColumn(MangleIdColumnName(keyField.NativeName),
                                                 keyField.DBType, keyField.Size, EntityFieldFlags.Key);

                // add...
                table.Columns.Add(column);
                extendedKeyColumns.Add(column);
            }

            // columns...
            foreach (EntityField field in et.GetExtendedProperties())
            {
                table.Columns.Add(new SqlColumn(field));
            }

            // relationship to parent...
            SqlChildToParentLink link = new SqlChildToParentLink(string.Format("FK_{0}_{1}", et.NativeName.Name,
                                                                               et.NativeNameExtended.Name), coreTable);

            link.LinkFields.AddRange((SqlColumn[])extendedKeyColumns.ToArray(typeof(SqlColumn)));
            link.Columns.AddRange(coreTable.GetKeyColumns());
            table.LinksToParents.Add(link);

            // add...
            entitySchema.Tables.Add(table);
        }
Exemple #4
0
        /// <summary>
        /// Gets the statement that can create a foreign key.
        /// </summary>
        /// <returns></returns>
        public override string[] GetDropForeignKeyStatement(SqlChildToParentLink foreignKey)
        {
            if (foreignKey == null)
            {
                throw new ArgumentNullException("foreignKey");
            }

            StringBuilder builder = new StringBuilder();

            builder = new StringBuilder();
            builder.Append(this.AlterTableKeyword);
            builder.Append(" ");
            builder.Append(this.FormatTableName(foreignKey.Table.NativeName));
            builder.Append(" ");
            builder.Append(" DROP CONSTRAINT ");
            builder.Append(" ");
            builder.Append(this.FormatConstraintName(foreignKey.NativeName));

            return(new string[] { builder.ToString() });
        }
Exemple #5
0
        /// <summary>
        /// Adds links to parents in the schema.
        /// </summary>
        /// <param name="schema"></param>
        private void AddParentLinks(SqlSchema schema)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            // get all constraints...
            DataTable table = this.ExecuteDataTable(new SqlStatement(@"select ctuparent.table_name as parent_table_name, unique_constraint_name as parent_constraint_name, 
			ctuchild.table_name as child_table_name, rc.constraint_name as child_constraint_name
				from information_schema.referential_constraints rc
					inner join information_schema.constraint_table_usage ctuparent on unique_constraint_name = ctuparent.constraint_name
					inner join information_schema.constraint_table_usage ctuchild on rc.constraint_name = ctuchild.constraint_name order by ctuparent.table_name, rc.constraint_name"                    ,
                                                                     this.Dialect));

            // walk each row...
            foreach (DataRow constraint in table.Rows)
            {
                // parent/child...
                SqlTable parent = schema.Tables[(string)constraint["parent_table_name"]];
                if (parent != null)
                {
                    // child...
                    SqlTable child = schema.Tables[(string)constraint["child_table_name"]];
                    if (child != null)
                    {
                        // get the names...
                        string parentConstraint = (string)constraint["parent_constraint_name"];
                        string childConstraint  = (string)constraint["child_constraint_name"];

                        // find the columns...
                        SqlColumn[] parentColumns = this.GetConstraintColumns(parent, parentConstraint);
                        SqlColumn[] childColumns  = this.GetConstraintColumns(child, childConstraint);

                        // check...
                        if (parentColumns == null)
                        {
                            throw new InvalidOperationException("parentColumns is null.");
                        }
                        if (childColumns == null)
                        {
                            throw new InvalidOperationException("childColumns is null.");
                        }
                        if (parentColumns.Length > 0 && parentColumns.Length == childColumns.Length)
                        {
                            // get the parent key columns...
                            ArrayList parentKeyColumns = new ArrayList(parent.GetKeyColumns());
                            if (parentKeyColumns.Count > 0)
                            {
                                // loop and remove...
                                foreach (SqlColumn parentColumn in parentColumns)
                                {
                                    parentKeyColumns.Remove(parentColumn);
                                }

                                // eliminated?
                                if (parentKeyColumns.Count == 0)
                                {
                                    // this is a legitimate match... create a link for that...
                                    SqlChildToParentLink link = new SqlChildToParentLink(childConstraint, parent);

                                    // add columns...
                                    link.Columns.AddRange(childColumns);
                                    link.LinkFields.AddRange(parent.GetKeyColumns());

                                    // add...
                                    child.LinksToParents.Add(link);

                                    // mbr - 04-10-2007 - set the owner...
                                    link.SetSchema(child.Schema);
                                }
                            }
                        }
                    }
                }
            }
        }
 public SqlChildToParentLinkView(SqlChildToParentLink link) : base(link)
 {
 }