public void Bind(IEnumerable<IEntityPropertyMapping> properties, Table table, IDictionary<string, MetaAttribute> inheritedMetas, Action<Property> modifier, Action<Property> addToModelAction) { if (table == null) { throw new ArgumentNullException("table"); } if (modifier == null) { throw new ArgumentNullException("modifier"); } if (addToModelAction == null) { throw new ArgumentNullException("addToModelAction"); } foreach (var entityPropertyMapping in properties) { Property property = null; string propertyName = entityPropertyMapping.Name; ICollectionPropertiesMapping collectionMapping; HbmManyToOne manyToOneMapping; HbmAny anyMapping; HbmOneToOne oneToOneMapping; HbmProperty propertyMapping; HbmComponent componentMapping; HbmDynamicComponent dynamicComponentMapping; HbmNestedCompositeElement nestedCompositeElementMapping; HbmKeyProperty keyPropertyMapping; HbmKeyManyToOne keyManyToOneMapping; if ((propertyMapping = entityPropertyMapping as HbmProperty) != null) { var value = new SimpleValue(table); new ValuePropertyBinder(value, Mappings).BindSimpleValue(propertyMapping, propertyName, true); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); BindValueProperty(propertyMapping, property); } else if ((collectionMapping = entityPropertyMapping as ICollectionPropertiesMapping) != null) { var collectionBinder = new CollectionBinder(Mappings, dialect); string propertyPath = propertyName == null ? null : StringHelper.Qualify(propertyBasePath, propertyName); Mapping.Collection collection = collectionBinder.Create(collectionMapping, entityName, propertyPath, persistentClass, mappedClass, inheritedMetas); mappings.AddCollection(collection); property = CreateProperty(collectionMapping, className, collection, inheritedMetas); BindCollectionProperty(collectionMapping, property); } else if ((manyToOneMapping = entityPropertyMapping as HbmManyToOne) != null) { var value = new ManyToOne(table); BindManyToOne(manyToOneMapping, value, propertyName, true); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); property.UnwrapProxy = manyToOneMapping.Lazy == HbmLaziness.NoProxy; BindManyToOneProperty(manyToOneMapping, property); } else if ((componentMapping = entityPropertyMapping as HbmComponent) != null) { string subpath = propertyName == null ? null : StringHelper.Qualify(propertyBasePath, propertyName); var value = CreateNewComponent(); // NH: Modified from H2.1 to allow specifying the type explicitly using class attribute System.Type reflectedClass = mappedClass == null ? null : GetPropertyType(componentMapping.Class, mappedClass, propertyName, componentMapping.Access); BindComponent(componentMapping, value, reflectedClass, entityName, subpath, componetDefaultNullable, inheritedMetas); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); BindComponentProperty(componentMapping, property); } else if ((oneToOneMapping = entityPropertyMapping as HbmOneToOne) != null) { var value = new OneToOne(table, persistentClass); BindOneToOne(oneToOneMapping, value); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); BindOneToOneProperty(oneToOneMapping, property); } else if ((dynamicComponentMapping = entityPropertyMapping as HbmDynamicComponent) != null) { string subpath = propertyName == null ? null : StringHelper.Qualify(propertyBasePath, propertyName); var value = CreateNewComponent(); // NH: Modified from H2.1 to allow specifying the type explicitly using class attribute System.Type reflectedClass = mappedClass == null ? null : GetPropertyType(dynamicComponentMapping.Class, mappedClass, propertyName, dynamicComponentMapping.Access); BindComponent(dynamicComponentMapping, value, reflectedClass, entityName, subpath, componetDefaultNullable, inheritedMetas); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); BindComponentProperty(dynamicComponentMapping, property); } else if ((anyMapping = entityPropertyMapping as HbmAny) != null) { var value = new Any(table); BindAny(anyMapping, value, true); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); BindAnyProperty(anyMapping, property); } else if ((nestedCompositeElementMapping = entityPropertyMapping as HbmNestedCompositeElement) != null) { if (component == null) { throw new AssertionFailure("Nested Composite Element without a owner component."); } string subpath = propertyName == null ? null : StringHelper.Qualify(propertyBasePath, propertyName); var value = CreateNewComponent(); // NH: Modified from H2.1 to allow specifying the type explicitly using class attribute System.Type reflectedClass = mappedClass == null ? null : GetPropertyType(nestedCompositeElementMapping.Class, mappedClass, propertyName, nestedCompositeElementMapping.access); BindComponent(nestedCompositeElementMapping, value, reflectedClass, entityName, subpath, componetDefaultNullable, inheritedMetas); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); } else if ((keyPropertyMapping = entityPropertyMapping as HbmKeyProperty) != null) { var value = new SimpleValue(table); new ValuePropertyBinder(value, Mappings).BindSimpleValue(keyPropertyMapping, propertyName, componetDefaultNullable); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); } else if ((keyManyToOneMapping = entityPropertyMapping as HbmKeyManyToOne) != null) { var value = new ManyToOne(table); BindKeyManyToOne(keyManyToOneMapping, value, propertyName, componetDefaultNullable); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); } if (property != null) { modifier(property); property.LogMapped(log); addToModelAction(property); } } }
protected void PropertiesFromXML(XmlNode node, PersistentClass model) { 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, model.EntityName, propertyName, model, model.MappedClass); 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)) { // 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, model.EntityName, propertyName, true); } else if ("join".Equals(name)) { Join join = new Join(); join.PersistentClass = model; BindJoin(subnode, join); model.AddJoin(join); } else if ("subclass".Equals(name)) new SubclassBinder(this).HandleSubclass(model, subnode); else if ("joined-subclass".Equals(name)) new JoinedSubclassBinder(this).HandleJoinedSubclass(model, subnode); else if ("union-subclass".Equals(name)) new UnionSubclassBinder(this).HandleUnionSubclass(model, subnode); else if ("filter".Equals(name)) ParseFilter(subnode, model); if (value != null) model.AddProperty(CreateProperty(value, propertyName, model.MappedClass, subnode)); } }
protected void BindComponent(XmlNode node, Component model, System.Type reflectedClass, string className, string path, bool isNullable) { XmlAttribute classNode = node.Attributes["class"]; if ("dynamic-component".Equals(node.Name)) { model.IsEmbedded = false; model.IsDynamic = true; } else if (classNode != null) { model.ComponentClass = ClassForNameChecked( classNode.Value, mappings, "component class not found: {0}"); model.ComponentClassName = FullClassName(classNode.Value, mappings); model.IsEmbedded = false; } else if (reflectedClass != null) { model.ComponentClass = reflectedClass; model.IsEmbedded = false; } else { // an "embedded" component (ids only) model.ComponentClass = model.Owner.MappedClass; model.IsEmbedded = true; } 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); string subpath = propertyName == null ? null : StringHelper.Qualify(path, propertyName); IValue value = null; CollectionBinder binder = new CollectionBinder(this); if (binder.CanCreate(name)) { Mapping.Collection collection = binder.Create(name, subnode, className, subpath, model.Owner, model.ComponentClass); mappings.AddCollection(collection); value = collection; } else if ("many-to-one".Equals(name) || "key-many-to-one".Equals(name)) { value = new ManyToOne(model.Table); BindManyToOne(subnode, (ManyToOne) value, subpath, isNullable); } else if ("one-to-one".Equals(name)) { value = new OneToOne(model.Table, model.Owner); BindOneToOne(subnode, (OneToOne) value); } else if ("any".Equals(name)) { value = new Any(model.Table); BindAny(subnode, (Any) value, isNullable); } else if ("property".Equals(name) || "key-property".Equals(name)) { value = new SimpleValue(model.Table); BindSimpleValue(subnode, (SimpleValue) value, isNullable, subpath); } else if ("component".Equals(name) || "dynamic-component".Equals(name) || "nested-composite-element".Equals(name)) { System.Type subreflectedClass = model.ComponentClass == null ? null : GetPropertyType(subnode, model.ComponentClass, propertyName); value = new Component(model); BindComponent(subnode, (Component) value, subreflectedClass, className, subpath, isNullable); } else if ("parent".Equals(name)) model.ParentProperty = propertyName; if (value != null) model.AddProperty(CreateProperty(value, propertyName, model.ComponentClass, subnode)); } }
public void Bind(IEnumerable <IEntityPropertyMapping> properties, Table table, IDictionary <string, MetaAttribute> inheritedMetas, Action <Property> modifier, Action <Property> addToModelAction) { if (table == null) { throw new ArgumentNullException("table"); } if (modifier == null) { throw new ArgumentNullException("modifier"); } if (addToModelAction == null) { throw new ArgumentNullException("addToModelAction"); } foreach (var entityPropertyMapping in properties) { Property property = null; string propertyName = entityPropertyMapping.Name; ICollectionPropertiesMapping collectionMapping; HbmManyToOne manyToOneMapping; HbmAny anyMapping; HbmOneToOne oneToOneMapping; HbmProperty propertyMapping; HbmComponent componentMapping; HbmDynamicComponent dynamicComponentMapping; HbmNestedCompositeElement nestedCompositeElementMapping; HbmKeyProperty keyPropertyMapping; HbmKeyManyToOne keyManyToOneMapping; if ((propertyMapping = entityPropertyMapping as HbmProperty) != null) { var value = new SimpleValue(table); new ValuePropertyBinder(value, Mappings).BindSimpleValue(propertyMapping, propertyName, true); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); BindValueProperty(propertyMapping, property); } else if ((collectionMapping = entityPropertyMapping as ICollectionPropertiesMapping) != null) { var collectionBinder = new CollectionBinder(Mappings, dialect); string propertyPath = propertyName == null ? null : StringHelper.Qualify(propertyBasePath, propertyName); Mapping.Collection collection = collectionBinder.Create(collectionMapping, entityName, propertyPath, persistentClass, mappedClass, inheritedMetas); mappings.AddCollection(collection); property = CreateProperty(collectionMapping, className, collection, inheritedMetas); BindCollectionProperty(collectionMapping, property); } else if ((manyToOneMapping = entityPropertyMapping as HbmManyToOne) != null) { var value = new ManyToOne(table); BindManyToOne(manyToOneMapping, value, propertyName, true); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); BindManyToOneProperty(manyToOneMapping, property); } else if ((componentMapping = entityPropertyMapping as HbmComponent) != null) { string subpath = propertyName == null ? null : StringHelper.Qualify(propertyBasePath, propertyName); var value = CreateNewComponent(table); // NH: Modified from H2.1 to allow specifying the type explicitly using class attribute System.Type reflectedClass = mappedClass == null ? null : GetPropertyType(componentMapping.Class, mappedClass, propertyName, componentMapping.Access); BindComponent(componentMapping, value, reflectedClass, entityName, subpath, componetDefaultNullable, inheritedMetas); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); BindComponentProperty(componentMapping, property, value); } else if ((oneToOneMapping = entityPropertyMapping as HbmOneToOne) != null) { var value = new OneToOne(table, persistentClass); BindOneToOne(oneToOneMapping, value); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); BindOneToOneProperty(oneToOneMapping, property); } else if ((dynamicComponentMapping = entityPropertyMapping as HbmDynamicComponent) != null) { string subpath = propertyName == null ? null : StringHelper.Qualify(propertyBasePath, propertyName); var value = CreateNewComponent(table); // NH: Modified from H2.1 to allow specifying the type explicitly using class attribute System.Type reflectedClass = mappedClass == null ? null : GetPropertyType(dynamicComponentMapping.Class, mappedClass, propertyName, dynamicComponentMapping.Access); BindComponent(dynamicComponentMapping, value, reflectedClass, entityName, subpath, componetDefaultNullable, inheritedMetas); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); BindComponentProperty(dynamicComponentMapping, property, value); } else if ((anyMapping = entityPropertyMapping as HbmAny) != null) { var value = new Any(table); BindAny(anyMapping, value, true); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); BindAnyProperty(anyMapping, property); } else if ((nestedCompositeElementMapping = entityPropertyMapping as HbmNestedCompositeElement) != null) { if (component == null) { throw new AssertionFailure("Nested Composite Element without a owner component."); } string subpath = propertyName == null ? null : StringHelper.Qualify(propertyBasePath, propertyName); var value = CreateNewComponent(table); // NH: Modified from H2.1 to allow specifying the type explicitly using class attribute System.Type reflectedClass = mappedClass == null ? null : GetPropertyType(nestedCompositeElementMapping.Class, mappedClass, propertyName, nestedCompositeElementMapping.access); BindComponent(nestedCompositeElementMapping, value, reflectedClass, entityName, subpath, componetDefaultNullable, inheritedMetas); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); } else if ((keyPropertyMapping = entityPropertyMapping as HbmKeyProperty) != null) { var value = new SimpleValue(table); new ValuePropertyBinder(value, Mappings).BindSimpleValue(keyPropertyMapping, propertyName, componetDefaultNullable); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); } else if ((keyManyToOneMapping = entityPropertyMapping as HbmKeyManyToOne) != null) { var value = new ManyToOne(table); BindKeyManyToOne(keyManyToOneMapping, value, propertyName, componetDefaultNullable); property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas); } if (property != null) { modifier(property); property.LogMapped(log); addToModelAction(property); } } }
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)); } } }
private void BindJoin(XmlNode node, Join join, IDictionary<string, MetaAttribute> inheritedMetas) { PersistentClass persistentClass = join.PersistentClass; String path = persistentClass.EntityName; // TABLENAME XmlAttribute schemaNode = node.Attributes["schema"]; string schema = schemaNode == null ? mappings.SchemaName : schemaNode.Value; XmlAttribute catalogNode = node.Attributes["catalog"]; string catalog = catalogNode == null ? mappings.CatalogName : catalogNode.Value; XmlAttribute actionNode = node.Attributes["schema-action"]; string action = actionNode == null ? "all" : actionNode.Value; Table table = mappings.AddTable(schema, catalog, GetClassTableName(persistentClass, node), null, false, action); join.Table = table; XmlAttribute fetchNode = node.Attributes["fetch"]; if (fetchNode != null) join.IsSequentialSelect = "select".Equals(fetchNode.Value); XmlAttribute invNode = node.Attributes["inverse"]; if (invNode != null) join.IsInverse = "true".Equals(invNode.Value); XmlAttribute nullNode = node.Attributes["optional"]; if (nullNode != null) join.IsOptional = "true".Equals(nullNode.Value); log.InfoFormat("Mapping class join: {0} -> {1}", persistentClass.EntityName, join.Table.Name); // KEY XmlNode keyNode = node.SelectSingleNode(HbmConstants.nsKey, namespaceManager); SimpleValue key = new DependantValue(table, persistentClass.Identifier); join.Key = key; if (keyNode.Attributes["on-delete"] != null) key.IsCascadeDeleteEnabled = "cascade".Equals(keyNode.Attributes["on-delete"].Value); BindSimpleValue(keyNode, key, false, persistentClass.EntityName); join.CreatePrimaryKey(dialect); join.CreateForeignKey(); // PROPERTIES //PropertiesFromXML(node, persistentClass, mappings); 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.Name; XmlAttribute nameAttribute = subnode.Attributes["name"]; string propertyName = nameAttribute == null ? null : nameAttribute.Value; IValue value = null; var collectionBinder = new CollectionBinder(this); if (collectionBinder.CanCreate(name)) { Mapping.Collection collection = collectionBinder.Create(name, subnode, persistentClass.EntityName, propertyName, persistentClass, persistentClass.MappedClass, inheritedMetas); mappings.AddCollection(collection); value = collection; } else { switch (name) { case "many-to-one": value = new ManyToOne(table); BindManyToOne(subnode, (ManyToOne) value, propertyName, true); break; case "any": value = new Any(table); BindAny(subnode, (Any) value, true); break; case "property": value = new SimpleValue(table); BindSimpleValue(subnode, (SimpleValue) value, true, propertyName); break; case "component": case "dynamic-component": string subpath = StringHelper.Qualify(path, propertyName); value = new Component(join); BindComponent(subnode, (Component) value, join.PersistentClass.MappedClass, join.PersistentClass.ClassName, propertyName, subpath, true, inheritedMetas); break; } } if (value != null) { var prop = CreateProperty(value, propertyName, persistentClass.MappedClass.AssemblyQualifiedName, subnode, inheritedMetas); prop.IsOptional = join.IsOptional; join.AddProperty(prop); } } // CUSTOM SQL HandleCustomSQL(node, join); }