private void ValidateVia(Fk fk) { if (!Map.Database.Views.ContainsKey(fk.ParentTable)) { return; } Durados.View parentView = Map.Database.Views[fk.ParentTable]; if (!Map.Database.Views.ContainsKey(fk.ChildTable)) { return; } Durados.View childView = Map.Database.Views[fk.ChildTable]; if (parentView.DataTable.Columns.Contains(fk.ParentColumn) || parentView.Fields.ContainsKey(fk.ParentColumn)) { throw new NewFkException(fk.ParentTable, fk.ParentColumn); } if (childView.DataTable.Columns.Contains(fk.ChildColumn) || childView.Fields.ContainsKey(fk.ChildColumn)) { throw new NewFkException(fk.ChildTable, fk.ChildColumn); } }
private void ValidateSql(string sql) { Fk fk = GetFk(sql); if (fk == null) { return; } ValidateVia(fk); }
private Fk GetFk(string sql) { const string Constraint = "constraint"; const string Foreign = "foreign"; const string Key = "key"; const char Space = ' '; const char SqlArgBoundery = '`'; const char _ = '_'; const char LeftBracket = '('; const char RightBracket = ')'; const string Table = "table"; const string References = "references"; if (!(sql.Contains(Constraint) && sql.Contains(Foreign))) { return(null); } string[] words = sql.Split(Space); Fk fk = new Fk(); for (int i = 0; i < words.Length - 1; i++) { string word = words[i]; string nextWord = words[i + 1]; if (word.Equals(Table)) { fk.ChildTable = nextWord.Trim(SqlArgBoundery); } if (word.Equals(Constraint)) { fk.ParentColumn = nextWord.Split(_).Last(); } if (word.Equals(Key)) { fk.ChildColumn = nextWord.Trim(LeftBracket).Trim(RightBracket).Trim(SqlArgBoundery); } if (word.Equals(References)) { fk.ParentTable = nextWord.Trim(SqlArgBoundery); } } return(fk); }
public static Column BuildColumn(PropertyInfo propertyInfo, Type tableType, List <Type> tableTypes, ITypeMapper typeMapper) { // property that is present in the db and in the object but needs to be initialized later var isShadowAttribute = false; var manyToOneUsed = false; var constraints = new HashSet <Constraint>(); Relation relation = null; // TODO: FK are not handled at all?! foreach (var propertyInfoCustomAttribute in propertyInfo.GetCustomAttributes()) { if (propertyInfoCustomAttribute.GetType() == typeof(Pk)) { constraints.Add(propertyInfoCustomAttribute as Constraint); var superClass = tableTypes.Where(tableType.IsSubclassOf).ToList(); // if its a primary key and it is a subclass then the teacher for example is also a person and needs a foreignkey on the personclass primary id if (superClass.Any()) { relation = new Fk(superClass.First()); } } else if (propertyInfoCustomAttribute.GetType() == typeof(ManyToMany)) { var manyToMany = propertyInfoCustomAttribute as ManyToMany; manyToMany.ToPocoType = propertyInfo.PropertyType.GetGenericArguments()[0]; relation = manyToMany; } else if (propertyInfoCustomAttribute.GetType() == typeof(OneToMany)) { var oneToMany = propertyInfoCustomAttribute as OneToMany; oneToMany.MappedByPocoType = propertyInfo.PropertyType.GetGenericArguments()[0]; relation = propertyInfoCustomAttribute as Relation; } else if (propertyInfoCustomAttribute.GetType() == typeof(ManyToOne)) { var manyToOne = propertyInfoCustomAttribute as ManyToOne; manyToOne.ToPocoType = propertyInfo.PropertyType; manyToOneUsed = true; relation = manyToOne; isShadowAttribute = true; } else { constraints.Add(propertyInfoCustomAttribute as Constraint); } } // TODO: think about avoiding checking by customattribute and naming separately (problem if no customattributes no loop) if ("id".Equals(propertyInfo.Name.ToLower())) { constraints.Add(new Pk()); var superClass = tableTypes.Where(tableType.IsSubclassOf).ToList(); if (superClass.Any()) { relation = new Fk(superClass.First()); } } // now check if it is a regular 1-1 relationship // beware of doing this double later! // TODO: think about avoiding doing it like this too if (!manyToOneUsed) { var oneToOne = tableTypes.Where(tType => propertyInfo.PropertyType == tType).ToList(); if (oneToOne.Any()) { relation = new Fk(oneToOne.First()); } } return(new Column( BuildColumnName(propertyInfo), relation, constraints, propertyInfo, DbType(propertyInfo.PropertyType, tableTypes, constraints, typeMapper), isShadowAttribute )); }