private DeviantTable MissingTable(Type t) { Dictionary <string, Type> propertyTypes = t.GetProperties(bindingFlags).ToDictionary(x => x.Name, y => y.PropertyType); var table = new DeviantTable { Name = t.Name, DeviantColumns = propertyTypes, TableExistsInDb = false }; return(table); }
/// <summary> /// Compares name and members of type t to table with corresponding name in database. /// </summary> /// <param name="t">The t.</param> /// <returns>Returns a DeviantTalbe that represents whats missing in the db, and if the table exists</returns> private DeviantTable CompareTableToObject(Type t) { string query = "SELECT TABLE_CATALOG, TABLE_NAME, COLUMN_NAME, DATA_TYPE, ORDINAL_POSITION " + "FROM KITT.INFORMATION_SCHEMA.COLUMNS " + "WHERE TABLE_NAME = N'" + t.Name + "'"; //Get all columns from table with name of t List <DatabaseColumn> columns = _connection.Query <DatabaseColumn>(query).ToList(); //check if database had such table if (!columns.Any()) { Log.Logger.Debug("The database does not contain table with name " + t.Name); return(MissingTable(t)); } Log.Logger.Debug("Comparing type " + t.Name + " to database equivalent"); Dictionary <string, Type> properties = t.GetProperties(bindingFlags).ToDictionary(x => x.Name, y => y.PropertyType); IDictionary <string, Type> deviantColumns = new Dictionary <string, Type>(); IDictionary <string, bool> deviantExistingColums = new Dictionary <string, bool>(); foreach (var property in properties) { var column = columns.FirstOrDefault(x => ConvertIdName(x.COLUMN_NAME).Equals(property.Key)); if (column != null) { if (ConvertToStandardType(column.DATA_TYPE) == property.Value) { Log.Logger.Debug("Table " + t.Name + " has column " + ConvertIdName(column.COLUMN_NAME) + ", with correct type: " + ConvertToStandardType(column.DATA_TYPE)); } else { Log.Logger.Debug("Table " + t.Name + " has column " + ConvertIdName(column.COLUMN_NAME) + ", with wrong type: " + ConvertToStandardType(column.DATA_TYPE) + " should be " + property.Value); deviantColumns.Add(property); deviantExistingColums.Add(property.Key, true); } } else { Log.Logger.Debug("Table " + t.Name + " is missing column " + property.Key); deviantColumns.Add(property); } } if (!deviantExistingColums.Any() && !deviantColumns.Any()) { Log.Logger.Debug("Table " + t.Name + " is a match"); return(null); } var deviant = new DeviantTable { TableExistsInDb = true, DeviantColumns = deviantColumns, ExistingColums = deviantExistingColums, Name = t.Name }; return(deviant); }