public void UnmatchingColumns()
		{
			Table primaryTable = new Table("pktable");
			primaryTable.PrimaryKey = new PrimaryKey();
			SimpleValue sv = new SimpleValue();
			sv.TypeName = NHibernateUtil.Int16.Name;
			Column pkColumn = new Column("pk_column");
			pkColumn.Value = sv;

			primaryTable.PrimaryKey.AddColumn(pkColumn);

			Table fkTable = new Table("fktable");

			ForeignKey fk = new ForeignKey();
			sv = new SimpleValue();
			sv.TypeName = NHibernateUtil.Int16.Name;
			Column fkColumn1 = new Column("col1");
			fkColumn1.Value = sv;

			sv = new SimpleValue();
			sv.TypeName = NHibernateUtil.Int16.Name;
			Column fkColumn2 = new Column("col2");
			fkColumn2.Value = sv;

			fk.AddColumn(fkColumn1);
			fk.AddColumn(fkColumn2);

			fk.Table = fkTable;

			fk.ReferencedTable = primaryTable;
			Assert.Throws<FKUnmatchingColumnsException>(() => fk.AlignColumns());
		}
Beispiel #2
0
		private void BindTimestamp(HbmTimestamp timestampSchema, PersistentClass rootClass, Table table)
		{
			if (timestampSchema == null)
				return;

			string propertyName = timestampSchema.name;
			SimpleValue simpleValue = new SimpleValue(table);

			BindColumns(timestampSchema, simpleValue, propertyName);

			if (!simpleValue.IsTypeSpecified)
				simpleValue.TypeName = NHibernateUtil.Timestamp.Name;

			Mapping.Property property = new Mapping.Property(simpleValue);
			BindProperty(timestampSchema, property);

			// for version properties marked as being generated, make sure they are "always"
			// generated; "insert" is invalid. This is dis-allowed by the schema, but just to make
			// sure...

			if (property.Generation == PropertyGeneration.Insert)
				throw new MappingException("'generated' attribute cannot be 'insert' for versioning property");

			simpleValue.NullValue = timestampSchema.unsavedvalue;
			rootClass.Version = property;
			rootClass.AddProperty(property);
		}
		private void BindSimpleValue(HbmDiscriminator discriminatorSchema, SimpleValue discriminator)
		{
			if (discriminatorSchema.type != null)
				discriminator.TypeName = discriminatorSchema.type;

			if (discriminatorSchema.formula != null)
			{
				var f = new Formula {FormulaString = discriminatorSchema.formula};
				discriminator.AddFormula(f);
			}
			else
			{
				new ColumnsBinder(discriminator, Mappings).Bind(discriminatorSchema.Columns, false,
				                                                () =>
				                                                new HbmColumn
				                                                	{
				                                                		name =
				                                                			mappings.NamingStrategy.PropertyToColumnName(
				                                                			RootClass.DefaultDiscriminatorColumnName),
				                                                		length = discriminatorSchema.length,
																														notnull = discriminatorSchema.notnull,
																														notnullSpecified = true
				                                                	});
			}
		}
Beispiel #4
0
		private void CreateIdentifierProperty(HbmId idSchema, PersistentClass rootClass, SimpleValue id)
		{
			if (idSchema.name != null)
			{
				string access = idSchema.access ?? mappings.DefaultAccess;
				id.SetTypeUsingReflection(rootClass.MappedClass.AssemblyQualifiedName, idSchema.name, access);

				Mapping.Property property = new Mapping.Property(id);
				property.Name = idSchema.name;

				if (property.Value.Type == null)
					throw new MappingException("could not determine a property type for: " + property.Name);

				property.PropertyAccessorName = idSchema.access ?? mappings.DefaultAccess;
				property.Cascade = mappings.DefaultCascade;
				property.IsUpdateable = true;
				property.IsInsertable = true;
				property.IsOptimisticLocked = true;
				property.Generation = PropertyGeneration.Never;
				property.MetaAttributes = GetMetas(idSchema);

				rootClass.IdentifierProperty = property;

				LogMappedProperty(property);
			}
		}
		public void BindId(HbmId idSchema, PersistentClass rootClass, Table table)
		{
			if (idSchema != null)
			{
				var id = new SimpleValue(table);
				new TypeBinder(id, Mappings).Bind(idSchema.Type);

				rootClass.Identifier = id;

				Func<HbmColumn> defaultColumn = () => new HbmColumn
				{
					name = idSchema.name ?? RootClass.DefaultIdentifierColumnName,
					length = idSchema.length
				};

				new ColumnsBinder(id, Mappings).Bind(idSchema.Columns, false, defaultColumn);

				CreateIdentifierProperty(idSchema, rootClass, id);
				VerifiyIdTypeIsValid(id.Type, rootClass.EntityName);

				new IdGeneratorBinder(Mappings).BindGenerator(id, GetIdGenerator(idSchema));

				id.Table.SetIdentifierValue(id);

				BindUnsavedValue(idSchema, id);
			}
		}
		public void BindGenerator(SimpleValue id, HbmGenerator generatorMapping)
		{
			if (generatorMapping != null)
			{
				if (generatorMapping.@class == null)
					throw new MappingException("no class given for generator");

				// NH Differen behavior : specific feature NH-1817
				TypeDef typeDef = mappings.GetTypeDef(generatorMapping.@class);
				if (typeDef != null)
				{
					id.IdentifierGeneratorStrategy = typeDef.TypeClass;
					// parameters on the property mapping should override parameters in the typedef
					var allParameters = new Dictionary<string, string>(typeDef.Parameters);
					ArrayHelper.AddAll(allParameters, GetGeneratorProperties(generatorMapping, id.Table.Schema));

					id.IdentifierGeneratorProperties = allParameters;
				}
				else
				{
					id.IdentifierGeneratorStrategy = generatorMapping.@class;
					id.IdentifierGeneratorProperties = GetGeneratorProperties(generatorMapping, id.Table.Schema);
				}
			}
		}
 protected virtual void BindColumn(SimpleValue model, ColumnMapping columnMapping)
 {
     Table table = model.Table;
     var column = CreateColumn(model, columnMapping);
     if (table != null)
         table.AddColumn(column);
     model.AddColumn(column);
 }
