public Status(XCon xCon) { this.xCon = xCon; }
/// <summary> /// Alters table based on attribute "MysqlCreateType" information /// </summary> /// <param name="t">Type of object</param> /// <param name="database">Database name</param> /// <param name="table">Table name</param> /// <param name="xcon">Multicon / Onecon instance</param> /// <param name="dropUnusedColumns">Removes unused columns</param> /// <returns></returns> public static string GetAlterTableString(Type t, string database, string table, XCon xcon, bool dropUnusedColumns) { StringBuilder sb = new StringBuilder(); List <string> existingDatabaseColumns = xcon.GetColumn <string>("SELECT `COLUMN_NAME` FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA` LIKE @database AND `TABLE_NAME` LIKE @table", 0, false, new ParameterData("database", database), new ParameterData("table", table)).ToList(); Dictionary <string, string> existingTypeColumns = new Dictionary <string, string>(); foreach (var prop in t.GetProperties()) { foreach (var cust in prop .GetCustomAttributes(false) .Where(n => n != null && n.GetType() == typeof(MySqlColumnAttribute)) .Select(n => (MySqlColumnAttribute)n) ) { existingTypeColumns.Add(string.IsNullOrWhiteSpace(cust.DatabaseColumnName) ? prop.Name : cust.DatabaseColumnName, cust.MysqlCreateType); } } if (dropUnusedColumns) { foreach (string remove in existingDatabaseColumns .Where(n => !existingTypeColumns.ContainsKey(n))) { sb.Append("ALTER TABLE `" + database + "`.`" + table + "` DROP COLUMN " + remove + ";\n"); } } foreach (var add in existingTypeColumns .Where(n => !existingDatabaseColumns.Any(n2 => n2.IndexOf(n.Key, StringComparison.InvariantCultureIgnoreCase) > -1))) { sb.Append("ALTER TABLE `" + database + "`.`" + table + "` ADD " + add.Key + " " + add.Value + ";\n"); } return(sb.ToString()); }