/// <summary> /// Visits the joined table. /// </summary> /// <remarks> /// Infer column name /// </remarks> /// <param name="model">The model.</param> public override void VisitJoinedTable(JoinedTableModel model) { if (model.JoinedTableAttribute.Column == null) { model.JoinedTableAttribute.Column = currentModel.PrimaryKey.PrimaryKeyAtt.Column; } }
/// <summary> /// Visits the nested model /// </summary> /// <remarks> /// Infer the column name and applies and column prefixes specified /// </remarks> /// <param name="model">The model.</param> public override void VisitNested(NestedModel model) { if (model.NestedAtt.MapType == null) { model.NestedAtt.MapType = model.Property.PropertyType; } if (model.NestedAtt.ColumnPrefix != null) { columnPrefix.Append(model.NestedAtt.ColumnPrefix); } base.VisitNested(model); if (model.NestedAtt.ColumnPrefix != null) { columnPrefix.Length -= model.NestedAtt.ColumnPrefix.Length; } JoinedTableModel joinedTable = ObtainJoinedTableIfPresent(model.Property, model.NestedAtt); if (joinedTable != null) { joinedTable.Components.Add(model); } }
/// <summary> /// Visits the field. /// </summary> /// <remarks> /// Infer column name and nullablity /// </remarks> /// <param name="model">The model.</param> public override void VisitField(FieldModel model) { if (model.FieldAtt.Column == null) { model.FieldAtt.Column = model.Field.Name; } // Append column prefix model.FieldAtt.Column = columnPrefix + model.FieldAtt.Column; Type fieldType = model.Field.FieldType; if (NHibernateNullablesSupport.IsNHibernateNullableType(fieldType) && (model.FieldAtt.ColumnType == null || model.FieldAtt.ColumnType.Length == 0)) { model.FieldAtt.NotNull = false; model.FieldAtt.ColumnType = NHibernateNullablesSupport.GetITypeTypeNameForNHibernateNullable(fieldType); } if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Nullable <>) && String.IsNullOrEmpty(model.FieldAtt.ColumnType)) { model.FieldAtt.NotNull = false; model.FieldAtt.ColumnType = ObtainNullableTypeNameForCLRNullable(fieldType); } JoinedTableModel joinedTable = ObtainJoinedTableIfPresent(model.Field, model.FieldAtt); if (joinedTable != null) { joinedTable.Fields.Add(model); } }
private static void ProcessJoinedTables(Type type, ActiveRecordModel model) { object[] attrs = type.GetCustomAttributes(typeof(JoinedTableAttribute), false); foreach (JoinedTableAttribute att in attrs) { JoinedTableModel jtm = new JoinedTableModel(att); model.JoinedTables.Add(jtm); } }
private JoinedTableModel ObtainJoinedTableIfPresent(MemberInfo propertyOrField, WithAccessOptionalTableAttribute access) { String tableName = access.Table; if (tableName == null) { return(null); } if (currentModel.IsNestedType) { throw new ActiveRecordException( String.Format("{0} {1} references table \"{2}\" which is not allowed on nested types.", propertyOrField is PropertyInfo ? "Property" : "Field", propertyOrField.Name, tableName)); } if (tableName == String.Empty || tableName == currentModel.ActiveRecordAtt.Table) { access.Table = null; return(null); } JoinedTableModel joinedTable = null; foreach (JoinedTableModel jtm in currentModel.JoinedTables) { if (jtm.JoinedTableAttribute.Table == tableName) { joinedTable = jtm; break; } } if (joinedTable == null) { throw new ActiveRecordException( String.Format("{0} {1} references table \"{2}\", which does not have a corresponding [JoinedTable] on the class.", propertyOrField is PropertyInfo ? "Property" : "Field", propertyOrField.Name, tableName)); } return(joinedTable); }
/// <summary> /// Visits any. /// </summary> /// <param name="model">The model.</param> public override void VisitAny(AnyModel model) { if (model.AnyAtt.TypeColumn == null) { model.AnyAtt.TypeColumn = model.Property.Name + "AnyType"; } if (model.AnyAtt.IdColumn == null) { model.AnyAtt.IdColumn = model.Property.Name + "AnyTypeId"; } JoinedTableModel joinedTable = ObtainJoinedTableIfPresent(model.Property, model.AnyAtt); if (joinedTable != null) { joinedTable.Anys.Add(model); } }
/// <summary> /// Visits the belongs to. /// </summary> /// <remarks> /// Infer column name and type /// Verify that the property is virtual if the class was marked lazy. /// </remarks> /// <param name="model">The model.</param> public override void VisitBelongsTo(BelongsToModel model) { if (currentModel.ActiveRecordAtt != null) { if (currentModel.ActiveRecordAtt.Lazy || (currentModel.ActiveRecordAtt.LazySpecified == false && ActiveRecordModel.isLazyByDefault)) { //Assuming that a property must have at least a single accessor MethodInfo accessor = model.Property.GetAccessors(true)[0]; if (!accessor.IsVirtual) { throw new ActiveRecordException( String.Format("Property {0} must be virtual because " + "class {1} support lazy loading [ActiveRecord(Lazy=true)]", model.Property.Name, model.Property.DeclaringType.Name)); } } } if (model.BelongsToAtt.Column == null && model.BelongsToAtt.CompositeKeyColumns == null) { model.BelongsToAtt.Column = model.Property.Name; } // Append column prefix if (model.BelongsToAtt.Column != null) { model.BelongsToAtt.Column = columnPrefix + model.BelongsToAtt.Column; } if (model.BelongsToAtt.Type == null) { model.BelongsToAtt.Type = model.Property.PropertyType; } JoinedTableModel joinedTable = ObtainJoinedTableIfPresent(model.Property, model.BelongsToAtt); if (joinedTable != null) { joinedTable.BelongsTo.Add(model); } }
/// <summary> /// Visits the property. /// </summary> /// <remarks> /// Infer column name and whatever this propery can be null or not /// Also catch common mistake of try to use [Property] on an entity, instead of [BelongsTo] /// Ensure that joined properties have a joined table. /// </remarks> /// <param name="model">The model.</param> public override void VisitProperty(PropertyModel model) { if (model.PropertyAtt.Column == null) { model.PropertyAtt.Column = model.Property.Name; } // Append column prefix model.PropertyAtt.Column = columnPrefix + model.PropertyAtt.Column; Type propertyType = model.Property.PropertyType; if (NHibernateNullablesSupport.IsNHibernateNullableType(propertyType) && (model.PropertyAtt.ColumnType == null || model.PropertyAtt.ColumnType.Length == 0)) { model.PropertyAtt.NotNull = false; model.PropertyAtt.ColumnType = NHibernateNullablesSupport.GetITypeTypeNameForNHibernateNullable(propertyType); } if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>) && String.IsNullOrEmpty(model.PropertyAtt.ColumnType)) { model.PropertyAtt.NotNull = false; model.PropertyAtt.ColumnType = ObtainNullableTypeNameForCLRNullable(propertyType); } if (ActiveRecordModel.GetModel(propertyType) != null) { throw new ActiveRecordException(String.Format( "You can't use [Property] on {0}.{1} because {2} is an active record class, did you mean to use BelongTo?", model.Property.DeclaringType.Name, model.Property.Name, propertyType.FullName)); } JoinedTableModel joinedTable = ObtainJoinedTableIfPresent(model.Property, model.PropertyAtt); if (joinedTable != null) { joinedTable.Properties.Add(model); } }
/// <summary> /// Visits the joined table. /// </summary> /// <remarks> /// Infer column name /// </remarks> /// <param name="model">The model.</param> public override void VisitJoinedTable(JoinedTableModel model) { JoinedTableAttribute att = model.JoinedTableAttribute; using (new TableScope(this, att.Table)) { AppendF("<join{0}{1}{2}{3}>", MakeAtt("table", att.Table), WriteIfNonNull("schema", att.Schema), WriteIfNonNull("fetch", TranslateFetch(att.Fetch)), WriteIfTrue("inverse", att.Inverse), WriteIfTrue("optional", att.Optional)); Ident(); WriteKey(att.Column); VisitNodes(model.Fields); VisitNodes(model.Properties); VisitNodes(model.BelongsTo); VisitNodes(model.Anys); VisitNodes(model.Components); Dedent(); Append("</join>"); } }
/// <summary> /// Visits the joined table configuration. /// </summary> /// <param name="model">The model.</param> public virtual void VisitJoinedTable(JoinedTableModel model) { }