Example #1
0
        private void BindListSecondPass(HbmList listMapping, List model,
                                        IDictionary <string, PersistentClass> persistentClasses, IDictionary <string, MetaAttribute> inheritedMetas)
        {
            BindCollectionSecondPass(listMapping, model, persistentClasses, inheritedMetas);

            // Index
            BindCollectionIndex(listMapping, model);
            if (listMapping.ListIndex != null && !string.IsNullOrEmpty(listMapping.ListIndex.@base))
            {
                model.BaseIndex = Convert.ToInt32(listMapping.ListIndex.@base);
            }

            if (model.IsOneToMany && !model.Key.IsNullable && !model.IsInverse)
            {
                string          entityName = ((OneToMany)model.Element).ReferencedEntityName;
                PersistentClass referenced = mappings.GetClass(entityName);
                var             ib         = new IndexBackref();
                ib.Name           = '_' + model.OwnerEntityName + "." + listMapping.Name + "IndexBackref";
                ib.IsUpdateable   = false;
                ib.IsSelectable   = false;
                ib.CollectionRole = model.Role;
                ib.EntityName     = model.Owner.EntityName;
                ib.Value          = model.Index;
                referenced.AddProperty(ib);
            }
        }
Example #2
0
        private void BindVersion(HbmVersion versionSchema, PersistentClass rootClass, Table table, IDictionary <string, MetaAttribute> inheritedMetas)
        {
            if (versionSchema == null)
            {
                return;
            }

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

            BindVersionType(versionSchema.type, simpleValue);
            BindColumns(versionSchema, simpleValue, false, 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);
        }
Example #3
0
        private static void AddColumn(PersistentClass mapping, string columnName, Type accessorType)
        {
            var  typeName   = DICT_TYPE_TO_NAME[accessorType];
            bool isNullable = typeName[typeName.Length - 1] == '?';

            if (isNullable)
            {
                typeName = typeName.Substring(0, typeName.Length - 1);
            }

            // String annotations can be null also
            isNullable = isNullable || Equals(STRING_TYPE_NAME, typeName);

            var column = new Column(columnName)
            {
                IsNullable = isNullable
            };

            mapping.Table.AddColumn(column);
            var value = new SimpleValue(mapping.Table)
            {
                TypeName = typeName
            };

            value.AddColumn(column);
            var property = new Property(value)
            {
                Name = columnName,
                PropertyAccessorName = accessorType.AssemblyQualifiedName
            };

            mapping.AddProperty(property);
        }
Example #4
0
 public void AddProperty(Property prop)
 {
     if (prop.Value is Component)
     {
         //TODO handle quote and non quote table comparison
         String tableName = prop.Value.Table.Name;
         if (GetJoinsPerRealTableName().ContainsKey(tableName))
         {
             GetJoinsPerRealTableName()[tableName].AddProperty(prop); //TODO review the Get from dictionary.
         }
         else
         {
             persistentClass.AddProperty(prop);
         }
     }
     else
     {
         persistentClass.AddProperty(prop);
     }
 }
Example #5
0
        private void BindTimestamp(HbmTimestamp timestampSchema, PersistentClass rootClass, Table table, IDictionary <string, MetaAttribute> inheritedMetas)
        {
            if (timestampSchema == null)
            {
                return;
            }

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

            new ColumnsBinder(simpleValue, Mappings).Bind(timestampSchema.Columns, false,
                                                          () =>
                                                          new HbmColumn
            {
                name = mappings.NamingStrategy.PropertyToColumnName(propertyName)
            });

            if (!simpleValue.IsTypeSpecified)
            {
                switch (timestampSchema.source)
                {
                case HbmTimestampSource.Vm:
                    simpleValue.TypeName = NHibernateUtil.DateTime.Name;
                    break;

                case HbmTimestampSource.Db:
                    simpleValue.TypeName = NHibernateUtil.DbTimestamp.Name;
                    break;

                default:
                    simpleValue.TypeName = NHibernateUtil.DateTime.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);
        }
Example #6
0
        private void BindProperties(PersistentClass pclass, IEnumerable <PropertyInfo> infos)
        {
            Table table = pclass.Table;

            foreach (PropertyInfo propertyInfo in infos)
            {
                IValue value;

                // Bind a simple value property
                value = new SimpleValue(table);
                BindColumn((SimpleValue)value, true, propertyInfo.Name);

                Property property = CreateProperty(value, propertyInfo, pclass.ClassName);
                //	property.IsUpdateable = false;
                pclass.AddProperty(property);
            }
        }
        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);
            }
        }
