Ejemplo n.º 1
0
		private void BindNaturalId(HbmNaturalId naturalid, PersistentClass rootClass, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			if (naturalid == null)
			{
				return;
			}
			//by default, natural-ids are "immutable" (constant)
			var propBinder = new PropertiesBinder(mappings, rootClass, dialect);
			var uk = new UniqueKey { Name = "_UniqueKey", Table = rootClass.Table };
			propBinder.Bind(naturalid.Properties, inheritedMetas, property =>
				{
					if (!naturalid.mutable)
						property.IsUpdateable = false;
					property.IsNaturalIdentifier = true;

					uk.AddColumns(property.ColumnIterator.OfType<Column>());
				});

			rootClass.Table.AddUniqueKey(uk);	
		}
Ejemplo n.º 2
0
		/// <summary>
		/// Constructor for creating a constraint from a Hibnerate UniqueKey object.
		/// </summary>
		/// <param name="table"></param>
		/// <param name="constraint"></param>
		internal ConstraintInfo(Table table, UniqueKey constraint)
			: this("UQ_", table.Name, constraint.ColumnIterator, constraint.Name)
		{
		}
Ejemplo n.º 3
0
		/// <summary>
		/// Gets the <see cref="UniqueKey"/> identified by the name.
		/// </summary>
		/// <param name="name">The name of the <see cref="UniqueKey"/> to get.</param>
		/// <returns>
		/// The <see cref="UniqueKey"/> identified by the name.  If the <see cref="UniqueKey"/>
		/// identified by the name does not exist then it is created.
		/// </returns>
		public UniqueKey GetUniqueKey( string name )
		{
			UniqueKey uk = ( UniqueKey ) uniqueKeys[ name ];

			if( uk == null )
			{
				uk = new UniqueKey();
				uk.Name = name;
				uk.Table = this;
				uniqueKeys.Add( name, uk );
			}

			return uk;
		}
Ejemplo n.º 4
0
		protected void PropertiesFromXML(XmlNode node, PersistentClass model, IDictionary<string, MetaAttribute> inheritedMetas, UniqueKey uniqueKey, bool mutable, bool nullable, bool naturalId)
		{
			string entityName = model.EntityName;

			Table table = model.Table;

			foreach (XmlNode subnode in node.ChildNodes)
			{
				//I am only concerned with elements that are from the nhibernate namespace
				if (subnode.NamespaceURI != Configuration.MappingSchemaXMLNS)
					continue;

				string name = subnode.LocalName; //.Name;
				string propertyName = GetPropertyName(subnode);

				IValue value = null;
				CollectionBinder collectionBinder = new CollectionBinder(this);
				if (collectionBinder.CanCreate(name))
				{
					Mapping.Collection collection = collectionBinder.Create(name, subnode, entityName, propertyName, model,
					                                                        model.MappedClass, inheritedMetas);

					mappings.AddCollection(collection);
					value = collection;
				}
				else if ("many-to-one".Equals(name))
				{
					value = new ManyToOne(table);
					BindManyToOne(subnode, (ManyToOne) value, propertyName, true);
				}
				else if ("any".Equals(name))
				{
					value = new Any(table);
					BindAny(subnode, (Any) value, true);
				}
				else if ("one-to-one".Equals(name))
				{
					value = new OneToOne(table, model);
					BindOneToOne(subnode, (OneToOne) value);
				}
				else if ("property".Equals(name))
				{
					value = new SimpleValue(table);
					BindSimpleValue(subnode, (SimpleValue) value, true, propertyName);
				}
				else if ("component".Equals(name) || "dynamic-component".Equals(name))
				{
					string subpath = StringHelper.Qualify(entityName, propertyName);
					// NH: Modified from H2.1 to allow specifying the type explicitly using class attribute
					System.Type reflectedClass = GetPropertyType(subnode, model.MappedClass, propertyName);
					value = new Component(model);
					BindComponent(subnode, (Component) value, reflectedClass, entityName, propertyName, subpath, true, inheritedMetas);
				}
				else if ("join".Equals(name))
				{
					Join join = new Join();
					join.PersistentClass = model;
					BindJoin(subnode, join, inheritedMetas);
					model.AddJoin(join);
				}
				else if ("subclass".Equals(name))
					new SubclassBinder(this).HandleSubclass(model, subnode, inheritedMetas);

				else if ("joined-subclass".Equals(name))
					new JoinedSubclassBinder(this).HandleJoinedSubclass(model, subnode, inheritedMetas);

				else if ("union-subclass".Equals(name))
					new UnionSubclassBinder(this).HandleUnionSubclass(model, subnode, inheritedMetas);

				else if ("filter".Equals(name))
					ParseFilter(subnode, model);
				else if ("natural-id".Equals(name))
				{
					UniqueKey uk = new UniqueKey();
					uk.Name = "_UniqueKey";
					uk.Table = table;
					//by default, natural-ids are "immutable" (constant)

					bool mutableId = false;
					if (subnode.Attributes["mutable"] != null)
					{
						mutableId = "true".Equals(subnode.Attributes["mutable"]);						
					}

					PropertiesFromXML(subnode, model, inheritedMetas, uk, mutableId, false, true);
					table.AddUniqueKey(uk);
				}

				if (value != null)
				{
					Property property = CreateProperty(value, propertyName, model.ClassName, subnode, inheritedMetas);
					if (!mutable)
						property.IsUpdateable = false;
					if (naturalId)
						property.IsNaturalIdentifier = true;
					model.AddProperty(property);
					if (uniqueKey != null)
						uniqueKey.AddColumns(new SafetyEnumerable<Column>(property.ColumnIterator));
				}
			}
		}
Ejemplo n.º 5
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());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Generates the SQL string to create this Table in the database.
        /// </summary>
        /// <param name="dialect">The <see cref="Dialect.Dialect"/> to use for SQL rules.</param>
        /// <param name="p"></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 defaultSchema)
        {
            StringBuilder buf = new StringBuilder("create table ")
                                .Append(GetQualifiedName(dialect, defaultSchema))
                                .Append(" (");

            bool identityColumn = idValue != null && idValue.CreateIdentifierGenerator(dialect) is IdentityGenerator;

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

            if (primaryKey != null && identityColumn)
            {
                foreach (Column col in primaryKey.ColumnCollection)
                {
                    pkname = col.GetQuotedName(dialect);                       //should only go through this loop once
                }
            }

            int i = 0;

            foreach (Column col in ColumnCollection)
            {
                i++;
                buf.Append(col.GetQuotedName(dialect))
                .Append(' ')
                .Append(col.GetSqlType(dialect, p));

                if (identityColumn && col.GetQuotedName(dialect).Equals(pkname))
                {
                    buf.Append(' ')
                    .Append(dialect.IdentityColumnString);
                }
                else
                {
                    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 (i < ColumnCollection.Count)
                {
                    buf.Append(StringHelper.CommaSpace);
                }
            }

            if (primaryKey != null)
            {
                //if ( dialect is HSQLDialect && identityColumn ) {
                // // skip the primary key definition
                // //ugly hack...
                //} else {
                buf.Append(',').Append(primaryKey.SqlConstraintString(dialect, defaultSchema));
                //}
            }

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

            buf.Append(StringHelper.ClosedParen);

            return(buf.ToString());
        }