Skip to content

Commit

Permalink
added a database sharing system
Browse files Browse the repository at this point in the history
  • Loading branch information
AshlyneS committed Feb 9, 2023
1 parent 0916fd9 commit b59d240
Show file tree
Hide file tree
Showing 20 changed files with 818 additions and 128 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ test-deploy-token.txt
/test/
assets/resources/database.properties
/logs/
/config/
4 changes: 2 additions & 2 deletions assets/resources/sql statements.kvp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
########## GUILD DATA #########
guild_data_insert = INSERT INTO `WatameBot`.`Guild` (GuildID, GuildProperties) VALUES (?, ?);
guild_data_insert = INSERT INTO `WatameBot`.`Guild` (GuildID, GuildProperties) VALUES (?, '{}');
guild_data_get = SELECT * FROM `WatameBot`.`Guild`;
guild_data_get_id = SELECT * FROM `WatameBot`.`Guild` WHERE GuildID = ?;

########## GUILD JSON DATA #########
guild_json_select = SELECT JSON_EXTRACT(GuildProperties, ?) FROM `WatameBot`.`Guild` WHERE GuildID = ?;
guild_json_update = UPDATE `WatameBot`.`Guild` SET GuildProperties = JSON_SET(GuildProperties, ?, ?) WHERE GuildID = ?;
guild_json_remove = UPDATE `WatameBot`.`Guild` SET GuildProperties = JSON_REMOVE(GuildProperties, ?);
guild_json_remove = UPDATE `WatameBot`.`Guild` SET GuildProperties = JSON_REMOVE(GuildProperties, ?) WHERE GuildID = ?;
27 changes: 6 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,31 @@
<groupId>net.foxgenesis.watame</groupId>
<artifactId>watamebot</artifactId>
<version>1.0.1</version>

<name>WatameBot</name>
<url>https://foxgenesis.net/watamebot</url>
<description>A plugin based Discord bot written in Java.</description>

<organization>
<name>FoxGenesis</name>
<url>https://foxgenesis.net</url>
</organization>

<licenses>
<license>
<name>GNU General Public License v2.0</name>
<url>https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html</url>
</license>
</licenses>

<developers>
<developer>
<id>FoxGenesis</id>
<name>FoxGenesis</name>
<email>[email protected]</email>
</developer>
</developers>

<scm>
<url>https://github.com/FoxGenesis/Watamebot/tree/main</url>
<connection>scm:git:[email protected];FoxGenesis/Watamebot.git</connection>
<developerConnection>scm:git:ssh://github.com;FoxGenesis/Watamebot.git</developerConnection>
</scm>

<build>
<sourceDirectory>src</sourceDirectory>
<resources>
Expand Down Expand Up @@ -62,7 +56,6 @@
<release>17</release>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand All @@ -77,64 +70,60 @@
<artifactId>JDA</artifactId>
<version>5.0.0-alpha.18</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.39.2.1</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.6</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.5</version>
</dependency>

<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>2.4.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
</dependency>

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>

<profiles>
<profile>
<id>ci-cd1</id>
Expand Down Expand Up @@ -163,7 +152,6 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand Down Expand Up @@ -202,7 +190,6 @@
</plugins>
</build>
</profile>

<profile>
<id>ci-cd2</id>
<distributionManagement>
Expand Down Expand Up @@ -232,7 +219,6 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand Down Expand Up @@ -271,7 +257,6 @@
</plugins>
</build>
</profile>

<profile>
<id>zip</id>
<build>
Expand Down
2 changes: 2 additions & 0 deletions src/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
requires com.zaxxer.hikari;
requires ch.qos.logback.classic;
requires ch.qos.logback.core;
requires org.apache.commons.configuration2;

exports net.foxgenesis.config;
exports net.foxgenesis.config.fields;
exports net.foxgenesis.database;
exports net.foxgenesis.property;
exports net.foxgenesis.log;
exports net.foxgenesis.watame.sql;
Expand Down
1 change: 1 addition & 0 deletions src/net/foxgenesis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/test/
2 changes: 1 addition & 1 deletion src/net/foxgenesis/config/fields/ConfigField.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void set(@Nonnull Guild g, E newState) {
protected JSONObjectAdv getDataForGuild(@Nonnull Guild guild) {
// FIXME need new way of field creation that doesn't require hard coded database
// call
return WatameBot.getInstance().getDatabase().getDataForGuild(guild).getConfig();
return WatameBot.getInstance().getDataForGuild(guild).getConfig();
}

abstract E optFrom(@Nonnull JSONObjectAdv config, @Nonnull Guild guild);
Expand Down
84 changes: 84 additions & 0 deletions src/net/foxgenesis/database/AConnectionProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package net.foxgenesis.database;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.Consumer;
import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.dv8tion.jda.internal.utils.IOUtil;

public abstract class AConnectionProvider implements AutoCloseable {

protected final Logger logger;
private final String name;
protected final Properties properties;

public AConnectionProvider(Properties properties, String name) {
this.name = Objects.requireNonNull(name);
this.logger = LoggerFactory.getLogger(name);
this.properties = Objects.requireNonNull(properties);

String type = properties.getProperty("databaseType", "mysql");
properties.remove("databaseType");

String ip = properties.getProperty("ip", "localhost");
properties.remove("ip");

String port = properties.getProperty("port", "3306");
properties.remove("port");

String database = properties.getProperty("database", "WatameBot");
properties.remove("database");

properties.put("jdbcUrl", "jdbc:%s://%s:%s/%s".formatted(type, ip, port, database));

properties.put("poolName", name);
}

protected abstract Connection openConnection() throws SQLException;

protected <U> U openAutoClosedConnection(ConnectionConsumer<U> consumer, Consumer<Throwable> error) {
try (Connection conn = openConnection()) {
return consumer.applyConnection(conn);
} catch (Exception e) {
if (error != null)
error.accept(e);
return null;
}
}

protected <U> CompletableFuture<U> asyncConnection(Function<Connection, U> function) {
CompletableFuture<Connection> conn = CompletableFuture.supplyAsync(() -> {
try {
return openConnection();
} catch (SQLException e) {
throw new CompletionException(e);
}
});

CompletableFuture<Connection> copy = conn.copy();
return conn.thenApplyAsync(function).whenCompleteAsync((result, error) -> {
copy.thenAcceptAsync(IOUtil::silentClose);
if (error != null)
throw new CompletionException(error);
});
}

public final String getName() { return name; }

@Override
public void close() throws Exception {
logger.debug("Shutting down {}", name);

}

@FunctionalInterface
public interface ConnectionConsumer<U> { public U applyConnection(Connection connection) throws SQLException; }
}
Loading

0 comments on commit b59d240

Please sign in to comment.