added broken code moved from mc-passive-mode code
This commit is contained in:
17
src/main/java/de/slpnetwork/SPL.java
Normal file
17
src/main/java/de/slpnetwork/SPL.java
Normal file
@ -0,0 +1,17 @@
|
||||
package de.slpnetwork;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class SPL {
|
||||
private static JavaPlugin plugin;
|
||||
|
||||
// For other classes in our library
|
||||
public static JavaPlugin getPlugin() {
|
||||
return SPL.plugin;
|
||||
}
|
||||
|
||||
// This method must not be used any where in the library!
|
||||
public static void setPlugin(final JavaPlugin plugin) {
|
||||
SPL.plugin = plugin;
|
||||
}
|
||||
}
|
433
src/main/java/de/slpnetwork/database/MySQL.java
Normal file
433
src/main/java/de/slpnetwork/database/MySQL.java
Normal file
@ -0,0 +1,433 @@
|
||||
package de.slpnetwork.database;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import de.slpnetwork.interfaces.IDataHandler;
|
||||
import de.slpnetwork.utils.ArrayHelper;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
// TODO: refactor all functions to throw end of function exception
|
||||
public class MySQL implements IDataHandler {
|
||||
|
||||
private Connection con;
|
||||
|
||||
/**
|
||||
* handles all mysql database operations
|
||||
* @param url the target url for the database
|
||||
* @param username the username for the database
|
||||
* @param password the password for the database
|
||||
* @param database the name of the datbase (not yet required)
|
||||
* @throws Exception if anything failes during connection attempt an exception throws
|
||||
*/
|
||||
public MySQL(String url, String username, String password, String database) throws Exception{
|
||||
try {
|
||||
connect(url, username, password);
|
||||
} catch(Exception ex) {
|
||||
throw new Exception("could not connect to database due to error: " + ex.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* connects to the Database
|
||||
* @param url target url to the database
|
||||
* @param username username for the database
|
||||
* @param password password for the database
|
||||
*/
|
||||
private Connection connect(String url, String username, String password){
|
||||
try {
|
||||
con = DriverManager.getConnection(url, username, password);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a table in the set database
|
||||
* @param name the tables name
|
||||
* @param fieldMap a map given with fieldnames and datatypes
|
||||
* @throws SQLException
|
||||
*/
|
||||
@Override
|
||||
public void createTable(String name, Map<String, String> fieldMap) {
|
||||
String fields = "";
|
||||
|
||||
fields += "(";
|
||||
for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
|
||||
System.out.println(entry.getKey() + ":" + entry.getValue());
|
||||
fields += entry.getKey() + " " + entry.getValue();
|
||||
}
|
||||
fields += ")";
|
||||
|
||||
try {
|
||||
this.con.createStatement().executeQuery("CREATE TABLE IF NOT EXISTS " + name + " " + fields);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* execute a given statement on a mysql database
|
||||
* this only returns a single given fields value for return of rows use getResults
|
||||
* @param stmt the statement used to query the database
|
||||
* @param target used to specify which field is required
|
||||
* @return if found values from the database of type int
|
||||
*/
|
||||
@Override
|
||||
public int getInt(String stmt, String target) {
|
||||
try {
|
||||
int returning = 0;
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
returning = res.getInt(target);
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute a given statement on a mysql database
|
||||
* this only returns a single given fields value for return of rows use getResults
|
||||
* @param stmt the statement used to query the database
|
||||
* @param target used to specify which field is required
|
||||
* @return if found values from the database of type int
|
||||
*/
|
||||
@Override
|
||||
public int[] getInts(String stmt, String target) {
|
||||
try {
|
||||
int[] returning = {};
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
ArrayHelper.push(returning, res.getInt(target));
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute a given statement on a mysql database
|
||||
* @param stmt the statement used to query the database
|
||||
* @param target not used
|
||||
* @return if found values from the database of type int
|
||||
*/
|
||||
@Override
|
||||
public double getDouble(String stmt, @Nullable String target) {
|
||||
try {
|
||||
double returning = 0;
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
returning = res.getDouble(target);
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double[] getDoubles(String stmt, String target) {
|
||||
try {
|
||||
double[] returning = {};
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
ArrayHelper.push(returning, res.getDouble(target));
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute a given statement on a mysql database
|
||||
* @param stmt the statement used to query the database
|
||||
* @param target states what field is wanted
|
||||
* @return if found values from the database of type string
|
||||
*/
|
||||
@Override
|
||||
public String getString(String stmt, String target) {
|
||||
try {
|
||||
String returning = "";
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
returning = res.getString(target);
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* execute a given statement on a mysql database
|
||||
* @param stmt the statement used to query the database
|
||||
* @param target not used
|
||||
* @return if found values from the database of type string as array
|
||||
*/
|
||||
@Override
|
||||
public String[] getStrings(String stmt, String target) {
|
||||
try {
|
||||
String[] returning = {};
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
ArrayHelper.push(returning, res.getString(target));
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute a given statement on a mysql database
|
||||
* @param stmt the statement used to query the database
|
||||
* @param target not used
|
||||
* @return if found values from the database of type boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean getBool(String stmt, String target) {
|
||||
try {
|
||||
boolean returning = false;
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
if(this.countResult(res) <= 0) {
|
||||
throw new Exception("error in MySQL class: received empty resultset");
|
||||
}
|
||||
|
||||
while (res.next()) {
|
||||
returning = res.getBoolean(target);
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute a given statement on a mysql database
|
||||
* @param stmt the statement used to query the database
|
||||
* @param target not used
|
||||
* @return if found values from the database of type boolean as array
|
||||
*/
|
||||
@Override
|
||||
public boolean[] getBools(String stmt, String target) {
|
||||
boolean[] result = {};
|
||||
|
||||
try {
|
||||
ResultSet rs = this.con.createStatement().executeQuery(stmt);
|
||||
while (rs.next()) {
|
||||
result = ArrayHelper.push(result,rs.getBoolean(target));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* executes a query and returns the unproccessed resultset
|
||||
* @param stmt the query given as a string
|
||||
* @return a resultset to continue proccessing with
|
||||
*/
|
||||
@Override
|
||||
public ResultSet getResults(String stmt) throws Exception {
|
||||
try {
|
||||
return this.con.createStatement().executeQuery(stmt);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
throw new Exception("reached end of function.. this should never happen");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JsonObject getResultAsJSON(String stmt) throws Exception {
|
||||
try {
|
||||
ResultSet rs = this.con.createStatement().executeQuery(stmt);
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
|
||||
while (rs.next()) {
|
||||
int totalColumns = rs.getMetaData().getColumnCount();
|
||||
|
||||
for (int i = 1; i <= totalColumns; i++) {
|
||||
jsonObject.add(rs.getMetaData().getColumnLabel(i), (JsonElement) rs.getObject(i));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
throw new Exception("reached end of function.. this should never happen");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JsonObject[] getResultsAsJSON(String stmt) throws Exception {
|
||||
try {
|
||||
ResultSet rs = this.con.createStatement().executeQuery(stmt);
|
||||
JsonObject[] jsonArray = new JsonObject[0];
|
||||
|
||||
while (rs.next()) {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
int totalColumns = rs.getMetaData().getColumnCount();
|
||||
|
||||
for (int i = 1; i <= totalColumns; i++) {
|
||||
jsonObject.add(rs.getMetaData().getColumnLabel(i), (JsonElement) rs.getObject(i));
|
||||
}
|
||||
|
||||
ArrayHelper.push(jsonArray, jsonObject);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
throw new Exception("reached end of function.. this should never happen");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Map<Byte[], Byte[]> getResultsAsMap(String a1) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getResultsAsMap'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeData(String tableName, Map<String, Byte[]> values) throws Exception {
|
||||
if (values.isEmpty()) {
|
||||
System.err.println("Data map is empty. No columns to insert values into.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tableName == "" ) {
|
||||
throw new Exception("found empty tableName");
|
||||
}
|
||||
|
||||
StringBuilder labels = new StringBuilder();
|
||||
StringBuilder placeholders = new StringBuilder();
|
||||
|
||||
Iterator<Map.Entry<String, Byte[]>> iterator = values.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<String, Byte[]> entry = iterator.next();
|
||||
labels.append(entry.getKey());
|
||||
placeholders.append("?");
|
||||
|
||||
if (iterator.hasNext()) {
|
||||
labels.append(",");
|
||||
placeholders.append(",");
|
||||
}
|
||||
}
|
||||
|
||||
String sql = "INSERT INTO " + tableName + " (" + labels + ") VALUES (" + placeholders + ")";
|
||||
|
||||
try (PreparedStatement statement = this.con.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
for (Byte[] value : values.values()) {
|
||||
statement.setObject(index++, value);
|
||||
}
|
||||
|
||||
int rowsInserted = statement.executeUpdate();
|
||||
return rowsInserted > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeData(String tableName, String condition, Map<String, Byte[]> values) throws Exception {
|
||||
if (values.isEmpty()) {
|
||||
System.err.println("Data map is empty. No columns to insert values into.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tableName == "" ) {
|
||||
throw new Exception("found empty tableName");
|
||||
}
|
||||
|
||||
if (condition == "" ) {
|
||||
throw new Exception("found empty condition");
|
||||
}
|
||||
|
||||
StringBuilder labels = new StringBuilder();
|
||||
StringBuilder placeholders = new StringBuilder();
|
||||
|
||||
Iterator<Map.Entry<String, Byte[]>> iterator = values.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<String, Byte[]> entry = iterator.next();
|
||||
labels.append(entry.getKey());
|
||||
placeholders.append("?");
|
||||
|
||||
if (iterator.hasNext()) {
|
||||
labels.append(",");
|
||||
placeholders.append(",");
|
||||
}
|
||||
}
|
||||
|
||||
String sql = "INSERT INTO " + tableName + " (" + labels + ") VALUES (" + placeholders + ") " + condition;
|
||||
|
||||
try (PreparedStatement statement = this.con.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
for (Byte[] value : values.values()) {
|
||||
statement.setObject(index++, value);
|
||||
}
|
||||
|
||||
int rowsInserted = statement.executeUpdate();
|
||||
return rowsInserted > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* counts elements of a given resultset
|
||||
* @param resultSet the requested resultset to count
|
||||
* @return the counted size of a resultset
|
||||
* @throws SQLException
|
||||
*/
|
||||
private int countResult(ResultSet resultSet) throws SQLException {
|
||||
int size = 0;
|
||||
|
||||
while(resultSet.next()) {
|
||||
size++;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
}
|
391
src/main/java/de/slpnetwork/database/SQLite.java
Normal file
391
src/main/java/de/slpnetwork/database/SQLite.java
Normal file
@ -0,0 +1,391 @@
|
||||
package de.slpnetwork.database;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import de.slpnetwork.MCPassiveMode;
|
||||
import de.slpnetwork.interfaces.IDataHandler;
|
||||
import de.slpnetwork.utils.ArrayHelper;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class SQLite implements IDataHandler {
|
||||
|
||||
private Connection con;
|
||||
private MCPassiveMode plugin;
|
||||
|
||||
public SQLite(MCPassiveMode plugin, String path) throws Exception {
|
||||
|
||||
this.plugin = plugin;
|
||||
|
||||
// setup connection
|
||||
try {
|
||||
this.con = connect(path);
|
||||
} catch (Exception ex) {
|
||||
throw new Exception("SQLite initailzatio failed due to exception" + ex.getLocalizedMessage(), ex.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
private Connection connect(String path) throws Exception {
|
||||
try {
|
||||
return DriverManager.getConnection("jdbc:sqlite:" + path);
|
||||
} catch (Exception ex) {
|
||||
throw new Exception("SQLite connection failed due to exception" + ex.getLocalizedMessage(), ex.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a table in the set database
|
||||
*
|
||||
* @param name the tables name
|
||||
* @throws SQLException
|
||||
*/
|
||||
@Override
|
||||
public void createTable(String name, Map<String, String> fieldMap) {
|
||||
String fields = "";
|
||||
|
||||
fields += "(";
|
||||
for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
|
||||
System.out.println(entry.getKey() + ":" + entry.getValue());
|
||||
fields += entry.getKey() + " " + entry.getValue();
|
||||
}
|
||||
fields += ")";
|
||||
|
||||
try {
|
||||
this.con.createStatement().executeQuery("CREATE TABLE IF NOT EXISTS " + name + " " + fields);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* executes a given query and returns a result of type int
|
||||
*
|
||||
* @param stmt the query to execute
|
||||
* @param target secondary argument used to define the required field
|
||||
* @return the found result of type int
|
||||
*/
|
||||
@Override
|
||||
public int getInt(String stmt, String target) {
|
||||
try {
|
||||
int returning = 0;
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
returning = res.getInt(target);
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getInts(String stmt, String target) {
|
||||
try {
|
||||
int[] returning = {};
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
ArrayHelper.push(returning, res.getInt(target));
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(String stmt, @Nullable String target) {
|
||||
try {
|
||||
double returning = 0;
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
returning = res.getDouble(target);
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double[] getDoubles(String stmt, String target) {
|
||||
try {
|
||||
double[] returning = {};
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
ArrayHelper.push(returning, res.getDouble(target));
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String stmt, String target) {
|
||||
try {
|
||||
String returning = "";
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
returning = res.getString(target);
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getStrings(String stmt, String target) {
|
||||
try {
|
||||
String[] returning = {};
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
ArrayHelper.push(returning, res.getString(target));
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute a given statement on a mysql database
|
||||
* @param stmt the statement used to query the database
|
||||
* @param target not used
|
||||
* @return if found values from the database of type boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean getBool(String stmt, String target) {
|
||||
try {
|
||||
boolean returning = false;
|
||||
ResultSet res = this.con.createStatement().executeQuery(stmt);
|
||||
|
||||
while (res.next()) {
|
||||
returning = res.getBoolean(target);
|
||||
}
|
||||
|
||||
return returning;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute a given statement on a mysql database
|
||||
* @param stmt the statement used to query the database
|
||||
* @param target not used
|
||||
* @return if found values from the database of type boolean as array
|
||||
*/
|
||||
@Override
|
||||
public boolean[] getBools(String stmt, String target) {
|
||||
boolean[] result = {};
|
||||
|
||||
try {
|
||||
ResultSet rs = this.con.createStatement().executeQuery(stmt);
|
||||
while (rs.next()) {
|
||||
result = ArrayHelper.push(result,rs.getBoolean(target));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* executes a query and returns the unproccessed resultset
|
||||
* @param stmt the query given as a string
|
||||
* @return a resultset to continue proccessing with
|
||||
*/
|
||||
@Override
|
||||
public ResultSet getResults(String stmt) throws Exception {
|
||||
try {
|
||||
return this.con.createStatement().executeQuery(stmt);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
throw new Exception("reached end of function.. this should never happen");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JsonObject getResultAsJSON(String stmt) throws Exception {
|
||||
try {
|
||||
ResultSet rs = this.con.createStatement().executeQuery(stmt);
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
|
||||
while (rs.next()) {
|
||||
int totalColumns = rs.getMetaData().getColumnCount();
|
||||
|
||||
for (int i = 1; i <= totalColumns; i++) {
|
||||
jsonObject.add(rs.getMetaData().getColumnLabel(i), (JsonElement) rs.getObject(i));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
throw new Exception("reached end of function.. this should never happen");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JsonObject[] getResultsAsJSON(String stmt) throws Exception {
|
||||
try {
|
||||
ResultSet rs = this.con.createStatement().executeQuery(stmt);
|
||||
JsonObject[] jsonArray = new JsonObject[0];
|
||||
|
||||
while (rs.next()) {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
int totalColumns = rs.getMetaData().getColumnCount();
|
||||
|
||||
for (int i = 1; i <= totalColumns; i++) {
|
||||
jsonObject.add(rs.getMetaData().getColumnLabel(i), (JsonElement) rs.getObject(i));
|
||||
}
|
||||
|
||||
ArrayHelper.push(jsonArray, jsonObject);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
throw new Exception("reached end of function.. this should never happen");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Map<Byte[], Byte[]> getResultsAsMap(String a1) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getResultsAsMap'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeData(String tableName, Map<String, Byte[]> values) throws Exception {
|
||||
if (values.isEmpty()) {
|
||||
System.err.println("Data map is empty. No columns to insert values into.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tableName == "" ) {
|
||||
throw new Exception("found empty tableName");
|
||||
}
|
||||
|
||||
StringBuilder labels = new StringBuilder();
|
||||
StringBuilder placeholders = new StringBuilder();
|
||||
|
||||
Iterator<Map.Entry<String, Byte[]>> iterator = values.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<String, Byte[]> entry = iterator.next();
|
||||
labels.append(entry.getKey());
|
||||
placeholders.append("?");
|
||||
|
||||
if (iterator.hasNext()) {
|
||||
labels.append(",");
|
||||
placeholders.append(",");
|
||||
}
|
||||
}
|
||||
|
||||
String sql = "INSERT INTO " + tableName + " (" + labels + ") VALUES (" + placeholders + ")";
|
||||
|
||||
try (PreparedStatement statement = this.con.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
for (Byte[] value : values.values()) {
|
||||
statement.setObject(index++, value);
|
||||
}
|
||||
|
||||
int rowsInserted = statement.executeUpdate();
|
||||
return rowsInserted > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeData(String tableName, String condition, Map<String, Byte[]> values) throws Exception {
|
||||
if (values.isEmpty()) {
|
||||
System.err.println("Data map is empty. No columns to insert values into.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tableName == "" ) {
|
||||
throw new Exception("found empty tableName");
|
||||
}
|
||||
|
||||
if (condition == "" ) {
|
||||
throw new Exception("found empty condition");
|
||||
}
|
||||
|
||||
StringBuilder labels = new StringBuilder();
|
||||
StringBuilder placeholders = new StringBuilder();
|
||||
|
||||
Iterator<Map.Entry<String, Byte[]>> iterator = values.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<String, Byte[]> entry = iterator.next();
|
||||
labels.append(entry.getKey());
|
||||
placeholders.append("?");
|
||||
|
||||
if (iterator.hasNext()) {
|
||||
labels.append(",");
|
||||
placeholders.append(",");
|
||||
}
|
||||
}
|
||||
|
||||
String sql = "INSERT INTO " + tableName + " (" + labels + ") VALUES (" + placeholders + ") " + condition;
|
||||
|
||||
try (PreparedStatement statement = this.con.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
for (Byte[] value : values.values()) {
|
||||
statement.setObject(index++, value);
|
||||
}
|
||||
|
||||
int rowsInserted = statement.executeUpdate();
|
||||
return rowsInserted > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* counts elements of a given resultset
|
||||
* @param resultSet the requested resultset to count
|
||||
* @return the counted size of a resultset
|
||||
* @throws SQLException
|
||||
*/
|
||||
private int countResult(ResultSet resultSet) throws SQLException {
|
||||
int size = 0;
|
||||
|
||||
while(resultSet.next()) {
|
||||
size++;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
}
|
86
src/main/java/de/slpnetwork/interfaces/IDataHandler.java
Normal file
86
src/main/java/de/slpnetwork/interfaces/IDataHandler.java
Normal file
@ -0,0 +1,86 @@
|
||||
package de.slpnetwork.interfaces;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import netscape.javascript.JSObject;
|
||||
|
||||
public interface IDataHandler {
|
||||
void createTable(String name, Map<String, String> fieldMap);
|
||||
|
||||
/**
|
||||
* get integers values from a datasource
|
||||
* @param a1 first argument usually used as target value
|
||||
* @param a2 second argument usually used as value or extra file
|
||||
* @return value of type int
|
||||
*/
|
||||
public int getInt(String a1, String a2);
|
||||
public int[] getInts(String a1, String a2);
|
||||
|
||||
/**
|
||||
* get floats values from a datasource
|
||||
* @param a1 first argument usually used as target value
|
||||
* @param a2 second argument usually used as value or extra file
|
||||
* @return value of type float
|
||||
*/
|
||||
public double getDouble(String a1, String a2);
|
||||
public double[] getDoubles(String a1, String a2);
|
||||
|
||||
/**
|
||||
* get strings values from a datasource
|
||||
* @param a1 first argument usually used as target value
|
||||
* @param a2 second argument usually used as value or extra file
|
||||
* @return value of type String
|
||||
*/
|
||||
public String getString(String a1, String a2);
|
||||
public String[] getStrings(String a1, String a2);
|
||||
|
||||
/**
|
||||
* get boolean values from a datasource
|
||||
* @param a1 first argument usually used as target value
|
||||
* @param a2 second argument usually used as value or extra file
|
||||
* @return value of type int
|
||||
*/
|
||||
public boolean getBool(String a1, String a2);
|
||||
public boolean[] getBools(String a1, String a2);
|
||||
|
||||
/**
|
||||
* executes a query and returns the unproccessed resultset
|
||||
* @param a1 the query given as a string
|
||||
* @return a resultset to continue proccessing with
|
||||
*/
|
||||
public ResultSet getResults(String a1) throws Exception;
|
||||
|
||||
/**
|
||||
* executes given query and returns result as json object
|
||||
* @param a1 the query to execute
|
||||
* @return the result as JSONObject
|
||||
*/
|
||||
public JsonObject getResultAsJSON(String a1) throws Exception;
|
||||
public JsonObject[] getResultsAsJSON(String a1) throws Exception;
|
||||
|
||||
/**
|
||||
* executes a query and returns untyped results as map
|
||||
* @param a1 the query given as a string
|
||||
* @return a resultset to continue proccessing with
|
||||
*/
|
||||
public Map<Byte[], Byte[]> getResultsAsMap(String a1);
|
||||
|
||||
/**
|
||||
* write values to a set datasource
|
||||
* @param a1 first argument usually used as location or statement
|
||||
* @param a3 third argument usually used as value or extra file
|
||||
* @return if the write operation was successful
|
||||
*/
|
||||
public boolean writeData(String a1, Map<String, Byte[]> a3) throws Exception;
|
||||
|
||||
/**
|
||||
* write values to a set datasource
|
||||
* @param a1 first argument location
|
||||
* @param a2 second argument condition
|
||||
* @param a3 third argument usually used as value or extra file
|
||||
* @return if the write operation was successful
|
||||
*/
|
||||
public boolean writeData(String a1, String a2, Map<String, Byte[]> a3) throws Exception;
|
||||
}
|
9
src/main/java/de/slpnetwork/interfaces/ILogger.java
Normal file
9
src/main/java/de/slpnetwork/interfaces/ILogger.java
Normal file
@ -0,0 +1,9 @@
|
||||
package de.slpnetwork.interfaces;
|
||||
|
||||
public interface ILogger {
|
||||
void Info(String Message, String Prefix);
|
||||
void Debug(String Message, String Prefix);
|
||||
void Warning(String Message, String Prefix);
|
||||
void Error(String Message, String Prefix);
|
||||
void Critical(String Message, String Prefix);
|
||||
}
|
62
src/main/java/de/slpnetwork/utils/ArrayHelper.java
Normal file
62
src/main/java/de/slpnetwork/utils/ArrayHelper.java
Normal file
@ -0,0 +1,62 @@
|
||||
package de.slpnetwork.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class ArrayHelper {
|
||||
public static int[] push(int[] arr, int item) {
|
||||
int[] tmp = Arrays.copyOf(arr, arr.length + 1);
|
||||
tmp[tmp.length - 1] = item;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static String[] push(String[] arr, String item) {
|
||||
String[] tmp = Arrays.copyOf(arr, arr.length + 1);
|
||||
tmp[tmp.length - 1] = item;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static double[] push(double[] arr, double item) {
|
||||
double[] tmp = Arrays.copyOf(arr, arr.length + 1);
|
||||
tmp[tmp.length - 1] = item;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static boolean[] push(boolean[] arr, boolean item) {
|
||||
boolean[] tmp = Arrays.copyOf(arr, arr.length + 1);
|
||||
tmp[tmp.length - 1] = item;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static JsonObject[] push(JsonObject[] arr, JsonObject item) {
|
||||
JsonObject[] tmp = Arrays.copyOf(arr, arr.length + 1);
|
||||
tmp[tmp.length - 1] = item;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static int[] pop(int[] arr) {
|
||||
int[] tmp = Arrays.copyOf(arr, arr.length - 1);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static String[] pop(String[] arr) {
|
||||
String[] tmp = Arrays.copyOf(arr, arr.length - 1);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static double[] pop(double[] arr) {
|
||||
double[] tmp = Arrays.copyOf(arr, arr.length - 1);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static boolean[] pop(boolean[] arr) {
|
||||
boolean[] tmp = Arrays.copyOf(arr, arr.length - 1);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static JsonObject[] pop(JsonObject[] arr) {
|
||||
JsonObject[] tmp = Arrays.copyOf(arr, arr.length - 1);
|
||||
return tmp;
|
||||
}
|
||||
}
|
37
src/main/java/de/slpnetwork/utils/Log.java
Normal file
37
src/main/java/de/slpnetwork/utils/Log.java
Normal file
@ -0,0 +1,37 @@
|
||||
package de.slpnetwork.utils;
|
||||
|
||||
import de.slpnetwork.SPL;
|
||||
import de.slpnetwork.interfaces.ILogger;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* gives a standard logging format
|
||||
*/
|
||||
public class Log implements ILogger {
|
||||
|
||||
@Override
|
||||
public void Info(String Message, String Prefix) {
|
||||
SPL.getPlugin().getLogger().log(Level.INFO, Prefix + ": " + Message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void Debug(String Message, String Prefix) {
|
||||
SPL.getPlugin().getLogger().log(Level.INFO, Prefix + " [DEBUG] >> " + Message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void Warning(String Message, String Prefix) {
|
||||
SPL.getPlugin().getLogger().log(Level.WARNING, Prefix + ": " + Message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void Error(String Message, String Prefix) {
|
||||
SPL.getPlugin().getLogger().log(Level.SEVERE, Prefix + ": " + Message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void Critical(String Message, String Prefix) {
|
||||
SPL.getPlugin().getLogger().log(Level.SEVERE, Prefix + " [CRITICAL] >> " + Message);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user