Example #8
0
        /// <remarks>
        /// Called for all collections
        /// </remarks>
        private void BindCollectionSecondPass(ICollectionPropertiesMapping collectionMapping, Mapping.Collection model,
                                              IDictionary <string, PersistentClass> persistentClasses, IDictionary <string, MetaAttribute> inheritedMetas)
        {
            if (model.IsOneToMany)
            {
                var             oneToMany            = (OneToMany)model.Element;
                string          associatedEntityName = oneToMany.ReferencedEntityName;
                PersistentClass persistentClass;
                if (persistentClasses.TryGetValue(associatedEntityName, out persistentClass) == false)
                {
                    throw new MappingException("Association references unmapped class: " + associatedEntityName);
                }
                oneToMany.AssociatedClass = persistentClass;
                model.CollectionTable     = persistentClass.Table;
                if (model.IsInverse && persistentClass.JoinClosureSpan > 0)
                {
                    // NH: bidirectional one-to-many with a class splitted in more tables; have to find in which table is the inverse side
                    foreach (var joined in persistentClass.JoinClosureIterator)
                    {
                        if (collectionMapping.Key.Columns.Select(x => x.name).All(x => joined.Table.ColumnIterator.Select(jc => jc.Name).Contains(x)))
                        {
                            model.CollectionTable = joined.Table;
                            break;
                        }
                    }
                }

                if (log.IsInfoEnabled)
                {
                    log.Info("mapping collection: " + model.Role + " -> " + model.CollectionTable.Name);
                }
            }

            //CHECK
            if (!string.IsNullOrEmpty(collectionMapping.Check))
            {
                model.CollectionTable.AddCheckConstraint(collectionMapping.Check);
            }
            BindKey(collectionMapping.Key, model);

            //contained elements:
            HbmCompositeElement compositeElementMapping;
            HbmElement          elementMapping;
            HbmManyToAny        manyToAnyMapping;
            HbmManyToMany       manyToManyMapping;

            if ((elementMapping = collectionMapping.ElementRelationship as HbmElement) != null)
            {
                BindElement(elementMapping, model);
            }
            else if ((manyToManyMapping = collectionMapping.ElementRelationship as HbmManyToMany) != null)
            {
                BindManyToMany(manyToManyMapping, model);
            }
            else if ((compositeElementMapping = collectionMapping.ElementRelationship as HbmCompositeElement) != null)
            {
                BindCompositeElement(compositeElementMapping, model, inheritedMetas);
            }
            else if ((manyToAnyMapping = collectionMapping.ElementRelationship as HbmManyToAny) != null)
            {
                BindManyToAny(manyToAnyMapping, model);
            }

            BindCache(collectionMapping.Cache, model);

            if (model.IsOneToMany && !model.IsInverse && !model.Key.IsNullable)
            {
                // for non-inverse one-to-many, with a not-null fk, add a backref!
                string          entityName = ((OneToMany)model.Element).ReferencedEntityName;
                PersistentClass referenced = mappings.GetClass(entityName);
                Backref         prop       = new Backref();
                prop.Name           = '_' + model.OwnerEntityName + "." + collectionMapping.Name + "Backref";
                prop.IsUpdateable   = false;
                prop.IsSelectable   = false;
                prop.CollectionRole = model.Role;
                prop.EntityName     = model.Owner.EntityName;
                prop.Value          = model.Key;
                referenced.AddProperty(prop);
            }
        }
