/// <summary></summary>
		public override void CreatePrimaryKey()
		{
			if ( !IsOneToMany )
			{
				PrimaryKey pk = new PrimaryKey();
				foreach( Column col in Key.ColumnCollection )
				{
					pk.AddColumn( col );
				}

				bool nullable = false;
				foreach( Column col in Element.ColumnCollection )
				{
					if( col.IsNullable )
					{
						nullable = true;
					}
					pk.AddColumn( col );
				}

				// some databases (Postgres) will tolerate nullable
				// column in a primary key - others (DB2) won't
				if( !nullable )
				{
					CollectionTable.PrimaryKey = pk;
				}
			}
			else
			{
				// Create an index on the key columns?
			}
		}
		public override void CreatePrimaryKey()
		{
			if (!IsOneToMany)
			{
				PrimaryKey pk = new PrimaryKey();
				pk.AddColumns(new SafetyEnumerable<Column>(Key.ColumnIterator));

				// index should be last column listed
				bool isFormula = false;
				foreach (ISelectable selectable in Index.ColumnIterator)
				{
					if(selectable.IsFormula)
						isFormula = true;
				}
				if (isFormula)
				{
					//if it is a formula index, use the element columns in the PK
					pk.AddColumns(new SafetyEnumerable<Column>(Element.ColumnIterator));
				}
				else
				{
					pk.AddColumns(new SafetyEnumerable<Column>(Index.ColumnIterator));
				}

				CollectionTable.PrimaryKey = pk;
			}
		}
		/// <summary></summary>
		public override void CreatePrimaryKey()
		{
			if (!IsOneToMany)
			{
				PrimaryKey pk = new PrimaryKey();
				pk.AddColumns(new SafetyEnumerable<Column>(Identifier.ColumnIterator));
				CollectionTable.PrimaryKey = pk;
			}
			//else  // Create an index on the key columns?
		}
		/// <summary></summary>
		public override void CreatePrimaryKey()
		{
			if ( !IsOneToMany )
			{
				PrimaryKey pk = new PrimaryKey();
				foreach( Column col in Identifier.ColumnCollection )
				{
					pk.AddColumn( col );
				}
				CollectionTable.PrimaryKey = pk;
			}
			//else  // Create an index on the key columns?
		}
Beispiel #5
0
    protected virtual void CopyPrimaryKey(Table dataTable,
      Table auditTable)
    {
      if (dataTable.PrimaryKey != null)
      {
        var pk = new PrimaryKey();

        pk.AddColumns(
          from column in dataTable.PrimaryKey.ColumnIterator
          select auditTable.GetColumn(column));

        auditTable.PrimaryKey = pk;
      }
    }
		public override void CreatePrimaryKey()
		{
			if (!IsOneToMany)
			{
				PrimaryKey pk = new PrimaryKey();

				foreach (Column col in Key.ColumnCollection)
				{
					pk.AddColumn(col);
				}

				// Index should be last column listed
				foreach (Column col in Index.ColumnCollection)
				{
					pk.AddColumn(col);
				}

				CollectionTable.PrimaryKey = pk;
			}
		}
Beispiel #7
0
        /// <summary>
        /// Creates the <see cref="PrimaryKey"/> for the <see cref="Table"/>
        /// this type is persisted in.
        /// </summary>
        /// <param name="dialect">The <see cref="Dialect.Dialect"/> that is used to Alias columns.</param>
        public virtual void CreatePrimaryKey(Dialect.Dialect dialect)
        {
            //Primary key constraint
            PrimaryKey pk = new PrimaryKey();
            Table table = Table;
            pk.Table = table;
            pk.Name = PKAlias.ToAliasString(table.Name, dialect);
            table.PrimaryKey = pk;

            pk.AddColumns(new SafetyEnumerable<Column>(Key.ColumnIterator));
        }
Beispiel #8
0
		public void CreatePrimaryKey(Dialect.Dialect dialect)
		{
			//Primary key constraint
			PrimaryKey pk = new PrimaryKey();
			pk.Table = table;
			pk.Name = PK_ALIAS.ToAliasString(table.Name, dialect);
			table.PrimaryKey = pk;

			pk.AddColumns(Key.ColumnIterator.OfType<Column>());
		}
		/// <summary>
		/// Creates the <see cref="PrimaryKey"/> for the <see cref="Table"/>
		/// this type is persisted in.
		/// </summary>
		/// <param name="dialect">The <see cref="Dialect.Dialect"/> that is used to Alias columns.</param>
		public virtual void CreatePrimaryKey(Dialect.Dialect dialect)
		{
			PrimaryKey pk = new PrimaryKey();
			pk.Table = table;
			pk.Name = PKAlias.ToAliasString(table.Name, dialect);
			table.PrimaryKey = pk;

			foreach (Column col in Key.ColumnCollection)
			{
				pk.AddColumn(col);
			}
		}
