public AvatarDataItem(int avatarID, int level = 1, int star = 0) { AvatarMetaData avatarMetaDataByKey = AvatarMetaDataReader.GetAvatarMetaDataByKey(avatarID); ClassMetaData classMetaDataByKey = ClassMetaDataReader.GetClassMetaDataByKey(avatarMetaDataByKey.classID); this.Init(avatarID, avatarMetaDataByKey, classMetaDataByKey, null, null, level, star); }
public XmlMapper() { if (MetaDataCache.Contains <T>()) { ClassMetaData metaData = MetaDataCache.Get <T>(); if (metaData.ClassAttributeContext.ContainsAttribute <XmlMapperAttribute>()) { xmlMapperAttribute = metaData.ClassAttributeContext.GetAttribute <XmlMapperAttribute>(); } } }
public virtual int CreateTable(ClassMetaData metadata) { var tableName = metadata.MappingTable; var properties = metadata.Properties; var queryBuilder = new StringBuilder("CREATE TABLE IF NOT EXISTS " + tableName); var createDefinitionBuilder = new StringBuilder("("); foreach (var pair in properties) { var mappingInfo = pair.Value; var propertyInfo = mappingInfo.PropertyInfo; var columnType = GetType(propertyInfo); var columnName = mappingInfo.MappingField; createDefinitionBuilder.Append("`" + columnName + "`" + " " + columnType); if (mappingInfo.NotNull) { createDefinitionBuilder.Append(" NOT NULL"); } if (mappingInfo.DefaultValue != null) { createDefinitionBuilder.Append(" DEFAULT " + mappingInfo.DefaultValue); } if (mappingInfo.AutoIncrement) { createDefinitionBuilder.Append(" AUTO_INCREMENT"); } if (propertyInfo.Name != "Id" && mappingInfo.Unique) { createDefinitionBuilder.Append(" UNIQUE"); } createDefinitionBuilder.Append(", "); if (mappingInfo.Indexing != PropertyAttribute.NO_INDEX) { createDefinitionBuilder.Append("INDEX (`" + columnName + "`), "); } } createDefinitionBuilder.Append(" PRIMARY KEY (Id) "); var createDefinitionBuilderStr = createDefinitionBuilder.ToString(); queryBuilder.Append(createDefinitionBuilderStr + ")"); using (var connection = connectionManager.GetUpdateConnection()) { var command = connection.CreateCommand(); command.CommandText = queryBuilder.ToString(); return(command.ExecuteNonQuery()); } }
public virtual int CreateTable(ClassMetaData metadata) { var tableName = metadata.MappingTable; var properties = metadata.Properties; var queryBuilder = new StringBuilder("CREATE TABLE IF NOT EXISTS " + tableName); var createDefinitionBuilder = new StringBuilder("("); foreach (var pair in properties) { var mappingInfo = pair.Value; var propertyInfo = mappingInfo.PropertyInfo; var columnType = GetType(propertyInfo); var columnName = mappingInfo.MappingField; createDefinitionBuilder.Append("`" + columnName + "`" + " " + columnType); if (mappingInfo.NotNull) { createDefinitionBuilder.Append(" NOT NULL"); } if (mappingInfo.DefaultValue != null) { createDefinitionBuilder.Append(" DEFAULT " + mappingInfo.DefaultValue); } if (mappingInfo.AutoIncrement) { createDefinitionBuilder.Append(" AUTO_INCREMENT"); } if (propertyInfo.Name != "Id" && mappingInfo.Unique) { createDefinitionBuilder.Append(" UNIQUE"); } createDefinitionBuilder.Append(", "); if (mappingInfo.Indexing != PropertyAttribute.NO_INDEX) { createDefinitionBuilder.Append("INDEX (`" + columnName + "`), "); } } createDefinitionBuilder.Append(" PRIMARY KEY (Id) "); var createDefinitionBuilderStr = createDefinitionBuilder.ToString(); queryBuilder.Append(createDefinitionBuilderStr + ")"); using (var connection = connectionManager.GetUpdateConnection()) { var command = connection.CreateCommand(); command.CommandText = queryBuilder.ToString(); return command.ExecuteNonQuery(); } }
private void UpdateRelations(IDbObject o, ClassMetaData classMetadata, DbConnection connection, IDictionary <IDbObject, long> objectGraph) { var relationProperties = classMetadata.RelationProperties; foreach (var relation in relationProperties.Values) { var propertyInfo = relation.PropertyInfo; var mappingTable = relation.MappingTable; var persistentRelation = new List <long>(); var command = connection.CreateCommand(); string queryField; if (relation.RelationKind == RelationInfo.RELATION_N_N) { command.CommandText = "SELECT `" + relation.PartnerKey + "` FROM " + mappingTable + " WHERE `" + relation.OriginalKey + "` = " + o.Id; queryField = relation.PartnerKey; } else { command.CommandText = "SELECT Id FROM " + mappingTable + " WHERE `" + relation.OriginalKey + "` = " + o.Id; queryField = "Id"; } using (var reader = command.ExecuteReader()) { while (reader.Read()) { persistentRelation.Add(reader.GetInt64(reader.GetOrdinal(queryField))); } } if (relation.RelationKind == RelationInfo.RELATION_1_1) { Update11Relation(objectGraph, o, connection, relation, command, mappingTable, persistentRelation, propertyInfo); } else if (relation.RelationKind == RelationInfo.RELATION_1_N) { Update1NRelation(objectGraph, o, connection, relation, command, mappingTable, persistentRelation, propertyInfo); } else if (relation.RelationKind == RelationInfo.RELATION_N_N) { UpdateNNRelation(objectGraph, o, connection, relation, command, mappingTable, persistentRelation, propertyInfo); } } }
private void UpdateRelations(IDbObject o, ClassMetaData classMetadata, DbConnection connection, IDictionary<IDbObject, long> objectGraph) { var relationProperties = classMetadata.RelationProperties; foreach (var relation in relationProperties.Values) { var propertyInfo = relation.PropertyInfo; var mappingTable = relation.MappingTable; var persistentRelation = new List<long>(); var command = connection.CreateCommand(); string queryField; if (relation.RelationKind == RelationInfo.RELATION_N_N) { command.CommandText = "SELECT `" + relation.PartnerKey + "` FROM " + mappingTable + " WHERE `" + relation.OriginalKey + "` = " + o.Id; queryField = relation.PartnerKey; } else { command.CommandText = "SELECT Id FROM " + mappingTable + " WHERE `" + relation.OriginalKey + "` = " + o.Id; queryField = "Id"; } using (var reader = command.ExecuteReader()) { while (reader.Read()) { persistentRelation.Add(reader.GetInt64(reader.GetOrdinal(queryField))); } } if (relation.RelationKind == RelationInfo.RELATION_1_1) { Update11Relation(objectGraph, o, connection, relation, command, mappingTable, persistentRelation, propertyInfo); } else if (relation.RelationKind == RelationInfo.RELATION_1_N) { Update1NRelation(objectGraph, o, connection, relation, command, mappingTable, persistentRelation, propertyInfo); } else if (relation.RelationKind == RelationInfo.RELATION_N_N) { UpdateNNRelation(objectGraph, o, connection, relation, command, mappingTable, persistentRelation, propertyInfo); } } }
private void Init(int avatarID, AvatarMetaData metaData, ClassMetaData classMetaData, AvatarStarMetaData starMetaData, AvatarLevelMetaData levelMetaData, int level, int star) { this.avatarID = avatarID; this.equipsMap = new Dictionary <EquipmentSlot, StorageDataItemBase>(); foreach (EquipmentSlot slot in EQUIP_SLOTS) { this.equipsMap.Add(slot, null); } this._metaData = metaData; this._classMetaData = classMetaData; this._starMetaData = starMetaData; this._levelMetaData = levelMetaData; this.Initialized = false; this.UnLocked = false; this.SetupDefaultSkillList(); this.star = (star != 0) ? star : this._metaData.unlockStar; this.OnStarUpdate(this.star, this.star); this.level = level; this.OnLevelUpdate(this.level, this.level); this._unlockNeedFragment = this.CalculateUnlockNeedFragment(); }
public EntityInfo(Type entityClass, string preferredName, IReadOnlyList <string> mapNames, ClassMetaData entityData, IReadOnlyDictionary <string, IMemberAccessor> keyValues, IReadOnlyList <IMemberAccessor> persistedMembers) { EntityClass = entityClass ?? throw new ArgumentNullException(nameof(entityClass)); if (!entityClass.IsClass) { throw new ArgumentException("Entity class type must be a class", nameof(entityClass)); } if (entityClass.IsAbstract) { throw new ArgumentException("Entity class must be a concrete type", nameof(entityClass)); } PreferredName = preferredName ?? throw new ArgumentNullException(nameof(preferredName)); MapNames = mapNames ?? throw new ArgumentNullException(nameof(mapNames)); EntityData = entityData ?? throw new ArgumentNullException(nameof(entityData)); KeyValues = keyValues ?? throw new ArgumentNullException(nameof(keyValues)); PersistedMembers = persistedMembers ?? throw new ArgumentNullException(nameof(persistedMembers)); }
private void LoadData(Type type) { PropertyMetaData[] data = null; if (type.BaseType != typeof(Enum)) { data = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Select(p => new PropertyMetaData { Name = p.Name, Type = Toolkit.GetTypeName(p.PropertyType), OriginalTypeIdentity = GetTypeInfoURL(p.PropertyType), IsCLRType = Toolkit.IsFrameworkType(p.PropertyType), Description = GetPropertyDescription(p), }) .ToArray(); } else { data = type.GetFields().Skip(1).Select(p => new PropertyMetaData { Name = p.Name, Type = Toolkit.GetTypeName(p.FieldType), OriginalTypeIdentity = GetTypeInfoURL(p.FieldType), IsCLRType = Toolkit.IsFrameworkType(p.FieldType), Description = GetFieldDescription(p), }) .ToArray(); } var obj = new ClassMetaData { Name = TargetType.Name, Description = GetTypeDescription(), Properties = data, }; DataSource = obj; }
private void InitDataAccessLayer(DataTable TableConnections, bool isTraceMode, bool hasMetaData) { _dtConnections = TableConnections; _intActiveIndex = -1; InitConnections(); _lstQueries = new List<Query>(); _isTraceMode = isTraceMode; _hasMetaData = hasMetaData; if (hasMetaData) { _metaData = new ClassMetaData(this); } }
private object ToObject(object instance, XmlNode xmlNode, string nodeName = null) { Type type = instance.GetType(); ClassMetaData classMetaData = MetaDataCache.Get(type); XmlMappingOperation xmlMappingOperation = xmlMapperAttribute.MappingOperation; if (classMetaData.ClassAttributeContext.ContainsAttribute <XmlMapperAttribute>()) { XmlMapperAttribute xmlMapper = classMetaData.ClassAttributeContext.GetAttribute <XmlMapperAttribute>(); xmlMappingOperation = xmlMapper.MappingOperation; if (nodeName == null) { nodeName = xmlMapper.ParentNodeName; xmlNode = xmlNode.SelectSingleNode($"//{nodeName}"); } } foreach (IFieldPropertyInfo property in classMetaData.Properties) { string propertyName = property.Name; System.Type propertyType = property.Type; if (classMetaData.HasPropertyAttributeContext <PropertyAttributeContext>(property)) { PropertyAttributeContext propertyAttributeContext = classMetaData.GetAttributeContextForProperty <PropertyAttributeContext>(property); if (propertyAttributeContext.HasIgnoreAttribute) { continue; } propertyName = propertyAttributeContext.HasNameAttribute ? propertyAttributeContext.NameAttribute.Name : propertyName; } object propertyValue = GetNodeValue(xmlMappingOperation, xmlNode, nodeName, propertyName); if (classMetaData.HasPropertyAttributeContext <XmlPropertyAttributeContext>(property)) { XmlPropertyAttributeContext xmlPropertyAttributeContext = classMetaData.GetAttributeContextForProperty <XmlPropertyAttributeContext>(property); if (xmlPropertyAttributeContext.HasXmlListAttribute) { XmlListAttribute xmlList = xmlPropertyAttributeContext.XmlListAttribute; XmlNodeList results = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/{xmlList.NodeName}"); if (results.Count > 0) { object childInstance = CollectionXmlNodeListToObject(results, propertyType); property.SetValue(instance, childInstance); } continue; } else if (xmlPropertyAttributeContext.HasXmlDictionaryAttribute) { XmlDictionaryAttribute xmlList = xmlPropertyAttributeContext.XmlDictionaryAttribute; XmlNodeList results = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/*"); if (results.Count > 0) { object childInstance = DictionaryXmlNodeListToObject(results, propertyType); property.SetValue(instance, childInstance); } continue; } else if (xmlPropertyAttributeContext.HasXmlFlattenHierarchyAttribute) { object childInstance = Activator.CreateInstance(propertyType); childInstance = ToObject(childInstance, xmlNode, nodeName); property.SetValue(instance, childInstance); continue; } else if (xmlPropertyAttributeContext.HasXmlPropertyConverterAttribute && propertyValue != null) { XmlPropertyConverterAttribute converter = xmlPropertyAttributeContext.XmlPropertyConverterAttribute; try { propertyValue = converter.ConvertToSourceType(propertyValue); } catch (Exception ex) { throw new XmlPropertyConverterException("XmlPropertyConverter threw an exception", ex); } } } else { if (propertyType.IsDictionary()) { XmlNodeList results = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/*"); if (results.Count > 0) { object childInstance = DictionaryXmlNodeListToObject(results, propertyType); property.SetValue(instance, childInstance); } continue; } else if (propertyType.IsCollection()) { string listItemNodeName = propertyType.GenericTypeArguments[0].Name; XmlNodeList results = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/{listItemNodeName}"); if (results.Count > 0) { object childInstance = CollectionXmlNodeListToObject(results, propertyType); property.SetValue(instance, childInstance); } continue; } if (propertyType.IsClass && MetaDataCache.Contains(propertyType)) { // TODO: Dont think this will work object childInstance = Activator.CreateInstance(propertyType); XmlNode results = xmlNode.SelectSingleNode($"//{nodeName}/{propertyName}"); if (results != null) { childInstance = ToObject(childInstance, results, propertyName); property.SetValue(instance, childInstance); } continue; } } if (propertyValue == null) { continue; } property.SetValue(instance, UniversalTypeConverter.Convert(propertyValue, propertyType)); } return(instance); }
private XElement ToXml(object instance, Type type, XElement element = null, string nodeName = null) { ClassMetaData classMetaData = MetaDataCache.Get(type); XmlMappingOperation xmlMappingOperation = xmlMapperAttribute.MappingOperation; bool ignoreNulls = xmlMapperAttribute.IgnoreNulls; if (classMetaData.ClassAttributeContext.ContainsAttribute <XmlMapperAttribute>()) { XmlMapperAttribute xmlMapper = classMetaData.ClassAttributeContext.GetAttribute <XmlMapperAttribute>(); xmlMappingOperation = xmlMapper.MappingOperation; ignoreNulls = xmlMapper.IgnoreNulls; if (element == null) { element = new XElement(xmlMapper.ParentNodeName); nodeName = xmlMapper.ParentNodeName; } } //element.Name = nodeName; foreach (IFieldPropertyInfo property in classMetaData.Properties) { string propertyName = property.Name; object propertyValue = property.GetValue(instance); Type propertyType = property.Type; if (propertyValue == null && ignoreNulls) { continue; } if (classMetaData.HasPropertyAttributeContext <PropertyAttributeContext>(property)) { PropertyAttributeContext propertyAttributeContext = classMetaData.GetAttributeContextForProperty <PropertyAttributeContext>(property); if (propertyAttributeContext.HasIgnoreAttribute) { continue; } propertyName = propertyAttributeContext.HasNameAttribute ? propertyAttributeContext.NameAttribute.Name : propertyName; } if (classMetaData.HasPropertyAttributeContext <XmlPropertyAttributeContext>(property)) { XmlPropertyAttributeContext xmlPropertyAttributeContext = classMetaData.GetAttributeContextForProperty <XmlPropertyAttributeContext>(property); if (xmlPropertyAttributeContext.HasXmlPropertyConverterAttribute && propertyValue != null) { XmlPropertyConverterAttribute converter = xmlPropertyAttributeContext.XmlPropertyConverterAttribute; try { propertyValue = converter.ConvertToDestinationType(propertyValue); AddToXElement(element, xmlMappingOperation, propertyName, propertyValue); continue; } catch (Exception ex) { throw new XmlPropertyConverterException("XmlPropertyConverter threw an exception", ex); } } else if (xmlPropertyAttributeContext.HasXmlDictionaryAttribute) { XmlDictionaryAttribute xmlDictionary = xmlPropertyAttributeContext.XmlDictionaryAttribute; element.Add(DictionaryToXElement(propertyValue, propertyName)); continue; } else if (xmlPropertyAttributeContext.HasXmlListAttribute) { XmlListAttribute xmlList = xmlPropertyAttributeContext.XmlListAttribute; element.Add(CollectionToXElement(propertyValue, propertyName, xmlList.NodeName)); continue; } else if (xmlPropertyAttributeContext.HasXmlFlattenHierarchyAttribute) { element = ToXml(propertyValue, propertyType, element, propertyName); continue; } } else { if (propertyType.IsDictionary()) { element.Add(DictionaryToXElement(propertyValue, propertyName)); continue; } else if (propertyType.IsCollection()) { element.Add(CollectionToXElement(propertyValue, propertyName, propertyType.GenericTypeArguments[0].Name)); continue; } else if (propertyType.IsClass && MetaDataCache.Contains(propertyType)) { XElement propertyElement = new XElement(propertyName); propertyElement = ToXml(propertyValue, propertyType, propertyElement, propertyName); element.Add(propertyElement); continue; } } AddToXElement(element, xmlMappingOperation, propertyName, propertyValue); } return(element); }
/// <summary> /// Gets all of the members of the given type marked as needing to be persisted /// </summary> /// <param name="type"></param> /// <param name="metaData"></param> /// <returns></returns> private static IReadOnlyList <IMemberAccessor> GetPersistAccessorsForType(Type type, ClassMetaData metaData) { //Use case insensitive comparison to match the engine var persistedMembers = new List <IMemberAccessor>(); VisitAccessibleMembers <PersistAttribute>(type, (_, info) => { var accessor = metaData.GetAccessor(info); if (accessor.CanRead && accessor.CanWrite) { persistedMembers.Add(accessor); } else { var messageBuilder = new StringBuilder(); messageBuilder.Append("Warning: cannot persist member ") .Append(type.FullName) .Append(".") .Append(accessor.Info.Name) .Append(" because it is not "); if (!accessor.CanRead) { messageBuilder.Append("readable "); if (!accessor.CanWrite) { messageBuilder.Append(" and not"); } } if (!accessor.CanWrite) { messageBuilder.Append("writable"); } Log.Message(messageBuilder.ToString()); } } ); return(persistedMembers); }
/// <summary> /// Gets all of the members of the given type marked as being a keyvalue /// </summary> /// <param name="type"></param> /// <param name="metaData"></param> /// <returns></returns> private static IReadOnlyDictionary <string, IMemberAccessor> GetKeyValueAccessorsForType(Type type, ClassMetaData metaData) { //Use case insensitive comparison to match the engine var keyValues = new Dictionary <string, IMemberAccessor>(StringComparer.OrdinalIgnoreCase); VisitAccessibleMembers <KeyValueAttribute>(type, (kv, info) => { var name = kv.KeyName ?? info.Name; if (!string.IsNullOrWhiteSpace(name)) { var accessor = metaData.GetAccessor(info); if (!keyValues.ContainsKey(name)) { keyValues.Add(name, accessor); } else { //TODO: consider allowing derived keyvalues to override base? Log.Message($"Warning: Cannot consider member {type.FullName}.{info.Name} for keyvalue usage because another keyvalue with name \"{name}\" already exists"); } } else { Log.Message($"Warning: Cannot consider member {type.FullName}.{info.Name} for keyvalue usage because it has an invalid name \"{name}\""); } } ); return(keyValues); }
public AvatarDataItem(int avatarID, AvatarMetaData metaData, ClassMetaData classMetaData, AvatarStarMetaData starMetaData, AvatarLevelMetaData levelMetaData, int level, int star) { this.Init(avatarID, metaData, classMetaData, starMetaData, levelMetaData, level, star); }