Skip to content

MEFRREEX/JOOQConnector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

03d45ac Β· Oct 4, 2024

History

31 Commits
Apr 23, 2024
Apr 22, 2024
May 15, 2024
Apr 23, 2024
Apr 23, 2024
Apr 23, 2024
Apr 23, 2024
Apr 22, 2024
Apr 22, 2024
Apr 22, 2024
Oct 4, 2024
Oct 4, 2024
Apr 25, 2024
Apr 22, 2024
Apr 22, 2024
Apr 22, 2024
Apr 22, 2024
May 15, 2024

Repository files navigation

JOOQConnector

A library for working with databases using JOOQ ORM for Java.

License: GNU GPLv3 Version Jitpack

πŸ“– Overview

JOOQConnector is a Java library designed for easy interaction with databases using the JOOQ ORM. It includes built-in support for SQLite and MySQL databases and is designed to work with various server software like Bukkit, Nukkit, PowerNukkitX, JukeboxMC, and WaterdogPE.

✨ Features

  • SQLite3 and MySQL Support: Seamless integration with SQLite and MySQL databases.
  • No Unnecessary Logs: Disable JOOQ logo and tips from appearing in the logs.
  • Bundled Drivers: Includes SQLite, and MySQL drivers in the JAR.
  • Cross-Platform Support: Compatible with different Minecraft server software.

πŸ›  Code Examples

Disable JOOQ Logs

You can disable the printing of the JOOQ logo and tips:

JOOQConnector.setJOOQMessagesEnabled(false);

SQLite3 Example

Table<?> table = DSL.table("test");
SQLiteDatabase database = new SQLiteDatabase(new File("database.db"));

// Creating table
database.getConnection().thenAcceptAsync(connection -> {
    DSL.using(connection)
            .createTableIfNotExists(table)
            .column("id", SQLDataType.INTEGER)
            .column("data", SQLDataType.VARCHAR)
            .unique("id")
            .execute();
}).join();

// Inserting value into the table
database.getConnection().thenAcceptAsync(connection -> {
    DSL.using(connection).insertInto(table)
            .set(DSL.field("id"), 1)
            .set(DSL.field("data"), "Test string")
            .execute();
}).join();

// Getting value from the table
String value = database.getConnection().thenApplyAsync(connection -> {
    Result<Record> result = DSL.using(connection).select()
            .from(table)
            .where(DSL.field("id").eq(1))
            .fetch();
    return result.isEmpty() ? null : result.get(0).get(DSL.field("data", String.class));
}).join();

System.out.println("Value from table: " + value);

MySQL Example

MySQLDatabase database = new MySQLDatabase("127.0.0.1:3306", "database", "user", "password");

// The rest of the code is identical to the SQLite example...

Switching Between SQLite and MySQL

IDatabase database = sqlite ? 
    new SQLiteDatabase(new File("database.db")) : 
    new MySQLDatabase("127.0.0.1:3306", "database", "user", "password");

Organizing Database Operations in a Class

public class Database {

    private final IDatabase database;
    private final Table<?> table;

    public Database(IDatabase database, Table<?> table) {
        this.database = database;
        this.table = table;

        database.getConnection().thenAcceptAsync(connection -> {
            DSL.using(connection)
                    .createTableIfNotExists(table)
                    .column("id", SQLDataType.INTEGER)
                    .column("data", SQLDataType.VARCHAR)
                    .unique("id")
                    .execute();
        }).join();
    }

    public CompletableFuture<Void> addValue(int id, String data) {
        return database.getConnection().thenAcceptAsync(connection -> {
            DSL.using(connection).insertInto(table)
                    .set(DSL.field("id"), id)
                    .set(DSL.field("data"), data)
                    .execute();
        });
    }

    public CompletableFuture<String> getValue(int id) {
        return database.getConnection().thenApplyAsync(connection -> {
            Result<Record> result = DSL.using(connection).select()
                    .from(table)
                    .where(DSL.field("id").eq(id))
                    .fetch();

            return result.isEmpty() ? null : result.get(0).get(DSL.field("data", String.class));
        });
    }
}

πŸ”Œ Installation

Plugin Setup

If you're not using the standalone API version, place the plugin JAR in your server's plugins folder.

Maven

Add the following repository and dependency to your pom.xml:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.github.MEFRREEX</groupId>
    <artifactId>JOOQConnector</artifactId>
    <version>1.0.1</version>
    <scope>provided</scope>
</dependency>

Gradle

Add the following repository and dependency to your build.gradle:

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.MEFRREEX:JOOQConnector:1.0.1'
}

Switch to Russian

About

A library for working with databases using JOOQ ORM for Java

Topics

Resources

License

Stars

Watchers

Forks

Languages