Ejemplo n.º 1
0
        private Property CreateProperty(ToOne value, string propertyName, System.Type parentClass,
                                        HbmKeyManyToOne keyManyToOneSchema)
        {
            if (parentClass != null && value.IsSimpleValue)
            {
                value.SetTypeUsingReflection(parentClass.AssemblyQualifiedName, propertyName,
                                             keyManyToOneSchema.access ?? mappings.DefaultAccess);
            }

            string propertyRef = value.ReferencedPropertyName;

            if (propertyRef != null)
            {
                mappings.AddUniquePropertyReference(value.ReferencedEntityName, propertyRef);
            }

            value.CreateForeignKey();
            var prop = new Property {
                Value = value
            };

            BindProperty(keyManyToOneSchema, prop);

            return(prop);
        }
        private void BindManyToOne(HbmKeyManyToOne keyManyToOneSchema, ManyToOne manyToOne, string defaultColumnName,
                                   bool isNullable)
        {
            BindColumns(keyManyToOneSchema, manyToOne, isNullable, true, defaultColumnName);

            manyToOne.FetchMode = FetchMode.Default;
            manyToOne.IsLazy    = !keyManyToOneSchema.lazySpecified
                                ? manyToOne.IsLazy
                                : keyManyToOneSchema.lazy == HbmRestrictedLaziness.Proxy;

            string typeNode = keyManyToOneSchema.@class;

            if (typeNode != null)
            {
                manyToOne.ReferencedEntityName = GetClassName(typeNode, mappings);
            }
            else
            {
                manyToOne.ReferencedEntityName = null;
            }

            manyToOne.IsIgnoreNotFound = false;

            if (keyManyToOneSchema.foreignkey != null)
            {
                manyToOne.ForeignKeyName = keyManyToOneSchema.foreignkey;
            }
        }
Ejemplo n.º 3
0
        private void BindManyToOne(HbmKeyManyToOne keyManyToOneSchema, ManyToOne manyToOne, string defaultColumnName,
                                   bool isNullable)
        {
            new ColumnsBinder(manyToOne, mappings).Bind(keyManyToOneSchema.Columns, isNullable,
                                                        () => new HbmColumn {
                name = mappings.NamingStrategy.PropertyToColumnName(defaultColumnName)
            });

            manyToOne.FetchMode = FetchMode.Default;
            manyToOne.IsLazy    = !keyManyToOneSchema.lazySpecified
                                                ? manyToOne.IsLazy
                                                : keyManyToOneSchema.lazy == HbmRestrictedLaziness.Proxy;

            string typeNode = keyManyToOneSchema.@class;

            if (typeNode != null)
            {
                manyToOne.ReferencedEntityName = GetClassName(typeNode, mappings);
            }
            else
            {
                manyToOne.ReferencedEntityName = null;
            }

            manyToOne.IsIgnoreNotFound = false;

            if (keyManyToOneSchema.foreignkey != null)
            {
                manyToOne.ForeignKeyName = keyManyToOneSchema.foreignkey;
            }
        }
Ejemplo n.º 4
0
        public void WhenAssignCascadeStyleThenNotThrows()
        {
            var hbmMapping = new HbmMapping();
            var hbm        = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(null, hbm, hbmMapping);

            mapper.Executing(m => m.Cascade(Cascade.Persist | Cascade.Remove)).NotThrows();
        }
        private void BindColumns(HbmKeyManyToOne keyManyToOneSchema, SimpleValue model, bool isNullable,
                                 bool autoColumn, string propertyPath)
        {
            Table table = model.Table;

            //COLUMN(S)
            if (keyManyToOneSchema.column1 == null)
            {
                int count = 0;

                foreach (HbmColumn columnSchema in keyManyToOneSchema.column ?? new HbmColumn[0])
                {
                    Column col = new Column();
                    col.Value     = model;
                    col.TypeIndex = count++;
                    BindColumn(columnSchema, col, isNullable);

                    string name = columnSchema.name;
                    col.Name = mappings.NamingStrategy.ColumnName(name);
                    if (table != null)
                    {
                        table.AddColumn(col);
                    }
                    //table=null -> an association, fill it in later
                    model.AddColumn(col);

                    //column index
                    BindIndex(columnSchema.index, table, col);
                    //column group index (although it can serve as a separate column index)

                    BindUniqueKey(columnSchema.uniquekey, table, col);
                }
            }
            else
            {
                Column col = new Column();
                col.Value = compositeId;
                BindColumn(col, isNullable);
                col.Name = mappings.NamingStrategy.ColumnName(keyManyToOneSchema.column1);
                if (table != null)
                {
                    table.AddColumn(col);
                }
                model.AddColumn(col);
                //column group index (although can serve as a separate column index)
            }

            if (autoColumn && model.ColumnSpan == 0)
            {
                Column col = new Column();
                col.Value = model;
                BindColumn(col, isNullable);
                col.Name = mappings.NamingStrategy.PropertyToColumnName(propertyPath);
                model.Table.AddColumn(col);
                model.AddColumn(col);
                //column group index (although can serve as a separate column index)
            }
        }
