Example #1
0
        ///// <summary>
        ///// 设置不同于配置的类型
        ///// </summary>
        ///// <param name="type"></param>
        //public void SetEntityType(Type type)
        //{
        //	if(!string.IsNullOrEmpty(_typeStr) && EntityType == type)
        //		return;

        //	foreach(var item in JoinList)
        //	{
        //		var propertyInfo = type.GetProperty(item.Name);
        //		if(propertyInfo != null)
        //		{
        //			if(string.IsNullOrEmpty(item.Target._typeStr) || item.Target.EntityType != propertyInfo.PropertyType)
        //				item.Target.SetEntityType(propertyInfo.PropertyType);
        //		}
        //	}
        //}

        public void Init(List <ClassNode> all)
        {
            if (!string.IsNullOrEmpty(Inherit))
            {
                _base = all.FirstOrDefault(p => p.Name.Equals(Inherit, StringComparison.OrdinalIgnoreCase));
                if (_base != null)
                {
                    var join = new JoinPropertyNode(_base._name, _base, JoinType.Inner);
                    var pks  = _propertyNodeList.Where(p => p.IsKey).ToList();
                    if (pks.Count == 0)
                    {
                        foreach (var pk in _base._propertyNodeList.Where(p => p.IsKey))
                        {
                            join.Member.Add(pk, pk);
                        }
                    }
                    else
                    {
                        foreach (var pk in pks)
                        {
                            PropertyNode pn = _base._propertyNodeList.First(p => p.IsKey && p.Name.Equals(pk.Name, StringComparison.OrdinalIgnoreCase));
                            if (pn == null)
                            {
                                throw new Exception(string.Format("继承时子类主键名称要和父类主键名称一致:{0} {1}", this._name, _base._name));
                            }
                            join.Member.Add(pk, pn);
                        }
                    }
                    _joinList.Add(join);
                }
            }
        }
Example #2
0
		public static JoinPropertyNode Create(ClassNode info, XElement property)
		{
			string name = null, target, attribuleValue;

			if(MappingInfo.GetAttribuleValue(property, "name", out attribuleValue))
				name = attribuleValue;
			else
				throw new FormatException(string.Format("{0}.property节点name属性未赋值", info.Name));

			if(MappingInfo.GetAttribuleValue(property, "relationTo", out attribuleValue))
				target = attribuleValue;
			else
				throw new FormatException(string.Format("{0}.property节点relationTo属性未赋值", info.Name));

			var joinPropertyInfo = new JoinPropertyNode(name, target);

			var join = property.Element("join");

			if(MappingInfo.GetAttribuleValue(join, "mode", out attribuleValue))
			{
				JoinType temp;
				joinPropertyInfo._type = Zongsoft.Common.Convert.TryConvertValue<JoinType>(attribuleValue, out temp) ? temp : JoinType.Inner;
			}

			foreach(var relation in join.Elements())
			{
				string from, to;
				if(MappingInfo.GetAttribuleValue(relation, "from", out attribuleValue))
					from = attribuleValue;
				else
					throw new FormatException(string.Format("{0}.property节点中有member项from属性未赋值", info.Name));

				if(MappingInfo.GetAttribuleValue(relation, "to", out attribuleValue))
					to = attribuleValue;
				else
					throw new FormatException(string.Format("{0}.property节点中有member项to属性未赋值", info.Name));

				joinPropertyInfo.Add(from, to);
			}

			return joinPropertyInfo;
		}
Example #3
0
        public static ClassNode Create(XElement element)
        {
            string attribuleValue;

            var info = new ClassNode();

            if (MappingInfo.GetAttribuleValue(element, "type", out attribuleValue))
            {
                info._typeStr = attribuleValue;
            }

            if (MappingInfo.GetAttribuleValue(element, "name", out attribuleValue) || !(attribuleValue = element.Name.LocalName).Equals("object", StringComparison.OrdinalIgnoreCase))
            {
                info._name = attribuleValue;
            }
            else
            {
                info._name = info.EntityType.Name;
            }

            if (MappingInfo.GetAttribuleValue(element, "table", out attribuleValue))
            {
                info._table = attribuleValue;
            }

            if (MappingInfo.GetAttribuleValue(element, "schema", out attribuleValue))
            {
                info._schema = attribuleValue;
            }

            if (MappingInfo.GetAttribuleValue(element, "inherits", out attribuleValue))
            {
                info._inherit = attribuleValue;
            }

            info._propertyNodeList = new List <PropertyNode>();
            info._joinList         = new List <JoinPropertyNode>();

            foreach (var property in element.Elements())
            {
                if (property.Name.LocalName.Equals("key", StringComparison.OrdinalIgnoreCase))
                {
                    foreach (var key in property.Elements())
                    {
                        var keyPropertyInfo = PropertyNode.Create(key);
                        if (string.IsNullOrWhiteSpace(keyPropertyInfo.Name))
                        {
                            throw new FormatException(string.Format("{0}节点下面有一个主键没有name属性", info.Name));
                        }
                        keyPropertyInfo.IsKey = true;
                        info.AddPropertyNodel(keyPropertyInfo);
                    }
                    continue;
                }
                else if (property.HasElements)
                {
                    var joinPropertyInfo = JoinPropertyNode.Create(info, property);
                    info.JoinList.Add(joinPropertyInfo);
                    continue;
                }

                var propertyInfo = PropertyNode.Create(property);
                if (string.IsNullOrWhiteSpace(propertyInfo.Name))
                {
                    throw new FormatException(string.Format("{0}节点下面有一个property节点没有name属性", info.Name));
                }

                info.AddPropertyNodel(propertyInfo);
            }

            return(info);
        }