#1367 MySQL not null changer: List 'not null' and default values separately

- Check not only for NOT NULL constraint but also if a column has a default value
- Use suffixes instead of colors only so the command is usable via console
This commit is contained in:
ljacqu 2017-10-28 15:41:16 +02:00
parent d40109929c
commit c6fbc1187f

View File

@ -30,6 +30,7 @@ import static fr.xephi.authme.command.executable.authme.debug.DebugSectionUtils.
import static fr.xephi.authme.data.auth.PlayerAuth.DB_EMAIL_DEFAULT; import static fr.xephi.authme.data.auth.PlayerAuth.DB_EMAIL_DEFAULT;
import static fr.xephi.authme.data.auth.PlayerAuth.DB_LAST_IP_DEFAULT; import static fr.xephi.authme.data.auth.PlayerAuth.DB_LAST_IP_DEFAULT;
import static fr.xephi.authme.data.auth.PlayerAuth.DB_LAST_LOGIN_DEFAULT; import static fr.xephi.authme.data.auth.PlayerAuth.DB_LAST_LOGIN_DEFAULT;
import static fr.xephi.authme.datasource.SqlDataSourceUtils.getColumnDefaultValue;
import static fr.xephi.authme.datasource.SqlDataSourceUtils.isNotNullColumn; import static fr.xephi.authme.datasource.SqlDataSourceUtils.isNotNullColumn;
import static java.lang.String.format; import static java.lang.String.format;
@ -39,6 +40,9 @@ import static java.lang.String.format;
*/ */
class MySqlDefaultChanger implements DebugSection { class MySqlDefaultChanger implements DebugSection {
private static final String NOT_NULL_SUFFIX = ChatColor.DARK_AQUA + "@" + ChatColor.RESET;
private static final String DEFAULT_VALUE_SUFFIX = ChatColor.GOLD + "#" + ChatColor.RESET;
@Inject @Inject
private Settings settings; private Settings settings;
@ -76,7 +80,9 @@ class MySqlDefaultChanger implements DebugSection {
Operation operation = matchToEnum(arguments, 0, Operation.class); Operation operation = matchToEnum(arguments, 0, Operation.class);
Columns column = matchToEnum(arguments, 1, Columns.class); Columns column = matchToEnum(arguments, 1, Columns.class);
if (operation == null || column == null) { if (operation == Operation.DETAILS) {
showColumnDetails(sender);
} else if (operation == null || column == null) {
displayUsageHints(sender); displayUsageHints(sender);
} else { } else {
try (Connection con = getConnection(mySql)) { try (Connection con = getConnection(mySql)) {
@ -165,6 +171,25 @@ class MySqlDefaultChanger implements DebugSection {
+ sender.getName() + "'"); + sender.getName() + "'");
} }
private void showColumnDetails(CommandSender sender) {
final String tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
try (Connection con = getConnection(mySql)) {
final DatabaseMetaData metaData = con.getMetaData();
for (Columns col : Columns.values()) {
String columnName = settings.getProperty(col.getColumnNameProperty());
String isNullText = isNotNullColumn(metaData, tableName, columnName) ? "NOT NULL" : "nullable";
Object defaultValue = getColumnDefaultValue(metaData, tableName, columnName);
String defaultText = defaultValue == null ? "no default" : "default: '" + defaultValue + "'";
sender.sendMessage(formatColumnWithMetadata(col, metaData, tableName)
+ " (" + columnName + "): " + isNullText + ", " + defaultText);
}
} catch (SQLException e) {
ConsoleLogger.logException("Failed while showing column details:", e);
sender.sendMessage("Failed while showing column details. See log for info");
}
}
/** /**
* Displays sample commands and the list of columns that can be changed. * Displays sample commands and the list of columns that can be changed.
* *
@ -172,37 +197,26 @@ class MySqlDefaultChanger implements DebugSection {
*/ */
private void displayUsageHints(CommandSender sender) { private void displayUsageHints(CommandSender sender) {
sender.sendMessage("Adds or removes a NOT NULL constraint for a column."); sender.sendMessage("Adds or removes a NOT NULL constraint for a column.");
sender.sendMessage(" Only available for MySQL.");
if (mySql == null) {
sender.sendMessage("You are currently not using MySQL!");
return;
}
sender.sendMessage("Examples: add a NOT NULL constraint with"); sender.sendMessage("Examples: add a NOT NULL constraint with");
sender.sendMessage(" /authme debug mysqldef add <column>"); sender.sendMessage(" /authme debug mysqldef add <column>");
sender.sendMessage("Remove a NOT NULL constraint with"); sender.sendMessage("Remove one with /authme debug mysqldef remove <column>");
sender.sendMessage(" /authme debug mysqldef remove <column>");
// Note ljacqu 20171015: Intentionally avoid green & red as to avoid suggesting that one state is good or bad sender.sendMessage("Available columns: " + constructColumnListWithMetadata());
sender.sendMessage("Available columns: " + constructColoredColumnList()); sender.sendMessage(" " + NOT_NULL_SUFFIX + ": not-null, " + DEFAULT_VALUE_SUFFIX
sender.sendMessage(" where " + ChatColor.DARK_AQUA + "blue " + ChatColor.RESET + ": has default. See /authme debug mysqldef details");
+ "is currently not-null, and " + ChatColor.GOLD + "gold " + ChatColor.RESET + "is null");
} }
/** /**
* @return list of {@link Columns} we can toggle, colored by their current not-null status * @return list of {@link Columns} we can toggle with suffixes indicating their NOT NULL and default value status
*/ */
private String constructColoredColumnList() { private String constructColumnListWithMetadata() {
try (Connection con = getConnection(mySql)) { try (Connection con = getConnection(mySql)) {
final DatabaseMetaData metaData = con.getMetaData(); final DatabaseMetaData metaData = con.getMetaData();
final String tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE); final String tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
List<String> formattedColumns = new ArrayList<>(Columns.values().length); List<String> formattedColumns = new ArrayList<>(Columns.values().length);
for (Columns col : Columns.values()) { for (Columns col : Columns.values()) {
String columnName = settings.getProperty(col.getColumnNameProperty()); formattedColumns.add(formatColumnWithMetadata(col, metaData, tableName));
boolean isNotNull = isNotNullColumn(metaData, tableName, columnName);
String formattedColumn = (isNotNull ? ChatColor.DARK_AQUA : ChatColor.GOLD) + col.name().toLowerCase();
formattedColumns.add(formattedColumn);
} }
return String.join(ChatColor.RESET + ", ", formattedColumns); return String.join(ChatColor.RESET + ", ", formattedColumns);
} catch (SQLException e) { } catch (SQLException e) {
@ -211,6 +225,16 @@ class MySqlDefaultChanger implements DebugSection {
} }
} }
private String formatColumnWithMetadata(Columns column, DatabaseMetaData metaData,
String tableName) throws SQLException {
String columnName = settings.getProperty(column.getColumnNameProperty());
boolean isNotNull = isNotNullColumn(metaData, tableName, columnName);
boolean hasDefaultValue = getColumnDefaultValue(metaData, tableName, columnName) != null;
return column.name()
+ (isNotNull ? NOT_NULL_SUFFIX : "")
+ (hasDefaultValue ? DEFAULT_VALUE_SUFFIX : "");
}
/** /**
* Gets the Connection object from the MySQL data source. * Gets the Connection object from the MySQL data source.
* *
@ -239,7 +263,7 @@ class MySqlDefaultChanger implements DebugSection {
} }
private enum Operation { private enum Operation {
ADD, REMOVE ADD, REMOVE, DETAILS
} }
/** MySQL columns which can be toggled between being NOT NULL and allowing NULL values. */ /** MySQL columns which can be toggled between being NOT NULL and allowing NULL values. */