Ejemplo n.º 6
0
 public void BindSimpleValue(HbmKeyManyToOne mapKeyManyToManyMapping, string propertyPath, bool isNullable)
 {
     new ColumnsBinder(value, Mappings).Bind(mapKeyManyToManyMapping.Columns, isNullable,
                                             () =>
                                             new HbmColumn
     {
         name = mappings.NamingStrategy.PropertyToColumnName(propertyPath),
     });
 }
Ejemplo n.º 7
0
        public void WhenForceClassRelationToIncompatibleTypeThenThrows()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("TheOtherRelation");
            var mapping    = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, mapping, hbmMapping);

            mapper.Executing(m => m.Class(typeof(Whatever))).Throws <ArgumentOutOfRangeException>();
        }
Ejemplo n.º 8
0
        public void WhenSetFetchModeToJoinThenNotThrows()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("Relation");
            var mapping    = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, mapping, hbmMapping);

            mapper.Executing(m => m.Fetch(FetchKind.Join)).NotThrows();
        }
        public void ManyToOne(MemberInfo property, Action <IManyToOneMapper> mapping)
        {
            var hbm = new HbmKeyManyToOne {
                name = property.Name
            };

            mapping(new KeyManyToOneMapper(property, hbm, mapDoc));
            AddProperty(hbm);
        }
Ejemplo n.º 10
0
        private void BindKeyManyToOne(HbmKeyManyToOne keyManyToOneMapping, ManyToOne model, string defaultColumnName, bool isNullable)
        {
            new ValuePropertyBinder(model, Mappings).BindSimpleValue(keyManyToOneMapping, defaultColumnName, isNullable);
            InitLaziness(keyManyToOneMapping.Lazy, model, true);

            model.ReferencedEntityName = GetEntityName(keyManyToOneMapping, mappings);
            model.IsIgnoreNotFound     = keyManyToOneMapping.NotFoundMode == HbmNotFoundMode.Ignore;

            BindForeingKey(keyManyToOneMapping.foreignkey, model);
        }
Ejemplo n.º 11
0
        public void CanSetAccessor()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("Relation");
            var hbm        = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, hbm, hbmMapping);

            mapper.Access(Accessor.ReadOnly);
            hbm.Access.Should().Be("readonly");
        }
Ejemplo n.º 12
0
        public void AfterSetMultiColumnsCantSetSimpleColumn()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("Relation");
            var mapping    = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, mapping, hbmMapping);

            mapper.Columns(cm => cm.Length(50), cm => cm.SqlType("VARCHAR(10)"));
            mapper.Executing(m => m.Column(cm => cm.Length(50))).Throws <MappingException>();
        }
Ejemplo n.º 13
0
        public void CanSetFk()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("Relation");
            var hbm        = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, hbm, hbmMapping);

            mapper.ForeignKey("MyFkName");

            hbm.foreignkey.Should().Be("MyFkName");
        }
Ejemplo n.º 14
0
        public void CanForceClassRelation()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("TheOtherRelation");
            var mapping    = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, mapping, hbmMapping);

            mapper.Class(typeof(Relation));

            mapping.Class.Should().Contain("Relation").And.Not.Contain("IRelation");
        }
Ejemplo n.º 15
0
        public void WhenSetMultiColumnsValuesThenAutoassignColumnNames()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("Relation");
            var mapping    = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, mapping, hbmMapping);

            mapper.Columns(cm => cm.Length(50), cm => cm.SqlType("VARCHAR(10)"));
            mapping.Columns.Should().Have.Count.EqualTo(2);
            mapping.Columns.All(cm => cm.name.Satisfy(n => !string.IsNullOrEmpty(n)));
        }
Ejemplo n.º 16
0
        public void WhenSetDefaultColumnNameThenDoesNotSetTheName()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("Relation");
            var mapping    = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, mapping, hbmMapping);

            mapper.Column(cm => cm.Name("Relation"));
            mapping.column.Should().Be.Null();
            mapping.Columns.Should().Be.Empty();
        }
Ejemplo n.º 17
0
        public void WhenSetDifferentColumnNameThenSetTheName()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("Relation");
            var hbm        = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, hbm, hbmMapping);

            mapper.Column(cm => cm.Name("RelationId"));

            hbm.Columns.Should().Have.Count.EqualTo(1);
            hbm.Columns.Single().name.Should().Be("RelationId");
        }
Ejemplo n.º 18
0
        public void WhenSetColumnValuesThenAddColumnTag()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("Relation");
            var mapping    = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, mapping, hbmMapping);

            mapper.Column(cm =>
            {
                cm.UniqueKey("theUnique");
                cm.NotNullable(true);
            });
            mapping.column.Should().Not.Be.Null();
            mapping.Columns.Should().Have.Count.EqualTo(1);
        }
Ejemplo n.º 19
0
        public void WhenSetColumnValuesMoreThanOnesThenMergeColumn()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("Relation");
            var mapping    = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, mapping, hbmMapping);

            mapper.Column(cm => cm.UniqueKey("theUnique"));
            mapper.Column(cm => cm.NotNullable(true));

            var hbmColumn = mapping.Columns.Single();

            hbmColumn.uniquekey.Should().Be("theUnique");
            hbmColumn.notnull.Should().Be(true);
            hbmColumn.notnullSpecified.Should().Be(true);
        }
