Implement system to run code when an SQL error occurs, and ensure that duplicate key errors on community creation inform the community creator

This commit is contained in:
AlexTheCoder 2016-12-23 00:05:00 -05:00 committed by cnr
parent ac8d8aa69c
commit 7a482c3386
3 changed files with 105 additions and 22 deletions

View File

@ -262,7 +262,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
} }
setting.parseValueInto(newValue, community); setting.parseValueInto(newValue, community);
//community.message(F.main(getName(), F.name(sender) + " has changed settings in " + F.name(community.getName()) + "!")); //community.message(F.main(getName(), F.name(sender) + " has changed settings in " + F.name(community.getName()) + "!"));
UtilServer.CallEvent(new CommunitySettingUpdateEvent(community)); runSync(() -> UtilServer.CallEvent(new CommunitySettingUpdateEvent(community)));
} }
public void handleCommunityNameUpdate(Integer id, String sender, String name) public void handleCommunityNameUpdate(Integer id, String sender, String name)
@ -275,7 +275,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
String oldName = community.getName(); String oldName = community.getName();
community.setName(name); community.setName(name);
community.message(F.main(getName(), F.name(sender) + " has changed the name of " + F.name(oldName) + " to " + F.name(community.getName()) + "!")); community.message(F.main(getName(), F.name(sender) + " has changed the name of " + F.name(oldName) + " to " + F.name(community.getName()) + "!"));
UtilServer.CallEvent(new CommunityNameUpdateEvent(community)); runSync(() -> UtilServer.CallEvent(new CommunityNameUpdateEvent(community)));
} }
public void handleCommunityMembershipRoleUpdate(Integer id, String sender, UUID uuid, CommunityRole role) public void handleCommunityMembershipRoleUpdate(Integer id, String sender, UUID uuid, CommunityRole role)
@ -297,7 +297,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
} }
String name = member.Name; String name = member.Name;
community.message(F.main(getName(), F.name(sender) + " has changed " + F.name(name + "'s") + " role to " + F.elem(role.getDisplay()) + " in " + F.name(community.getName()) + "!"), CommunityRole.COLEADER); community.message(F.main(getName(), F.name(sender) + " has changed " + F.name(name + "'s") + " role to " + F.elem(role.getDisplay()) + " in " + F.name(community.getName()) + "!"), CommunityRole.COLEADER);
UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community)); runSync(() -> UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community)));
} }
public void handleCommunityMembershipUpdate(Integer id, String sender, String playerName, UUID playerUUID, Integer accountId, boolean kick, boolean leave) public void handleCommunityMembershipUpdate(Integer id, String sender, String playerName, UUID playerUUID, Integer accountId, boolean kick, boolean leave)
@ -337,12 +337,15 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
community.message(F.main(getName(), F.name(playerName) + " has joined " + F.name(community.getName()) + "!")); community.message(F.main(getName(), F.name(playerName) + " has joined " + F.name(community.getName()) + "!"));
} }
UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community)); runSync(() ->
if (Bukkit.getPlayer(playerUUID) != null)
{ {
UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(playerUUID))); UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community));
} if (Bukkit.getPlayer(playerUUID) != null)
{
UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(playerUUID)));
}
});
} }
public void handleCommunityInvite(Integer id, String sender, String targetName, UUID targetUUID) public void handleCommunityInvite(Integer id, String sender, String targetName, UUID targetUUID)
@ -410,7 +413,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
community.getJoinRequests().put(playerUUID, new CommunityJoinRequestInfo(playerName, playerUUID, accountId)); community.getJoinRequests().put(playerUUID, new CommunityJoinRequestInfo(playerName, playerUUID, accountId));
community.message(F.main(getName(), F.name(playerName) + " has requested to join " + F.name(community.getName()) + "!"), CommunityRole.COLEADER); community.message(F.main(getName(), F.name(playerName) + " has requested to join " + F.name(community.getName()) + "!"), CommunityRole.COLEADER);
UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community)); runSync(() -> UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community)));
} }
public void handleCommunityCloseJoinRequest(Integer id, String sender, String playerName, UUID playerUUID, Integer accountId, boolean announce) public void handleCommunityCloseJoinRequest(Integer id, String sender, String playerName, UUID playerUUID, Integer accountId, boolean announce)
@ -430,7 +433,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
community.message(F.main(getName(), F.name(playerName) + "'s request to join " + F.name(community.getName()) + " has been denied by " + F.name(sender) + "!"), CommunityRole.COLEADER); community.message(F.main(getName(), F.name(playerName) + "'s request to join " + F.name(community.getName()) + " has been denied by " + F.name(sender) + "!"), CommunityRole.COLEADER);
} }
UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community)); runSync(() -> UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community)));
} }
public void handleCommunityCreation(Integer id, String name, Integer leaderId, UUID leaderUUID, String leaderName) public void handleCommunityCreation(Integer id, String name, Integer leaderId, UUID leaderUUID, String leaderName)

View File

