diff --git a/bot/build.gradle.kts b/bot/build.gradle.kts index 2a9e1b6..125df5d 100644 --- a/bot/build.gradle.kts +++ b/bot/build.gradle.kts @@ -17,23 +17,18 @@ dependencies { implementation(project(":plugin-api")) // discord - implementation("de.chojo", "cjda-util", "2.7.8+alpha.22-DEV") { + implementation("de.chojo", "cjda-util", "2.9.8+jda-5.0.0") { exclude(module = "opus-java") } - implementation("io.javalin", "javalin-bundle", "4.4.0") + implementation(libs.javalin.bundle) implementation("net.lingala.zip4j", "zip4j", "2.11.2") // database - implementation("de.chojo.sadu", "sadu-queries", "1.2.0") - implementation("de.chojo.sadu", "sadu-updater", "1.2.0") - implementation("de.chojo.sadu", "sadu-datasource", "1.2.0") - implementation("de.chojo.sadu", "sadu-postgresql", "1.2.0") + implementation(libs.bundles.sadu) implementation("org.postgresql", "postgresql", "42.3.3") // Logging - implementation("org.slf4j", "slf4j-api", "2.0.3") - implementation("org.apache.logging.log4j", "log4j-core", "2.19.0") - implementation("org.apache.logging.log4j", "log4j-slf4j2-impl", "2.19.0") + implementation(libs.bundles.logging) implementation("club.minnced", "discord-webhooks", "0.7.5") // unit testing diff --git a/bot/src/main/java/de/chojo/gamejam/Bot.java b/bot/src/main/java/de/chojo/gamejam/Bot.java index 412ca5a..26d0fa6 100644 --- a/bot/src/main/java/de/chojo/gamejam/Bot.java +++ b/bot/src/main/java/de/chojo/gamejam/Bot.java @@ -23,14 +23,14 @@ import de.chojo.jdautil.interactions.dispatching.InteractionHub; import de.chojo.jdautil.localization.ILocalizer; import de.chojo.jdautil.localization.Localizer; -import de.chojo.sadu.databases.PostgreSql; +import de.chojo.sadu.core.exceptions.ExceptionTransformer; import de.chojo.sadu.datasource.DataSourceCreator; -import de.chojo.sadu.exceptions.ExceptionTransformer; -import de.chojo.sadu.mapper.PostgresqlMapper; import de.chojo.sadu.mapper.RowMapperRegistry; +import de.chojo.sadu.postgresql.databases.PostgreSql; +import de.chojo.sadu.postgresql.mapper.PostgresqlMapper; +import de.chojo.sadu.queries.api.configuration.QueryConfiguration; import de.chojo.sadu.updater.QueryReplacement; import de.chojo.sadu.updater.SqlUpdater; -import de.chojo.sadu.wrapper.QueryBuilderConfig; import net.dv8tion.jda.api.interactions.DiscordLocale; import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.requests.RestAction; @@ -128,7 +128,7 @@ private void buildLocale() { localizer = Localizer.builder(DiscordLocale.ENGLISH_US) .addLanguage(DiscordLocale.GERMAN) .withLanguageProvider(guild -> Optional.ofNullable(guilds.guild(guild).settings().locale()) - .map(DiscordLocale::from)) + .map(DiscordLocale::from)) .build(); } @@ -144,48 +144,48 @@ private void buildCommands() { new Server(guilds, serverService, configuration), new ServerAdmin(guilds, serverService)) .withPagination(builder -> builder.withLocalizer(localizer) - .withCache(cache -> cache.expireAfterAccess(30, TimeUnit.MINUTES))) + .withCache(cache -> cache.expireAfterAccess(30, TimeUnit.MINUTES))) .withMenuService(builder -> builder.withLocalizer(localizer) - .withCache(cache -> cache.expireAfterAccess(30, TimeUnit.MINUTES))) + .withCache(cache -> cache.expireAfterAccess(30, TimeUnit.MINUTES))) .withModalService(builder -> builder.withLocalizer(localizer)) .build(); } private void initBot() { shardManager = DefaultShardManagerBuilder.createDefault(configuration.baseSettings().token()) - .enableIntents( - GatewayIntent.GUILD_MEMBERS, - GatewayIntent.DIRECT_MESSAGES, - GatewayIntent.GUILD_MESSAGES) - .setMemberCachePolicy(MemberCachePolicy.DEFAULT) - .setEventPool(Executors.newScheduledThreadPool(5, createThreadFactory("Event Worker"))) - .build(); + .enableIntents( + GatewayIntent.GUILD_MEMBERS, + GatewayIntent.DIRECT_MESSAGES, + GatewayIntent.GUILD_MESSAGES) + .setMemberCachePolicy(MemberCachePolicy.DEFAULT) + .setEventPool(Executors.newScheduledThreadPool(5, createThreadFactory("Event Worker"))) + .build(); RestAction.setDefaultFailure(throwable -> log.error("Unhandled exception occured: ", throwable)); - serverService.inject(new Teams(dataSource, guilds, shardManager)); + serverService.inject(new Teams(guilds, shardManager)); serverService.syncVelocity(); } private void initDb() throws IOException, SQLException { var mapperRegistry = new RowMapperRegistry(); mapperRegistry.register(PostgresqlMapper.getDefaultMapper()); - QueryBuilderConfig.setDefault(QueryBuilderConfig.builder() - .withExceptionHandler(err -> log.error(ExceptionTransformer.prettyException(err), err)) - .withExecutor(createExecutor("DataWorker")) - .rowMappers(mapperRegistry) - .build()); - dataSource = DataSourceCreator.create(PostgreSql.get()) .configure(config -> { config.host(configuration.database().host()) - .port(configuration.database().port()) - .database(configuration.database().database()) - .user(configuration.database().user()) - .password(configuration.database().password()); + .port(configuration.database().port()) + .database(configuration.database().database()) + .user(configuration.database().user()) + .password(configuration.database().password()); }) .create() .forSchema(configuration.database().schema()) .build(); + QueryConfiguration.setDefault(QueryConfiguration.builder(dataSource) + .setExceptionHandler(err -> log.error(ExceptionTransformer.prettyException(err), err)) + .setRowMapperRegistry(mapperRegistry) + .build()); + + SqlUpdater.builder(dataSource, PostgreSql.get()) .setReplacements(new QueryReplacement("gamejam", configuration.database().schema())) .setSchemas(configuration.database().schema()) diff --git a/bot/src/main/java/de/chojo/gamejam/commands/serveradmin/handler/info/Detailed.java b/bot/src/main/java/de/chojo/gamejam/commands/serveradmin/handler/info/Detailed.java index 2889ef4..f47d5e0 100644 --- a/bot/src/main/java/de/chojo/gamejam/commands/serveradmin/handler/info/Detailed.java +++ b/bot/src/main/java/de/chojo/gamejam/commands/serveradmin/handler/info/Detailed.java @@ -14,6 +14,7 @@ import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.utils.messages.MessageEditData; import java.util.Collections; import java.util.concurrent.CompletableFuture; @@ -55,8 +56,8 @@ public void onSlashCommand(SlashCommandInteractionEvent event, EventContext cont context.registerPage(new ListPageBag<>(servers) { @Override - public CompletableFuture buildPage() { - return currentElement().detailStatus(context); + public CompletableFuture buildPage() { + return currentElement().detailStatus(context).thenApply(MessageEditData::fromEmbeds); } }); } diff --git a/bot/src/main/java/de/chojo/gamejam/commands/settings/handler/Locale.java b/bot/src/main/java/de/chojo/gamejam/commands/settings/handler/Locale.java index 23c3de6..0dd3c67 100644 --- a/bot/src/main/java/de/chojo/gamejam/commands/settings/handler/Locale.java +++ b/bot/src/main/java/de/chojo/gamejam/commands/settings/handler/Locale.java @@ -27,7 +27,7 @@ public void onSlashCommand(SlashCommandInteractionEvent event, EventContext cont .ifPresentOrElse(language -> { settings.locale(language.getLocale()); event.reply(context.localize("command.settings.locale.message.updated")).setEphemeral(true).queue(); - context.commandHub().refreshGuildCommands(event.getGuild()); + context.interactionHub().refreshGuildCommands(event.getGuild()); }, () -> event.reply(context.localize("command.settings.locale.message.invalid")).setEphemeral(true).queue()); } diff --git a/bot/src/main/java/de/chojo/gamejam/commands/team/handler/List.java b/bot/src/main/java/de/chojo/gamejam/commands/team/handler/List.java index f80c9d9..2e41dd6 100644 --- a/bot/src/main/java/de/chojo/gamejam/commands/team/handler/List.java +++ b/bot/src/main/java/de/chojo/gamejam/commands/team/handler/List.java @@ -13,6 +13,7 @@ import de.chojo.jdautil.wrapper.EventContext; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.utils.messages.MessageEditData; import java.util.concurrent.CompletableFuture; @@ -35,8 +36,8 @@ public void onSlashCommand(SlashCommandInteractionEvent event, EventContext cont context.registerPage(new PrivateListPageBag<>(jam.teams().teams(), event.getUser().getIdLong()) { @Override - public CompletableFuture buildPage() { - return CompletableFuture.supplyAsync(() -> currentElement().profileEmbed(context.guildLocalizer())); + public CompletableFuture buildPage() { + return CompletableFuture.supplyAsync(() -> MessageEditData.fromEmbeds(currentElement().profileEmbed(context.guildLocalizer()))); } }, true); } diff --git a/bot/src/main/java/de/chojo/gamejam/commands/vote/handler/Ranking.java b/bot/src/main/java/de/chojo/gamejam/commands/vote/handler/Ranking.java index b5d6c89..404d10a 100644 --- a/bot/src/main/java/de/chojo/gamejam/commands/vote/handler/Ranking.java +++ b/bot/src/main/java/de/chojo/gamejam/commands/vote/handler/Ranking.java @@ -13,6 +13,8 @@ import de.chojo.jdautil.wrapper.EventContext; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.utils.messages.MessageCreateData; +import net.dv8tion.jda.api.utils.messages.MessageEditData; import java.util.concurrent.CompletableFuture; @@ -43,13 +45,13 @@ public void onSlashCommand(SlashCommandInteractionEvent event, EventContext cont var pageBag = new ListPageBag<>(ranking) { @Override - public CompletableFuture buildPage() { + public CompletableFuture buildPage() { var teamVote = currentElement(); var embed = new LocalizedEmbedBuilder(context.guildLocalizer()) .setTitle(teamVote.rank() + " | " + teamVote.team().meta().name()) .addField("command.votes.ranking.embed.votes", String.valueOf(teamVote.votes()), true) .build(); - return CompletableFuture.completedFuture(embed); + return CompletableFuture.completedFuture(MessageEditData.fromEmbeds(embed)); } }; context.registerPage(pageBag, true); diff --git a/bot/src/main/java/de/chojo/gamejam/data/access/Guilds.java b/bot/src/main/java/de/chojo/gamejam/data/access/Guilds.java index 98cd3a2..d945561 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/access/Guilds.java +++ b/bot/src/main/java/de/chojo/gamejam/data/access/Guilds.java @@ -18,7 +18,7 @@ public class Guilds { private final Cache cache = CacheBuilder.newBuilder().expireAfterAccess(10, TimeUnit.MINUTES) - .build(); + .build(); private final DataSource dataSource; public Guilds(DataSource dataSource) { @@ -31,15 +31,16 @@ public JamGuild guild(Interaction interaction) { public JamGuild guild(Guild guild) { try { - return cache.get(guild.getIdLong(), () -> new JamGuild(dataSource, guild)).refresh(guild); + return cache.get(guild.getIdLong(), () -> new JamGuild(guild)).refresh(guild); } catch (ExecutionException e) { throw new RuntimeException(e); } } + public JamGuild guild(long guild) { try { - return cache.get(guild, () -> new JamGuild(dataSource, guild)); + return cache.get(guild, () -> new JamGuild(guild)); } catch (ExecutionException e) { throw new RuntimeException(e); } diff --git a/bot/src/main/java/de/chojo/gamejam/data/access/Teams.java b/bot/src/main/java/de/chojo/gamejam/data/access/Teams.java index d4f4727..611f472 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/access/Teams.java +++ b/bot/src/main/java/de/chojo/gamejam/data/access/Teams.java @@ -7,36 +7,34 @@ package de.chojo.gamejam.data.access; import de.chojo.gamejam.data.dao.guild.jams.jam.teams.Team; -import de.chojo.sadu.base.QueryFactory; import net.dv8tion.jda.api.sharding.ShardManager; import org.slf4j.Logger; -import javax.sql.DataSource; import java.util.Optional; +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; import static org.slf4j.LoggerFactory.getLogger; -public class Teams extends QueryFactory { +public class Teams { private static final Logger log = getLogger(Teams.class); private final Guilds guilds; private final ShardManager shardManager; - public Teams(DataSource dataSource, Guilds guilds, ShardManager shardManager) { - super(dataSource); + public Teams(Guilds guilds, ShardManager shardManager) { this.guilds = guilds; this.shardManager = shardManager; } public Optional byId(int id) { - return builder(Team.class) - .query(""" - SELECT guild_id - FROM team t - LEFT JOIN jam j ON t.jam_id = j.id - WHERE t.id = ? - """) - .parameter(stmt -> stmt.setInt(id)) - .readRow(row -> { + return query(""" + SELECT guild_id + FROM team t + LEFT JOIN jam j ON t.jam_id = j.id + WHERE t.id = ? + """) + .single(call().bind(id)) + .map(row -> { var guildId = row.getLong("guild_id"); var guildById = shardManager.getGuildById(guildId); if (guildById == null) { @@ -44,7 +42,6 @@ public Optional byId(int id) { return guilds.guild(guildId).teams().byId(id).orElse(null); } return guilds.guild(guildById).teams().byId(id).orElse(null); - }) - .firstSync(); + }).first(); } } diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/JamGuild.java b/bot/src/main/java/de/chojo/gamejam/data/dao/JamGuild.java index 7316e22..cf77893 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/JamGuild.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/JamGuild.java @@ -10,27 +10,25 @@ import de.chojo.gamejam.data.dao.guild.Jams; import de.chojo.gamejam.data.dao.guild.Settings; import de.chojo.gamejam.data.dao.guild.Teams; -import de.chojo.sadu.base.QueryFactory; import net.dv8tion.jda.api.entities.Guild; -import javax.sql.DataSource; +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; -public class JamGuild extends QueryFactory { +public class JamGuild { private final long guildId; private Guild guild; private final Jams jams; private final Teams teams; - public JamGuild(DataSource dataSource, Guild guild) { - super(dataSource); + public JamGuild(Guild guild) { this.guild = guild; this.guildId = guild.getIdLong(); jams = new Jams(this); teams = new Teams(this); } - public JamGuild(DataSource dataSource, long guild) { - super(dataSource); + public JamGuild(long guild) { this.guild = null; this.guildId = guild; jams = new Jams(this); @@ -38,20 +36,18 @@ public JamGuild(DataSource dataSource, long guild) { } public Settings settings() { - return builder(Settings.class) - .query("SELECT manager_role, locale FROM settings WHERE guild_id = ?") - .parameter(p -> p.setLong(guild.getIdLong())) - .readRow(r -> new Settings(this, guild.getIdLong(), r.getString("locale"), r.getLong("manager_role"))) - .firstSync() - .orElseGet(() -> new Settings(this, guild.getIdLong())); + return query("SELECT manager_role, locale FROM settings WHERE guild_id = ?") + .single(call().bind(guild.getIdLong())) + .map(r -> new Settings(guild.getIdLong(), r.getString("locale"), r.getLong("manager_role"))) + .first() + .orElseGet(() -> new Settings(guild.getIdLong())); } public JamSettings jamSettings() { - return builder(JamSettings.class) - .query("SELECT jam_role, team_size FROM jam_settings WHERE guild_id = ?") - .parameter(p -> p.setLong(guild.getIdLong())) - .readRow(r -> new JamSettings(this, r.getInt("team_size"), r.getLong("jam_role"))) - .firstSync() + return query("SELECT jam_role, team_size FROM jam_settings WHERE guild_id = ?") + .single(call().bind(guild.getIdLong())) + .map(r -> new JamSettings(this, r.getInt("team_size"), r.getLong("jam_role"))) + .first() .orElseGet(() -> new JamSettings(this)); } diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/JamSettings.java b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/JamSettings.java index 00698c6..c83af5c 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/JamSettings.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/JamSettings.java @@ -7,14 +7,15 @@ package de.chojo.gamejam.data.dao.guild; import de.chojo.gamejam.data.dao.JamGuild; -import de.chojo.sadu.base.QueryFactory; -import de.chojo.sadu.exceptions.ThrowingConsumer; -import de.chojo.sadu.wrapper.util.ParamBuilder; +import de.chojo.sadu.queries.api.call.Call; import net.dv8tion.jda.api.entities.Role; -import java.sql.SQLException; +import java.util.function.Function; -public class JamSettings extends QueryFactory { +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; + +public class JamSettings { private final JamGuild jamGuild; private int teamSize; private long jamRole; @@ -24,7 +25,6 @@ public JamSettings(JamGuild jamGuild) { } public JamSettings(JamGuild jamGuild, int teamSize, long jamRole) { - super(jamGuild); this.jamGuild = jamGuild; this.teamSize = teamSize; this.jamRole = jamRole; @@ -39,31 +39,26 @@ public long jamRole() { } public void teamSize(int teamSize) { - if (set("team_size", stmt -> stmt.setInt(teamSize))) { + if (set("team_size", stmt -> stmt.bind(teamSize))) { this.teamSize = teamSize; } } public void jamRole(Role jamRole) { - if (set("jam_role", stmt -> stmt.setLong(jamRole.getIdLong()))) { + if (set("jam_role", stmt -> stmt.bind(jamRole.getIdLong()))) { this.jamRole = jamRole.getIdLong(); } } - private boolean set(String column, ThrowingConsumer stmt) { - return builder() - .query(""" - INSERT INTO jam_settings(guild_id, %s) VALUES(?,?) - ON CONFLICT(guild_id) - DO UPDATE - SET %s = excluded.%s - """, column, column, column) - .parameter(p -> { - p.setLong(jamGuild.guildId()); - stmt.accept(p); - }) + private boolean set(String column, Function stmt) { + return query(""" + INSERT INTO jam_settings(guild_id, %s) VALUES(?,?) + ON CONFLICT(guild_id) + DO UPDATE + SET %s = excluded.%s + """, column, column, column) + .single(stmt.apply(call().bind(jamGuild.guildId()))) .update() - .sendSync() .changed(); } } diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Jams.java b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Jams.java index f3b7ff7..8ad97fa 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Jams.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Jams.java @@ -9,112 +9,111 @@ import de.chojo.gamejam.data.dao.JamGuild; import de.chojo.gamejam.data.dao.guild.jams.Jam; import de.chojo.gamejam.data.wrapper.jam.JamCreator; -import de.chojo.sadu.base.QueryFactory; +import de.chojo.sadu.queries.api.configuration.QueryConfiguration; import java.util.Optional; -public class Jams extends QueryFactory { +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; + +public class Jams { private final JamGuild jamGuild; public Jams(JamGuild jamGuild) { - super(jamGuild); this.jamGuild = jamGuild; } public void create(JamCreator jamCreator) { - builder(Integer.class) - .query("INSERT INTO jam(guild_id) VALUES(?) RETURNING id") - .parameter(stmt -> stmt.setLong(jamGuild.guildId())) - .readRow(r -> r.getInt("id")) - .firstSync() - .map(id -> { - var times = jamCreator.times(); - builder().query(""" - INSERT INTO jam_time( - jam_id, - registration_start, registration_end, - jam_start, jam_end, - zone_id) - VALUES (?,?,?,?,?,?) - """) - .parameter(stmt -> stmt.setInt(id) - .setTimestamp(times.registration().startTimestamp()) - .setTimestamp(times.registration().endTimestamp()) - .setTimestamp(times.jam().startTimestamp()) - .setTimestamp(times.jam().endTimestamp()) - .setString(times.zone().getId()) - ).append() - .query("INSERT INTO jam_meta(jam_id, topic) VALUES(?,?)") - .parameter(stmt -> stmt.setInt(id).setString(jamCreator.topic())) - .append() - .query("INSERT INTO jam_state(jam_id) VALUES(?)") - .parameter(stmt -> stmt.setInt(id)) - .insert() - .sendSync(); - return id; - }); + try (var conf = QueryConfiguration.getDefault().withSingleTransaction()) { + conf.query("INSERT INTO jam(guild_id) VALUES(?) RETURNING id") + .single(call().bind(jamGuild.guildId())) + .map(r -> r.getInt("id")) + .first() + .map(id -> { + var times = jamCreator.times(); + conf.query(""" + INSERT INTO jam_time( + jam_id, + registration_start, registration_end, + jam_start, jam_end, + zone_id) + VALUES (?,?,?,?,?,?) + """) + .single(call().bind(id) + .bind(times.registration().startTimestamp()) + .bind(times.registration().endTimestamp()) + .bind(times.jam().startTimestamp()) + .bind(times.jam().endTimestamp()) + .bind(times.zone().getId()) + ) + .insert(); + conf.query("INSERT INTO jam_meta(jam_id, topic) VALUES(?,?)") + .single(call().bind(id).bind(jamCreator.topic())) + .insert(); + conf.query("INSERT INTO jam_state(jam_id) VALUES(?)") + .single(call().bind(id)) + .insert(); + return id; + }); + } } public Optional getCurrentJam() { - return builder(Jam.class) - .query(""" - SELECT - id - FROM jam_time t - LEFT JOIN jam j ON j.id = t.jam_id - WHERE registration_start < NOW() AT TIME ZONE 'utc' - AND t.jam_end > NOW() AT TIME ZONE 'utc' - AND guild_id = ?; - """) - .parameter(stmt -> stmt.setLong(jamGuild.guildId())) - .readRow(r -> new Jam(jamGuild, r.getInt("id"))) - .firstSync(); + return query(""" + SELECT + id + FROM jam_time t + LEFT JOIN jam j ON j.id = t.jam_id + WHERE registration_start < NOW() AT TIME ZONE 'utc' + AND t.jam_end > NOW() AT TIME ZONE 'utc' + AND guild_id = ?; + """) + .single(call().bind(jamGuild.guildId())) + .map(r -> new Jam(jamGuild, r.getInt("id"))) + .first(); } public Optional nextOrCurrent() { - return builder(Integer.class) - .query(""" - SELECT - id - FROM jam_time t - LEFT JOIN jam j ON j.id = t.jam_id - LEFT JOIN jam_state js ON j.id = js.jam_id - WHERE js.active OR t.jam_end > NOW() AT TIME ZONE 'utc' - AND guild_id = ? - ORDER BY t.jam_end ASC - LIMIT 1; - """) - .parameter(stmt -> stmt.setLong(jamGuild.guildId())) - .readRow(r -> r.getInt("id")) - .firstSync() + return query(""" + SELECT + id + FROM jam_time t + LEFT JOIN jam j ON j.id = t.jam_id + LEFT JOIN jam_state js ON j.id = js.jam_id + WHERE js.active OR t.jam_end > NOW() AT TIME ZONE 'utc' + AND guild_id = ? + ORDER BY t.jam_end ASC + LIMIT 1; + """) + .single(call().bind(jamGuild.guildId())) + .map(r -> r.getInt("id")) + .first() .flatMap(this::byId); } public Optional activeJam() { - return builder(Integer.class) - .query(""" - SELECT - id - FROM jam_state s - LEFT JOIN jam j ON j.id = s.jam_id - WHERE s.active - AND guild_id = ? - LIMIT 1; - """) - .parameter(stmt -> stmt.setLong(jamGuild.guildId())) - .readRow(r -> r.getInt("id")) - .firstSync() + return query(""" + SELECT + id + FROM jam_state s + LEFT JOIN jam j ON j.id = s.jam_id + WHERE s.active + AND guild_id = ? + LIMIT 1; + """) + .single(call().bind(jamGuild.guildId())) + .map(r -> r.getInt("id")) + .first() .flatMap(this::byId); } public Optional byId(int id) { - return builder(Jam.class) - .query(""" - SELECT id FROM jam WHERE guild_id = ? AND id = ? - """) - .parameter(stmt -> stmt.setLong(jamGuild.guildId()).setInt(id)) - .readRow(row -> new Jam(jamGuild, id)) - .firstSync(); + return query(""" + SELECT id FROM jam WHERE guild_id = ? AND id = ? + """) + .single(call().bind(jamGuild.guildId()).bind(id)) + .map(row -> new Jam(jamGuild, id)) + .first(); } } diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Settings.java b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Settings.java index 6f6ebd3..6a8c7d3 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Settings.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Settings.java @@ -6,24 +6,23 @@ package de.chojo.gamejam.data.dao.guild; -import de.chojo.sadu.base.QueryFactory; -import de.chojo.sadu.exceptions.ThrowingConsumer; -import de.chojo.sadu.wrapper.util.ParamBuilder; +import de.chojo.sadu.queries.api.call.Call; -import java.sql.SQLException; +import java.util.function.Function; -public class Settings extends QueryFactory { +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; + +public class Settings { private final long guildId; private String locale = "en_US"; private long orgaRole = 0; - public Settings(QueryFactory queryFactory, long guildId) { - super(queryFactory); + public Settings(long guildId) { this.guildId = guildId; } - public Settings(QueryFactory queryFactory, long guildId, String locale, long orgaRole) { - super(queryFactory); + public Settings(long guildId, String locale, long orgaRole) { this.guildId = guildId; this.locale = locale; this.orgaRole = orgaRole; @@ -38,13 +37,13 @@ public long orgaRole() { } public void locale(String locale) { - if (set("locale", stmt -> stmt.setString(locale))) { + if (set("locale", stmt -> stmt.bind(locale))) { this.locale = locale; } } public void orgaRole(long orgaRole) { - if (set("manager_role", stmt -> stmt.setLong(orgaRole))) { + if (set("manager_role", stmt -> stmt.bind(orgaRole))) { this.orgaRole = orgaRole; } } @@ -53,20 +52,15 @@ public Long guildId() { return guildId; } - private boolean set(String column, ThrowingConsumer stmt) { - return builder() - .query(""" - INSERT INTO settings(guild_id, %s) VALUES(?,?) - ON CONFLICT(guild_id) - DO UPDATE - SET %s = excluded.%s - """, column, column, column) - .parameter(p -> { - p.setLong(guildId()); - stmt.accept(p); - }) + private boolean set(String column, Function stmt) { + return query(""" + INSERT INTO settings(guild_id, %s) VALUES(?,?) + ON CONFLICT(guild_id) + DO UPDATE + SET %s = excluded.%s + """, column, column, column) + .single(stmt.apply(call().bind(guildId()))) .update() - .sendSync() .changed(); } } diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Teams.java b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Teams.java index f2d375d..849a753 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Teams.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/Teams.java @@ -8,26 +8,25 @@ import de.chojo.gamejam.data.dao.JamGuild; import de.chojo.gamejam.data.dao.guild.jams.jam.teams.Team; -import de.chojo.sadu.base.QueryFactory; import java.util.Optional; -public class Teams extends QueryFactory { +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; + +public class Teams { private final JamGuild guild; public Teams(JamGuild guild) { - super(guild); this.guild = guild; } public Optional byId(int id) { - return builder(Team.class) - .query("SELECT jam_id, id, team_name FROM team t LEFT JOIN team_meta m ON t.id = m.team_id WHERE id = ?") - .parameter(stmt -> stmt.setInt(id)) - .readRow(r -> { + return query("SELECT jam_id, id, team_name FROM team t LEFT JOIN team_meta m ON t.id = m.team_id WHERE id = ?") + .single(call().bind(id)) + .map(r -> { var team = guild.jams().byId(r.getInt("jam_id")).orElse(null); return new Team(team, r.getInt("id")); - }) - .firstSync(); + }).first(); } } diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/Jam.java b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/Jam.java index 855e4c5..50bfd27 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/Jam.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/Jam.java @@ -9,69 +9,65 @@ import de.chojo.gamejam.data.dao.JamGuild; import de.chojo.gamejam.data.dao.guild.jams.jam.JamMeta; import de.chojo.gamejam.data.dao.guild.jams.jam.JamState; -import de.chojo.gamejam.data.dao.guild.jams.jam.JamTimes; import de.chojo.gamejam.data.dao.guild.jams.jam.JamTeams; +import de.chojo.gamejam.data.dao.guild.jams.jam.JamTimes; +import de.chojo.gamejam.data.dao.guild.jams.jam.teams.team.TeamVote; import de.chojo.gamejam.data.dao.guild.jams.jam.user.JamUser; import de.chojo.gamejam.data.wrapper.jam.TimeFrame; -import de.chojo.gamejam.data.dao.guild.jams.jam.teams.team.TeamVote; -import de.chojo.sadu.base.QueryFactory; import net.dv8tion.jda.api.entities.Member; import java.time.ZoneId; import java.util.List; -public class Jam extends QueryFactory { +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; + +public class Jam { private final JamGuild jamGuild; private final int id; private final JamTeams jamTeams; public Jam(JamGuild jamGuild, int id) { - super(jamGuild); this.jamGuild = jamGuild; this.id = id; jamTeams = new JamTeams(this); } public void register(Member member) { - builder() - .query("INSERT INTO jam_registrations(jam_id, user_id) VALUES(?,?) ON CONFLICT DO NOTHING") - .parameter(stmt -> stmt.setInt(id).setLong(member.getIdLong())) - .insert() - .sendSync(); + query("INSERT INTO jam_registrations(jam_id, user_id) VALUES(?,?) ON CONFLICT DO NOTHING") + .single(call().bind(id).bind(member.getIdLong())) + .insert(); } public JamMeta meta() { - return builder(JamMeta.class) - .query(""" - SELECT topic FROM jam_meta WHERE jam_id = ? - """) - .parameter(stmt -> stmt.setInt(id)) - .readRow(row -> new JamMeta(row.getString("topic"))) - .firstSync() + return query(""" + SELECT topic FROM jam_meta WHERE jam_id = ? + """) + .single(call().bind(id)) + .map(row -> new JamMeta(row.getString("topic"))) + .first() .orElseThrow(); } public List registrations() { - return builder(Long.class) - .query("SELECT user_id FROM jam_registrations WHERE jam_id = ?") - .parameter(stmt -> stmt.setInt(id)) - .readRow(row -> row.getLong("user_id")) - .allSync(); + return query("SELECT user_id FROM jam_registrations WHERE jam_id = ?") + .single(call().bind(id)) + .map(row -> row.getLong("user_id")) + .all(); } public JamTimes times() { - return builder(JamTimes.class) - .query(""" - SELECT registration_start, - registration_end, - zone_id, - jam_start, - jam_end - FROM jam_time - WHERE jam_id = ? - """) - .parameter(stmt -> stmt.setInt(id)) - .readRow(r -> { + return query(""" + SELECT registration_start, + registration_end, + zone_id, + jam_start, + jam_end + FROM jam_time + WHERE jam_id = ? + """) + .single(call().bind(id)) + .map(r -> { var zone = ZoneId.of(r.getString("zone_id")); return new JamTimes(zone, TimeFrame.fromTimestamp(r.getTimestamp("registration_start"), @@ -80,36 +76,34 @@ public JamTimes times() { r.getTimestamp("jam_end"), zone) ); }) - .firstSync() + .first() .orElseThrow(); } public JamState state() { - return builder(JamState.class) - .query(""" - SELECT active, - voting, - ended - FROM jam_state - WHERE jam_id = ? - """) - .parameter(stmt -> stmt.setInt(jamId())) - .readRow(r -> new JamState(this, r.getBoolean("active"), r.getBoolean("voting"), r.getBoolean("ended"))) - .firstSync() + return query(""" + SELECT active, + voting, + ended + FROM jam_state + WHERE jam_id = ? + """) + .single(call().bind(jamId())) + .map(r -> new JamState(this, r.getBoolean("active"), r.getBoolean("voting"), r.getBoolean("ended"))) + .first() .orElseThrow(); } public List votes() { - return builder(TeamVote.class) - .query(""" - SELECT - rank, team_id, points, jam_id - FROM team_ranking r - WHERE r.jam_id = ? - """) - .parameter(p -> p.setInt(id)) - .readRow(r -> new TeamVote(jamTeams.byId(r.getInt("team_id")).orElseThrow(), r.getInt("rank"), r.getInt("points"))) - .allSync(); + return query(""" + SELECT + rank, team_id, points, jam_id + FROM team_ranking r + WHERE r.jam_id = ? + """) + .single(call().bind(id)) + .map(r -> new TeamVote(jamTeams.byId(r.getInt("team_id")).orElseThrow(), r.getInt("rank"), r.getInt("points"))) + .all(); } public JamTeams teams() { diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/JamState.java b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/JamState.java index 799f5f9..46adac6 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/JamState.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/JamState.java @@ -7,16 +7,17 @@ package de.chojo.gamejam.data.dao.guild.jams.jam; import de.chojo.gamejam.data.dao.guild.jams.Jam; -import de.chojo.sadu.base.QueryFactory; -public class JamState extends QueryFactory { +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; + +public class JamState { private final Jam jam; private boolean active; private boolean voting; private boolean ended; public JamState(Jam jam, boolean active, boolean voting, boolean ended) { - super(jam); this.jam = jam; this.active = active; this.voting = voting; @@ -53,27 +54,25 @@ public void ended(boolean ended) { } } - public void finish(){ + public void finish() { active(false); voting(false); ended(true); for (var team : jam.teams().teams()) { - team.delete(); + team.delete(); } } private boolean set(String column, boolean state) { - return builder() - .query(""" - INSERT INTO jam_state(jam_id, %s) VALUES(?,?) - ON CONFLICT(jam_id) - DO UPDATE - SET %s = excluded.%s - """, column, column, column) - .parameter(p -> p.setInt(jam.jamId()).setBoolean(state)) + return query(""" + INSERT INTO jam_state(jam_id, %s) VALUES(?,?) + ON CONFLICT(jam_id) + DO UPDATE + SET %s = excluded.%s + """, column, column, column) + .single(call().bind(jam.jamId()).bind(state)) .update() - .sendSync() .changed(); } } diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/JamTeams.java b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/JamTeams.java index a6aea88..3e49d6c 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/JamTeams.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/JamTeams.java @@ -8,7 +8,6 @@ import de.chojo.gamejam.data.dao.guild.jams.Jam; import de.chojo.gamejam.data.dao.guild.jams.jam.teams.Team; -import de.chojo.sadu.base.QueryFactory; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.interactions.commands.Command; @@ -16,44 +15,43 @@ import java.util.List; import java.util.Optional; -public class JamTeams extends QueryFactory { +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; + +public class JamTeams { private final Jam jam; public JamTeams(Jam jam) { - super(jam); this.jam = jam; } public Team create(String name) { - var teamId = builder(Integer.class) - .query(""" - INSERT INTO team(jam_id) VALUES(?) RETURNING id AS team_id; - """) - .parameter(stmt -> stmt.setInt(jam.jamId())) - .readRow(row -> row.getInt("team_id")) - .firstSync() + var teamId = query(""" + INSERT INTO team(jam_id) VALUES(?) RETURNING id AS team_id; + """) + .single(call().bind(jam.jamId())) + .map(row -> row.getInt("team_id")) + .first() .orElseThrow(); - builder().query(""" - INSERT INTO team_meta(team_id, team_name) VALUES (?,?); - """) - .parameter(stmt -> stmt.setInt(teamId).setString(name)) - .insert() - .sendSync(); + query(""" + INSERT INTO team_meta(team_id, team_name) VALUES (?,?); + """) + .single(call().bind(teamId).bind(name)) + .insert(); return new Team(jam, teamId); } public List teams() { - return builder(Team.class) - .query(""" - SELECT id, - team_name - FROM team t - LEFT JOIN team_meta m ON t.id = m.team_id - WHERE jam_id = ? - """) - .parameter(stmt -> stmt.setInt(jam.jamId())) - .readRow(r -> new Team(jam, r.getInt("id"))) - .allSync(); + return query(""" + SELECT id, + team_name + FROM team t + LEFT JOIN team_meta m ON t.id = m.team_id + WHERE jam_id = ? + """) + .single(call().bind(jam.jamId())) + .map(r -> new Team(jam, r.getInt("id"))) + .all(); } public Optional byMember(Member member) { @@ -61,54 +59,51 @@ public Optional byMember(Member member) { } public Optional byMember(User member) { - return builder(Integer.class) - .query(""" - SELECT m.team_id - FROM team_member m - LEFT JOIN team t ON t.id = m.team_id - LEFT JOIN jam j ON j.id = t.jam_id - WHERE j.id = ? - AND user_id = ? - """) - .parameter(p -> p.setInt(jam.jamId()).setLong(member.getIdLong())) - .readRow(r -> r.getInt("team_id")) - .firstSync() + return query(""" + SELECT m.team_id + FROM team_member m + LEFT JOIN team t ON t.id = m.team_id + LEFT JOIN jam j ON j.id = t.jam_id + WHERE j.id = ? + AND user_id = ? + """) + .single(call().bind(jam.jamId()).bind(member.getIdLong())) + .map(r -> r.getInt("team_id")) + .first() .flatMap(this::byId); } public Optional byName(String name) { - return builder(Integer.class) - .query(""" - SELECT id FROM team t LEFT JOIN team_meta m ON t.id = m.team_id - WHERE jam_id = ? - AND LOWER(m.team_name) = LOWER(?) - """) - .parameter(p -> p.setInt(jam.jamId()).setString(name)) - .readRow(r -> r.getInt("id")) - .firstSync() + return query(""" + SELECT id FROM team t LEFT JOIN team_meta m ON t.id = m.team_id + WHERE jam_id = ? + AND LOWER(m.team_name) = LOWER(?) + """) + .single(call().bind(jam.jamId()).bind(name)) + .map(r -> r.getInt("id")) + .first() .flatMap(this::byId); } public Optional byId(int id) { - return builder(Team.class) - .query("SELECT id, team_name FROM team t LEFT JOIN team_meta m ON t.id = m.team_id WHERE id = ?") - .parameter(stmt -> stmt.setInt(id)) - .readRow(r -> new Team(jam, r.getInt("id"))) - .firstSync(); + return query("SELECT id, team_name FROM team t LEFT JOIN team_meta m ON t.id = m.team_id WHERE id = ?") + .single(call().bind(id)) + .map(r -> new Team(jam, r.getInt("id"))) + .first(); } public List completeTeam(String name) { if (name.isBlank()) { return teams().stream() - .map(team -> team.meta().name()) - .map(team -> new Command.Choice(team, team)) - .toList(); + .map(team -> team.meta().name()) + .map(team -> new Command.Choice(team, team)) + .toList(); } return teams().stream() - .filter(team -> team.matchName(name)) - .map(team -> team.meta().name()) - .map(team -> new Command.Choice(team, team)) - .toList(); + .filter(team -> team.matchName(name)) + .map(team -> team.meta().name()) + .map(team -> new Command.Choice(team, team)) + .toList(); } } diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/Team.java b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/Team.java index c66a141..78baffe 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/Team.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/Team.java @@ -13,7 +13,6 @@ import de.chojo.jdautil.localization.LocalizationContext; import de.chojo.jdautil.localization.util.LocalizedEmbedBuilder; import de.chojo.jdautil.util.MentionUtil; -import de.chojo.sadu.base.QueryFactory; import net.dv8tion.jda.api.entities.ISnowflake; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.MessageEmbed; @@ -24,13 +23,15 @@ import java.util.Optional; import java.util.stream.Collectors; -public class Team extends QueryFactory { +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; + +public class Team { private final Jam jam; private final int id; private TeamMeta meta; public Team(Jam jam, int id) { - super(jam); this.jam = jam; this.id = id; } @@ -45,8 +46,8 @@ public void delete() { public MessageEmbed profileEmbed(LocalizationContext localizer) { var member = member().stream() - .map(u -> u.member().getAsMention()) - .collect(Collectors.joining(", ")); + .map(u -> u.member().getAsMention()) + .collect(Collectors.joining(", ")); var meta = meta(); return new LocalizedEmbedBuilder(localizer) @@ -60,10 +61,9 @@ public MessageEmbed profileEmbed(LocalizationContext localizer) { } public List member() { - return builder(TeamMember.class) - .query("SELECT user_id FROM team_member WHERE team_id = ?") - .parameter(p -> p.setInt(id())) - .readRow(r -> { + return query("SELECT user_id FROM team_member WHERE team_id = ?") + .single(call().bind(id())) + .map(r -> { try { var member = jam.jamGuild().guild().retrieveMemberById(r.getLong("user_id")).complete(); return new TeamMember(this, member); @@ -71,7 +71,7 @@ public List member() { return null; } }) - .allSync() + .all() .stream() .filter(Objects::nonNull) .toList(); @@ -104,28 +104,25 @@ public int hashCode() { } public List votes() { - return builder(TeamVote.class) - .query(""" - SELECT - rank, team_id, points, jam_id - FROM team_ranking - WHERE team_id = ? - """) - .parameter(p -> p.setInt(id())) - .readRow(r -> new TeamVote(this, r.getInt("rank"), r.getInt("points"))) - .allSync(); + return query(""" + SELECT + rank, team_id, points, jam_id + FROM team_ranking + WHERE team_id = ? + """) + .single(call().bind(id())) + .map(r -> new TeamVote(this, r.getInt("rank"), r.getInt("points"))) + .all(); } public boolean vote(Member member, int points) { - return builder() - .query(""" - INSERT INTO vote(team_id, voter_id, points) VALUES (?,?,?) - ON CONFLICT (team_id, voter_id) - DO UPDATE SET points = excluded.points; - """) - .parameter(p -> p.setInt(id()).setLong(member.getIdLong()).setInt(points)) + return query(""" + INSERT INTO vote(team_id, voter_id, points) VALUES (?,?,?) + ON CONFLICT (team_id, voter_id) + DO UPDATE SET points = excluded.points; + """) + .single(call().bind(id()).bind(member.getIdLong()).bind(points)) .insert() - .sendSync() .changed(); } @@ -136,29 +133,26 @@ public Jam jam() { public boolean disband() { delete(); - return builder() - .query("DELETE FROM team WHERE id = ?") - .parameter(p -> p.setInt(id())) + return query("DELETE FROM team WHERE id = ?") + .single(call().bind(id())) .insert() - .sendSync() .changed(); } public TeamMeta meta() { if (meta == null) { - meta = builder(TeamMeta.class) - .query(""" - SELECT team_name, - leader_id, - role_id, - text_channel_id, - voice_channel_id, - project_description, - project_url - FROM team_meta WHERE team_id = ? - """) - .parameter(stmt -> stmt.setInt(id)) - .readRow(row -> new TeamMeta(this, + meta = query(""" + SELECT team_name, + leader_id, + role_id, + text_channel_id, + voice_channel_id, + project_description, + project_url + FROM team_meta WHERE team_id = ? + """) + .single(call().bind(id)) + .map(row -> new TeamMeta(this, row.getString("team_name"), row.getLong("leader_id"), row.getLong("role_id"), @@ -166,18 +160,17 @@ public TeamMeta meta() { row.getLong("voice_channel_id"), row.getString("project_description"), row.getString("project_url"))) - .firstSync() + .first() .orElseThrow(); } return meta; } public Optional member(Member member) { - return builder(TeamMember.class) - .query("SELECT user_id FROM team_member WHERE team_id = ? AND user_id = ?") - .parameter(p -> p.setInt(id()).setLong(member.getIdLong())) - .readRow(r -> new TeamMember(this, member)) - .firstSync(); + return query("SELECT user_id FROM team_member WHERE team_id = ? AND user_id = ?") + .single(call().bind(id()).bind(member.getIdLong())) + .map(r -> new TeamMember(this, member)) + .first(); } @Override @@ -186,11 +179,10 @@ public String toString() { } public Integer votes(Member member) { - return builder(Integer.class) - .query("SELECT sum(points) as points FROM vote WHERE team_id = ? AND voter_id = ?") - .parameter(stmt -> stmt.setInt(id()).setLong(member.getIdLong())) - .readRow(r -> r.getInt("points")) - .firstSync() + return query("SELECT sum(points) as points FROM vote WHERE team_id = ? AND voter_id = ?") + .single(call().bind(id()).bind(member.getIdLong())) + .map(r -> r.getInt("points")) + .first() .orElse(0); } } diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/team/TeamMember.java b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/team/TeamMember.java index b3e0002..0dbf3d9 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/team/TeamMember.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/team/TeamMember.java @@ -7,15 +7,16 @@ package de.chojo.gamejam.data.dao.guild.jams.jam.teams.team; import de.chojo.gamejam.data.dao.guild.jams.jam.teams.Team; -import de.chojo.sadu.base.QueryFactory; import net.dv8tion.jda.api.entities.Member; -public final class TeamMember extends QueryFactory { +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; + +public final class TeamMember { private final Team team; private final Member member; public TeamMember(Team team, Member member) { - super(team); this.team = team; this.member = member; } @@ -33,11 +34,9 @@ public boolean leave() { var roleById = team.meta().role(); roleById.ifPresent(role -> guild.removeRoleFromMember(member, role).queue()); - return builder() - .query("DELETE FROM team_member WHERE team_id = ? AND user_id = ?") - .parameter(p -> p.setInt(team.id()).setLong(member.getIdLong())) + return query("DELETE FROM team_member WHERE team_id = ? AND user_id = ?") + .single(call().bind(team.id()).bind(member.getIdLong())) .insert() - .sendSync() .changed(); } } diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/team/TeamMeta.java b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/team/TeamMeta.java index 6f72395..f98e516 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/team/TeamMeta.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/teams/team/TeamMeta.java @@ -7,19 +7,20 @@ package de.chojo.gamejam.data.dao.guild.jams.jam.teams.team; import de.chojo.gamejam.data.dao.guild.jams.jam.teams.Team; -import de.chojo.sadu.base.QueryFactory; -import de.chojo.sadu.exceptions.ThrowingConsumer; -import de.chojo.sadu.wrapper.util.ParamBuilder; +import de.chojo.sadu.queries.api.call.Call; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; -import java.sql.SQLException; import java.util.Optional; +import java.util.function.Function; -public class TeamMeta extends QueryFactory { +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; + +public class TeamMeta { private final Team team; private String name; private long leader; @@ -31,7 +32,6 @@ public class TeamMeta extends QueryFactory { public TeamMeta(Team team, String name, long leader, long role, long textChannel, long voiceChannel, String projectDescription, String projectUrl) { - super(team); this.team = team; this.name = name; this.leader = leader; @@ -112,13 +112,11 @@ public String name() { } public void rename(String name) { - var changed = builder() - .query(""" - UPDATE team_meta SET team_name = ? WHERE team_id = ? - """) - .parameter(stmt -> stmt.setString(name).setInt(team.id())) + var changed = query(""" + UPDATE team_meta SET team_name = ? WHERE team_id = ? + """) + .single(call().bind(name).bind(team.id())) .update() - .sendSync() .changed(); if (changed) { this.name = name; @@ -129,27 +127,22 @@ public void rename(String name) { } private boolean set(String column, long value) { - return set(column, p -> p.setLong(value)); + return set(column, p -> p.bind(value)); } private boolean set(String column, String value) { - return set(column, p -> p.setString(value)); - } - - private boolean set(String column, ThrowingConsumer value) { - return builder() - .query(""" - INSERT INTO team_meta(team_id, team_name, %s) VALUES(?,'',?) - ON CONFLICT(team_id) - DO UPDATE - SET %s = excluded.%s - """, column, column, column) - .parameter(p -> { - p.setInt(team.id()); - value.accept(p); - }) + return set(column, p -> p.bind(value)); + } + + private boolean set(String column, Function value) { + return query(""" + INSERT INTO team_meta(team_id, team_name, %s) VALUES(?,'',?) + ON CONFLICT(team_id) + DO UPDATE + SET %s = excluded.%s + """, column, column, column) + .single(value.apply(call().bind(team.id()))) .update() - .sendSync() .changed(); } } diff --git a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/user/JamUser.java b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/user/JamUser.java index 8727c1a..399e290 100644 --- a/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/user/JamUser.java +++ b/bot/src/main/java/de/chojo/gamejam/data/dao/guild/jams/jam/user/JamUser.java @@ -9,53 +9,52 @@ import de.chojo.gamejam.data.dao.guild.jams.Jam; import de.chojo.gamejam.data.dao.guild.jams.jam.teams.Team; import de.chojo.gamejam.data.wrapper.votes.VoteEntry; -import de.chojo.sadu.base.QueryFactory; import net.dv8tion.jda.api.entities.Member; import java.util.List; import java.util.Optional; -public class JamUser extends QueryFactory { +import static de.chojo.sadu.queries.api.call.Call.call; +import static de.chojo.sadu.queries.api.query.Query.query; + +public class JamUser { private final Jam jam; private final Member member; public JamUser(Jam jam, Member member) { - super(jam); this.jam = jam; this.member = member; } public List votes() { - return builder(VoteEntry.class) - .query(""" - SELECT - v.team_id, - v.voter_id, - v.points - FROM vote v - LEFT JOIN team t ON t.id = v.team_id - WHERE t.jam_id = ? - AND voter_id = ? - """) - .parameter(p -> p.setInt(jam.jamId()).setLong(member.getIdLong())) - .readRow(r -> new VoteEntry(jam.teams().byId(r.getInt("team_id")).orElseThrow(), + return query(""" + SELECT + v.team_id, + v.voter_id, + v.points + FROM vote v + LEFT JOIN team t ON t.id = v.team_id + WHERE t.jam_id = ? + AND voter_id = ? + """) + .single(call().bind(jam.jamId()).bind(member.getIdLong())) + .map(r -> new VoteEntry(jam.teams().byId(r.getInt("team_id")).orElseThrow(), r.getLong("voter_id"), r.getInt("points"))) - .allSync(); + .all(); } public int votesGiven() { - return builder(Integer.class) - .query(""" - SELECT - sum(points) as points - FROM vote v - LEFT JOIN team t ON t.id = v.team_id - WHERE t.jam_id = ? - AND voter_id = ? - """) - .parameter(p -> p.setInt(jam.jamId()).setLong(member.getIdLong())) - .readRow(r -> r.getInt("points")) - .firstSync() + return query(""" + SELECT + sum(points) as points + FROM vote v + LEFT JOIN team t ON t.id = v.team_id + WHERE t.jam_id = ? + AND voter_id = ? + """) + .single(call().bind(jam.jamId()).bind(member.getIdLong())) + .map(r -> r.getInt("points")) + .first() .orElse(0); } @@ -69,11 +68,9 @@ public boolean join(Team team) { roleById.ifPresent(role -> guild.addRoleToMember(member, role).queue()); - return builder() - .query("INSERT INTO team_member(team_id, user_id) VALUES(?,?)") - .parameter(p -> p.setInt(team.id()).setLong(member.getIdLong())) + return query("INSERT INTO team_member(team_id, user_id) VALUES(?,?)") + .single(call().bind(team.id()).bind(member.getIdLong())) .insert() - .sendSync() .changed(); } } diff --git a/plugin-paper/build.gradle.kts b/plugin-paper/build.gradle.kts index f036156..4abfbff 100644 --- a/plugin-paper/build.gradle.kts +++ b/plugin-paper/build.gradle.kts @@ -9,10 +9,10 @@ version = "1.0.0" dependencies { implementation(project(":plugin-api")) - compileOnly("io.papermc.paper", "paper-api", "1.19.2-R0.1-SNAPSHOT") - implementation("io.javalin", "javalin", "4.6.8") - implementation("de.eldoria", "eldo-util", "1.14.0-DEV") - implementation("org.slf4j", "slf4j-api", "1.7.36") + compileOnly(libs.paper) + implementation(libs.javalin.core) + implementation(libs.eldoutil) + implementation(libs.slf4j) } tasks { diff --git a/plugin-velocity/build.gradle.kts b/plugin-velocity/build.gradle.kts index f7dc0b9..946e6cb 100644 --- a/plugin-velocity/build.gradle.kts +++ b/plugin-velocity/build.gradle.kts @@ -9,10 +9,10 @@ repositories { } dependencies{ - implementation("io.javalin", "javalin", "4.6.8") implementation(project(":plugin-api")) - compileOnly("com.velocitypowered:velocity-api:3.3.0-SNAPSHOT") - annotationProcessor("com.velocitypowered:velocity-api:3.3.0-SNAPSHOT") + implementation(libs.javalin.core) + compileOnly(libs.velocity) + annotationProcessor(libs.velocity) } tasks { diff --git a/settings.gradle.kts b/settings.gradle.kts index 5c6463c..f776175 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -9,6 +9,27 @@ findProject(":plugin-paper:Readme.md")?.name = "Readme.md" dependencyResolutionManagement { versionCatalogs { create("libs") { + version("sadu", "2.2.4") + library("sadu-queries", "de.chojo.sadu", "sadu-queries").versionRef("sadu") + library("sadu-updater", "de.chojo.sadu", "sadu-updater").versionRef("sadu") + library("sadu-datasource", "de.chojo.sadu", "sadu-datasource").versionRef("sadu") + library("sadu-postgresql", "de.chojo.sadu", "sadu-postgresql").versionRef("sadu") + bundle("sadu", listOf("sadu-queries", "sadu-updater", "sadu-datasource", "sadu-postgresql")) + + version("log4j", "2.19.0") + library("slf4j", "org.slf4j:slf4j-api:2.0.3") + library("log4j-core", "org.apache.logging.log4j", "log4j-core").versionRef("log4j") + library("log4j-slf4j2", "org.apache.logging.log4j", "log4j-slf4j2-impl").versionRef("log4j") + bundle("logging", listOf("slf4j","log4j-core","log4j-slf4j2")) + + + version("javalin", "4.6.8") + library("javalin-core", "io.javalin","javalin").versionRef("javalin") + library("javalin-bundle", "io.javalin","javalin-bundle").versionRef("javalin") + + library("velocity", "com.velocitypowered:velocity-api:3.3.0-SNAPSHOT") + library("paper", "io.papermc.paper:paper-api:1.19.2-R0.1-SNAPSHOT") + library("eldoutil", "de.eldoria:eldo-util:1.14.5") plugin("shadow", "com.gradleup.shadow").version("8.3.0") plugin("pluginyml", "net.minecrell.plugin-yml.bukkit").version("0.6.0") }