public AssociationLink(IEntityComplexPropertyMetadata owner, string name, string role) { _owner = owner; _name = name; _role = role; _principal = _foreign = null; }
private bool IsLinked(IEntityComplexPropertyMetadata owner, IEntitySimplexPropertyMetadata property) { var links = owner.Links; for (int i = 0; i < links.Length; i++) { if (object.Equals(links[i].Foreign, property)) { return(true); } } return(false); }
public SequenceMetadata(IEntitySimplexPropertyMetadata property, string name, int seed, int interval = 1, IList <string> references = null) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(nameof(name)); } this.Property = property ?? throw new ArgumentNullException(nameof(property)); this.Name = name.Trim(); this.Seed = seed; this.Interval = interval == 0 ? 1 : interval; if (this.Name == "#") { this.Name = "#" + property.Entity.Name + ":" + property.Name; } _referenceNames = references; }
private bool IsLinked(SchemaMember owner, IEntitySimplexPropertyMetadata property) { if (owner == null || owner.Token.Property.IsSimplex) { return(false); } var links = ((IEntityComplexPropertyMetadata)owner.Token.Property).Links; for (int i = 0; i < links.Length; i++) { if (object.Equals(links[i].Foreign, property)) { return(true); } } return(false); }
/// <summary> /// 创建一个字段定义并添加到当前表定义的 <see cref="Fields"/> 集中,如果同名字段已经定义则返回空(null)。 /// </summary> /// <param name="property">指定的要添加字段的单值属性元信息。</param> /// <returns>返回的新增字段定义项,如果指定属性对应的字段已经存在则返回空(null)。</returns> public FieldDefinition Field(IEntitySimplexPropertyMetadata property) { if (property == null) { throw new ArgumentNullException(nameof(property)); } var fieldName = property.GetFieldName(out _); if (this.Fields.Contains(fieldName)) { return(null); } var field = new FieldDefinition(fieldName, property.Type, property.Nullable) { Length = property.Length, Precision = property.Precision, Scale = property.Scale, }; this.Fields.Add(field); return(field); }
private IEntityMetadata ResolveEntity(XmlReader reader, MetadataFile provider, string @namespace, Action unrecognize) { //创建实体元素对象 var entity = new MetadataEntity(provider, this.GetFullName(reader.GetAttribute(XML_NAME_ATTRIBUTE), @namespace), this.GetFullName(reader.GetAttribute(XML_INHERITS_ATTRIBUTE), @namespace)); //设置其他属性 entity.Alias = reader.GetAttribute(XML_ALIAS_ATTRIBUTE) ?? reader.GetAttribute(XML_TABLE_ATTRIBUTE); var keys = new HashSet <string>(); int depth = reader.Depth; while (reader.Read() && reader.Depth > depth) { if (reader.NodeType != XmlNodeType.Element) { continue; } switch (reader.LocalName) { case XML_KEY_ELEMENT: while (reader.Read() && reader.Depth > depth + 1) { if (reader.NodeType != XmlNodeType.Element) { continue; } if (reader.Name == XML_MEMBER_ELEMENT) { keys.Add(reader.GetAttribute(XML_NAME_ATTRIBUTE)); } else { unrecognize(); } } break; case XML_PROPERTY_ELEMENT: var propertyType = this.ParseDbType(this.GetAttributeValue <string>(reader, XML_TYPE_ATTRIBUTE)); var property = new MetadataEntitySimplexProperty(entity, reader.GetAttribute(XML_NAME_ATTRIBUTE), propertyType) { Alias = this.GetAttributeValue <string>(reader, XML_ALIAS_ATTRIBUTE) ?? this.GetAttributeValue <string>(reader, XML_FIELD_ATTRIBUTE), Length = this.GetAttributeValue <int>(reader, XML_LENGTH_ATTRIBUTE), Nullable = this.GetAttributeValue <bool>(reader, XML_NULLABLE_ATTRIBUTE, true), Precision = this.GetAttributeValue <byte>(reader, XML_PRECISION_ATTRIBUTE), Scale = this.GetAttributeValue <byte>(reader, XML_SCALE_ATTRIBUTE), }; //设置默认值的字面量 property.SetDefaultValue(this.GetAttributeValue <string>(reader, XML_DEFAULT_ATTRIBUTE)); //设置序号器元数据信息 property.SetSequence(this.GetAttributeValue <string>(reader, XML_SEQUENCE_ATTRIBUTE)); //将解析成功的属性元素加入到实体的属性集合 entity.Properties.Add(property); break; case XML_COMPLEXPROPERTY_ELEMENT: var complexProperty = new MetadataEntityComplexProperty(entity, reader.GetAttribute(XML_NAME_ATTRIBUTE), this.GetRoleName(reader.GetAttribute(XML_ROLE_ATTRIBUTE), @namespace)); var multiplicity = reader.GetAttribute(XML_MULTIPLICITY_ATTRIBUTE); if (multiplicity != null && multiplicity.Length > 0) { switch (multiplicity) { case "*": complexProperty.Multiplicity = AssociationMultiplicity.Many; break; case "1": case "!": complexProperty.Multiplicity = AssociationMultiplicity.One; break; case "?": case "0..1": complexProperty.Multiplicity = AssociationMultiplicity.ZeroOrOne; break; default: throw new DataException($"Invalid '{multiplicity}' value of the multiplicity attribute."); } } var links = new List <AssociationLink>(); while (reader.Read() && reader.Depth > depth + 1) { if (reader.NodeType != XmlNodeType.Element) { continue; } if (reader.LocalName == XML_LINK_ELEMENT) { links.Add( new AssociationLink(complexProperty, this.GetAttributeValue <string>(reader, XML_NAME_ATTRIBUTE), this.GetAttributeValue <string>(reader, XML_ROLE_ATTRIBUTE))); } else if (reader.LocalName == XML_CONSTRAINTS_ELEMENT) { if (reader.IsEmptyElement) { continue; } var constraints = new List <AssociationConstraint>(); while (reader.Read() && reader.Depth > depth + 2) { if (reader.NodeType != XmlNodeType.Element) { continue; } if (reader.LocalName == XML_CONSTRAINT_ELEMENT) { constraints.Add( new AssociationConstraint( this.GetAttributeValue <string>(reader, XML_NAME_ATTRIBUTE), this.GetAttributeValue(reader, XML_ACTOR_ATTRIBUTE, AssociationConstraintActor.Principal), this.GetAttributeValue <object>(reader, XML_VALUE_ATTRIBUTE))); } else { unrecognize(); } } if (constraints.Count > 0) { complexProperty.Constraints = constraints.ToArray(); } } else { unrecognize(); } } if (links == null || links.Count == 0) { throw new DataException($"Missing links of the '{complexProperty.Name}' complex property in the '{provider.FilePath}' mapping file."); } //设置复合属性的链接集属性 complexProperty.Links = links.ToArray(); //将解析成功的属性元素加入到实体的属性集合 entity.Properties.Add(complexProperty); break; default: unrecognize(); break; } } //确认实体的主键信息 if (keys.Count > 0) { var array = new IEntitySimplexPropertyMetadata[keys.Count]; var index = 0; foreach (var key in keys) { if (!entity.Properties.TryGet(key, out var property)) { throw new MetadataFileException(""); } if (property.IsComplex) { throw new MetadataFileException(""); } //将主键属性的是否主键开关打开 ((MetadataEntitySimplexProperty)property).SetPrimaryKey(); array[index++] = (IEntitySimplexPropertyMetadata)property; } entity.Key = array; } return(entity); }