Beispiel #8
0
		private static SimpleValue CreateIdentifier(HbmId idSchema, PersistentClass rootClass, Table table)
		{
			SimpleValue iv = new SimpleValue(table);
			iv.TypeName = idSchema.type;
			rootClass.Identifier = iv;

			return iv;
		}
		public void YesNoSqlType()
		{
			SimpleValue sv = new SimpleValue();
			sv.TypeName = NHibernateUtil.YesNo.Name;
			Column column = new Column();
			column.Value = sv;
			string type = column.GetSqlType(_dialect, null);
			Assert.AreEqual("CHAR(1)", type);
		}
		public static void BindFk(PersistentClass referencedEntity,
		                          PersistentClass destinationEntity,
		                          Ejb3JoinColumn[] columns,
		                          SimpleValue value,
		                          bool unique,
		                          ExtendedMappings mappings)
		{
			throw new NotImplementedException();
		}
		public TypeBinder(SimpleValue value, Mappings mappings)
			: base(mappings)
		{
			if (value == null)
			{
				throw new ArgumentNullException("value");
			}
			this.value = value;
		}
Beispiel #12
0
		private void AddColumns(HbmId idSchema, SimpleValue id)
		{
			if (idSchema.column1 != null)
				AddColumnFromAttribute(idSchema, id);
			else
				AddColumnsFromList(idSchema, id);

			if (id.ColumnSpan == 0)
				AddDefaultColumn(idSchema, id);
		}
Beispiel #13
0
 public static void MakeVersion(XmlNode node, SimpleValue model)
 {
     if (node != null && node.Attributes != null)
     {
         XmlAttribute attribute = node.Attributes["unsaved-value"];
         model.NullValue = attribute == null ? null : attribute.Value;
     }
     else
         model.NullValue = null;
 }
		public void StringSqlType()
		{
			SimpleValue sv = new SimpleValue();
			sv.TypeName = NHibernateUtil.String.Name;
			Column column = new Column();
			column.Value = sv;
			Assert.AreEqual("NVARCHAR(255)", column.GetSqlType(_dialect, null));

			column.Length = 100;
			Assert.AreEqual("NVARCHAR(100)", column.GetSqlType(_dialect, null));
		}
		private void CheckDefaultUnsavedValue( string versionTag )
		{
			string XML = GetXmlForTesting( versionTag );
			XmlDocument document = LoadAndValidate( XML );
			XmlNode node = document.GetElementsByTagName( versionTag )[0];
			SimpleValue model = new SimpleValue();
			Binder.MakeVersion( node, model );
			Assert.IsNull( model.NullValue,
				"default unsaved-value for tag {0} should be null, but is '{1}'",
				versionTag, model.NullValue );
		}
