simplify database handling across sql flavors (#279)

This commit is contained in:
Jason authored and GitHub committed 2023-08-22 19:44:09 -07:00
1 parent dbd01c52da
commit 7ed005afeb
33 files changed
+188 -210

No files matched your search

@@ -36,6 +36,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.statement.PreparedBatch;
import org.jdbi.v3.core.statement.Update;
@DefaultQualifier(NonNull.class)
public abstract class DatabaseUserManager extends CachingUserManager {
@@ -69,16 +70,10 @@ public abstract class DatabaseUserManager extends CachingUserManager {
@Override
public final void saveSync(final CarbonPlayerCommon player) {
this.jdbi.useTransaction(handle -> {
handle.createUpdate(this.locator.query("insert-player"))
.bind("id", player.uuid())
.bind("muted", player.muted())
.bind("deafened", player.deafened())
.bind("selectedchannel", player.selectedChannelKey())
.bind("displayname", player.displayNameRaw())
.bind("lastwhispertarget", player.lastWhisperTarget())
.bind("whisperreplytarget", player.whisperReplyTarget())
.bind("spying", player.spying())
.execute();
final int inserted = bindPlayerArguments(handle.createUpdate(this.locator.query("insert-player")), player).execute();
if (inserted != 1) {
bindPlayerArguments(handle.createUpdate(this.locator.query("update-player")), player).execute();
}
handle.createUpdate(this.locator.query("clear-ignores"))
.bind("id", player.uuid())
@@ -104,9 +99,18 @@ public abstract class DatabaseUserManager extends CachingUserManager {
}
batch.execute();
}
handle.commit();
});
}
private static Update bindPlayerArguments(final Update update, final CarbonPlayerCommon player) {
return update.bind("id", player.uuid())
.bind("muted", player.muted())
.bind("deafened", player.deafened())
.bind("selectedchannel", player.selectedChannelKey())
.bind("displayname", player.displayNameRaw())
.bind("lastwhispertarget", player.lastWhisperTarget())
.bind("whisperreplytarget", player.whisperReplyTarget())
.bind("spying", player.spying());
}
}
@@ -28,20 +28,19 @@ import org.jdbi.v3.core.locator.internal.ClasspathBuilder;
public final class QueriesLocator {
private static final String PREFIX = "queries/";
private static final Splitter SPLITTER = Splitter.on(';');
private final ClasspathSqlLocator locator = ClasspathSqlLocator.create();
private final DBType dbType;
public QueriesLocator(final DBType dbType) {
this.dbType = dbType;
public QueriesLocator() {
}
public @NonNull String query(final @NonNull String name) {
return this.locate(this.dbType.basePath() + name);
return this.locate(PREFIX + name);
}
public @NonNull List<@NonNull String> queries(final @NonNull String name) {
return SPLITTER.splitToList(this.locator.locate(this.dbType.basePath() + name));
return SPLITTER.splitToList(this.locator.locate(PREFIX + name));
}
private String locate(final String name) {
@@ -17,7 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.users.db.mysql;
package net.draycia.carbon.common.users.db.argument;
import java.sql.Types;
import java.util.UUID;
@@ -25,16 +25,25 @@ import org.jdbi.v3.core.argument.AbstractArgumentFactory;
import org.jdbi.v3.core.argument.Argument;
import org.jdbi.v3.core.config.ConfigRegistry;
// https://github.com/broccolai/tickets/blob/55a23b5fcfdc8e4b4bfa861ea7ec620506bd0dfa/core/src/main/java/broccolai/tickets/core/storage/factory/UUIDArgumentFactory.java
public final class MySQLUUIDArgumentFactory extends AbstractArgumentFactory<UUID> {
public final class BinaryUUIDArgumentFactory extends AbstractArgumentFactory<UUID> {
public MySQLUUIDArgumentFactory() {
super(Types.VARCHAR);
public BinaryUUIDArgumentFactory() {
super(Types.BINARY); // BINARY(16)
}
@Override
public Argument build(final UUID value, final ConfigRegistry config) {
return (position, statement, ctx) -> statement.setString(position, value.toString());
return (position, statement, ctx) -> statement.setBytes(position, unhex(value.toString().replace("-", "")));
}
private static byte[] unhex(final String s) {
final int len = s.length();
final byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
}
@@ -17,7 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.users.db;
package net.draycia.carbon.common.users.db.argument;
import java.sql.Types;
import net.kyori.adventure.text.Component;
@@ -28,15 +28,13 @@ import org.jdbi.v3.core.config.ConfigRegistry;
public final class ComponentArgumentFactory extends AbstractArgumentFactory<Component> {
final GsonComponentSerializer serializer = GsonComponentSerializer.gson();
public ComponentArgumentFactory() {
super(Types.VARCHAR);
}
@Override
public Argument build(final Component value, final ConfigRegistry config) {
return (position, statement, ctx) -> statement.setString(position, this.serializer.serialize(value));
return (position, statement, ctx) -> statement.setString(position, GsonComponentSerializer.gson().serialize(value));
}
}
@@ -17,7 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.users.db;
package net.draycia.carbon.common.users.db.argument;
import java.sql.Types;
import net.kyori.adventure.key.Key;
@@ -0,0 +1,44 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.users.db.mapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import net.draycia.carbon.common.util.FastUuidSansHyphens;
import net.draycia.carbon.common.util.Strings;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.statement.StatementContext;
public final class BinaryUUIDColumnMapper implements ColumnMapper<UUID> {
@Override
public UUID map(final ResultSet rs, final int columnNumber, final StatementContext ctx) throws SQLException {
final byte @Nullable [] bytes = rs.getBytes(columnNumber);
if (bytes != null) {
return FastUuidSansHyphens.parseUuid(Strings.asHexString(bytes));
}
return null;
}
}
@@ -0,0 +1,37 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.users.db.mapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.draycia.carbon.common.util.Strings;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.statement.StatementContext;
public final class ComponentColumnMapper implements ColumnMapper<Component> {
@Override
public Component map(final ResultSet rs, final int columnNumber, final StatementContext ctx) throws SQLException {
return GsonComponentSerializer.gson().deserializeOrNull(Strings.trim(rs.getString(columnNumber)));
}
}
@@ -17,7 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.users.db;
package net.draycia.carbon.common.users.db.mapper;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -28,7 +28,7 @@ import org.intellij.lang.annotations.Subst;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.statement.StatementContext;
public class KeyColumnMapper implements ColumnMapper<Key> {
public final class KeyColumnMapper implements ColumnMapper<Key> {
@Override
public Key map(final ResultSet rs, final int columnNumber, final StatementContext ctx) throws SQLException {
@@ -17,21 +17,20 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.users.db;
package net.draycia.carbon.common.users.db.mapper;
public final class DBType {
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.statement.StatementContext;
private final String basePath;
// todo: I'm not entirely sure that this is necessary, but I don't feel like testing PSQL
public final class NativeUUIDColumnMapper implements ColumnMapper<UUID> {
private DBType(final String basePath) {
this.basePath = basePath;
@Override
public UUID map(final ResultSet rs, final int columnNumber, final StatementContext ctx) throws SQLException {
return rs.getObject(columnNumber, UUID.class);
}
public String basePath() {
return this.basePath;
}
public static final DBType MYSQL = new DBType("queries/mysql/");
public static final DBType POSTGRESQL = new DBType("queries/postgresql/");
}
@@ -17,36 +17,32 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.users.db.postgresql;
package net.draycia.carbon.common.users.db.mapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.util.Strings;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.intellij.lang.annotations.Subst;
import net.kyori.adventure.text.Component;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
public class PostgreSQLPlayerRowMapper implements RowMapper<CarbonPlayerCommon> {
public final class PlayerRowMapper implements RowMapper<CarbonPlayerCommon> {
@Override
public CarbonPlayerCommon map(final ResultSet rs, final StatementContext ctx) throws SQLException {
final @Nullable @Subst("carbon:global") String selectedChannel = Strings.trim(rs.getString("selectedchannel"));
final @Nullable String displayName = Strings.trim(rs.getString("displayname"));
final ColumnMapper<UUID> uuid = ctx.findColumnMapperFor(UUID.class).orElseThrow();
return new CarbonPlayerCommon(
rs.getBoolean("muted"),
rs.getBoolean("deafened"),
selectedChannel == null ? null : Key.key(selectedChannel),
ctx.findColumnMapperFor(Key.class).orElseThrow().map(rs, "selectedchannel", ctx),
null,
rs.getObject("id", UUID.class),
displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName),
rs.getObject("lastwhispertarget", UUID.class),
rs.getObject("whisperreplytarget", UUID.class),
uuid.map(rs, "id", ctx),
ctx.findColumnMapperFor(Component.class).orElseThrow().map(rs, "displayname", ctx),
uuid.map(rs, "lastwhispertarget", ctx),
uuid.map(rs, "whisperreplytarget", ctx),
rs.getBoolean("spying")
);
}
@@ -1,57 +0,0 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.users.db.mysql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.util.Strings;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.intellij.lang.annotations.Subst;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
public class MySQLPlayerRowMapper implements RowMapper<CarbonPlayerCommon> {
@Override
public CarbonPlayerCommon map(final ResultSet rs, final StatementContext ctx) throws SQLException {
final String id = Strings.trim(rs.getString("id"));
final @Nullable String lastWhisperTarget = Strings.trim(rs.getString("lastwhispertarget"));
final @Nullable String whisperReplyTarget = Strings.trim(rs.getString("whisperreplytarget"));
final @Nullable @Subst("carbon:global") String selectedChannel = Strings.trim(rs.getString("selectedchannel"));
final @Nullable String displayName = Strings.trim(rs.getString("displayname"));
return new CarbonPlayerCommon(
rs.getBoolean("muted"),
rs.getBoolean("deafened"),
selectedChannel == null ? null : Key.key(selectedChannel),
null,
UUID.fromString(id),
displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName),
lastWhisperTarget == null ? null : UUID.fromString(lastWhisperTarget),
whisperReplyTarget == null ? null : UUID.fromString(whisperReplyTarget),
rs.getBoolean("spying")
);
}
}
@@ -36,15 +36,19 @@ import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.messaging.packets.PacketFactory;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.common.users.db.ComponentArgumentFactory;
import net.draycia.carbon.common.users.db.DBType;
import net.draycia.carbon.common.users.db.DatabaseUserManager;
import net.draycia.carbon.common.users.db.KeyArgumentFactory;
import net.draycia.carbon.common.users.db.KeyColumnMapper;
import net.draycia.carbon.common.users.db.QueriesLocator;
import net.draycia.carbon.common.users.db.argument.BinaryUUIDArgumentFactory;
import net.draycia.carbon.common.users.db.argument.ComponentArgumentFactory;
import net.draycia.carbon.common.users.db.argument.KeyArgumentFactory;
import net.draycia.carbon.common.users.db.mapper.BinaryUUIDColumnMapper;
import net.draycia.carbon.common.users.db.mapper.ComponentColumnMapper;
import net.draycia.carbon.common.users.db.mapper.KeyColumnMapper;
import net.draycia.carbon.common.users.db.mapper.PlayerRowMapper;
import net.draycia.carbon.common.util.ConcurrentUtil;
import net.draycia.carbon.common.util.SQLDrivers;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -67,7 +71,7 @@ public final class MySQLUserManager extends DatabaseUserManager {
) {
super(
jdbi,
new QueriesLocator(DBType.MYSQL),
new QueriesLocator(),
logger,
profileResolver,
playerInjector,
@@ -165,10 +169,12 @@ public final class MySQLUserManager extends DatabaseUserManager {
final Jdbi jdbi = Jdbi.create(dataSource)
.registerArgument(new ComponentArgumentFactory())
.registerArgument(new KeyArgumentFactory())
.registerArgument(new MySQLUUIDArgumentFactory())
.registerArgument(new BinaryUUIDArgumentFactory())
.registerColumnMapper(Key.class, new KeyColumnMapper())
.registerColumnMapper(Component.class, new ComponentColumnMapper())
.registerColumnMapper(UUID.class, new BinaryUUIDColumnMapper())
.registerRowMapper(new PlayerRowMapper())
.registerArrayType(UUID.class, "uuid")
.registerColumnMapper(new KeyColumnMapper())
.registerRowMapper(new MySQLPlayerRowMapper())
.installPlugin(new SqlObjectPlugin());
return new MySQLUserManager(jdbi, this.logger, this.profileResolver, this.playerInjector, this.messagingManager, this.packetFactory, this.channelRegistry);
@@ -35,15 +35,18 @@ import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.messaging.packets.PacketFactory;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.common.users.db.ComponentArgumentFactory;
import net.draycia.carbon.common.users.db.DBType;
import net.draycia.carbon.common.users.db.DatabaseUserManager;
import net.draycia.carbon.common.users.db.KeyArgumentFactory;
import net.draycia.carbon.common.users.db.KeyColumnMapper;
import net.draycia.carbon.common.users.db.QueriesLocator;
import net.draycia.carbon.common.users.db.argument.ComponentArgumentFactory;
import net.draycia.carbon.common.users.db.argument.KeyArgumentFactory;
import net.draycia.carbon.common.users.db.mapper.ComponentColumnMapper;
import net.draycia.carbon.common.users.db.mapper.KeyColumnMapper;
import net.draycia.carbon.common.users.db.mapper.NativeUUIDColumnMapper;
import net.draycia.carbon.common.users.db.mapper.PlayerRowMapper;
import net.draycia.carbon.common.util.ConcurrentUtil;
import net.draycia.carbon.common.util.SQLDrivers;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -67,7 +70,7 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
) {
super(
jdbi,
new QueriesLocator(DBType.POSTGRESQL),
new QueriesLocator(),
logger,
profileResolver,
playerInjector,
@@ -166,8 +169,10 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
final Jdbi jdbi = Jdbi.create(dataSource)
.registerArgument(new ComponentArgumentFactory())
.registerArgument(new KeyArgumentFactory())
.registerColumnMapper(new KeyColumnMapper())
.registerRowMapper(new PostgreSQLPlayerRowMapper())
.registerColumnMapper(Key.class, new KeyColumnMapper())
.registerColumnMapper(Component.class, new ComponentColumnMapper())
.registerColumnMapper(UUID.class, new NativeUUIDColumnMapper())
.registerRowMapper(new PlayerRowMapper())
.installPlugin(new SqlObjectPlugin())
.installPlugin(new PostgresPlugin());