Ejemplo n.º 20
0
        public void WhenSetMultiColumnsValuesThenAddColumns()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("Relation");
            var mapping    = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, mapping, hbmMapping);

            mapper.Columns(cm =>
            {
                cm.Name("column1");
                cm.Length(50);
            }, cm =>
            {
                cm.Name("column2");
                cm.SqlType("VARCHAR(10)");
            });
            mapping.Columns.Should().Have.Count.EqualTo(2);
        }
 public KeyManyToOneMapper(MemberInfo member, HbmKeyManyToOne manyToOne, HbmMapping mapDoc)
 {
     this.member    = member;
     this.manyToOne = manyToOne;
     this.mapDoc    = mapDoc;
     if (member == null)
     {
         this.manyToOne.access = "none";
     }
     if (member == null)
     {
         entityPropertyMapper = new NoMemberPropertyMapper();
     }
     else
     {
         entityPropertyMapper = new AccessorPropertyMapper(member.DeclaringType, member.Name, x => manyToOne.access = x);
     }
 }
Ejemplo n.º 22
0
        private void BindProperty(HbmKeyManyToOne keyManyToOneSchema, Property property)
        {
            property.Name = keyManyToOneSchema.name;

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

            property.PropertyAccessorName = keyManyToOneSchema.access ?? mappings.DefaultAccess;
            property.Cascade            = mappings.DefaultCascade;
            property.IsUpdateable       = true;
            property.IsInsertable       = true;
            property.IsOptimisticLocked = true;
            property.Generation         = PropertyGeneration.Never;
            property.MetaAttributes     = new Dictionary <string, MetaAttribute>();

            property.LogMapped(log);
        }
Ejemplo n.º 23
0
        public void CanSetLazyness()
        {
            var hbmMapping = new HbmMapping();
            var member     = typeof(MyComponent).GetProperty("TheOtherRelation");
            var mapping    = new HbmKeyManyToOne();
            var mapper     = new KeyManyToOneMapper(member, mapping, hbmMapping);

            mapper.Lazy(LazyRelation.NoProxy);
            mapping.Lazy.Should().Have.Value();
            mapping.Lazy.Should().Be(HbmRestrictedLaziness.False);

            mapper.Lazy(LazyRelation.NoLazy);
            mapping.Lazy.Should().Have.Value();
            mapping.Lazy.Should().Be(HbmRestrictedLaziness.False);

            mapper.Lazy(LazyRelation.Proxy);
            mapping.Lazy.Should().Have.Value();
            mapping.Lazy.Should().Be(HbmRestrictedLaziness.Proxy);
        }
        private void BindComponent(System.Type reflectedClass, string path,
                                   HbmCompositeId idSchema)
        {
            if (idSchema.@class != null)
            {
                compositeId.ComponentClass = ClassForNameChecked(idSchema.@class, mappings,
                                                                 "component class not found: {0}");

                compositeId.IsEmbedded = false;
            }
            else if (reflectedClass != null)
            {
                compositeId.ComponentClass = reflectedClass;
                compositeId.IsEmbedded     = false;
            }
            else
            {
                //<Simon date='26.04.2010'>
                if (compositeId.Owner.HasPocoRepresentation)
                {
                    //</Simon>
                    // an "embedded" component (ids only)
                    compositeId.ComponentClass = compositeId.Owner.MappedClass;
                    compositeId.IsEmbedded     = true;

                    //<Simon date='26.04.2010'>
                }
                else
                {
                    compositeId.IsDynamic = true;
                }
                //</Simon>
            }

            foreach (object item in idSchema.Items ?? new object[0])
            {
                HbmKeyManyToOne keyManyToOneSchema = item as HbmKeyManyToOne;
                HbmKeyProperty  keyPropertySchema  = item as HbmKeyProperty;

                if (keyManyToOneSchema != null)
                {
                    ManyToOne manyToOne = new ManyToOne(compositeId.Table);

                    string propertyName = keyManyToOneSchema.name == null
                                                ? null
                                                : StringHelper.Qualify(path, keyManyToOneSchema.name);

                    BindManyToOne(keyManyToOneSchema, manyToOne, propertyName, false);

                    Mapping.Property property = CreateProperty(manyToOne, keyManyToOneSchema.name,
                                                               compositeId.ComponentClass, keyManyToOneSchema);

                    compositeId.AddProperty(property);
                }
                else if (keyPropertySchema != null)
                {
                    SimpleValue value = new SimpleValue(compositeId.Table);

                    string propertyName = keyPropertySchema.name == null
                                                ? null
                                                : StringHelper.Qualify(path, keyPropertySchema.name);

                    BindSimpleValue(keyPropertySchema, value, false, propertyName);

                    Mapping.Property property = CreateProperty(value, keyPropertySchema.name,
                                                               compositeId.ComponentClass, keyPropertySchema);

                    compositeId.AddProperty(property);
                }
            }
        }