Beispiel #16
0
        private static void BindUnsavedValue(HbmId idSchema, SimpleValue id)
        {
            if (idSchema.unsavedvalue != null)
                id.NullValue = idSchema.unsavedvalue;

            else if (id.IdentifierGeneratorStrategy == "assigned")
                // TODO: H3 has id.setNullValue("undefined") here, but NH doesn't (yet) allow "undefined"
                // for id unsaved-value, so we use "null" here
                id.NullValue = "null";

            else
                id.NullValue = null;
        }
		private void BindSimpleValue(HbmDiscriminator discriminatorSchema, SimpleValue discriminator)
		{
			if (discriminatorSchema.type != null)
				discriminator.TypeName = discriminatorSchema.type;

			if (discriminatorSchema.formula != null)
			{
				Formula f = new Formula();
				f.FormulaString = discriminatorSchema.formula;
				discriminator.AddFormula(f);
			}
			else
				BindColumns(discriminatorSchema, discriminator);
		}
		private void BindColumns(HbmDiscriminator discriminatorSchema, SimpleValue discriminator)
		{
			Table table = discriminator.Table;

			//COLUMN(S)
			if (discriminatorSchema.column != null)
			{
				Column col = new Column();
				col.Value = discriminator;
				BindColumn(discriminatorSchema, col);
				col.Name = mappings.NamingStrategy.ColumnName(discriminatorSchema.column);

				if (table != null)
					table.AddColumn(col);

				discriminator.AddColumn(col);
			}
			else if (discriminatorSchema.Item != null && discriminatorSchema.Item is HbmColumn)
			{
				HbmColumn theCol = (HbmColumn)discriminatorSchema.Item;
				Column col = new Column();
				col.Value = discriminator;
				BindColumn(theCol, col, false);

				col.Name = mappings.NamingStrategy.ColumnName(theCol.name);

				if (table != null)
					table.AddColumn(col);
				//table=null -> an association, fill it in later

				discriminator.AddColumn(col);

				BindIndex(theCol.index, table, col);
				BindUniqueKey(theCol.uniquekey, table, col);
			}

			if (discriminator.ColumnSpan == 0)
			{
				Column col = new Column();
				col.Value = discriminator;
				BindColumn(discriminatorSchema, col);

				col.Name = mappings.NamingStrategy.PropertyToColumnName(
					RootClass.DefaultDiscriminatorColumnName);

				discriminator.Table.AddColumn(col);
				discriminator.AddColumn(col);
			}
		}
 public void BindId(IdMapping idMapping, PersistentClass clazz, Table table)
 {
     SimpleValue id = new SimpleValue(table);
     id.TypeName = idMapping.PropertyInfo.PropertyType.FullName;
     var property = CreateProperty(clazz, id, idMapping.PropertyInfo);
     clazz.IdentifierProperty = property;
     id.Table = clazz.Table;
     clazz.Identifier = id;
     foreach (var column in idMapping.Columns)
     {
         BindColumn(id, column);
     }
     BindGenerator(idMapping, id);
     id.Table.SetIdentifierValue(id);
 }
		public void BindDiscriminator(HbmDiscriminator discriminatorSchema, Table table)
		{
			if (discriminatorSchema == null)
				return;

			//DISCRIMINATOR
			var discriminator = new SimpleValue(table);
			rootClass.Discriminator = discriminator;
			BindSimpleValue(discriminatorSchema, discriminator);

			if (discriminator.Type == null)
			{
				discriminator.TypeName = NHibernateUtil.String.Name;
			}

			rootClass.IsPolymorphic = true;
			rootClass.IsForceDiscriminator = discriminatorSchema.force;
			rootClass.IsDiscriminatorInsertable = discriminatorSchema.insert;
		}
		private void BindTimestamp(HbmTimestamp timestampSchema, PersistentClass rootClass, Table table, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			if (timestampSchema == null)
				return;

			string propertyName = timestampSchema.name;
			SimpleValue simpleValue = new SimpleValue(table);

			BindColumns(timestampSchema, simpleValue, propertyName);

			if (!simpleValue.IsTypeSpecified)
			{
				switch (timestampSchema.source)
				{
					case HbmTimestampSource.Vm:
						simpleValue.TypeName = NHibernateUtil.Timestamp.Name; 
						break;
					case HbmTimestampSource.Db:
						simpleValue.TypeName = NHibernateUtil.DbTimestamp.Name; 
						break;
					default:
						simpleValue.TypeName = NHibernateUtil.Timestamp.Name;
						break;
				}
			}

			var property = new Property(simpleValue);
			BindProperty(timestampSchema, property, inheritedMetas);

			// for version properties marked as being generated, make sure they are "always"
			// generated; "insert" is invalid. This is dis-allowed by the schema, but just to make
			// sure...

			if (property.Generation == PropertyGeneration.Insert)
				throw new MappingException("'generated' attribute cannot be 'insert' for versioning property");
			simpleValue.NullValue = timestampSchema.unsavedvalue == HbmTimestampUnsavedvalue.Null ? null : "undefined";
			rootClass.Version = property;
			rootClass.AddProperty(property);
		}
		public void BindDiscriminator(HbmDiscriminator discriminatorSchema, PersistentClass rootClass,
			Table table)
		{
			if (discriminatorSchema == null)
				return;

			//DISCRIMINATOR
			SimpleValue discriminator = new SimpleValue(table);
			rootClass.Discriminator = discriminator;
			BindSimpleValue(discriminatorSchema, discriminator);

			if (discriminator.Type == null)
			{
				discriminator.TypeName = NHibernateUtil.String.Name;
			}

			rootClass.IsPolymorphic = true;

			if (discriminatorSchema.force)
				rootClass.IsForceDiscriminator = true;

			if (discriminatorSchema.insertSpecified && !discriminatorSchema.insert)
				rootClass.IsDiscriminatorInsertable = false;
		}