@ -301,18 +301,19 @@ public class CommunityRepository extends MinecraftRepository
if (resultSet.next()) if (resultSet.next())
{ {
int id = resultSet.getInt(1); int id = resultSet.getInt(1);
executeUpdate(connection, ADD_TO_COMMUNITY, new ColumnInt("accountId", leaderAccount), new ColumnInt("communityId", id), new ColumnVarChar("communityRole", 20, CommunityRole.LEADER.toString())); executeUpdate(connection, ADD_TO_COMMUNITY, null, new ColumnInt("accountId", leaderAccount), new ColumnInt("communityId", id), new ColumnVarChar("communityRole", 20, CommunityRole.LEADER.toString()));
idCallback.run(id); idCallback.run(id);
} }
else else
{ {
idCallback.run(-1); idCallback.run(-1);
} }
}, new ColumnVarChar("name", 15, name), new ColumnVarChar("region", 5, _us ? "US" : "EU")); }, () -> idCallback.run(-1), new ColumnVarChar("name", 15, name), new ColumnVarChar("region", 5, _us ? "US" : "EU"));
} }
catch (SQLException e) catch (SQLException e)
{ {
e.printStackTrace(); e.printStackTrace();
idCallback.run(-1);
} }
} }
@ -320,11 +321,11 @@ public class CommunityRepository extends MinecraftRepository
{ {
try (Connection connection = getConnection()) try (Connection connection = getConnection())
{ {
executeUpdate(connection, "DELETE FROM communities WHERE id=?;", new ColumnInt("id", communityId)); executeUpdate(connection, "DELETE FROM communities WHERE id=?;", null, new ColumnInt("id", communityId));
executeUpdate(connection, "DELETE FROM communitySettings WHERE communityId=?;", new ColumnInt("communityId", communityId)); executeUpdate(connection, "DELETE FROM communitySettings WHERE communityId=?;", null, new ColumnInt("communityId", communityId));
executeUpdate(connection, "DELETE FROM communityMembers WHERE communityId=?;", new ColumnInt("communityId", communityId)); executeUpdate(connection, "DELETE FROM communityMembers WHERE communityId=?;", null, new ColumnInt("communityId", communityId));
executeUpdate(connection, "DELETE FROM communityInvites WHERE communityId=?;", new ColumnInt("communityId", communityId)); executeUpdate(connection, "DELETE FROM communityInvites WHERE communityId=?;", null, new ColumnInt("communityId", communityId));
executeUpdate(connection, "DELETE FROM communityJoinRequests WHERE communityId=?", new ColumnInt("communityId", communityId)); executeUpdate(connection, "DELETE FROM communityJoinRequests WHERE communityId=?", null, new ColumnInt("communityId", communityId));
} }
catch (SQLException e) catch (SQLException e)
{ {

View File

@ -76,9 +76,14 @@ public abstract class RepositoryBase
} }
} }
protected int executeUpdate(Connection connection, String query, Column<?>...columns) protected int executeUpdate(Connection connection, String query, Runnable onSQLError, Column<?>...columns)
{ {
return executeInsert(connection, query, null, columns); return executeInsert(connection, query, null, onSQLError, columns);
}
protected int executeUpdate(String query, Runnable onSQLError, Column<?>...columns)
{
return executeInsert(query, null, columns);
} }
/** /**
@ -92,7 +97,7 @@ public abstract class RepositoryBase
return executeInsert(query, null, columns); return executeInsert(query, null, columns);
} }
protected int executeInsert(Connection connection, String query, ResultSetCallable callable, Column<?>...columns) protected int executeInsert(Connection connection, String query, ResultSetCallable callable, Runnable onSQLError, Column<?>...columns)
{ {
int affectedRows = 0; int affectedRows = 0;
@ -114,6 +119,33 @@ public abstract class RepositoryBase
} }
} }
catch (SQLException exception) catch (SQLException exception)
{
exception.printStackTrace();
if (onSQLError != null)
{
onSQLError.run();
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
return affectedRows;
}
protected int executeInsert(String query, ResultSetCallable callable, Runnable onSQLError, Column<?>...columns)
{
int affectedRows = 0;
// Automatic resource management for handling/closing objects.
try (
Connection connection = getConnection();
)
{
affectedRows = executeInsert(connection, query, callable, onSQLError, columns);
}
catch (SQLException exception)
{ {
exception.printStackTrace(); exception.printStackTrace();
} }
@ -134,7 +166,7 @@ public abstract class RepositoryBase
Connection connection = getConnection(); Connection connection = getConnection();
) )
{ {
affectedRows = executeInsert(connection, query, callable, columns); affectedRows = executeInsert(connection, query, callable, null, columns);
} }
catch (SQLException exception) catch (SQLException exception)
{ {
@ -148,7 +180,7 @@ public abstract class RepositoryBase
return affectedRows; return affectedRows;
} }
protected void executeQuery(PreparedStatement statement, ResultSetCallable callable, Column<?>...columns) protected void executeQuery(PreparedStatement statement, ResultSetCallable callable, Runnable onSQLError, Column<?>...columns)
{ {
try try
{ {
@ -166,6 +198,34 @@ public abstract class RepositoryBase
} }
} }
catch (SQLException exception) catch (SQLException exception)
{
exception.printStackTrace();
if (onSQLError != null)
{
onSQLError.run();
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
protected void executeQuery(PreparedStatement statement, ResultSetCallable callable, Column<?>...columns)
{
executeQuery(statement, callable, null, columns);
}
protected void executeQuery(Connection connection, String query, ResultSetCallable callable, Runnable onSQLError, Column<?>...columns)
{
// Automatic resource management for handling/closing objects.
try (
PreparedStatement preparedStatement = connection.prepareStatement(query)
)
{
executeQuery(preparedStatement, callable, onSQLError, columns);
}
catch (SQLException exception)
{ {
exception.printStackTrace(); exception.printStackTrace();
} }
@ -194,6 +254,25 @@ public abstract class RepositoryBase
} }
} }
protected void executeQuery(String query, ResultSetCallable callable, Runnable onSQLError, Column<?>...columns)
{
// Automatic resource management for handling/closing objects.
try (
Connection connection = getConnection();
)
{
executeQuery(connection, query, callable, onSQLError, columns);
}
catch (SQLException exception)
{
exception.printStackTrace();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
protected void executeQuery(String query, ResultSetCallable callable, Column<?>...columns) protected void executeQuery(String query, ResultSetCallable callable, Column<?>...columns)
{ {
// Automatic resource management for handling/closing objects. // Automatic resource management for handling/closing objects.