Made ForeignKeyConstraint inner class of MySQLSchemaQueries

This commit is contained in:
Rsl1122 2019-01-18 17:31:39 +02:00
parent c3b9637a1c
commit 0f37da5663
3 changed files with 56 additions and 76 deletions

View File

@ -23,7 +23,6 @@ import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.parsing.TableSqlParser;
import com.djrapitops.plan.db.sql.queries.MySQLSchemaQueries;
import com.djrapitops.plan.system.database.databases.sql.objects.ForeignKeyConstraint;
import com.djrapitops.plan.system.settings.paths.DatabaseSettings;
import com.djrapitops.plugin.utilities.Verify;
@ -169,9 +168,9 @@ public abstract class Patch {
}
String schema = db.getConfig().get(DatabaseSettings.MYSQL_DATABASE);
List<ForeignKeyConstraint> constraints = query(MySQLSchemaQueries.foreignKeyConstraintsOf(schema, referencedTable));
List<MySQLSchemaQueries.ForeignKeyConstraint> constraints = query(MySQLSchemaQueries.foreignKeyConstraintsOf(schema, referencedTable));
for (ForeignKeyConstraint constraint : constraints) {
for (MySQLSchemaQueries.ForeignKeyConstraint constraint : constraints) {
// Uses information from https://stackoverflow.com/a/34574758
db.execute("ALTER TABLE " + constraint.getTable() +
" DROP FOREIGN KEY " + constraint.getConstraintName());
@ -184,7 +183,7 @@ public abstract class Patch {
}
String schema = db.getConfig().get(DatabaseSettings.MYSQL_DATABASE);
List<ForeignKeyConstraint> constraints = query(MySQLSchemaQueries.foreignKeyConstraintsOf(schema, table));
List<MySQLSchemaQueries.ForeignKeyConstraint> constraints = query(MySQLSchemaQueries.foreignKeyConstraintsOf(schema, table));
Verify.isTrue(constraints.isEmpty(), () -> new DBOpException("Table '" + table + "' has constraints '" + constraints + "'"));
}

View File

@ -17,7 +17,6 @@
package com.djrapitops.plan.db.sql.queries;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.system.database.databases.sql.objects.ForeignKeyConstraint;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -87,4 +86,57 @@ public class MySQLSchemaQueries {
};
}
/**
* Represents a FOREIGN KEY constraint in a MySQL database.
*
* @author Rsl1122
*/
public static class ForeignKeyConstraint {
private final String table;
private final String referencedTable;
private final String column;
private final String refrencedColumn;
private final String constraintName;
public ForeignKeyConstraint(
String table, String referencedTable,
String column, String refrencedColumn,
String constraintName
) {
this.table = table;
this.referencedTable = referencedTable;
this.column = column;
this.refrencedColumn = refrencedColumn;
this.constraintName = constraintName;
}
public String getTable() {
return table;
}
public String getReferencedTable() {
return referencedTable;
}
public String getColumn() {
return column;
}
public String getRefrencedColumn() {
return refrencedColumn;
}
public String getConstraintName() {
return constraintName;
}
@Override
public String toString() {
return "FK '" + constraintName + "' " +
table + "." + column +
" references " +
referencedTable + "." + refrencedColumn;
}
}
}

View File

@ -1,71 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.system.database.databases.sql.objects;
/**
* Represents a FOREIGN KEY constraint in a MySQL database.
*
* @author Rsl1122
*/
public class ForeignKeyConstraint {
private final String table;
private final String referencedTable;
private final String column;
private final String refrencedColumn;
private final String constraintName;
public ForeignKeyConstraint(
String table, String referencedTable,
String column, String refrencedColumn,
String constraintName
) {
this.table = table;
this.referencedTable = referencedTable;
this.column = column;
this.refrencedColumn = refrencedColumn;
this.constraintName = constraintName;
}
public String getTable() {
return table;
}
public String getReferencedTable() {
return referencedTable;
}
public String getColumn() {
return column;
}
public String getRefrencedColumn() {
return refrencedColumn;
}
public String getConstraintName() {
return constraintName;
}
@Override
public String toString() {
return "FK '" + constraintName + "' " +
table + "." + column +
" references " +
referencedTable + "." + refrencedColumn;
}
}