Sueta

SQL Library

2.0.3-SNAPSHOT

A simple sql library that doesn't require you to write a single line of sql

·Plugin

Gradle

repositories {
    maven {
        url('http://repo.byncing.eu/snapshots')
        allowInsecureProtocol(true)
    }
}

dependencies {
    implementation('eu.byncing:sql-lib:2.0.3-SNAPSHOT')
}

A small example of how to add a player to the database.

How to create a SqlLib instance.

SqlLib lib = new SqlLib();

How to connect to sql server.

lib.connect(new Profile("127.0.0.1", 3306, "root", "bridge", ""));


Create a table with keys and data types

Table table = lib.table("players");
table.setKeys("UUID", "NAME", "CREATE_TIME", "ONLINE");
table.setTypes(DataTypes.STRING, DataTypes.STRING, DataTypes.LONG, DataTypes.BOOLEAN);
table.createTable();


Create a PlayerData class

PlayerData data = new PlayerData(UUID.randomUUID(), "byncing", System.currentTimeMillis(), true);

Retrieve a table

table.fetch(fetch -> {
    if (!fetch.find("NAME", "byncing")) {
        table.insert(data.serialize());
    }
    fetch.setWhere("NAME").setWhereValues("byncing");
    PlayerData playerData = PlayerData.deserialize(fetch.single("UUID", "NAME", "CREATE_TIME", "ONLINE"));
    System.out.println(playerData);
}, true);

Here the online value is changed by the player byncing.

table.update(update -> {
    update.setWhere("NAME").setWhereValues("byncing");
    table.fetch(fetch -> {
        fetch.setWhere("NAME").setWhereValues("byncing");
        update.setKeys("ONLINE").change(!fetch.single("ONLINE", Boolean.class));
    }, false);
}, true);

Here is a player data class with which you can serialize and deserialize the player.

public class PlayerData {

    private final UUID uniqueId;
    private final String name;
    private final long createTime;
    private boolean online;

    public PlayerData(UUID uniqueId, String name, long createTime, boolean online) {
        this.uniqueId = uniqueId;
        this.name = name;
        this.createTime = createTime;
        this.online = online;
    }

    public Object[] serialize() {
        return new Object[]{uniqueId, name, createTime, online};
    }

    public static PlayerData deserialize(Map<String, Object> values) {
        return new PlayerData(
            UUID.fromString((String) values.get("UUID")),
            ((String) values.get("NAME")),
            ((Long) values.get("CREATE_TIME")),
            ((Boolean) values.get("ONLINE")));
    }

    public UUID getUniqueId() {
        return uniqueId;
    }

    public String getName() {
        return name;
    }

    public long getCreateTime() {
        return createTime;
    }

    public boolean isOnline() {
        return online;
    }

    public void setOnline(boolean online) {
        this.online = online;
    }
}

Similar projects

Plugin →