Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

#9 Database setup #11

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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());
```
Comment on lines +84 to +100
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good, but I do not use databases enough to understand some of this terminology. Could someone else review this and confirm it makes sense. @AzatothTheAwakened can you take a look? Would be nice to finish up this PR and get it merged!


## 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)

94 changes: 94 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@

<properties>
<jbock.version>2.8.0</jbock.version>
<db.jdbc.url>jdbc:sqlite:tjbot.sqlite</db.jdbc.url>
</properties>

<build>
@@ -45,6 +46,59 @@
<finalName>${project.name}</finalName>
</configuration>
</plugin>

<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.12.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<jdbc>
<driver>org.sqlite.JDBC</driver>
<url>${db.jdbc.url}</url>
</jdbc>
<generator>
<database>
<name>org.jooq.meta.sqlite.SQLiteDatabase</name>
<includes>.*</includes>
</database>
<generate>
<daos>true</daos>
</generate>
<target>
<packageName>org.togetherjava.discordbot.db.autogen</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
</plugin>

<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.1.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>migrate</goal>
</goals>
</execution>
</executions>
<configuration>
<url>${db.jdbc.url}</url>
<locations>
<location>filesystem:src/main/resources/db</location>
</locations>
</configuration>
</plugin>
</plugins>
</build>

@@ -55,14 +109,23 @@
<version>-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<type>maven-plugin</type>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
@@ -72,6 +135,7 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j18-impl</artifactId>
@@ -83,6 +147,36 @@
<version>${jbock.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.12.3</version>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
<version>3.12.3</version>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>3.12.3</version>
</dependency>

<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.30.1</version>
</dependency>

<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>6.1.3</version>
</dependency>
</dependencies>

<repositories>
19 changes: 11 additions & 8 deletions src/main/java/org/togetherjava/discordbot/config/TjBotConfig.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
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.
*/
public class TjBotConfig {

private List<String> prefixes;
@JsonProperty("botToken")
private String botToken;
private CommandConfig commands;
private String moderationChannel;
private String dburl;

@JsonCreator
public TjBotConfig(List<String> 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;
}

/**
Original file line number Diff line number Diff line change
@@ -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<T> implements Repository<T> {

protected DSLContext dslContext;

public JooqRepository(CommandContext context) {
dslContext = DSL.using(context.getConfig().getDburl());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.togetherjava.discordbot.db.repository;

import java.util.List;

public interface Repository<T> {

void add(T item);

void update(T item);

void remove(T item);

List<T> getAll();

}

2 changes: 2 additions & 0 deletions src/main/resources/sample-config.yml
Original file line number Diff line number Diff line change
@@ -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"