Skip to content

Commit

Permalink
Migrate code
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed Jun 6, 2024
1 parent 36d8ca1 commit b4c4d65
Show file tree
Hide file tree
Showing 28 changed files with 353 additions and 432 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ bin/

### Mac OS ###
.DS_Store
.direnv
6 changes: 6 additions & 0 deletions src/main/java/de/chojo/krile/commands/preview/Preview.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.krile.commands.preview;

import de.chojo.jdautil.interactions.slash.Argument;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.krile.commands.preview.handler;

import de.chojo.jdautil.modals.handler.ModalHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.krile.commands.preview.handler;

import de.chojo.jdautil.wrapper.EventContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.krile.commands.preview.handler;

import de.chojo.jdautil.interactions.slash.structure.handler.SlashHandler;
Expand Down
21 changes: 7 additions & 14 deletions src/main/java/de/chojo/krile/core/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
import de.chojo.krile.data.access.GuildData;
import de.chojo.krile.data.access.RepositoryData;
import de.chojo.krile.data.access.TagData;
import de.chojo.krile.data.bind.StaticQueryAdapter;
import de.chojo.logutil.marker.LogNotify;
import de.chojo.sadu.databases.PostgreSql;
import de.chojo.sadu.datasource.DataSourceCreator;
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.configuration.QueryConfiguration;
import de.chojo.sadu.updater.QueryReplacement;
import de.chojo.sadu.updater.SqlUpdater;
import de.chojo.sadu.wrapper.QueryBuilderConfig;
import org.slf4j.Logger;

import java.io.IOException;
Expand Down Expand Up @@ -53,10 +52,9 @@ public static Data create(Threading threading, Configuration<ConfigFile> configu
}

public void init() throws SQLException, IOException, InterruptedException {
configure();
initConnection();
configure();
updateDatabase();
initSaduAdapter();
initDao();
}

Expand Down Expand Up @@ -101,10 +99,6 @@ public TagData tags() {
return tagData;
}

private void initSaduAdapter() {
StaticQueryAdapter.start(dataSource);
}

private void updateDatabase() throws IOException, SQLException {
var schema = configuration.config().database().schema();
SqlUpdater.builder(dataSource, PostgreSql.get())
Expand All @@ -119,10 +113,9 @@ private void configure() {
var logger = getLogger("DbLogger");
RowMapperRegistry rowMapperRegistry = new RowMapperRegistry();
rowMapperRegistry.register(PostgresqlMapper.getDefaultMapper());
QueryBuilderConfig.setDefault(QueryBuilderConfig.builder()
.withExceptionHandler(err -> logger.error(LogNotify.NOTIFY_ADMIN, "An error occurred during a database request", err))
.withExecutor(threading.botWorker())
.rowMappers(rowMapperRegistry)
QueryConfiguration.setDefault(QueryConfiguration.builder(dataSource)
.setExceptionHandler(err -> logger.error(LogNotify.NOTIFY_ADMIN, "An error occurred during a database request", err))
.setRowMapperRegistry(rowMapperRegistry)
.build());
}

Expand Down
32 changes: 15 additions & 17 deletions src/main/java/de/chojo/krile/data/access/AuthorData.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import static de.chojo.krile.data.bind.StaticQueryAdapter.builder;
import static de.chojo.sadu.queries.api.call.Call.call;
import static de.chojo.sadu.queries.api.query.Query.query;

public class AuthorData {
private final Cache<Integer, Author> authorCache = CacheBuilder.newBuilder().expireAfterAccess(30, TimeUnit.MINUTES).build();
Expand All @@ -38,11 +39,10 @@ INSERT INTO author(name, mail)
ON CONFLICT(name, mail)
DO NOTHING
RETURNING id,name, mail""";
return cache(builder(Author.class)
.query(query)
.parameter(stmt -> stmt.setString(author.name()).setString(author.mail()))
.readRow(Author::build)
.firstSync());
return cache(query(query)
.single(call().bind(author.name()).bind(author.mail()))
.map(Author::build)
.first());
}

/**
Expand All @@ -55,11 +55,10 @@ public Optional<Author> get(RawAuthor author) {
@Language("postgresql")
var query = """
SELECT id, name, mail FROM author WHERE name = ? AND mail = ?""";
return cache(builder(Author.class)
.query(query)
.parameter(stmt -> stmt.setString(author.name()).setString(author.mail()))
.readRow(Author::build)
.firstSync());
return cache(query(query)
.single(call().bind(author.name()).bind(author.mail()))
.map(Author::build)
.first());
}

/**
Expand All @@ -71,7 +70,7 @@ public Optional<Author> get(RawAuthor author) {
*/
public Optional<Author> get(int id) {
return Optional.ofNullable(authorCache.getIfPresent(id))
.or(() ->retrieveById(id))
.or(() -> retrieveById(id))
.map(this::cache);
}

Expand All @@ -89,10 +88,9 @@ private Optional<Author> retrieveById(int id) {
@Language("postgresql")
var query = """
SELECT id, name, mail FROM author WHERE id = ?""";
return builder(Author.class)
.query(query)
.parameter(stmt -> stmt.setInt(id))
.readRow(Author::build)
.firstSync();
return query(query)
.single(call().bind(id))
.map(Author::build)
.first();
}
}
30 changes: 14 additions & 16 deletions src/main/java/de/chojo/krile/data/access/CategoryData.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import static de.chojo.krile.data.bind.StaticQueryAdapter.builder;
import static de.chojo.sadu.queries.api.call.Call.call;
import static de.chojo.sadu.queries.api.query.Query.query;

public class CategoryData {
private final Cache<Integer, Category> categpryCache = CacheBuilder.newBuilder().expireAfterAccess(30, TimeUnit.MINUTES).build();
Expand All @@ -39,11 +40,10 @@ INSERT INTO category(category) VALUES(lower(?))
ON CONFLICT(lower(category))
DO NOTHING
RETURNING id, category""";
return cache(builder(Category.class)
.query(query)
.parameter(stmt -> stmt.setString(harmonize(name)))
.readRow(Category::build)
.firstSync());
return cache(query(query)
.single(call().bind(harmonize(name)))
.map(Category::build)
.first());
}

private Category cache(Category category) {
Expand Down Expand Up @@ -80,11 +80,10 @@ public Optional<Category> get(String name) {
@Language("postgresql")
var query = """
SELECT id, c.category FROM category c WHERE lower(?) = category""";
return cache(builder(Category.class)
.query(query)
.parameter(stmt -> stmt.setString(harmonize(name)))
.readRow(Category::build)
.firstSync());
return cache(query(query)
.single(call().bind(harmonize(name)))
.map(Category::build)
.first());
}

/**
Expand All @@ -97,11 +96,10 @@ public Optional<Category> get(int id) {
@Language("postgresql")
var query = """
SELECT id, c.category FROM category c WHERE id = ?""";
return cache(builder(Category.class)
.query(query)
.parameter(stmt -> stmt.setInt(id))
.readRow(Category::build)
.firstSync());
return cache(query(query)
.single(call().bind(id))
.map(Category::build)
.first());
}

private String harmonize(@Nullable String name) {
Expand Down
Loading

0 comments on commit b4c4d65

Please sign in to comment.