From e8544ff76110e08960b82a5bc105183b484dc516 Mon Sep 17 00:00:00 2001 From: mathias Date: Thu, 19 Aug 2021 17:11:26 +0200 Subject: [PATCH] Added the sql storage medium to easily manage storages --- .../SQLToolkit/storage/SQLStorageMedium.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/main/java/de/gnmyt/SQLToolkit/storage/SQLStorageMedium.java diff --git a/src/main/java/de/gnmyt/SQLToolkit/storage/SQLStorageMedium.java b/src/main/java/de/gnmyt/SQLToolkit/storage/SQLStorageMedium.java new file mode 100644 index 0000000..da52336 --- /dev/null +++ b/src/main/java/de/gnmyt/SQLToolkit/storage/SQLStorageMedium.java @@ -0,0 +1,88 @@ +package de.gnmyt.SQLToolkit.storage; + +import de.gnmyt.SQLToolkit.drivers.MySQLConnection; + +import java.util.ArrayList; +import java.util.HashMap; + +public abstract class SQLStorageMedium extends SQLTable { + + /** + * The basic constructor of the {@link SQLStorageMedium} + * + * @param connection The mysql connection you want to use + */ + public SQLStorageMedium(MySQLConnection connection) { + super(connection); + } + + @Override + protected void tableFields() { + string("keyName", 255, ""); + string("value", 2000, ""); + } + + /** + * Checks if a key exists in the storage medium + * + * @param key The key you want to check + * @return true if the key exists, otherwise false + */ + public boolean exists(String key) { + return select().where("keyName", key).getResult().exists(); + } + + /** + * Inserts a key into the storage medium + * + * @param key The key you want to insert + * @param value The value you want to insert + */ + public void insert(String key, String value) { + insert().value("keyName", key).value("value", value).execute(); + } + + /** + * Updates a key in the storage medium + * + * @param key The key you want to use + * @param value The value you want to update + */ + public void update(String key, String value) { + update().where("keyName", key).set("value", value).update(); + } + + /** + * Deletes a key from the storage medium + * + * @param key The key you want to delete + */ + public void delete(String key) { + update().where("keyName", key).delete(); + } + + /** + * Gets all entries from the storage medium + * + * @return the entries from the storage medium + */ + public ArrayList> getEntries() { + return select().getResult().getList(); + } + + /** + * Inserts or updates the key of the storage medium. + * If the entry is currently in the storage medium, it updates it. Otherwise, it will insert it + * + * @param key The key you want to insert / update + * @param value The value you want to insert / update + */ + public void insertOrUpdate(String key, String value) { + if (exists(key)) { + update(key, value); + } else { + insert(key, value); + } + } + +}