added broken code moved from mc-passive-mode code
This commit is contained in:
parent
d0122e672b
commit
bc0be940bb
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
13
.idea/compiler.xml
generated
Normal file
13
.idea/compiler.xml
generated
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="steevs-plugin-lib" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
7
.idea/encodings.xml
generated
Normal file
7
.idea/encodings.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
25
.idea/jarRepositories.xml
generated
Normal file
25
.idea/jarRepositories.xml
generated
Normal file
@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="spigot-repo" />
|
||||
<option name="name" value="spigot-repo" />
|
||||
<option name="url" value="https://hub.spigotmc.org/nexus/content/repositories/snapshots/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
12
.idea/misc.xml
generated
Normal file
12
.idea/misc.xml
generated
Normal file
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK" />
|
||||
</project>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/steevs-plugin-lib.iml" filepath="$PROJECT_DIR$/steevs-plugin-lib.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
42
pom.xml
Normal file
42
pom.xml
Normal file
@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>de.slpnetwork</groupId>
|
||||
<artifactId>spl</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<!-- This adds the Spigot Maven repository to the build -->
|
||||
<repository>
|
||||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<!--This adds the Spigot API artifact to the build -->
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.20.4-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.33</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
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);
|
||||
}
|
||||
}
|
13
steevs-plugin-lib.iml
Normal file
13
steevs-plugin-lib.iml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="minecraft" name="Minecraft">
|
||||
<configuration>
|
||||
<autoDetectTypes>
|
||||
<platformType>SPIGOT</platformType>
|
||||
</autoDetectTypes>
|
||||
<projectReimportVersion>1</projectReimportVersion>
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
</module>
|
Loading…
x
Reference in New Issue
Block a user