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

            // This is done here 'cos we might only know the type here (ugly!)
            var toOne = value as ToOne;

            if (toOne != null)
            {
                string propertyRef = toOne.ReferencedPropertyName;
                if (propertyRef != null)
                {
                    mappings.AddUniquePropertyReference(toOne.ReferencedEntityName, propertyRef);
                }
            }

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

            BindProperty(keyPropertySchema, prop);

            return(prop);
        }
 private void BindSimpleValue(HbmKeyProperty keyPropertySchema, SimpleValue model, bool isNullable, string path)
 {
     if (keyPropertySchema.type1 != null)
     {
         model.TypeName = keyPropertySchema.type1;
     }
     BindColumns(keyPropertySchema, model, isNullable, true, path);
 }
Exemplo n.º 3
0
        public void AfterSetMultiColumnsCantSetSimpleColumn()
        {
            var member  = typeof(MyClass).GetProperty("ReadOnly");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            mapper.Columns(cm => cm.Length(50), cm => cm.SqlType("VARCHAR(10)"));
            mapper.Executing(m => m.Column(cm => cm.Length(50))).Throws <ConfOrm.MappingException>();
        }
Exemplo n.º 4
0
        public void WhenSetInvalidTypeThenThrow()
        {
            var member  = typeof(MyClass).GetProperty("ReadOnly");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            mapper.Executing(m => m.Type(typeof(object), null)).Throws <ArgumentOutOfRangeException>();
            mapper.Executing(m => m.Type(null, null)).Throws <ArgumentNullException>();
        }
        public void Property(MemberInfo property, Action <IPropertyMapper> mapping)
        {
            var hbmProperty = new HbmKeyProperty {
                name = property.Name
            };

            mapping(new KeyPropertyMapper(property, hbmProperty));
            AddProperty(hbmProperty);
        }
        private void BindColumns(HbmKeyProperty keyPropertySchema, SimpleValue model, bool isNullable, bool autoColumn,
                                 string propertyPath)
        {
            Table table = model.Table;

            if (keyPropertySchema.column1 == null)
            {
                int count = 0;

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

                    col.Name = mappings.NamingStrategy.ColumnName(columnSchema.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(keyPropertySchema, col, isNullable);
                col.Name = mappings.NamingStrategy.ColumnName(keyPropertySchema.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(keyPropertySchema, 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)
            }
        }
Exemplo n.º 7
0
        public void WhenSettingByTypeThenCheckCompatibility()
        {
            var member  = typeof(MyClass).GetProperty("ReadOnly");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            mapper.Executing(m => m.Access(typeof(object))).Throws <ArgumentOutOfRangeException>();
            mapper.Executing(m => m.Access(typeof(FieldAccessor))).NotThrows();
            mapping.Access.Should().Be.EqualTo(typeof(FieldAccessor).AssemblyQualifiedName);
        }
Exemplo n.º 8
0
        public void WhenSetMultiColumnsValuesThenAutoassignColumnNames()
        {
            var member  = typeof(MyClass).GetProperty("ReadOnly");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            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)));
        }
Exemplo n.º 9
0
        public void WhenSetBasicColumnValuesThenSetPlainValues()
        {
            var member  = typeof(MyClass).GetProperty("Autoproperty");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            mapper.Column(cm => cm.Length(50));
            mapping.column.Should().Be.Null();
            mapping.length.Should().Be("50");
        }
Exemplo n.º 10
0
        public void WhenSetTypeByITypeThenSetTypeName()
        {
            var member  = typeof(MyClass).GetProperty("ReadOnly");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            mapper.Type(NHibernateUtil.String);

            mapping.Type.name.Should().Be.EqualTo("String");
        }
Exemplo n.º 11
0
        public void WhenSetTypeByICompositeUserTypeThenSetTypeName()
        {
            var member  = typeof(MyClass).GetProperty("ReadOnly");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            Assert.That(() => mapper.Type <MyCompoType>(), Throws.Nothing);
            Assert.That(mapping.Type.name, Does.Contain(nameof(MyCompoType)));
            Assert.That(mapping.type, Is.Null);
        }
Exemplo n.º 12
0
 private void BindSimpleValue(HbmKeyProperty keyPropertySchema, SimpleValue model, bool isNullable, string path)
 {
     if (keyPropertySchema.type1 != null)
     {
         model.TypeName = keyPropertySchema.type1;
     }
     new ColumnsBinder(model, Mappings).Bind(keyPropertySchema.Columns, isNullable,
                                             () => new HbmColumn {
         name = mappings.NamingStrategy.PropertyToColumnName(path), length = keyPropertySchema.length
     });
 }
Exemplo n.º 13
0
        public void WhenSetTypeByIUserTypeWithNullParamsThenSetTypeName()
        {
            var member  = typeof(MyClass).GetProperty("ReadOnly");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            mapper.Type <MyType>(null);

            mapping.Type.name.Should().Contain("MyType");
            mapping.type.Should().Be.Null();
        }
