private string MapPropertyNameToFieldName(PropertyInfo propertyInfo) { string fieldName; ////Çizgiler arasýdaki bölüm 08.07.2009 tarihinde nonde olarak iþaretlenmiþ attiributelerin maplanmemesi için yapýldý. ////derviþ aygün. FieldDefinitionAttribute fielddefinition = ReflectionHelper.GetAttribute <FieldDefinitionAttribute>(propertyInfo); if (fielddefinition != null && fielddefinition.MappingType == FieldMappingType.No) { fieldName = string.Empty; return(fieldName); } /// fieldName = propertyInfo.Name; if (propertyInfo.Name != "Id" && propertyInfo.PropertyType.IsClass && !propertyInfo.PropertyType.Equals(typeof(string)) && !propertyInfo.PropertyType.Equals(typeof(byte[]))) { fieldName += "_Id"; } return(fieldName); }
private string GetParameterName(PropertyInfo item) { object[] attribs = item.GetCustomAttributes(typeof(FieldDefinitionAttribute), false); if (attribs.Count() > 0) { FieldDefinitionAttribute f = attribs[0] as FieldDefinitionAttribute; return("@" + f.FieldName); } return("@" + item.Name); }
/// <summary> /// retorna true se for um tablefield /// </summary> /// <param name="item">propriedade da classe</param> /// <param name="returnIfJoin">se true valida se o campo é um join e retorna </param> /// <returns></returns> private bool IsTableField(System.Reflection.PropertyInfo item, bool returnIfJoin) { //valida se pode usar este campo object[] attribs = item.GetCustomAttributes(typeof(FieldDefinitionAttribute), false); if (attribs.Count() > 0) { FieldDefinitionAttribute f = attribs[0] as FieldDefinitionAttribute; if (returnIfJoin) { return(f.IsJoinField || f.IsTableField); } return(f.IsTableField); } return(true); }
private static string MapType(FieldDefinitionAttribute definition) { switch (definition.TypeName) { case "Byte": return("tinyint"); case "String": return("varchar(" + definition.Length + ")"); case "Int32": return("int"); case "Int64": return("bigint"); case "Boolean": return("bit"); case "Decimal": return("decimal(18,4)"); case "DateTime": return("DateTime"); case "Text": return("ntext"); case "Byte[]": if (definition.Length == 8000) { return("varbinary(MAX)"); } return("varbinary(" + definition.Length + ")"); case "nvarchar": return("nvarchar(" + definition.Length + ")"); default: return("bigint"); //id field of foregin keys. } }
private static string MapTypeMysql(FieldDefinitionAttribute definition) { switch (definition.TypeName) { case "String": return("nvarchar(" + definition.Length + ")"); case "Int32": return("int"); case "Boolean": return("tinyint(1)"); case "Decimal": return("decimal(18,4)"); case "DateTime": return("DateTime"); default: return("bigint"); //id field of foregin keys. } }
public virtual void Validate() { //Loop each field // if field has definition and validations in it // then call validate method Type type = GetType(); PropertyInfo[] properties = PersistenceStrategyProvider.FindStrategyFor(type).GetPersistentProperties(type); if (properties != null) { foreach (PropertyInfo property in properties) { FieldDefinitionAttribute definition = DataDictionary.Instance.GetFieldDefinition(type.Name + "." + property.Name); if (definition == null) { throw new Exception("Definition okunamadý: " + property.Name); } object value = GetValue(property.Name); //Length validation for string fields if (definition.TypeName == typeof(string).Name) { if (value != null && definition.Length < value.ToString().Length) //! value can be enumeration so call toString { throw new Exception("Ýzin verilenden daha uzun bilgi! " + property.Name); } } //Required validation if (definition.IsRequired) { ValidateRequiredField(value, definition.Text); } } } }
private string GetFieldName(System.Reflection.PropertyInfo item, TableDefinitionAttribute tableDefinition, bool withTableName) { object[] attribs = item.GetCustomAttributes(typeof(FieldDefinitionAttribute), false); string name = ""; string table = ""; if (attribs.Count() > 0) { FieldDefinitionAttribute f = attribs[0] as FieldDefinitionAttribute; if (f.IsJoinField) { table = f.JoinTable; name = f.FieldName; } else { table = tableDefinition.TableName; name = f.FieldName; } } else { table = tableDefinition.TableName; name = item.Name; } if (withTableName) { return(string.Format("{0}.{1}", table, name)); } return(name); }
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])); } } }
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/>"); } } }