/// <summary> /// 生成主键 /// </summary> /// <param name="def"></param> /// <param name="dojoCompile"></param> /// <param name="jsonWriter"></param> private void BuildPrimaryString(EntityDefinition def, DojoCompile dojoCompile, JsonWriter jsonWriter) { foreach (var primary in def.Members.OfType <PrimaryMember>()) { dynamic type = primary.Content; jsonWriter.WritePropertyName(primary.Name); jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("Title"); jsonWriter.WriteValue(primary.Title); jsonWriter.WritePropertyName("Type"); jsonWriter.WriteValue(BuildCommonMethod.GetTypeName(type.BaseType)); jsonWriter.WriteEndObject(); } }
/// <summary> /// 生成模型数据定义 /// </summary> /// <param name="compile">编译对象</param> /// <param name="jsonWriter">JsonWriter</param> private void BuildAllDefinition(CompileBase compile, JsonWriter jsonWriter) { DojoCompile dojoCompile = compile as DojoCompile; var entityItem = dojoCompile.ProjectItems.Where(a => a.Value.DocumentType == GlobalIds.DocumentType.Entity).ToList(); foreach (var item in entityItem) { //jsonWriter.WriteStartObject(); //1 jsonWriter.WritePropertyName(item.Value.Name); jsonWriter.WriteStartObject(); var def = dojoCompile.GetDocumentBody(item.Value) as EntityDefinition; #region 获取所有自定义外键 StringBuilder sbAllForeigns = new StringBuilder(); StringBuilder sbForeignKey = new StringBuilder(); BuildCommonMethod.GetAllRelationForeignContent(item.Value.Propertys, def, dojoCompile, sbForeignKey); if (sbForeignKey.ToString().Length > 0) { sbAllForeigns.Append(sbForeignKey.ToString()); } #endregion #region EntityID jsonWriter.WritePropertyName("entityId"); jsonWriter.WriteValue(item.Key); #endregion #region Primary jsonWriter.WritePropertyName("primaryKey"); jsonWriter.WriteStartObject(); BuildPrimaryContent(item.Value.Propertys, def, dojoCompile, jsonWriter); jsonWriter.WriteEndObject(); #endregion #region Fields jsonWriter.WritePropertyName("fields"); jsonWriter.WriteStartObject(); BuildItemsContent(item.Value.Propertys, def, dojoCompile, jsonWriter, sbAllForeigns.ToString()); jsonWriter.WriteEndObject(); #endregion jsonWriter.WriteEndObject(); //jsonWriter.WriteEndObject(); //1 } }
private static MetaDataProperty GetDataMemberContent(MemberBase mb) { var item = mb as DataMember; var typeContent = item.Content as CommonDataType; string defaultValue = string.Empty, maxValue = string.Empty, minValue = string.Empty, maxLength = string.Empty, minLength = string.Empty; BuildCommonMethod.GetDataTypeValue(typeContent, ref defaultValue, ref maxValue, ref minValue, ref maxLength, ref minLength); var propertyMember = new MetaDataProperty() { Name = item.Name, Title = item.Title, IsRequired = item.IsRequired, IsCollection = false, DefaultValue = defaultValue, MaxValue = maxValue, MinValue = minValue, MaxLength = maxLength, MinLength = minLength, DataType = BuildCommonMethod.GetTypeNameByIon(typeContent.BaseType) }; return(propertyMember); }
/// <summary> /// 生成字段 /// </summary> /// <param name="def"></param> /// <param name="dojoCompile"></param> /// <param name="jsonWriter"></param> private void BuildFiledsString(EntityDefinition def, DojoCompile dojoCompile, JsonWriter jsonWriter, string AllForeigns) { foreach (var member in def.Members) { if (member.MemberType == EMemberType.Calculate || member.MemberType == EMemberType.Column || member.MemberType == EMemberType.Common) { var typeContent = (CommonDataType)member.Content; //时间戳格式不生成 if (typeContent.BaseType == Foundation.EDataBaseType.Timestamp) { continue; } //标识该字段是否是自定义外键 bool IsForeignKey = false; if (AllForeigns.Length > 0) { string[] foreigns = AllForeigns.Split(','); for (var i = 0; i < foreigns.Length; i++) { if (foreigns[i] == member.Name) { IsForeignKey = true; break; } } } jsonWriter.WritePropertyName(member.Name); jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("Title"); jsonWriter.WriteValue(member.Title); jsonWriter.WritePropertyName("Type"); jsonWriter.WriteValue(BuildCommonMethod.GetTypeName(typeContent.BaseType)); jsonWriter.WritePropertyName("DataType"); jsonWriter.WriteValue(typeContent.BaseType.ToString()); #region IsRequired if (member.MemberType == EMemberType.Calculate) { var memberBase = member as ColumnMember; jsonWriter.WritePropertyName("IsRequired"); jsonWriter.WriteValue(memberBase.IsRequired); } else if (member.MemberType == EMemberType.Column) { var memberBase = member as ColumnMember; jsonWriter.WritePropertyName("IsRequired"); jsonWriter.WriteValue(memberBase.IsRequired); } else if (member.MemberType == EMemberType.Common) { var memberBase = member as CommonMember; jsonWriter.WritePropertyName("IsRequired"); jsonWriter.WriteValue(memberBase.IsRequired); } #endregion if (IsForeignKey) { jsonWriter.WritePropertyName("IsForeignKey"); jsonWriter.WriteValue(IsForeignKey); } bool serverRefresh = false; if (member.MemberType == EMemberType.Column) { if ((member as ColumnMember).GenerateMode != EColumnGenerateMode.None) { serverRefresh = true; } } jsonWriter.WritePropertyName("ServerRefresh"); jsonWriter.WriteValue(serverRefresh); jsonWriter.WriteEndObject(); } else if (member.MemberType == EMemberType.Navigation) { var navigationMember = member as NavigationMember; string toEntity = (from t in dojoCompile.ProjectItems where t.Key == navigationMember.ToEntityId select t.Value).FirstOrDefault().Name; jsonWriter.WritePropertyName(navigationMember.Name); jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("Title"); jsonWriter.WriteValue(navigationMember.Title); jsonWriter.WritePropertyName("Type"); jsonWriter.WriteValue(navigationMember.ToCardinality == EMappingCardinality.Many ? "array" : "object"); jsonWriter.WritePropertyName("DataType"); jsonWriter.WriteValue(navigationMember.ToCardinality == EMappingCardinality.Many ? "array" : "object"); jsonWriter.WritePropertyName("ToName"); jsonWriter.WriteValue(navigationMember.ToName); jsonWriter.WritePropertyName("ToEntity"); jsonWriter.WriteValue(toEntity); bool isSingleRelation = false; if (string.IsNullOrEmpty(navigationMember.ToName)) { isSingleRelation = true; } jsonWriter.WritePropertyName("SingleRelation"); jsonWriter.WriteValue(isSingleRelation); if (navigationMember.ToCardinality != EMappingCardinality.Many) { string navPrimaryKey = BuildCommonMethod.GetNavPrimaryContent(def, dojoCompile, toEntity); string foreignKey = BuildCommonMethod.GetRealForeignKey(navigationMember, toEntity, navPrimaryKey); if (navigationMember.Cardinality == EMappingCardinality.One && navigationMember.ToCardinality == EMappingCardinality.One) { foreignKey = navPrimaryKey; } jsonWriter.WritePropertyName("RelationForeignKey"); jsonWriter.WriteStartArray(); jsonWriter.WriteValue("" + foreignKey + ""); jsonWriter.WriteEndArray(); } jsonWriter.WriteEndObject(); } } }
/// <summary> /// 初始化数据 /// </summary> private void InitialMembers() { foreach (var member in this.EntityDef.Members.Where(a => a.MemberType == EMemberType.Primary)) { var typeContent = member.Content as CommonDataType; var dateType = typeContent.BaseType.ToString(); var dateTypeFront = BuildCommonMethod.GetTypeNameByIon(typeContent.BaseType); PrimaryPropertys.Add(member.Name, new Tuple <string, string, string>(member.Title, dateType, dateTypeFront)); } foreach (var member in this.EntityDef.Members.Where(a => a.MemberType == EMemberType.Column)) { var typeContent = member.Content as CommonDataType; if (typeContent.BaseType == EDataBaseType.Timestamp) { continue; } var dateType = typeContent.BaseType.ToString(); var dateTypeFront = BuildCommonMethod.GetTypeNameByIon(typeContent.BaseType); ColumnPropertys.Add(member.Name, new Tuple <string, string, string>(member.Title, dateType, dateTypeFront)); } foreach (var member in this.EntityDef.Members.Where(a => a.MemberType == EMemberType.Common)) { var typeContent = member.Content as CommonDataType; if (typeContent.BaseType == EDataBaseType.Timestamp) { continue; } var dateType = typeContent.BaseType.ToString(); var dateTypeFront = BuildCommonMethod.GetTypeNameByIon(typeContent.BaseType); CommonPropertys.Add(member.Name, new Tuple <string, string, string>(member.Title, dateType, dateTypeFront)); } foreach (var member in this.EntityDef.Members.Where(a => a.MemberType == EMemberType.Calculate)) { var typeContent = member.Content as CommonDataType; if (typeContent.BaseType == EDataBaseType.Timestamp) { continue; } var dateType = typeContent.BaseType.ToString(); var dateTypeFront = BuildCommonMethod.GetTypeNameByIon(typeContent.BaseType); CalculatePropertys.Add(member.Name, new Tuple <string, string, string>(member.Title, dateType, dateTypeFront)); } foreach (var member in this.EntityDef.Members.Where(a => a.MemberType == EMemberType.Navigation)) { var navigationMember = member as NavigationMember; string toEntity = (from t in this.Compile.ProjectItems where t.Key == navigationMember.ToEntityId select t.Value).FirstOrDefault().Name; bool isScalar = navigationMember.ToCardinality == EMappingCardinality.Many ? false : true; string foreignKeyNames = navigationMember.ForeignKeyName == null ? "" : navigationMember.ForeignKeyName; #region //foreach (var primaryMember in this.EntityDef.Members.OfType<PrimaryMember>()) //{ // string primaryKey = primaryMember.Name; // string fkeyNames = GetRealForeignKey(navigationMember, toEntity, primaryKey); // if (string.IsNullOrEmpty(foreignKeyNames)) foreignKeyNames = fkeyNames; // else foreignKeyNames += "," + fkeyNames; //} #endregion NavigationPropertys.Add(member.Name, new Tuple <string, string, string, bool>(member.Title, toEntity, foreignKeyNames, isScalar)); ImportMember.Add(toEntity, toEntity); } }