Beispiel #10
0
		/// <summary>
		/// Constructor for creating a constraint from a Hibnerate PrimaryKey object.
		/// </summary>
		/// <param name="table"></param>
		/// <param name="constraint"></param>
		internal ConstraintInfo(Table table, PrimaryKey constraint)
			: this("PK_", table.Name, constraint.ColumnIterator, null)
		{
		}
Beispiel #11
0
        /// <summary>
        /// Generates the SQL string to create this Table in the database.
        /// </summary>
        /// <param name="dialect">The <see cref="Dialect"/> to use for SQL rules.</param>
        /// <param name="p"></param>
        /// <param name="defaultCatalog"></param>
        /// <param name="defaultSchema"></param>
        /// <returns>
        /// A string that contains the SQL to create this Table, Primary Key Constraints
        /// , and Unique Key Constraints.
        /// </returns>
        public string SqlCreateString(Dialect.Dialect dialect, IMapping p, string defaultCatalog, string defaultSchema)
        {
            StringBuilder buf =
                new StringBuilder(HasPrimaryKey ? dialect.CreateTableString : dialect.CreateMultisetTableString).Append(' ').Append(
                    GetQualifiedName(dialect, defaultCatalog, defaultSchema)).Append(" (");

            bool identityColumn = idValue != null && idValue.IsIdentityColumn(dialect);

            // try to find out the name of the pk to create it as identity if the
            // identitygenerator is used
            string pkname = null;

            if (HasPrimaryKey && identityColumn)
            {
                foreach (Column col in PrimaryKey.ColumnIterator)
                {
                    pkname = col.GetQuotedName(dialect);                     //should only go through this loop once
                }
            }

            bool commaNeeded = false;

            foreach (Column col in ColumnIterator)
            {
                if (commaNeeded)
                {
                    buf.Append(StringHelper.CommaSpace);
                }
                commaNeeded = true;

                buf.Append(col.GetQuotedName(dialect)).Append(' ');

                if (identityColumn && col.GetQuotedName(dialect).Equals(pkname))
                {
                    // to support dialects that have their own identity data type
                    if (dialect.HasDataTypeInIdentityColumn)
                    {
                        buf.Append(col.GetSqlType(dialect, p));
                    }
                    buf.Append(' ').Append(dialect.GetIdentityColumnString(col.GetSqlTypeCode(p).DbType));
                }
                else
                {
                    buf.Append(col.GetSqlType(dialect, p));

                    if (!string.IsNullOrEmpty(col.DefaultValue))
                    {
                        buf.Append(" default ").Append(col.DefaultValue).Append(" ");
                    }

                    if (col.IsNullable)
                    {
                        buf.Append(dialect.NullColumnString);
                    }
                    else
                    {
                        buf.Append(" not null");
                    }
                }

                if (col.IsUnique)
                {
                    if (dialect.SupportsUnique)
                    {
                        buf.Append(" unique");
                    }
                    else
                    {
                        UniqueKey uk = GetUniqueKey(col.GetQuotedName(dialect) + "_");
                        uk.AddColumn(col);
                    }
                }

                if (col.HasCheckConstraint && dialect.SupportsColumnCheck)
                {
                    buf.Append(" check( ").Append(col.CheckConstraint).Append(") ");
                }

                if (string.IsNullOrEmpty(col.Comment) == false)
                {
                    buf.Append(dialect.GetColumnComment(col.Comment));
                }
            }
            if (HasPrimaryKey && (dialect.GenerateTablePrimaryKeyConstraintForIdentityColumn || !identityColumn))
            {
                buf.Append(StringHelper.CommaSpace).Append(PrimaryKey.SqlConstraintString(dialect, defaultSchema));
            }

            foreach (UniqueKey uk in UniqueKeyIterator)
            {
                buf.Append(',').Append(uk.SqlConstraintString(dialect));
            }

            if (dialect.SupportsTableCheck)
            {
                foreach (string checkConstraint in checkConstraints)
                {
                    buf.Append(", check (").Append(checkConstraint).Append(") ");
                }
            }

            if (!dialect.SupportsForeignKeyConstraintInAlterTable)
            {
                foreach (ForeignKey foreignKey in ForeignKeyIterator)
                {
                    if (foreignKey.HasPhysicalConstraint)
                    {
                        buf.Append(",").Append(foreignKey.SqlConstraintString(dialect, foreignKey.Name, defaultCatalog, defaultSchema));
                    }
                }
            }

            buf.Append(StringHelper.ClosedParen);

            if (string.IsNullOrEmpty(comment) == false)
            {
                buf.Append(dialect.GetTableComment(comment));
            }
            buf.Append(dialect.TableTypeString);

            return(buf.ToString());
        }