Example #9
0
        /// <summary>
        /// Called for Maps
        /// </summary>
        private void BindMapSecondPass(HbmMap mapMapping, Map model,
                                       IDictionary <string, PersistentClass> persistentClasses, IDictionary <string, MetaAttribute> inheritedMetas)
        {
            BindCollectionSecondPass(mapMapping, model, persistentClasses, inheritedMetas);

            HbmIndex  indexMapping;
            HbmMapKey mapKeyMapping;

            HbmIndexManyToMany  indexManyToManyMapping;
            HbmMapKeyManyToMany mapKeyManyToManyMapping;

            HbmCompositeIndex  compositeIndexMapping;
            HbmCompositeMapKey compositeMapKeyMapping;

            HbmIndexManyToAny indexManyToAnyMapping;

            if ((indexMapping = mapMapping.Item as HbmIndex) != null)
            {
                var value = new SimpleValue(model.CollectionTable);
                new ValuePropertyBinder(value, Mappings).BindSimpleValue(indexMapping, IndexedCollection.DefaultIndexColumnName,
                                                                         model.IsOneToMany);
                model.Index = value;
                if (string.IsNullOrEmpty(model.Index.TypeName))
                {
                    throw new MappingException("map index element must specify a type: " + model.Role);
                }
            }
            else if ((mapKeyMapping = mapMapping.Item as HbmMapKey) != null)
            {
                var value = new SimpleValue(model.CollectionTable);
                new ValuePropertyBinder(value, Mappings).BindSimpleValue(mapKeyMapping, IndexedCollection.DefaultIndexColumnName,
                                                                         model.IsOneToMany);
                model.Index = value;
                if (string.IsNullOrEmpty(model.Index.TypeName))
                {
                    throw new MappingException("map index element must specify a type: " + model.Role);
                }
            }
            else if ((indexManyToManyMapping = mapMapping.Item as HbmIndexManyToMany) != null)
            {
                var manyToOne = new ManyToOne(model.CollectionTable);
                BindIndexManyToMany(indexManyToManyMapping, manyToOne, IndexedCollection.DefaultIndexColumnName, model.IsOneToMany);
                model.Index = manyToOne;
            }
            else if ((mapKeyManyToManyMapping = mapMapping.Item as HbmMapKeyManyToMany) != null)
            {
                var manyToOne = new ManyToOne(model.CollectionTable);
                BindMapKeyManyToMany(mapKeyManyToManyMapping, manyToOne, IndexedCollection.DefaultIndexColumnName, model.IsOneToMany);
                model.Index = manyToOne;
            }
            else if ((compositeIndexMapping = mapMapping.Item as HbmCompositeIndex) != null)
            {
                var component = new Component(model);
                BindComponent(compositeIndexMapping, component, null, null, model.Role + ".index", model.IsOneToMany, inheritedMetas);
                model.Index = component;
            }
            else if ((compositeMapKeyMapping = mapMapping.Item as HbmCompositeMapKey) != null)
            {
                var component = new Component(model);
                BindComponent(compositeMapKeyMapping, component, null, null, model.Role + ".index", model.IsOneToMany, inheritedMetas);
                model.Index = component;
            }
            else if ((indexManyToAnyMapping = mapMapping.Item as HbmIndexManyToAny) != null)
            {
                var any = new Any(model.CollectionTable);
                BindIndexManyToAny(indexManyToAnyMapping, any, model.IsOneToMany);
                model.Index = any;
            }

            bool indexIsFormula = model.Index.ColumnIterator.Any(x => x.IsFormula);

            if (model.IsOneToMany && !model.Key.IsNullable && !model.IsInverse && !indexIsFormula)
            {
                string          entityName = ((OneToMany)model.Element).ReferencedEntityName;
                PersistentClass referenced = mappings.GetClass(entityName);
                var             ib         = new IndexBackref();
                ib.Name           = '_' + model.OwnerEntityName + "." + mapMapping.Name + "IndexBackref";
                ib.IsUpdateable   = false;
                ib.IsSelectable   = false;
                ib.CollectionRole = model.Role;
                ib.EntityName     = model.Owner.EntityName;
                ib.Value          = model.Index;
                referenced.AddProperty(ib);
            }
        }
Example #10
0
 public void Bind(IEnumerable <IEntityPropertyMapping> properties, IDictionary <string, MetaAttribute> inheritedMetas, Action <Property> modifier)
 {
     Bind(properties, persistentClass.Table, inheritedMetas, modifier, p => persistentClass.AddProperty(p));
 }