/// <summary> /// Update DB attribute values. /// </summary> /// <param name="attributes"></param> /// <param name="s"></param> private static void UpdateAttributes(Type type, object[] attributes, GXSerializedItem s) { int settings = 0; PropertyInfo pi = s.Target as PropertyInfo; if (pi != null && pi.Name == "Id") { foreach (var i in type.GetInterfaces()) { if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IUnique <>)) { settings |= (int)(Attributes.Id | Attributes.PrimaryKey); break; } } } foreach (object att in attributes) { //If field is ignored. if (att is IgnoreAttribute && (((IgnoreAttribute)att).IgnoreType & IgnoreType.Db) != 0) { settings |= (int)Attributes.Ignored; } else { if (att is DefaultValueAttribute) { DefaultValueAttribute def = att as DefaultValueAttribute; s.DefaultValue = def.Value; } //Is property indexed. if (att is IndexAttribute) { settings |= (int)Attributes.Index; } //Is property auto indexed value. if (att is AutoIncrementAttribute) { settings |= (int)Attributes.AutoIncrement; } //Primary key value. if (att is PrimaryKeyAttribute) { settings |= (int)Attributes.PrimaryKey; } //Foreign key value. if (att is ForeignKeyAttribute) { settings |= (int)Attributes.ForeignKey; } //Relation field. if (att is RelationAttribute) { settings |= (int)Attributes.Relation; } if (att is StringLengthAttribute) { settings |= (int)Attributes.StringLength; } if (att is DataMemberAttribute) { DataMemberAttribute n = att as DataMemberAttribute; if (n.IsRequired) { settings |= (int)Attributes.IsRequired; } } } } s.Attributes = (Attributes)settings; }
internal string ToString(ref string post) { if (Parent.Updated || Updated) { ColumnList.Clear(); List <GXJoin> joinList = new List <GXJoin>(); GXOrderByCollection.UpdateJoins(Parent.Settings, Joins, joinList); string[] list; StringBuilder sb = new StringBuilder(); //Get columns. Dictionary <string, GXSerializedItem> properties; Dictionary <Type, GXSerializedItem> neededTables = new Dictionary <Type, GXSerializedItem>(); foreach (var it in List) { //No relations. if (!neededTables.ContainsKey(it.Key.Parameters[0].Type)) { neededTables.Add(it.Key.Parameters[0].Type, null); } list = GXDbHelpers.GetMembers(Parent.Settings, it.Key, '\0', false, ref post); foreach (var it2 in list) { properties = GXSqlBuilder.GetProperties(it.Key.Parameters[0].Type); if (it2 != "*" && ColumnList.ContainsKey(it.Key.Parameters[0].Type)) { if (properties.ContainsKey(it2)) { GXSerializedItem si = properties[it2]; if (si.Relation != null) { //Get properties. GetColumns(si.Relation.ForeignTable, ColumnList, neededTables); } if (si.Relation == null || si.Relation.RelationType != RelationType.ManyToMany) { ColumnList[it.Key.Parameters[0].Type].Add(it2); } } else { string str = it2; if (it.Value != null) { string[] tmp = GXDbHelpers.GetMembers(Parent.Settings, it.Value, '\0', false, ref post); str += " AS " + tmp[0]; } ColumnList[it.Key.Parameters[0].Type].Add(str); } } else { if (it2 == "*") { GetColumns(it.Key.Parameters[0].Type, ColumnList, neededTables); } else { if (neededTables.ContainsKey(it.Key.Parameters[0].Type)) { neededTables.Remove(it.Key.Parameters[0].Type); } List <string> columns2 = new List <string>(); columns2.Add(it2); ColumnList.Add(it.Key.Parameters[0].Type, columns2); if (properties.ContainsKey(it2)) { GXSerializedItem si = properties[it2]; if (si.Relation != null) { //Get properties. GetColumns(si.Relation.ForeignTable, ColumnList, neededTables); } } } } } } foreach (var it in ColumnList) { foreach (KeyValuePair <Type, LambdaExpression> x in Excluded) { if (x.Key == it.Key) { string[] removed = GXDbHelpers.GetMembers(null, x.Value, '\0', false, ref post); foreach (string col in removed) { bool includeQuery = false; string col2 = GXDbHelpers.AddQuotes(col, Parent.Settings.ColumnQuotation); //Joins are not removed from the qyery or 1:1 doesn't work. foreach (var j in joinList) { if ((it.Key == j.Table1Type && j.Column1 == col2) || (it.Key == j.Table2Type && j.Column2 == col2)) { includeQuery = true; break; } } if (!includeQuery) { it.Value.Remove(col); } } } } } SelectToString(Parent.Settings, sb, Parent.Distinct, ColumnList, joinList, Parent.Index, Parent.Count, post); sql = sb.ToString(); Updated = false; } return(sql); }
/// <summary> /// Update DB relations. /// </summary> /// <param name="mainType"></param> /// <param name="s"></param> private static void UpdateRelations(Type mainType, Dictionary <string, GXSerializedItem> properties, GXSerializedItem s, bool primaryData) { Type type; if (primaryData) { s.Relation = new GXRelationTable(); s.Relation.Column = s; s.Relation.PrimaryTable = mainType; s.Relation.PrimaryId = s; if ((s.Attributes & Attributes.ForeignKey) != 0) { ForeignKeyAttribute fk = ((ForeignKeyAttribute[])(s.Target as PropertyInfo).GetCustomAttributes(typeof(ForeignKeyAttribute), false))[0]; type = fk.Type; //If type is not give in ForeignKeyAttribute. if (type == null) { type = s.Type; if (typeof(IEnumerable).IsAssignableFrom(type)) { type = GXInternal.GetPropertyType(type); } } s.Relation.ForeignTable = type; if (fk.MapTable != null) { s.Relation.RelationType = RelationType.ManyToMany; } else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(s.Type)) { s.Relation.RelationType = RelationType.OneToMany; s.Relation.PrimaryId = GXSqlBuilder.FindRelation(type, mainType); if (s.Relation.PrimaryId == null) { throw new Exception(string.Format("Relation create failed. Foreign table '{0}' do not have relation to table '{1}'.", GXDbHelpers.GetTableName(type, false, null), GXDbHelpers.OriginalTableName(mainType))); } } else { s.Relation.RelationType = RelationType.OneToOne; } } else if ((s.Attributes & Attributes.Relation) != 0) { RelationAttribute ra = ((RelationAttribute[])(s.Target as PropertyInfo).GetCustomAttributes(typeof(RelationAttribute), false))[0]; type = ra.Target; if (type == null) { type = s.Type; } if (typeof(IEnumerable).IsAssignableFrom(type)) { type = GXInternal.GetPropertyType(type); } s.Relation.ForeignTable = type; } } else { if ((s.Attributes & Attributes.ForeignKey) != 0) { ForeignKeyAttribute fk = ((ForeignKeyAttribute[])(s.Target as PropertyInfo).GetCustomAttributes(typeof(ForeignKeyAttribute), true))[0]; type = fk.Type; //If type is not give in ForeignKeyAttribute. if (type == null) { type = s.Type; if (typeof(IEnumerable).IsAssignableFrom(type)) { type = GXInternal.GetPropertyType(type); } } GXSerializedItem secondary = FindUnique(type); if (secondary == null) { throw new Exception(string.Format("Table {0} Relation create failed. Class must be derived from IUnique or target type must set in ForeignKey or Relation attribute.", GXDbHelpers.GetTableName(mainType, true, '\'', null))); } s.Relation.ForeignId = secondary; //Update relation map fields. if (fk.MapTable != null) { foreach (var it in GetProperties(fk.MapTable)) { if ((it.Value.Attributes & Attributes.ForeignKey) != 0 && s.Relation.ForeignTable == it.Value.Relation.ForeignTable) { s.Relation.RelationMapTable = it.Value; break; } } } } else if ((s.Attributes & Attributes.Relation) != 0) { RelationAttribute ra = ((RelationAttribute[])(s.Target as PropertyInfo).GetCustomAttributes(typeof(RelationAttribute), false))[0]; type = ra.Target; if (type == null) { type = s.Type; } if (typeof(IEnumerable).IsAssignableFrom(type)) { type = GXInternal.GetPropertyType(type); } GXSerializedItem secondary = FindUnique(type); if (secondary == null) { throw new Exception(string.Format("Table {0} Relation create failed. Class must be derived from IUnique or target type must set in ForeignKey or Relation attribute.", GXDbHelpers.GetTableName(mainType, true, '\'', null))); } s.Relation.ForeignId = secondary; } } }