diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 884b7d0..6197cff 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -81,6 +81,24 @@ Again those points are not required but help future contributors to easier track ``` 5. For more information on adding commands, refer to the documentation for Brigardier or ask :) +### Database Access +1. Create a schema in resources/db, be sure to start it with a correct version number i.e. V1__example + ```sqlite + CREATE TABLE IF NOT EXISTS EXAMPLE + ( + ID INTEGER AUTO_INCREMENT PRIMARY KEY, + MEMBER varchar(30) NOT NULL, + TEXT varchar(30) NOT NULL + ); + ``` +2. Make a new class in org.togetherjava.db.repositories that extends JooqRepository. +3. Create a Constructor that accepts CommandContext as a parameter. +4. You can then either create a DAO instance using Jooqs generated DAO class that is generated from your schema using + the DSLContext for configuration provided in the JooqRepository class. Or you can directly use the DSLContext to create SQL queries + ```java + ExampleDao dao = new ExampleDao(dslContext.configuration()); + ``` + ## Committing changes If your commit is related to an Issue please link it accordingly within the message (see [this](https://help.github.com/en/github/writing-on-github/autolinked-references-and-urls) page, essentially a `#` followed by the issuenumber) diff --git a/pom.xml b/pom.xml index 27679ec..483c9ec 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,7 @@ 2.8.0 + jdbc:sqlite:tjbot.sqlite @@ -45,6 +46,59 @@ ${project.name} + + + org.jooq + jooq-codegen-maven + 3.12.3 + + + generate-sources + + generate + + + + + + org.sqlite.JDBC + ${db.jdbc.url} + + + + org.jooq.meta.sqlite.SQLiteDatabase + .* + + + true + + + org.togetherjava.discordbot.db.autogen + target/generated-sources/jooq + + + + + + + org.flywaydb + flyway-maven-plugin + 6.1.3 + + + generate-sources + + migrate + + + + + ${db.jdbc.url} + + filesystem:src/main/resources/db + + + @@ -55,14 +109,23 @@ -SNAPSHOT + + org.codehaus.mojo + properties-maven-plugin + 1.0.0 + maven-plugin + + com.fasterxml.jackson.core jackson-databind + com.fasterxml.jackson.dataformat jackson-dataformat-yaml + com.fasterxml.jackson.module jackson-module-parameter-names @@ -72,6 +135,7 @@ org.slf4j slf4j-api + org.apache.logging.log4j log4j-slf4j18-impl @@ -83,6 +147,36 @@ ${jbock.version} provided + + + org.jooq + jooq + 3.12.3 + + + + org.jooq + jooq-meta + 3.12.3 + + + + org.jooq + jooq-codegen + 3.12.3 + + + + org.xerial + sqlite-jdbc + 3.30.1 + + + + org.flywaydb + flyway-core + 6.1.3 + diff --git a/src/main/java/org/togetherjava/discordbot/config/TjBotConfig.java b/src/main/java/org/togetherjava/discordbot/config/TjBotConfig.java index f837378..7ac5182 100644 --- a/src/main/java/org/togetherjava/discordbot/config/TjBotConfig.java +++ b/src/main/java/org/togetherjava/discordbot/config/TjBotConfig.java @@ -1,13 +1,12 @@ package org.togetherjava.discordbot.config; -import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; import java.io.IOException; import java.io.InputStream; import java.util.List; -import java.util.Objects; /** * The pojo for the main config file for this bot. @@ -15,16 +14,20 @@ public class TjBotConfig { private List prefixes; + @JsonProperty("botToken") private String botToken; private CommandConfig commands; private String moderationChannel; + private String dburl; - @JsonCreator - public TjBotConfig(List prefixes, String botToken, String moderationChannel, CommandConfig commands) { - this.prefixes = Objects.requireNonNull(prefixes, "prefixes can not be null!"); - this.botToken = Objects.requireNonNull(botToken, "botToken can not be null!"); - this.commands = Objects.requireNonNull(commands, "commands can not be null!"); - this.moderationChannel = Objects.requireNonNull(moderationChannel, "channel can not be null!"); + + /** + * Returns the database url + * + * @return the database url + */ + public String getDburl() { + return dburl; } /** diff --git a/src/main/java/org/togetherjava/discordbot/db/repository/JooqRepository.java b/src/main/java/org/togetherjava/discordbot/db/repository/JooqRepository.java new file mode 100644 index 0000000..5af8a41 --- /dev/null +++ b/src/main/java/org/togetherjava/discordbot/db/repository/JooqRepository.java @@ -0,0 +1,15 @@ +package org.togetherjava.discordbot.db.repository; + +import org.jooq.*; +import org.jooq.impl.DSL; +import org.togetherjava.discordbot.commands.CommandContext; + +public abstract class JooqRepository implements Repository { + + protected DSLContext dslContext; + + public JooqRepository(CommandContext context) { + dslContext = DSL.using(context.getConfig().getDburl()); + } + +} diff --git a/src/main/java/org/togetherjava/discordbot/db/repository/Repository.java b/src/main/java/org/togetherjava/discordbot/db/repository/Repository.java new file mode 100644 index 0000000..6001274 --- /dev/null +++ b/src/main/java/org/togetherjava/discordbot/db/repository/Repository.java @@ -0,0 +1,16 @@ +package org.togetherjava.discordbot.db.repository; + +import java.util.List; + +public interface Repository { + + void add(T item); + + void update(T item); + + void remove(T item); + + List getAll(); + +} + diff --git a/src/main/resources/sample-config.yml b/src/main/resources/sample-config.yml index c2ef1f3..ae42059 100644 --- a/src/main/resources/sample-config.yml +++ b/src/main/resources/sample-config.yml @@ -3,6 +3,8 @@ # The command prefixes prefixes: ["!", "?"] botToken: "your token" +# the url for the database connection +dburl: "jdbc:sqlite:tjbot.sqlite" # channel for modmail moderationChannel: "your channel"