public static void Register(Type type) { try { if (!type.IsSubclassOf(typeof(Entity))) { return; } EntityDefinitionAttribute attr = type.GetCustomAttribute <EntityDefinitionAttribute>(); if (attr == null) { Logger.Log(LogLevel.Error, $"Entity {type} does not have a definition attribute"); return; } string id = attr.ID; ConstructorInfo ctor = type.GetConstructor(new Type[] { typeof(EntityData), typeof(Room) }); if (ctor == null) { Logger.Log(LogLevel.Error, $"Entity of type {type} with ID {id} does not have a valid ctor"); return; } FieldInfo placementsField = null; if ((placementsField = type.GetField("Placements", BindingFlags.Public | BindingFlags.Static)) != null) { if (placementsField.FieldType == typeof(PlacementList)) { PlacementList placements = (PlacementList)placementsField.GetValue(null); foreach (Placement p in placements) { p.Parent = type; EntityPlacements.Add(p); } Logger.Log($"Registered {placements.Count} placements for {attr.ID}"); } } EntityCreators.Add(id, (EntityData d, Room r) => (Entity)ctor.Invoke(new object[] { d, r })); Logger.Log($"Registered entity {id} of type {type}"); } catch (Exception e) { Logger.Log(LogLevel.Error, $"Encountered an error while attempting to register entity {type}"); Logger.LogException(e); } }
private static void updateMs(StringBuilder str, Assembly assembly, Type baseEntityType) { foreach (Type entityType in assembly.GetTypes()) { bool Table = true; if (entityType.GetCustomAttributes(typeof(EntityDefinitionAttribute), false).Length > 0) { object obje = (EntityDefinitionAttribute)entityType.GetCustomAttributes(typeof(EntityDefinitionAttribute), false)[0]; Table = ((EntityDefinitionAttribute)obje).IsTable; } if (!entityType.IsClass || !entityType.IsSubclassOf(baseEntityType) || entityType.IsGenericType || entityType.Name.Contains("<") || !Table) { continue; } PropertyInfo[] props = PersistenceStrategyProvider.FindStrategyFor(entityType) .GetPersistentProperties(entityType); object[] memberinfo = entityType.GetCustomAttributes(typeof(EntityDefinitionAttribute), true); string className = entityType.Name; if (Configuration.GetValue("DbType") != "System.Data.Sqlite" && TableExistMs(className)) { DataTable table = executeSql("select * from " + className + " where Id=-1"); System.Collections.Hashtable fieldDictionary = new System.Collections.Hashtable(); foreach (PropertyInfo property in props) { //Derviş Aygün FieldDefinitionAttribute fielddefinition = ReflectionHelper.GetAttribute <FieldDefinitionAttribute>(property); if (fielddefinition != null && fielddefinition.MappingType == FieldMappingType.No) { continue; } //Derviş Aygün FieldDefinitionAttribute definition = DataDictionary.Instance.GetFieldDefinition(className + "." + property.Name); string fieldname = (property.PropertyType.IsSubclassOf(baseEntityType)) ? definition.Name + "_Id" : definition.Name; fieldDictionary[fieldname] = 1; if (!table.Columns.Contains(fieldname)) { string sql = "alter table " + className + " add " + fieldname + " " + MapType(definition); executeNonQuery(sql); str.Append("New Field: " + fieldname + ", Table: " + className + "<br/>"); } else if (!fieldname.EndsWith("_Id") && table.Columns[fieldname].DataType.Name != definition.TypeName) { string sql = "alter table " + className + " alter column " + fieldname + " " + MapType(definition); executeNonQuery(sql); str.Append("Alter Field: " + fieldname + ", Table: " + className + "<br/>"); } } foreach (DataColumn column in table.Columns) { if (!fieldDictionary.ContainsKey(column.ColumnName)) { string sql = "alter table " + className + " drop column " + column.ColumnName; executeNonQuery(sql); str.Append("Drop Field: " + column.ColumnName + ", Table: " + className + "<br/>"); } } } else { //Hiç özelliği olmayan class lar olabilir if (props.Length == 0) { continue; } StringBuilder s = new System.Text.StringBuilder(); s.Append("CREATE TABLE " + className + " ("); foreach (PropertyInfo property in props) { FieldDefinitionAttribute definition = DataDictionary.Instance.GetFieldDefinition(className + "." + property.Name); string fieldname = (property.PropertyType.IsSubclassOf(baseEntityType)) ? definition.Name + "_Id" : definition.Name; s.Append(fieldname + " " + MapType(definition)); if (definition.Name == "Id") { if (memberinfo != null && memberinfo.Length > 0) { EntityDefinitionAttribute attirebute = (EntityDefinitionAttribute)memberinfo[0]; if (attirebute.IdMethod == IdMethod.UserSubmitted) { if (Configuration.GetValue("DbType") != "System.Data.Sqlite") { s.Append(" CONSTRAINT PK_" + className + "_Id PRIMARY KEY CLUSTERED"); } else { s.Append(" PRIMARY KEY"); } } } else { s.Append(" IDENTITY(1,1) CONSTRAINT PK_" + className + "_Id PRIMARY KEY CLUSTERED"); } } s.Append(", "); } s.Remove(s.Length - 2, 2); s.Append(")"); string sql = s.ToString(); executeNonQuery(sql); str.Append("New Table: " + className + "<br/>"); } System.Collections.Hashtable fields = new System.Collections.Hashtable(); Dictionary <string, List <string> > uniqueGroup = new Dictionary <string, List <string> >(); Dictionary <string, List <string> > nonUniqueGroup = new Dictionary <string, List <string> >(); foreach (PropertyInfo property in props) { FieldDefinitionAttribute definition = DataDictionary.Instance.GetFieldDefinition(className + "." + property.Name); string fieldname = (property.PropertyType.IsSubclassOf(baseEntityType)) ? definition.Name + "_Id" : definition.Name; if (!string.IsNullOrEmpty(definition.UniqueIndexGroup)) { if (!uniqueGroup.ContainsKey(definition.UniqueIndexGroup)) { uniqueGroup[definition.UniqueIndexGroup] = new List <string>(); } uniqueGroup[definition.UniqueIndexGroup].Add(fieldname); } if (!string.IsNullOrEmpty(definition.NonUniqueIndexGroup)) { if (!nonUniqueGroup.ContainsKey(definition.NonUniqueIndexGroup)) { nonUniqueGroup[definition.NonUniqueIndexGroup] = new List <string>(); } nonUniqueGroup[definition.NonUniqueIndexGroup].Add(fieldname); } } foreach (string indexGroup in uniqueGroup.Keys) { executeNonQuery(CreateIndex(className, true, indexGroup, uniqueGroup[indexGroup])); } foreach (string indexGroup in nonUniqueGroup.Keys) { executeNonQuery(CreateIndex(className, false, indexGroup, nonUniqueGroup[indexGroup])); } } }
/// <summary> /// Applies an attribute based EntityDefinitionAttribute entity defintion to this entity /// </summary> /// <param name="attribute">An attribute that will be applied to modify the defintion of this entity</param> /// <returns>The builder for fluent chaining.</returns> public EntityDefinitionBuilder <TEntity> ApplyAttribute(EntityDefinitionAttribute attribute) { attribute.ApplyToEntityDefinition(definition); return(this); }
private static void updateMysql(StringBuilder str, Assembly assembly, Type baseEntityType) { foreach (Type entityType in assembly.GetTypes()) { bool Table = true; if (entityType.GetCustomAttributes(typeof(EntityDefinitionAttribute), false).Length > 0) { object obje = (EntityDefinitionAttribute)entityType.GetCustomAttributes(typeof(EntityDefinitionAttribute), false)[0]; Table = ((EntityDefinitionAttribute)obje).IsTable; } if (!entityType.IsClass || !entityType.IsSubclassOf(baseEntityType) || entityType.IsGenericType || entityType.Name.Contains("<") || !Table) { continue; } PropertyInfo[] props = PersistenceStrategyProvider.FindStrategyFor(entityType) .GetPersistentProperties(entityType); object[] memberinfo = entityType.GetCustomAttributes(typeof(EntityDefinitionAttribute), true); string className = entityType.Name; if (TableExistMy(className, Transaction.Instance.GetSchema())) { DataTable table = executeSql("select * from " + className + " where Id=-1"); DataTable metaTable = Transaction.Instance.MetaTableColumns(className); Dictionary <string, int> metaLengths = new Dictionary <string, int>(); foreach (DataRow row in metaTable.Rows) { if ((string)row["DATA_TYPE"] != "varchar") { continue; } metaLengths[(string)row["COLUMN_NAME"]] = Convert.ToInt32(row["CHARACTER_MAXIMUM_LENGTH"]); } System.Collections.Hashtable fieldDictionary = new System.Collections.Hashtable(); foreach (PropertyInfo property in props) { //Derviş Aygün FieldDefinitionAttribute fielddefinition = ReflectionHelper.GetAttribute <FieldDefinitionAttribute>(property); if (fielddefinition != null && fielddefinition.MappingType == FieldMappingType.No) { continue; } //Derviş aygün FieldDefinitionAttribute definition = DataDictionary.Instance.GetFieldDefinition(className + "." + property.Name); string fieldname = (property.PropertyType.IsSubclassOf(baseEntityType)) ? definition.Name + "_Id" : definition.Name; fieldDictionary[fieldname] = 1; if (!table.Columns.Contains(fieldname)) { string sql = "alter table " + className + " add " + fieldname + " " + MapTypeMysql(definition); executeNonQuery(sql); str.Append("New Field: " + fieldname + ", Table: " + className + "<br/>"); } else if (!fieldname.EndsWith("_Id") && definition.TypeName != "Boolean" && table.Columns[fieldname].DataType.Name != definition.TypeName) { string sql = "alter table " + className + " modify column " + fieldname + " " + MapTypeMysql(definition); executeNonQuery(sql); str.Append("Alter Field: " + fieldname + ", Table: " + className + "<br/>"); } else if (!fieldname.EndsWith("_Id") && definition.TypeName == "Boolean" && table.Columns[fieldname].DataType.Name != "SByte") { string sql = "alter table " + className + " modify column " + fieldname + " " + MapTypeMysql(definition); executeNonQuery(sql); str.Append("Alter Field: " + fieldname + ", Table: " + className + "<br/>"); } else if (metaLengths.ContainsKey(fieldname) && definition.Length != metaLengths[fieldname]) { string sql = "alter table " + className + " modify column " + fieldname + " " + MapTypeMysql(definition); executeNonQuery(sql); str.Append("Resize Field: " + fieldname + ", Table: " + className + ", Length: " + definition.Length + "<br/>"); } } foreach (DataColumn column in table.Columns) { if (!fieldDictionary.ContainsKey(column.ColumnName)) { string sql = "alter table " + className + " drop column " + column.ColumnName; executeNonQuery(sql); str.Append("Drop Field: " + column.ColumnName + ", Table: " + className + "<br/>"); } } } else { //Hiç özelliği olmayan class lar olabilir if (props.Length == 0) { continue; } StringBuilder s = new System.Text.StringBuilder(); s.Append("CREATE TABLE " + className + " ("); string pkstr = ""; foreach (PropertyInfo property in props) { FieldDefinitionAttribute definition = DataDictionary.Instance.GetFieldDefinition(className + "." + property.Name); string fieldname = (property.PropertyType.IsSubclassOf(baseEntityType)) ? definition.Name + "_Id" : definition.Name; s.Append("`" + fieldname + "` " + MapTypeMysql(definition)); if (definition.Name == "Id") { if (memberinfo != null && memberinfo.Length > 0) { EntityDefinitionAttribute attirebute = (EntityDefinitionAttribute)memberinfo[0]; if (attirebute.IdMethod == IdMethod.UserSubmitted) { s.Append(" NOT NULL "); } } else { s.Append(" NOT NULL AUTO_INCREMENT"); } pkstr = " ,PRIMARY KEY (`Id`)"; } s.Append(", "); } s.Remove(s.Length - 2, 2); s.Append(pkstr); s.Append(")"); string sql = s.ToString(); executeNonQuery(sql); str.Append("New Table: " + className + "<br/>"); } } }