private DataProperty ParseCsdlComplexProperty(StructuralType parentType, JObject csdlProperty) { // Complex properties are never nullable ( per EF specs) // var isNullable = csdlProperty.nullable === 'true' || csdlProperty.nullable == null; var complexTypeName = GetClientTypeNameFromClrTypeName((String)csdlProperty["type"]); // can't set the name until we go thru namingConventions and these need the dp. var nameOnServer = (String)csdlProperty["name"]; var name = NamingConvention.ServerPropertyNameToClient(nameOnServer, parentType); var dp = parentType.GetDataProperty(name); if (dp == null) { MetadataStore.OnMetadataMismatch(parentType.Name, name, MetadataMismatchTypes.MissingCLRDataProperty); return(null); } if (!dp.IsComplexProperty) { var detail = "Defined as a ComplexProperty on the server but not on the client"; MetadataStore.OnMetadataMismatch(parentType.Name, name, MetadataMismatchTypes.InconsistentCLRPropertyDefinition, detail); return(null); } CheckProperty(dp, dp.ComplexType.Name, complexTypeName, "ComplexTypeName"); return(dp); }
internal void UpdateFromJNode(JNode jNode, bool isFromServer) { var complexTypeName = MetadataStore.GetStructuralTypeNameFromJNode(jNode, "complexTypeName", isFromServer); if (complexTypeName == null) { Check(DataType, DataType.FromName(jNode.Get <String>("dataType")), "DataType"); } else { Check(ComplexType.Name, complexTypeName, "ComplexTypeName"); } Check(this.IsScalar, jNode.Get <bool>("isScalar", true), "IsScalar"); IsNullable = jNode.Get <bool>("isNullable", true); if (DataType != null) { DefaultValue = jNode.Get("defaultValue", DataType.ClrType); if (DefaultValue == null && !IsNullable) { DefaultValue = DataType.DefaultValue; } } IsPartOfKey = jNode.Get <bool>("isPartOfKey", false); IsUnmapped = jNode.Get <bool>("isUnmapped", false); IsAutoIncrementing = jNode.Get <bool>("isAutoIncrementing", false); ConcurrencyMode = (ConcurrencyMode)Enum.Parse(typeof(ConcurrencyMode), jNode.Get <String>("concurrencyMode", ConcurrencyMode.None.ToString())); MaxLength = jNode.Get <int?>("maxLength"); // EnumType is best determined on the client and not from the server // so for now just ignore the 'enumType' // var enumTypeName = jNode.Get<String>("enumType"); _validators = new ValidatorCollection(jNode.GetJNodeArray("validators")); }
/// <summary> /// For internal use. /// </summary> /// <returns></returns> public override String GetResourcePath(MetadataStore metadataStore) { var dsq = this.DataServiceQuery; var requestUri = dsq.RequestUri.AbsoluteUri; var s2 = requestUri.Replace(__placeholderServiceName, ""); var resourceName = (String.IsNullOrEmpty(ResourceName)) ? metadataStore.GetDefaultResourceName(this.QueryableType) : ResourceName; if (String.IsNullOrEmpty(resourceName)) { throw new Exception("Cannot find a default resource name for CLR type: " + this.QueryableType.FullName); } // if any filter conditions var queryResource = s2.Replace(__placeholderResourceName + "()", resourceName); // if no filter conditions queryResource = queryResource.Replace(__placeholderResourceName, resourceName); // TODO: Hack to avoid DataServiceQuery from inferring the entity key queryResource = queryResource.Replace("$filter=true%20and%20", "$filter="); queryResource = queryResource.Replace("$filter=true", ""); // HACK queryResource = RewriteResourcePath(queryResource); return(queryResource); }
public static void __Reset() { lock (__lock) { var x = __instance._probedAssemblies; __instance = new MetadataStore(); // __instance.ProbeAssemblies(x.ToArray()); } }
internal EntityKey(JNode jn, MetadataStore metadataStore) { var etName = jn.Get <String>("entityType"); EntityType = metadataStore.GetEntityType(etName); ClrType = EntityType.ClrType; // coerce the incoming data Values = jn.GetArray("values", EntityType.KeyProperties.Select(kp => kp.ClrType)).ToArray(); }
private NavigationProperty ParseCsdlNavigationProperty(EntityType parentType, JObject csdlProperty) { var association = GetAssociation(csdlProperty); var toRoleVal = (String)csdlProperty["toRole"]; var fromRoleVal = (String)csdlProperty["fromRole"]; var nameOnServer = (String)csdlProperty["name"]; var toEnd = ToEnumerable(association["end"]).FirstOrDefault(end => (String)end["role"] == toRoleVal); var isScalar = (String)toEnd["multiplicity"] != "*"; var dataEtName = GetClientTypeNameFromClrTypeName((String)toEnd["type"]); var constraintVal = association["referentialConstraint"]; if (constraintVal == null) { return(null); // TODO: Revisit this later - right now we just ignore many-many and assocs with missing constraints. // // if (association.end[0].multiplicity == "*" && association.end[1].multiplicity == "*") { // // many to many relation // ??? // } else { // throw new Error("Foreign Key Associations must be turned on for this model"); // } } var name = NamingConvention.ServerPropertyNameToClient(nameOnServer, parentType); var np = parentType.GetNavigationProperty(name); if (np == null) { MetadataStore.OnMetadataMismatch(parentType.Name, name, MetadataMismatchType.MissingCLRNavigationProperty); return(null); } CheckProperty(np, np.EntityType.Name, dataEtName, "EntityTypeName"); CheckProperty(np, np.IsScalar, isScalar, "IsScalar"); np.AssociationName = (String)association["name"]; var principal = constraintVal["principal"]; var dependent = constraintVal["dependent"]; var propRefs = ToEnumerable(dependent["propertyRef"]); var fkNamesOnServer = propRefs.Select(pr => (String)pr["name"]).ToSafeList(); var fkNames = fkNamesOnServer.Select(fkn => NamingConvention.ServerPropertyNameToClient(fkn, parentType)); if (fromRoleVal == (String)principal["role"]) { np.SetInvFkNames(fkNames); } else { np.SetFkNames(fkNames); } return(np); }
private Tuple <EntityKey, EntityKey> ToEntityKeys(KeyMapping keyMapping, MetadataStore metadataStore) { var serverTypeInfo = TypeNameInfo.FromClrTypeName(keyMapping.EntityTypeName); var clientEntityTypeName = serverTypeInfo.ToClient(metadataStore).StructuralTypeName; var et = metadataStore.GetEntityType(clientEntityTypeName); var oldKey = new EntityKey(et, keyMapping.TempValue); var newKey = new EntityKey(et, keyMapping.RealValue); return(Tuple.Create(oldKey, newKey)); }
public void ProcessMetadata(MetadataStore metadataStore, String jsonMetadata) { _metadataStore = metadataStore; var json = (JObject)JsonConvert.DeserializeObject(jsonMetadata); _schema = json["schema"]; _namespace = (String)_schema["namespace"]; var mapping = (String)_schema["cSpaceOSpaceMapping"]; if (mapping != null) { var tmp = (JArray)JsonConvert.DeserializeObject(mapping); _cSpaceOSpaceMap = tmp.ToDictionary(v => (String)v[0], v => (String)v[1]); } var entityTypes = ToEnumerable(_schema["entityType"]).Cast <JObject>() .Select(ParseCsdlEntityType).Where(et => et != null).ToList(); // fixup all related nav props. entityTypes.ForEach(et => { if (et.KeyProperties.Count == 0) { throw new Exception("Unable to locate key property for EntityType: " + et.Name); } et.UpdateNavigationProperties(); }); var complexTypes = ToEnumerable(_schema["complexType"]).Cast <JObject>() .Select(ParseCsdlComplexType).Where(ct => ct != null).ToList(); var entityContainer = _schema["entityContainer"]; if (entityContainer != null) { var entitySets = ToEnumerable(entityContainer["entitySet"]).Cast <JObject>().ToList(); entitySets.ForEach(es => { var clientEtName = GetClientTypeNameFromClrTypeName((String)es["entityType"]); var entityType = _metadataStore.GetEntityType(clientEtName, true); if (entityType != null) { var resourceName = (String)es["name"]; _metadataStore.SetResourceName(resourceName, entityType, true); } }); } if (_errorMessages.Any()) { throw new Exception("Metadata errors encountered: \n" + _errorMessages.Select(em => em.Text).ToAggregateString("\n")); } }
internal void UpdateFromJNode(JNode jNode, bool isFromServer) { var etName = MetadataStore.GetStructuralTypeNameFromJNode(jNode, "entityTypeName", isFromServer); Check(EntityType.Name, etName, "EntityTypeName"); IsScalar = jNode.Get <bool>("isScalar", true); AssociationName = jNode.Get <String>("associationName"); _validators = new ValidatorCollection(jNode.GetJNodeArray("validators")); SetFkNames(jNode.GetArray <String>(isFromServer ? "foreignKeyNamesOnServer" : "foreignKeyNames")); SetInvFkNames(jNode.GetArray <String>(isFromServer ? "invForeignKeyNamesOnServer" : "invForeignKeyNames")); // custom }
private EntityType ParseCsdlEntityType(JObject csdlEntityType) { var abstractVal = (String)csdlEntityType["abstract"]; var baseTypeVal = (String)csdlEntityType["baseType"]; var shortNameVal = (String)csdlEntityType["name"]; var isAbstract = abstractVal == "true"; var etName = GetClientTypeNameFromShortName(shortNameVal); var entityType = MetadataStore.GetEntityType(etName, true); if (entityType == null) { MetadataStore.OnMetadataMismatch(etName, null, MetadataMismatchType.MissingCLREntityType); return(null); } entityType.IsAbstract = isAbstract; var baseKeyNamesOnServer = new List <string>(); if (baseTypeVal != null) { var baseEtName = GetClientTypeNameFromClrTypeName(baseTypeVal); var baseEntityType = MetadataStore.GetEntityType(baseEtName, true); if (baseEntityType == null) { var detail = String.Format("BaseType of: '{0}' not found", baseEtName); MetadataStore.OnMetadataMismatch(etName, null, MetadataMismatchType.InconsistentCLRTypeDefinition, detail); return(null); } CheckStructuralType(entityType, entityType.BaseEntityType, baseEntityType, "BaseEntityType"); entityType.AutoGeneratedKeyType = baseEntityType.AutoGeneratedKeyType; baseKeyNamesOnServer = baseEntityType.KeyProperties.Select(dp => dp.NameOnServer).ToList(); } var keyVal = csdlEntityType["key"]; var keyNamesOnServer = keyVal == null ? new List <String>() : ToEnumerable(keyVal["propertyRef"]).Select(x => (String)x["name"]).ToList(); keyNamesOnServer.AddRange(baseKeyNamesOnServer); ToEnumerable(csdlEntityType["property"]).ForEach(csdlDataProp => { ParseCsdlDataProperty(entityType, (JObject)csdlDataProp, keyNamesOnServer); }); ToEnumerable(csdlEntityType["navigationProperty"]).ForEach(csdlNavProp => { ParseCsdlNavigationProperty(entityType, (JObject)csdlNavProp); }); return(entityType); }
internal void Check(Object v1, Object v2, String name) { if (v1 == null && v2 == null) { return; } if (Object.Equals(v1, v2)) { return; } var msg = String.Format("EntityType metadata mismatch. EntityType: '{0}'. Metadata property: '{1}'. Client value: '{2}', Server value: '{3}'", this.Name, name, (v1 ?? "").ToString(), (v2 ?? "").ToString()); MetadataStore.AddMessage(msg, MessageType.Error); }
internal void CheckStructuralType(StructuralType stType, Object v1, Object v2, String name) { if (v1 == null && v2 == null) { return; } if (Object.Equals(v1, v2)) { return; } var detail = String.Format("Type property: {0}, Client value: '{1}', Server value: '{2}'", name, (v1 ?? "").ToString(), (v2 ?? "").ToString()); MetadataStore.OnMetadataMismatch(stType.Name, null, MetadataMismatchType.InconsistentCLRTypeDefinition, detail); }
internal void Check(Object v1, Object v2, String name) { if (v1 == null && v2 == null) { return; } if (Object.Equals(v1, v2)) { return; } var msg = String.Format("EntityType metadata mismatch. EntityType: '{0}'. Metadata property: '{1}'. Client value: '{2}', Server value: '{3}'", this.Name, name, (v1 ?? "").ToString(), (v2 ?? "").ToString()); MetadataStore.OnMetadataMismatch(Name, null, MetadataMismatchTypes.InconsistentCLRTypeDefinition, msg); }
internal void CheckProperty(StructuralProperty prop, Object v1, Object v2, String name) { if (v1 == null && v2 == null) { return; } if (Object.Equals(v1, v2)) { return; } var detail = String.Format("Client value: '{0}', Server value: '{1}'", (v1 ?? "").ToString(), (v2 ?? "").ToString()); MetadataStore.OnMetadataMismatch(prop.ParentType.Name, prop.Name, MetadataMismatchType.InconsistentCLRPropertyDefinition, detail); }
internal override void UpdateFromJNode(JNode jNode, bool isFromServer) { var name = this.MetadataStore.GetStructuralTypeNameFromJNode(jNode, isFromServer); var et = MetadataStore.GetEntityType(name); var baseTypeName = this.MetadataStore.GetStructuralTypeNameFromJNode(jNode, "baseTypeName", isFromServer); if (baseTypeName != null) { Check(et.BaseEntityTypeName, baseTypeName, "BaseEntityType"); } // For now this doesn't need to match et.IsAbstract = jNode.Get <bool>("isAbstract"); et.AutoGeneratedKeyType = jNode.GetEnum <AutoGeneratedKeyType>("autoGeneratedKeyType"); var drn = jNode.Get <String>("defaultResourceName"); if (drn != null) { MetadataStore.SetResourceName(drn, et, true); } jNode.GetJNodeArray("dataProperties").ForEach(jn => { var dpName = GetPropertyNameFromJNode(jn); var dp = et.GetDataProperty(dpName); if (dp == null) { MetadataStore.OnMetadataMismatch(name, dpName, MetadataMismatchTypes.MissingCLRDataProperty); return; } dp.UpdateFromJNode(jn, isFromServer); }); jNode.GetJNodeArray("navigationProperties").ForEach(jn => { var npName = GetPropertyNameFromJNode(jn); var np = et.GetNavigationProperty(npName); if (np == null) { MetadataStore.OnMetadataMismatch(name, npName, MetadataMismatchTypes.MissingCLRNavigationProperty); return; } np.UpdateFromJNode(jn, isFromServer); }); et._validators = new ValidatorCollection(jNode.GetJNodeArray("validators")); // custom // next 2 lines are only needed if importing 'native' breeze metadata. if (et.KeyProperties.Count == 1 && et.AutoGeneratedKeyType != AutoGeneratedKeyType.None) { et.KeyProperties.First().IsAutoIncrementing = true; } }
private ComplexType ParseCsdlComplexType(JObject csdlComplexType) { var nameVal = (String)csdlComplexType["name"]; var clientTypeName = GetClientTypeNameFromShortName(nameVal); var complexType = MetadataStore.GetComplexType(clientTypeName); if (complexType == null) { MetadataStore.OnMetadataMismatch(clientTypeName, null, MetadataMismatchType.MissingCLRComplexType); return(null); } ToEnumerable(csdlComplexType["property"]) .ForEach(prop => ParseCsdlDataProperty(complexType, (JObject)prop, null)); return(complexType); }
/// <summary> /// For internal use. /// </summary> /// <returns></returns> public override String GetResourcePath(MetadataStore metadataStore) { var resourceName = (String.IsNullOrEmpty(ResourceName)) ? metadataStore.GetDefaultResourceName(this.QueryableType) : ResourceName; if (String.IsNullOrEmpty(resourceName)) { throw new Exception("Cannot find a default resource name for CLR type: " + this.QueryableType.FullName); } if (Configuration.Instance.QueryUriStyle == QueryUriStyle.JSON) { return(GetJsonResourcePath(resourceName)); } else { return(GetOdataResourcePath(resourceName)); } }
public ClrTypeMap(MetadataStore metadataStore) { _metadataStore = metadataStore; }
private DataProperty ParseCsdlSimpleProperty(StructuralType parentType, JObject csdlProperty, List <String> keyNamesOnServer) { var typeVal = (String)csdlProperty["type"]; var nameVal = (String)csdlProperty["name"]; var nullableVal = (String)csdlProperty["nullable"]; var maxLengthVal = (String)csdlProperty["maxLength"]; var concurrencyModeVal = (String)csdlProperty["concurrencyMode"]; var dataType = DataType.FromEdmType(typeVal); if (dataType == DataType.Undefined) { parentType.Warnings.Add("Unable to recognize DataType for property: " + nameVal + " DateType: " + typeVal); } var dpName = NamingConvention.ServerPropertyNameToClient(nameVal, parentType); var dp = parentType.GetDataProperty(dpName); if (dp == null) { MetadataStore.OnMetadataMismatch(parentType.Name, dpName, MetadataMismatchType.MissingCLRDataProperty); return(null); } var isNullable = nullableVal == "true" || nullableVal == null; var entityType = parentType as EntityType; bool isPartOfKey = false; bool isAutoIncrementing = false; if (entityType != null) { isPartOfKey = keyNamesOnServer != null && keyNamesOnServer.IndexOf(nameVal) >= 0; if (isPartOfKey && entityType.AutoGeneratedKeyType == AutoGeneratedKeyType.None) { if (IsIdentityProperty(csdlProperty)) { isAutoIncrementing = true; entityType.AutoGeneratedKeyType = AutoGeneratedKeyType.Identity; } } } Object defaultValue; var rawDefaultValue = csdlProperty["defaultValue"]; if (rawDefaultValue == null) { defaultValue = isNullable ? null : dataType.DefaultValue; } else { defaultValue = rawDefaultValue.ToObject(dataType.ClrType); } // TODO: nit - don't set maxLength if null; var maxLength = (maxLengthVal == null || maxLengthVal == "Max") ? (Int64?)null : Int64.Parse(maxLengthVal); var concurrencyMode = concurrencyModeVal == "fixed" ? ConcurrencyMode.Fixed : ConcurrencyMode.None; CheckProperty(dp, dp.DataType, dataType, "DataType"); CheckProperty(dp, dp.IsScalar, true, "IsScalar"); dp.IsPartOfKey = isPartOfKey; dp.IsNullable = isNullable; dp.MaxLength = maxLength; dp.DefaultValue = defaultValue; // fixedLength: fixedLength, dp.ConcurrencyMode = concurrencyMode; dp.IsAutoIncrementing = isAutoIncrementing; if (dataType == DataType.Undefined) { dp.RawTypeName = typeVal; } return(dp); }
public override bool CanConvert(Type objectType) { return(MetadataStore.IsStructuralType(objectType)); }
// TODO: also need a ComplexTypeBuilder; public EntityTypeBuilder(MetadataStore metadataStore, bool checkOnly = false) : base(metadataStore) { EntityType = MetadataStore.GetEntityType(typeof(TEntity)); CheckOnly = checkOnly; }
public ComplexType(MetadataStore metadataStore) : base(metadataStore) { }
public TypeNameInfo ToServer(MetadataStore metadataStore) { return(metadataStore.NamingConvention.ClientTypeNameToServer(this)); }
public StructuralTypeBuilder(MetadataStore metadataStore) { MetadataStore = metadataStore; }
internal EntityType(MetadataStore metadataStore) : base(metadataStore) { _selfAndSubEntityTypes.Add(this); }
/// <summary> /// Constructs a new EntityKey. Each entity within an EntityManager will have a unique EntityKey. /// </summary> public EntityKey(Type clrType, MetadataStore metadataStore, params Object[] values) { ClrType = clrType; EntityType = metadataStore.GetEntityType(ClrType); InitializeValues(values, true); }
public StructuralType(MetadataStore metadataStore) { Warnings = new List <string>(); MetadataStore = metadataStore; }