private static void GetEntityMemberTypeStringAndReadOnly(EntityMemberModel mm, ref string typeString, ref bool readOnly, DesignTree designTree) { switch (mm.Type) { case EntityMemberType.DataField: //判断是否是枚举 DataFieldModel dmm = mm as DataFieldModel; //if (dmm.DataType == EntityFieldType.Enum) //{ // if (string.IsNullOrEmpty(dmm.EnumModelID)) // typeString = "int"; // else // { // string[] sr = dmm.EnumModelID.Split('.'); // typeString = sr[0] + ".Enums." + sr[1]; // } //} if (dmm.DataType == EntityFieldType.EntityId) { typeString = "EntityId"; } else { typeString = dmm.DataType.GetValueType().FullName; //TODO:简化类型名称 } //系统存储分区键与sql存储的主键为只读 readOnly |= dmm.IsPartitionKey || dmm.IsPrimaryKey; if (dmm.AllowNull && (dmm.DataType != EntityFieldType.String && dmm.DataType != EntityFieldType.EntityId && dmm.DataType != EntityFieldType.Binary && typeString != "object")) { typeString += "?"; } break; case EntityMemberType.EntityRef: EntityRefModel rm = (EntityRefModel)mm; if (rm.IsAggregationRef) { typeString = TypeHelper.Type_EntityBase; } else { if (rm.RefModelIds.Count == 0) //Todo:待移除,因误删除模型引用项导致异常 { typeString = TypeHelper.Type_EntityBase; } else { var targetModelNode = designTree.FindModelNode(ModelType.Entity, rm.RefModelIds[0]); typeString = $"{targetModelNode.AppNode.Model.Name}.Entities.{targetModelNode.Model.Name}"; } } break; case EntityMemberType.EntitySet: { EntitySetModel sm = (EntitySetModel)mm; var targetModelNode = designTree.FindModelNode(ModelType.Entity, sm.RefModelId); typeString = $"EntityList<{targetModelNode.AppNode.Model.Name}.Entities.{targetModelNode.Model.Name}>"; readOnly = true; } break; case EntityMemberType.AggregationRefField: typeString = "object"; readOnly = true; break; //case EntityMemberType.Formula: //case EntityMemberType.Aggregate: //FormulaModel fmm = mm as FormulaModel; //typeString = TypeService.GetEntityFieldValueType(fmm.DataType).FullName; //readOnly = true; //break; case EntityMemberType.Tracker: throw ExceptionHelper.NotImplemented(); //GetEntityMemberTypeStringAndReadOnly( // (mm as TrackerModel).TargetMember, ref typeString, ref readOnly); //readOnly = true; //break; case EntityMemberType.AutoNumber: typeString = "string"; readOnly = true; break; //case EntityMemberType.ImageRef: //typeString = TypeHelper.Type_IImageSource; //readOnly = false; //break; default: typeString = "object"; break; } }
static TestHelper() { SysAppModel = new ApplicationModel("appbox", Consts.SYS, Consts.SYS_APP_ID); AdminPermissionModel = new PermissionModel(Consts.SYS_PERMISSION_ADMIN_ID, "Admin"); DeveloperPermissionModel = new PermissionModel(Consts.SYS_PERMISSION_DEVELOPER_ID, "Developer"); OrderStatusModel = new EnumModel(SYS_ENUM_MODEL_ID | (1 << IdUtil.MODELID_SEQ_OFFSET), "OrderStatus"); OrderStatusModel.Items.Add(new EnumModelItem("New", 0)); OrderStatusModel.Items.Add(new EnumModelItem("Paid", 1)); EmploeeModel = new EntityModel(Consts.SYS_EMPLOEE_MODEL_ID, Consts.EMPLOEE, EntityStoreType.StoreWithMvcc); var name = new DataFieldModel(EmploeeModel, Consts.NAME, EntityFieldType.String); EmploeeModel.AddSysMember(name, Consts.EMPLOEE_NAME_ID); var account = new DataFieldModel(EmploeeModel, Consts.ACCOUNT, EntityFieldType.String); account.AllowNull = true; EmploeeModel.AddSysMember(account, Consts.EMPLOEE_ACCOUNT_ID); var password = new DataFieldModel(EmploeeModel, Consts.PASSWORD, EntityFieldType.Binary); password.AllowNull = true; EmploeeModel.AddSysMember(password, Consts.EMPLOEE_PASSWORD_ID); //Add indexes var ui_account_pass = new EntityIndexModel(EmploeeModel, "UI_Account_Password", true, new FieldWithOrder[] { new FieldWithOrder(Consts.EMPLOEE_ACCOUNT_ID) }, new ushort[] { Consts.EMPLOEE_PASSWORD_ID }); EmploeeModel.SysStoreOptions.AddSysIndex(EmploeeModel, ui_account_pass, Consts.EMPLOEE_UI_ACCOUNT_ID); //测试用分区表 VehicleStateModel = new EntityModel(((ulong)Consts.SYS_APP_ID << 32) | 18, "VehicleState", EntityStoreType.StoreWithMvcc); var vid = new DataFieldModel(VehicleStateModel, "VehicleId", EntityFieldType.Int32); VehicleStateModel.AddMember(vid); var lng = new DataFieldModel(VehicleStateModel, "Lng", EntityFieldType.Float); VehicleStateModel.AddMember(lng); var lat = new DataFieldModel(VehicleStateModel, "Lat", EntityFieldType.Float); VehicleStateModel.AddMember(lat); var pks = new PartitionKey[1]; pks[0] = new PartitionKey() { MemberId = vid.MemberId, OrderByDesc = false }; VehicleStateModel.SysStoreOptions.SetPartitionKeys(VehicleStateModel, pks); //测试树状结构 ulong orgUnitModelId = ((ulong)Consts.SYS_APP_ID << 32) | 19; OrgUnitModel = new EntityModel(orgUnitModelId, "OrgUnit", EntityStoreType.StoreWithMvcc); var ouName = new DataFieldModel(OrgUnitModel, "Name", EntityFieldType.String); OrgUnitModel.AddSysMember(ouName, 1 << IdUtil.MEMBERID_SEQ_OFFSET); var parentId = new DataFieldModel(OrgUnitModel, "ParentId", EntityFieldType.EntityId, true); parentId.AllowNull = true; OrgUnitModel.AddSysMember(parentId, 2 << IdUtil.MEMBERID_SEQ_OFFSET); var parent = new EntityRefModel(OrgUnitModel, "Parent", orgUnitModelId, new ushort[] { parentId.MemberId }); parent.AllowNull = true; OrgUnitModel.AddSysMember(parent, 3 << IdUtil.MEMBERID_SEQ_OFFSET); var childs = new EntitySetModel(OrgUnitModel, "Childs", orgUnitModelId, parent.MemberId); OrgUnitModel.AddSysMember(childs, 4 << IdUtil.MEMBERID_SEQ_OFFSET); //----以下测试映射至SqlStore的实体--- SqlStoreModel = new DataStoreModel(DataStoreKind.Sql, "appbox.Store.PostgreSQL;appbox.Store.PgSqlStore", "DemoDB"); ulong cityModelId = ((ulong)Consts.SYS_APP_ID << 32) | 25; CityModel = new EntityModel(cityModelId, "City", new SqlStoreOptions(SqlStoreModel.Id)); var cityCode = new DataFieldModel(CityModel, "Code", EntityFieldType.Int32); CityModel.AddMember(cityCode); var cityName = new DataFieldModel(CityModel, "Name", EntityFieldType.String); CityModel.AddMember(cityName); var cityPerson = new DataFieldModel(CityModel, "Persons", EntityFieldType.Int32); CityModel.AddMember(cityPerson); var cityPk = new List <FieldWithOrder>(); cityPk.Add(new FieldWithOrder { MemberId = cityCode.MemberId, OrderByDesc = false }); CityModel.SqlStoreOptions.SetPrimaryKeys(CityModel, cityPk); ulong customerModelId = ((ulong)Consts.SYS_APP_ID << 32) | 26; CustomerModel = new EntityModel(customerModelId, "Customer", new SqlStoreOptions(SqlStoreModel.Id)); var customerId = new DataFieldModel(CustomerModel, "Id", EntityFieldType.Int32); CustomerModel.AddMember(customerId); var customerName = new DataFieldModel(CustomerModel, "Name", EntityFieldType.String); CustomerModel.AddMember(customerName); var customerCityId = new DataFieldModel(CustomerModel, "CityId", EntityFieldType.Int32, true); CustomerModel.AddMember(customerCityId); var customerCity = new EntityRefModel(CustomerModel, "City", cityModelId, new ushort[] { customerCityId.MemberId }); CustomerModel.AddMember(customerCity); var customerPk = new List <FieldWithOrder>(); customerPk.Add(new FieldWithOrder { MemberId = customerId.MemberId, OrderByDesc = false }); CustomerModel.SqlStoreOptions.SetPrimaryKeys(CustomerModel, customerPk); ulong orderModelId = ((ulong)Consts.SYS_APP_ID << 32) | 27; OrderModel = new EntityModel(orderModelId, "Order", new SqlStoreOptions(SqlStoreModel.Id)); var orderId = new DataFieldModel(OrderModel, "Id", EntityFieldType.Int32); OrderModel.AddMember(orderId); var orderCustomerId = new DataFieldModel(OrderModel, "CustomerId", EntityFieldType.Int32, true); OrderModel.AddMember(orderCustomerId); var orderCustomer = new EntityRefModel(OrderModel, "Customer", customerModelId, new ushort[] { orderCustomerId.MemberId }); OrderModel.AddMember(orderCustomer); var orderPk = new List <FieldWithOrder>(); orderPk.Add(new FieldWithOrder { MemberId = orderId.MemberId, OrderByDesc = false }); OrderModel.SqlStoreOptions.SetPrimaryKeys(OrderModel, orderPk); }
/// <summary> /// 初始化树状结构根目录列表 /// </summary> internal EntityList(EntitySetModel entitySetMember) //TODO:排序表达式 { EntitySetMember = entitySetMember ?? throw new ArgumentNullException(nameof(entitySetMember)); }
/// <summary> /// 初始化用于EntitySet成员的实体列表 /// </summary> internal EntityList(Entity parent, EntitySetModel entitySetMember) { Parent = parent ?? throw new ArgumentNullException(nameof(parent)); EntitySetMember = entitySetMember; }
/// <summary> /// 返回树状结构的实体集合 /// </summary> /// <param name="childrenMember">例:q.T["SubItems"]</param> /// <returns></returns> public async Task <EntityList> ToTreeListAsync(MemberExpression childrenMember) { //TODO:目前实现仅支持单一主键且为Guid的树状结构 Debug.Assert(ReferenceEquals(childrenMember.Owner, T)); var children = (EntitySetExpression)childrenMember; EntityModel model = await RuntimeContext.Current.GetModelAsync <EntityModel>(T.ModelID); EntitySetModel childrenModel = (EntitySetModel)model.GetMember(children.Name, true); EntityRefModel parentModel = (EntityRefModel)model.GetMember(childrenModel.RefMemberId, true); DataFieldModel parentIdModel = (DataFieldModel)model.GetMember(parentModel.FKMemberIds[0], true); TreeParentIDMember = (FieldExpression)T[parentIdModel.Name]; var pk = model.SqlStoreOptions.PrimaryKeys[0].MemberId; AddAllSelects(this, model, T, null); //TODO:加入自动排序 //if (!string.IsNullOrEmpty(setmodel.RefRowNumberMemberName)) //{ // SqlSortItem sort = new SqlSortItem(T[setmodel.RefRowNumberMemberName], SortType.ASC); // SortItems.Insert(0, sort); //} //如果没有设置任何条件,则设置默认条件为查询根级开始 if (Equals(null, Filter)) { Filter = TreeParentIDMember == null; } Purpose = QueryPurpose.ToEntityTreeList; EntityList list = new EntityList(childrenModel); var db = SqlStore.Get(model.SqlStoreOptions.StoreModelId); var dic = new Dictionary <Guid, Entity>(); //TODO: fix pk using var cmd = db.BuildQuery(this); using var conn = db.MakeConnection(); await conn.OpenAsync(); cmd.Connection = conn; Log.Debug(cmd.CommandText); using var reader = await cmd.ExecuteReaderAsync(); while (await reader.ReadAsync()) { Entity obj = FillEntity(model, reader); //设置obj本身的EntitySet成员为已加载,防止从数据库中再次加载 obj.InitEntitySetForLoad(childrenModel); var parentId = obj.GetGuidNullable(parentIdModel.MemberId); if (parentId.HasValue && dic.TryGetValue(parentId.Value, out Entity parent)) { parent.GetEntitySet(childrenModel.MemberId).Add(obj); } else { list.Add(obj); } dic.Add(obj.GetGuid(pk), obj); } return(list); }
private static void InitEntitySetForLoad(Entity owner, EntitySetModel entitySetModel, EntityList list) { //设置EntitySet成员, eg: Order.Items ref EntityMember m = ref owner.GetMember(entitySetModel.MemberId);