Exemplo n.º 14
0
        public void WhenSetDifferentColumnNameThenSetTheName()
        {
            var member  = typeof(MyClass).GetProperty("Autoproperty");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

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

            mapping.Columns.Should().Have.Count.EqualTo(1);
            mapping.Columns.Single().name.Should().Be("pepe");
        }
Exemplo n.º 15
0
 public void BindSimpleValue(HbmKeyProperty mapKeyManyToManyMapping, string propertyPath, bool isNullable)
 {
     new TypeBinder(value, Mappings).Bind(mapKeyManyToManyMapping.Type);
     new ColumnsBinder(value, Mappings).Bind(mapKeyManyToManyMapping.Columns, isNullable,
                                             () =>
                                             new HbmColumn
     {
         name   = mappings.NamingStrategy.PropertyToColumnName(propertyPath),
         length = mapKeyManyToManyMapping.length,
     });
 }
Exemplo n.º 16
0
        public void WhenSetDefaultColumnNameThenDoesNotSetTheName()
        {
            var member  = typeof(MyClass).GetProperty("Autoproperty");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            mapper.Column(cm => { cm.Name("Autoproperty"); cm.Length(50); });
            mapping.column.Should().Be.Null();
            mapping.length.Should().Be("50");
            mapping.Columns.Should().Be.Empty();
        }
        private static void BindColumn(HbmKeyProperty keyPropertySchema, Column model, bool isNullable)
        {
            if (keyPropertySchema.length != null)
            {
                model.Length = int.Parse(keyPropertySchema.length);
            }

            model.IsNullable      = isNullable;
            model.IsUnique        = false;
            model.CheckConstraint = string.Empty;
            model.SqlType         = null;
        }
Exemplo n.º 18
0
        public void WhenSetTypeByITypeTypeThenSetType()
        {
            var member = ForClass <MyClass> .Property(c => c.EnumProp);

            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            mapper.Type <EnumStringType <MyEnum> >();

            mapping.Type.name.Should().Contain(typeof(EnumStringType <MyEnum>).FullName);
            mapping.type.Should().Be.Null();
        }
Exemplo n.º 19
0
        public void WhenSetColumnValuesThenAddColumnTag()
        {
            var member  = typeof(MyClass).GetProperty("Autoproperty");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            mapper.Column(cm =>
            {
                cm.SqlType("VARCHAR(50)");
                cm.NotNullable(true);
            });
            mapping.column.Should().Not.Be.Null();
            mapping.Columns.Should().Have.Count.EqualTo(1);
        }
Exemplo n.º 20
0
        public void WhenSetTypeByIUserTypeWithParamsThenSetType()
        {
            var member  = typeof(MyClass).GetProperty("ReadOnly");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            mapper.Type <MyType>(new { Param1 = "a", Param2 = 12 });

            mapping.type1.Should().Be.Null();
            mapping.Type.name.Should().Contain("MyType");
            mapping.Type.param.Should().Have.Count.EqualTo(2);
            mapping.Type.param.Select(p => p.name).Should().Have.SameValuesAs("Param1", "Param2");
            mapping.Type.param.Select(p => p.GetText()).Should().Have.SameValuesAs("a", "12");
        }
Exemplo n.º 21
0
        private void BindProperty(HbmKeyProperty keyPropertySchema, Property property)
        {
            property.Name = keyPropertySchema.name;

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

            property.PropertyAccessorName = keyPropertySchema.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);
        }
Exemplo n.º 22
0
        public void WhenSetMultiColumnsValuesThenAddColumns()
        {
            var member  = typeof(MyClass).GetProperty("ReadOnly");
            var mapping = new HbmKeyProperty();
            var mapper  = new KeyPropertyMapper(member, mapping);

            mapper.Type <MyType>();
            mapper.Columns(cm =>
            {
                cm.Name("column1");
                cm.Length(50);
            }, cm =>
            {
                cm.Name("column2");
                cm.SqlType("VARCHAR(10)");
            });
            mapping.Columns.Should().Have.Count.EqualTo(2);
        }
Exemplo n.º 23
0
 public KeyPropertyMapper(MemberInfo member, HbmKeyProperty propertyMapping)
 {
     if (propertyMapping == null)
     {
         throw new ArgumentNullException("propertyMapping");
     }
     this.member          = member;
     this.propertyMapping = propertyMapping;
     if (member == null)
     {
         this.propertyMapping.access = "none";
     }
     if (member == null)
     {
         entityPropertyMapper = new NoMemberPropertyMapper();
     }
     else
     {
         entityPropertyMapper = new AccessorPropertyMapper(member.DeclaringType, member.Name, x => propertyMapping.access = x);
     }
 }
        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);
                }
            }
        }