Beispiel #23
0
 /// <summary>
 /// Sets the Identifier of the Table.
 /// </summary>
 /// <param name="identifierValue">The <see cref="SimpleValue"/> that represents the Identifier.</param>
 public void SetIdentifierValue(SimpleValue identifierValue)
 {
     idValue = identifierValue;
 }
Beispiel #24
0
        private void AddColumn(SimpleValue id, string propName)
        {
            Column column = CreateColumn();
            column.Value = id;
            column.Name = mapper.Mappings.NamingStrategy.ColumnName(propName);

            if (id.Table != null) {
                id.Table.AddColumn(column);
            }

            id.AddColumn(column);
        }
Beispiel #25
0
 private static void BindGenerator(SimpleValue identifier)
 {
     identifier.IdentifierGeneratorStrategy = DefaultValues.PoIDGenerator;
 }
Beispiel #26
0
 public PropertyInfo Bind(PersistentClass pclass, Table table)
 {
     var identifier = new SimpleValue(table);
     pclass.Identifier = identifier;
     PropertyInfo result = GetIdProperty();
     string propName = result.Name;
     AddColumn(identifier, propName);
     CreateIdentifierProperty(pclass, identifier, propName);
     BindGenerator(identifier);
     identifier.Table.SetIdentifierValue(identifier);
     return result;
 }
Beispiel #27
0
        private void BindProperties(PersistentClass pclass, IEnumerable<PropertyInfo> infos)
        {
            var table = pclass.Table;

            foreach (var propertyInfo in infos) {
                if (typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType) && propertyInfo.PropertyType != typeof(string))
                    continue;
                // Bind a simple value property
                IValue value = new SimpleValue(table);
                BindColumn((SimpleValue) value, true, propertyInfo.Name);

                Property property = CreateProperty(value, propertyInfo, pclass.ClassName);
                //	property.IsUpdateable = false;
                pclass.AddProperty(property);
            }
        }
Beispiel #28
0
        private void BindColumn(SimpleValue value, bool nullable, string propName)
        {
            var col = new Column {Value = value, IsNullable = nullable, Name = mappings.NamingStrategy.ColumnName(propName)};
            value.Table.AddColumn(col);
            value.AddColumn(col);

            value.AddColumn(col);
        }
		public ReferenceDependantValue(Table table, SimpleValue prototype)
			: base(table, prototype)
		{
			_prototype = prototype;
		}
Beispiel #30
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="table"></param>
 /// <param name="identifier"></param>
 public OneToOne(Table table, SimpleValue identifier) : base(table)
 {
     this.identifier = identifier;
 }
Beispiel #31
0
		private void BindVersion(HbmVersion versionSchema, PersistentClass rootClass, Table table, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			if (versionSchema == null)
				return;

			string propertyName = versionSchema.name;
			var simpleValue = new SimpleValue(table);
			new TypeBinder(simpleValue, Mappings).Bind(versionSchema.type);
			new ColumnsBinder(simpleValue, Mappings).Bind(versionSchema.Columns, false,
			                                              () =>
			                                              new HbmColumn
			                                              	{name = mappings.NamingStrategy.PropertyToColumnName(propertyName)});

			if (!simpleValue.IsTypeSpecified)
				simpleValue.TypeName = NHibernateUtil.Int32.Name;

			var property = new Property(simpleValue);
			BindProperty(versionSchema, property, inheritedMetas);

			// for version properties marked as being generated, make sure they are "always"
			// generated; "insert" is invalid. This is dis-allowed by the schema, but just to make
			// sure...

			if (property.Generation == PropertyGeneration.Insert)
				throw new MappingException("'generated' attribute cannot be 'insert' for versioning property");

			simpleValue.NullValue = versionSchema.unsavedvalue;
			rootClass.Version = property;
			rootClass.AddProperty(property);
		}
		public FkSecondPass(ToOne value, Ejb3JoinColumn[] columns)
		{
			this.value = value;
			this.columns = columns;
			uniqueCounter = globalCounter.GetAndIncrement();
		}
 public ReferenceDependantValue(Table table, SimpleValue prototype)
     : base(table, prototype)
 {
     _prototype = prototype;
 }